#include <ctype.h>
#include <signal.h>
#include <sys/types.h>
+#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <limits.h>
#include <netinet/in.h>
#include <netdb.h>
+#include <sys/un.h>
#include <string.h>
#include <pwd.h>
#include <errno.h>
fd_set masterfds; /* Master sockets etc. */
int masterhighest;
+time_t last_timer = 0L; /* Last timer hook processing */
+
/*
* lprintf() ... Write logging information
*/
void begin_critical_section(int which_one)
{
+ /* lprintf(9, "begin_critical_section(%d)\n", which_one); */
pthread_mutex_lock(&Critters[which_one]);
}
*/
void end_critical_section(int which_one)
{
+ /* lprintf(9, "end_critical_section(%d)\n", which_one); */
pthread_mutex_unlock(&Critters[which_one]);
}
/*
* This is a generic function to set up a master socket for listening on
* a TCP port. The server shuts down if the bind fails.
+ *
*/
int ig_tcp_server(int port_number, int queue_len)
{
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
-
- if (port_number == 0) {
- lprintf(1, "citserver: illegal port number specified\n");
- return(-1);
- }
-
sin.sin_port = htons((u_short)port_number);
- s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
+ s = socket(PF_INET, SOCK_STREAM,
+ (getprotobyname("tcp")->p_proto));
+
if (s < 0) {
lprintf(1, "citserver: Can't create a socket: %s\n",
strerror(errno));
return(-1);
}
- /* Set the SO_REUSEADDR socket option, because it makes sense. */
i = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
- lprintf(1, "citserver: Can't bind: %s\n", strerror(errno));
+ lprintf(1, "citserver: Can't bind: %s\n",
+ strerror(errno));
+ return(-1);
+ }
+
+ if (listen(s, queue_len) < 0) {
+ lprintf(1, "citserver: Can't listen: %s\n", strerror(errno));
+ return(-1);
+ }
+
+ return(s);
+}
+
+
+
+/*
+ * Create a Unix domain socket and listen on it
+ */
+int ig_uds_server(char *sockpath, int queue_len)
+{
+ struct sockaddr_un addr;
+ int s;
+
+ unlink(sockpath);
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sun_family = AF_UNIX;
+ safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
+
+ s = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (s < 0) {
+ lprintf(1, "citserver: Can't create a socket: %s\n",
+ strerror(errno));
+ return(-1);
+ }
+
+ if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+ lprintf(1, "citserver: Can't bind: %s\n",
+ strerror(errno));
return(-1);
}
return(-1);
}
+ chmod(sockpath, 0777);
return(s);
}
{
int bytes_written = 0;
int retval;
+ int sock;
+
+ if (CC->redirect_fp != NULL) {
+ fwrite(buf, nbytes, 1, CC->redirect_fp);
+ return;
+ }
+
+ if (CC->redirect_sock > 0) {
+ sock = CC->redirect_sock; /* and continue below... */
+ }
+ else {
+ sock = CC->client_socket;
+ }
+
while (bytes_written < nbytes) {
- retval = write(CC->client_socket, &buf[bytes_written],
+ retval = write(sock, &buf[bytes_written],
nbytes - bytes_written);
if (retval < 1) {
lprintf(2, "client_write() failed: %s\n",
strerror(errno));
- CC->kill_me = 1;
+ if (sock == CC->client_socket) CC->kill_me = 1;
return;
}
bytes_written = bytes_written + retval;
* The system-dependent part of master_cleanup() - close the master socket.
*/
void sysdep_master_cleanup(void) {
- /* FIX close all protocol master sockets here */
+ struct ServiceFunctionHook *serviceptr;
+
+ /*
+ * close all protocol master sockets
+ */
+ for (serviceptr = ServiceHookTable; serviceptr != NULL;
+ serviceptr = serviceptr->next ) {
+ lprintf(3, "Closing listener on port %d\n",
+ serviceptr->tcp_port);
+ close(serviceptr->msock);
+
+ /* If it's a Unix domain socket, remove the file. */
+ if (serviceptr->sockpath != NULL) {
+ unlink(serviceptr->sockpath);
+ }
+ }
}
}
-
+
+
+
+/*
+ * Redirect a session's output to a file or socket.
+ * This function may be called with a file handle *or* a socket (but not
+ * both). Call with neither to return output to its normal client socket.
+ */
+void CtdlRedirectOutput(FILE *fp, int sock) {
+
+ if (fp != NULL) CC->redirect_fp = fp;
+ else CC->redirect_fp = NULL;
+
+ if (sock > 0) CC->redirect_sock = sock;
+ else CC->redirect_sock = (-1);
+
+}
+
+
+/*
+ * masterCC is the context we use when not attached to a session. This
+ * function initializes it.
+ */
+void InitializeMasterCC(void) {
+ memset(&masterCC, 0, sizeof(struct CitContext));
+ masterCC.internal_pgm = 1;
+}
+
+
/*
* Here's where it all begins.
/* specify default port name and trace file */
strcpy(tracefile, "");
+ /* initialize the master context */
+ InitializeMasterCC();
+
/* parse command-line arguments */
for (a=1; a<argc; ++a) {
/* Tell 'em who's in da house */
lprintf(1,
"\nMultithreaded message server for Citadel/UX\n"
-"Copyright (C) 1987-1999 by the Citadel/UX development team.\n"
+"Copyright (C) 1987-2000 by the Citadel/UX development team.\n"
"Citadel/UX is free software, covered by the GNU General Public License, and\n"
"you are welcome to change it and/or distribute copies of it under certain\n"
"conditions. There is absolutely no warranty for this software. Please\n"
/* Initialize... */
init_sysdep();
- openlog("citserver",LOG_PID,LOG_USER);
+ openlog("citserver", LOG_PID, LOG_USER);
/* Load site-specific parameters */
lprintf(7, "Loading citadel.config\n");
/*
* Bind the server to our favorite ports.
*/
- CtdlRegisterServiceHook(config.c_port_number,
+ CtdlRegisterServiceHook(config.c_port_number, /* TCP */
+ NULL,
+ citproto_begin_session,
+ do_command_loop);
+ CtdlRegisterServiceHook(0, /* Unix */
+ "citadel.socket",
citproto_begin_session,
do_command_loop);
for (serviceptr = ServiceHookTable; serviceptr != NULL;
serviceptr = serviceptr->next ) {
- serviceptr->msock = ig_tcp_server(
- serviceptr->tcp_port, config.c_maxsessions);
- if (serviceptr->msock >= 0) {
- FD_SET(serviceptr->msock, &masterfds);
- if (serviceptr->msock > masterhighest)
- masterhighest = serviceptr->msock;
- lprintf(7, "Bound to port %-5d (socket %d)\n",
- serviceptr->tcp_port,
- serviceptr->msock);
- }
- else {
- lprintf(1, "Unable to bind to port %d\n",
- serviceptr->tcp_port);
+ FD_SET(serviceptr->msock, &masterfds);
+ if (serviceptr->msock > masterhighest) {
+ masterhighest = serviceptr->msock;
}
}
}
-
-
+/*
+ * Bind a thread to a context.
+ */
+inline void become_session(struct CitContext *which_con) {
+ pthread_setspecific(MyConKey, (void *)which_con );
+}
struct sockaddr_in fsin; /* Data for master socket */
int alen; /* Data for master socket */
int ssock; /* Descriptor for client socket */
+ struct timeval tv;
++num_threads;
+
while (!time_to_die) {
/*
}
end_critical_section(S_SESSION_TABLE);
- retval = select(highest + 1, &readfds, NULL, NULL, NULL);
+ tv.tv_sec = 60; /* wake up every minute if no input */
+ tv.tv_usec = 0;
+ retval = select(highest + 1, &readfds, NULL, NULL, &tv);
/* Now figure out who made this select() unblock.
* First, check for an error or exit condition.
}
/* Next, check to see if it's a new client connecting
- * on the master socket.
+ * on a master socket.
*/
else for (serviceptr = ServiceHookTable; serviceptr != NULL;
serviceptr = serviceptr->next ) {
con->client_socket = ssock;
con->h_command_function =
serviceptr->h_command_function;
+
+ /* Determine whether local socket */
+ if (serviceptr->sockpath != NULL)
+ con->is_local_socket = 1;
/* Set the SO_REUSEADDR socket option */
i = 1;
SO_REUSEADDR,
&i, sizeof(i));
- pthread_setspecific(MyConKey,
- (void *)con);
+ become_session(con);
begin_session(con);
serviceptr->h_greeting_function();
- pthread_setspecific(MyConKey,
- (void *)NULL);
+ become_session(NULL);
con->state = CON_IDLE;
goto SETUP_FD;
}
* thread that the &readfds needs to be refreshed with more
* current data.
*/
- if (!time_to_die) if (FD_ISSET(rescan[0], &readfds)) {
+ if (time_to_die)
+ break;
+
+ if (FD_ISSET(rescan[0], &readfds)) {
read(rescan[0], &junk, 1);
goto SETUP_FD;
}
/* We're bound to a session, now do *one* command */
if (bind_me != NULL) {
- pthread_setspecific(MyConKey, (void *)bind_me);
+ become_session(bind_me);
CC->h_command_function();
- pthread_setspecific(MyConKey, (void *)NULL);
+ become_session(NULL);
bind_me->state = CON_IDLE;
if (bind_me->kill_me == 1) {
RemoveContext(bind_me);
}
write(rescan[1], &junk, 1);
}
- else {
- lprintf(9, "Thread found nothing to do!\n");
- }
}
dead_session_purge();
+ if ((time(NULL) - last_timer) > 60L) {
+ last_timer = time(NULL);
+ PerformSessionHooks(EVT_TIMER);
+ }
}
/* If control reaches this point, the server is shutting down */