4 * Citadel "system dependent" stuff.
5 * See copyright.txt for copyright information.
7 * Here's where we (hopefully) have most parts of the Citadel server that
8 * would need to be altered to run the server in a non-POSIX environment.
10 * If we ever port to a different platform and either have multiple
11 * variants of this file or simply load it up with #ifdefs.
22 #include <sys/types.h>
25 #include <sys/socket.h>
27 #include <sys/syslog.h>
29 #if TIME_WITH_SYS_TIME
30 # include <sys/time.h>
34 # include <sys/time.h>
41 #include <sys/resource.h>
42 #include <netinet/in.h>
43 #include <netinet/tcp.h>
44 #include <arpa/inet.h>
52 #include <libcitadel.h>
55 #include "sysdep_decls.h"
56 #include "citserver.h"
60 #include "housekeeping.h"
61 #include "modules/crypto/serv_crypto.h" /* Needed for init_ssl, client_write_ssl, client_read_ssl, destruct_ssl */
64 #ifdef HAVE_SYS_SELECT_H
65 #include <sys/select.h>
72 #include "ctdl_module.h"
77 #ifdef DEBUG_MEMORY_LEAKS
85 struct igheap *igheap = NULL;
89 citthread_key_t MyConKey; /* TSD key for MyContext() */
91 int verbosity = DEFAULT_VERBOSITY; /* Logging level */
93 struct CitContext masterCC;
94 time_t last_purge = 0; /* Last dead session purge */
95 int num_sessions = 0; /* Current number of sessions */
97 int syslog_facility = LOG_DAEMON;
98 int enable_syslog = 0;
102 * CtdlLogPrintf() ... Write logging information
104 void CtdlLogPrintf(enum LogLevel loglevel, const char *format, ...) {
106 va_start(arg_ptr, format);
107 vCtdlLogPrintf(loglevel, format, arg_ptr);
111 void vCtdlLogPrintf(enum LogLevel loglevel, const char *format, va_list arg_ptr)
113 char buf[SIZ], buf2[SIZ];
116 vsyslog((syslog_facility | loglevel), format, arg_ptr);
119 /* stderr output code */
120 if (enable_syslog || running_as_daemon) return;
122 /* if we run in forground and syslog is disabled, log to terminal */
123 if (loglevel <= verbosity) {
128 gettimeofday(&tv, NULL);
129 /* Promote to time_t; types differ on some OSes (like darwin) */
130 unixtime = tv.tv_sec;
131 localtime_r(&unixtime, &tim);
132 if (CC->cs_pid != 0) {
134 "%04d/%02d/%02d %2d:%02d:%02d.%06ld [%3d] ",
135 tim.tm_year + 1900, tim.tm_mon + 1,
136 tim.tm_mday, tim.tm_hour, tim.tm_min,
137 tim.tm_sec, (long)tv.tv_usec,
141 "%04d/%02d/%02d %2d:%02d:%02d.%06ld ",
142 tim.tm_year + 1900, tim.tm_mon + 1,
143 tim.tm_mday, tim.tm_hour, tim.tm_min,
144 tim.tm_sec, (long)tv.tv_usec);
146 vsnprintf(buf2, SIZ, format, arg_ptr);
148 fprintf(stderr, "%s%s", buf, buf2);
156 * Signal handler to shut down the server.
159 volatile int exit_signal = 0;
160 volatile int shutdown_and_halt = 0;
161 volatile int restart_server = 0;
162 volatile int running_as_daemon = 0;
164 static RETSIGTYPE signal_cleanup(int signum) {
165 #ifdef THREADS_USESIGNALS
171 CtdlLogPrintf(CTDL_DEBUG, "Caught signal %d; shutting down.\n", signum);
172 exit_signal = signum;
179 * Some initialization stuff...
181 void init_sysdep(void) {
184 /* Avoid vulnerabilities related to FD_SETSIZE if we can. */
188 getrlimit(RLIMIT_NOFILE, &rl);
189 rl.rlim_cur = FD_SETSIZE;
190 rl.rlim_max = FD_SETSIZE;
191 setrlimit(RLIMIT_NOFILE, &rl);
195 /* If we've got OpenSSL, we're going to use it. */
201 * Set up a place to put thread-specific data.
202 * We only need a single pointer per thread - it points to the
203 * CitContext structure (in the ContextList linked list) of the
204 * session to which the calling thread is currently bound.
206 if (citthread_key_create(&MyConKey, NULL) != 0) {
207 CtdlLogPrintf(CTDL_CRIT, "Can't create TSD key: %s\n",
212 * The action for unexpected signals and exceptions should be to
213 * call signal_cleanup() to gracefully shut down the server.
216 sigaddset(&set, SIGINT);
217 sigaddset(&set, SIGQUIT);
218 sigaddset(&set, SIGHUP);
219 sigaddset(&set, SIGTERM);
220 // sigaddset(&set, SIGSEGV); commented out because
221 // sigaddset(&set, SIGILL); we want core dumps
222 // sigaddset(&set, SIGBUS);
223 sigprocmask(SIG_UNBLOCK, &set, NULL);
225 signal(SIGINT, signal_cleanup);
226 signal(SIGQUIT, signal_cleanup);
227 signal(SIGHUP, signal_cleanup);
228 signal(SIGTERM, signal_cleanup);
229 // signal(SIGSEGV, signal_cleanup); commented out because
230 // signal(SIGILL, signal_cleanup); we want core dumps
231 // signal(SIGBUS, signal_cleanup);
234 * Do not shut down the server on broken pipe signals, otherwise the
235 * whole Citadel service would come down whenever a single client
238 signal(SIGPIPE, SIG_IGN);
245 * This is a generic function to set up a master socket for listening on
246 * a TCP port. The server shuts down if the bind fails.
249 int ig_tcp_server(char *ip_addr, int port_number, int queue_len, char **errormessage)
251 struct sockaddr_in sin;
253 int actual_queue_len;
255 actual_queue_len = queue_len;
256 if (actual_queue_len < 5) actual_queue_len = 5;
258 memset(&sin, 0, sizeof(sin));
259 sin.sin_family = AF_INET;
260 sin.sin_port = htons((u_short)port_number);
261 if (ip_addr == NULL) {
262 sin.sin_addr.s_addr = INADDR_ANY;
265 sin.sin_addr.s_addr = inet_addr(ip_addr);
268 if (sin.sin_addr.s_addr == !INADDR_ANY) {
269 sin.sin_addr.s_addr = INADDR_ANY;
272 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
275 *errormessage = (char*) malloc(SIZ + 1);
276 snprintf(*errormessage, SIZ,
277 "citserver: Can't create a socket: %s",
279 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
284 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
286 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
287 *errormessage = (char*) malloc(SIZ + 1);
288 snprintf(*errormessage, SIZ,
289 "citserver: Can't bind: %s",
291 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
296 /* set to nonblock - we need this for some obscure situations */
297 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
298 *errormessage = (char*) malloc(SIZ + 1);
299 snprintf(*errormessage, SIZ,
300 "citserver: Can't set socket to non-blocking: %s",
302 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
307 if (listen(s, actual_queue_len) < 0) {
308 *errormessage = (char*) malloc(SIZ + 1);
309 snprintf(*errormessage, SIZ,
310 "citserver: Can't listen: %s",
312 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
323 * Create a Unix domain socket and listen on it
325 int ig_uds_server(char *sockpath, int queue_len, char **errormessage)
327 struct sockaddr_un addr;
330 int actual_queue_len;
332 actual_queue_len = queue_len;
333 if (actual_queue_len < 5) actual_queue_len = 5;
335 i = unlink(sockpath);
336 if (i != 0) if (errno != ENOENT) {
337 *errormessage = (char*) malloc(SIZ + 1);
338 snprintf(*errormessage, SIZ, "citserver: can't unlink %s: %s",
339 sockpath, strerror(errno));
340 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
344 memset(&addr, 0, sizeof(addr));
345 addr.sun_family = AF_UNIX;
346 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
348 s = socket(AF_UNIX, SOCK_STREAM, 0);
350 *errormessage = (char*) malloc(SIZ + 1);
351 snprintf(*errormessage, SIZ,
352 "citserver: Can't create a socket: %s",
354 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
358 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
359 *errormessage = (char*) malloc(SIZ + 1);
360 snprintf(*errormessage, SIZ,
361 "citserver: Can't bind: %s",
363 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
367 /* set to nonblock - we need this for some obscure situations */
368 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
369 *errormessage = (char*) malloc(SIZ + 1);
370 snprintf(*errormessage, SIZ,
371 "citserver: Can't set socket to non-blocking: %s",
373 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
378 if (listen(s, actual_queue_len) < 0) {
379 *errormessage = (char*) malloc(SIZ + 1);
380 snprintf(*errormessage, SIZ,
381 "citserver: Can't listen: %s",
383 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
387 chmod(sockpath, S_ISGID|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
394 * Return a pointer to the CitContext structure bound to the thread which
395 * called this function. If there's no such binding (for example, if it's
396 * called by the housekeeper thread) then a generic 'master' CC is returned.
398 * This function is used *VERY* frequently and must be kept small.
400 struct CitContext *MyContext(void) {
402 register struct CitContext *c;
404 return ((c = (struct CitContext *) citthread_getspecific(MyConKey),
405 c == NULL) ? &masterCC : c
411 * Initialize a new context and place it in the list. The session number
412 * used to be the PID (which is why it's called cs_pid), but that was when we
413 * had one process per session. Now we just assign them sequentially, starting
414 * at 1 (don't change it to 0 because masterCC uses 0).
416 struct CitContext *CreateNewContext(void) {
417 struct CitContext *me;
418 static int next_pid = 0;
420 me = (struct CitContext *) malloc(sizeof(struct CitContext));
422 CtdlLogPrintf(CTDL_ALERT, "citserver: can't allocate memory!!\n");
425 memset(me, 0, sizeof(struct CitContext));
427 /* The new context will be created already in the CON_EXECUTING state
428 * in order to prevent another thread from grabbing it while it's
431 me->state = CON_EXECUTING;
433 * Generate a unique session number and insert this context into
436 begin_critical_section(S_SESSION_TABLE);
437 me->cs_pid = ++next_pid;
439 me->next = ContextList;
441 if (me->next != NULL) {
445 end_critical_section(S_SESSION_TABLE);
450 struct CitContext *CtdlGetContextArray(int *count)
453 struct CitContext *nptr, *cptr;
455 nContexts = num_sessions;
456 nptr = malloc(sizeof(struct CitContext) * nContexts);
459 begin_critical_section(S_SESSION_TABLE);
460 for (cptr = ContextList, i=0; cptr != NULL && i < nContexts; cptr = cptr->next, i++)
461 memcpy(&nptr[i], cptr, sizeof (struct CitContext));
462 end_critical_section (S_SESSION_TABLE);
471 * This function returns a private context with the user filled in correctly
473 void CtdlFillPrivateContext(struct CitContext *context, char *name)
475 char sysname[USERNAME_SIZE];
477 memset(context, 0, sizeof(struct CitContext));
478 context->internal_pgm = 1;
480 strcpy (sysname, "SYS_");
481 strcat (sysname, name);
482 if (getuser(&(context->user), sysname))
484 strcpy(context->user.fullname, sysname);
485 putuser(&(context->user));
490 * The following functions implement output buffering. If the kernel supplies
491 * native TCP buffering (Linux & *BSD), use that; otherwise, emulate it with
492 * user-space buffering.
496 # define HAVE_TCP_BUFFERING
499 # define HAVE_TCP_BUFFERING
500 # define TCP_CORK TCP_NOPUSH
502 #endif /* TCP_CORK */
503 #endif /* HAVE_DARWIN */
505 #ifdef HAVE_TCP_BUFFERING
506 static unsigned on = 1, off = 0;
507 void buffer_output(void) {
508 struct CitContext *ctx = MyContext();
509 setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
513 void unbuffer_output(void) {
514 struct CitContext *ctx = MyContext();
515 setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
519 void flush_output(void) {
520 struct CitContext *ctx = MyContext();
521 setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
522 setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
526 /* Stub functions for Darwin/OS X where TCP buffering isn't liked at all */
527 void buffer_output(void) {
530 void unbuffer_output(void) {
533 void flush_output(void) {
536 void buffer_output(void) {
537 if (CC->buffering == 0) {
540 CC->output_buffer = malloc(SIZ);
544 void flush_output(void) {
545 if (CC->buffering == 1) {
546 client_write(CC->output_buffer, CC->buffer_len);
551 void unbuffer_output(void) {
552 if (CC->buffering == 1) {
554 /* We don't call flush_output because we can't. */
555 client_write(CC->output_buffer, CC->buffer_len);
557 free(CC->output_buffer);
558 CC->output_buffer = NULL;
561 #endif /* HAVE_DARWIN */
562 #endif /* HAVE_TCP_BUFFERING */
567 * client_write() ... Send binary data to the client.
569 int client_write(char *buf, int nbytes)
571 int bytes_written = 0;
573 #ifndef HAVE_TCP_BUFFERING
574 int old_buffer_len = 0;
581 if (Ctx->redirect_buffer != NULL) {
582 if ((Ctx->redirect_len + nbytes + 2) >= Ctx->redirect_alloc) {
583 Ctx->redirect_alloc = (Ctx->redirect_alloc * 2) + nbytes;
584 Ctx->redirect_buffer = realloc(Ctx->redirect_buffer,
585 Ctx->redirect_alloc);
587 memcpy(&Ctx->redirect_buffer[Ctx->redirect_len], buf, nbytes);
588 Ctx->redirect_len += nbytes;
589 Ctx->redirect_buffer[Ctx->redirect_len] = 0;
593 #ifndef HAVE_TCP_BUFFERING
594 /* If we're buffering for later, do that now. */
595 if (Ctx->buffering) {
596 old_buffer_len = Ctx->buffer_len;
597 Ctx->buffer_len += nbytes;
598 Ctx->output_buffer = realloc(Ctx->output_buffer, Ctx->buffer_len);
599 memcpy(&Ctx->output_buffer[old_buffer_len], buf, nbytes);
604 /* Ok, at this point we're not buffering. Go ahead and write. */
607 if (Ctx->redirect_ssl) {
608 client_write_ssl(buf, nbytes);
613 fdflags = fcntl(Ctx->client_socket, F_GETFL);
615 while (bytes_written < nbytes) {
616 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
618 FD_SET(Ctx->client_socket, &wset);
619 if (select(1, NULL, &wset, NULL, NULL) == -1) {
620 CtdlLogPrintf(CTDL_ERR,
621 "client_write(%d bytes) select failed: %s (%d)\n",
622 nbytes - bytes_written,
623 strerror(errno), errno);
630 retval = write(Ctx->client_socket, &buf[bytes_written],
631 nbytes - bytes_written);
633 CtdlLogPrintf(CTDL_ERR,
634 "client_write(%d bytes) failed: %s (%d)\n",
635 nbytes - bytes_written,
636 strerror(errno), errno);
638 // CtdlLogPrintf(CTDL_DEBUG, "Tried to send: %s", &buf[bytes_written]);
642 bytes_written = bytes_written + retval;
649 * cprintf() ... Send formatted printable data to the client. It is
650 * implemented in terms of client_write() but remains in
651 * sysdep.c in case we port to somewhere without va_args...
653 void cprintf(const char *format, ...) {
657 va_start(arg_ptr, format);
658 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
659 buf[sizeof buf - 2] = '\n';
660 client_write(buf, strlen(buf));
666 * Read data from the client socket.
668 * 1 Requested number of bytes has been read.
669 * 0 Request timed out.
670 * -1 The socket is broken.
671 * If the socket breaks, the session will be terminated.
673 int client_read_to(char *buf, int bytes, int timeout)
682 if (CC->redirect_ssl) {
683 return (client_read_ssl(buf, bytes, timeout));
687 fd = CC->client_socket;
694 retval = select( (fd)+1,
695 &rfds, NULL, NULL, &tv);
700 CtdlLogPrintf(CTDL_DEBUG, "Interrupted select().\n");
706 if (FD_ISSET(fd, &rfds) == 0) {
710 rlen = read(fd, &buf[len], bytes-len);
712 /* The socket has been disconnected! */
722 * Read data from the client socket with default timeout.
723 * (This is implemented in terms of client_read_to() and could be
724 * justifiably moved out of sysdep.c)
726 INLINE int client_read(char *buf, int bytes)
728 return(client_read_to(buf, bytes, config.c_sleeping));
733 * client_getln() ... Get a LF-terminated line of text from the client.
734 * (This is implemented in terms of client_read() and could be
735 * justifiably moved out of sysdep.c)
737 int client_getln(char *buf, int bufsize)
741 /* Read one character at a time.
744 retval = client_read(&buf[i], 1);
745 if (retval != 1 || buf[i] == '\n' || i == (bufsize-1))
749 /* If we got a long line, discard characters until the newline.
751 if (i == (bufsize-1))
752 while (buf[i] != '\n' && retval == 1)
753 retval = client_read(&buf[i], 1);
755 /* Strip the trailing LF, and the trailing CR if present.
759 && ( (buf[i - 1]==13)
760 || ( buf[i - 1]==10)) ) {
764 if (retval < 0) safestrncpy(&buf[i], "000", bufsize - i);
770 * Cleanup any contexts that are left lying around
772 void context_cleanup(void)
774 struct CitContext *ptr = NULL;
775 struct CitContext *rem = NULL;
778 * Clean up the contexts.
779 * There are no threads so no critical_section stuff is needed.
783 /* We need to update the ContextList because some modules may want to itterate it
784 * Question is should we NULL it before iterating here or should we just keep updating it
785 * as we remove items?
787 * Answer is to NULL it first to prevent modules from doing any actions on the list at all
791 /* Remove the session from the active list */
795 CtdlLogPrintf(CTDL_DEBUG, "Purging session %d\n", ptr->cs_pid);
804 void close_masters (void)
806 struct ServiceFunctionHook *serviceptr;
809 * close all protocol master sockets
811 for (serviceptr = ServiceHookTable; serviceptr != NULL;
812 serviceptr = serviceptr->next ) {
814 if (serviceptr->tcp_port > 0)
816 CtdlLogPrintf(CTDL_INFO, "Closing listener on port %d\n",
817 serviceptr->tcp_port);
818 serviceptr->tcp_port = 0;
821 if (serviceptr->sockpath != NULL)
822 CtdlLogPrintf(CTDL_INFO, "Closing listener on '%s'\n",
823 serviceptr->sockpath);
825 close(serviceptr->msock);
826 /* If it's a Unix domain socket, remove the file. */
827 if (serviceptr->sockpath != NULL) {
828 unlink(serviceptr->sockpath);
829 serviceptr->sockpath = NULL;
836 * The system-dependent part of master_cleanup() - close the master socket.
838 void sysdep_master_cleanup(void) {
847 CtdlDestroyProtoHooks();
848 CtdlDestroyDeleteHooks();
849 CtdlDestroyXmsgHooks();
850 CtdlDestroyNetprocHooks();
851 CtdlDestroyUserHooks();
852 CtdlDestroyMessageHook();
853 CtdlDestroyCleanupHooks();
854 CtdlDestroyFixedOutputHooks();
855 CtdlDestroySessionHooks();
856 CtdlDestroyServiceHook();
857 CtdlDestroyRoomHooks();
858 CtdlDestroyDirectoryServiceFuncs();
859 #ifdef HAVE_BACKTRACE
867 * Terminate another session.
868 * (This could justifiably be moved out of sysdep.c because it
869 * no longer does anything that is system-dependent.)
871 void kill_session(int session_to_kill) {
872 struct CitContext *ptr;
874 begin_critical_section(S_SESSION_TABLE);
875 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
876 if (ptr->cs_pid == session_to_kill) {
880 end_critical_section(S_SESSION_TABLE);
884 void graceful_shutdown(int signum) {
885 kill(current_child, signum);
886 unlink(file_pid_file);
892 * Start running as a daemon.
894 void start_daemon(int unused) {
902 /* Close stdin/stdout/stderr and replace them with /dev/null.
903 * We don't just call close() because we don't want these fd's
904 * to be reused for other files.
913 signal(SIGHUP, SIG_IGN);
914 signal(SIGINT, SIG_IGN);
915 signal(SIGQUIT, SIG_IGN);
919 freopen("/dev/null", "r", stdin);
920 freopen("/dev/null", "w", stdout);
921 freopen("/dev/null", "w", stderr);
924 current_child = fork();
926 signal(SIGTERM, graceful_shutdown);
928 if (current_child < 0) {
933 else if (current_child == 0) {
934 return; /* continue starting citadel. */
938 fp = fopen(file_pid_file, "w");
940 fprintf(fp, ""F_PID_T"\n", getpid());
943 waitpid(current_child, &status, 0);
948 /* Did the main process exit with an actual exit code? */
949 if (WIFEXITED(status)) {
951 /* Exit code 0 means the watcher should exit */
952 if (WEXITSTATUS(status) == 0) {
956 /* Exit code 101-109 means the watcher should exit */
957 else if ( (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109) ) {
961 /* Any other exit code means we should restart. */
967 /* Any other type of termination (signals, etc.) should also restart. */
972 } while (do_restart);
974 unlink(file_pid_file);
975 exit(WEXITSTATUS(status));
981 * Generic routine to convert a login name to a full name (gecos)
982 * Returns nonzero if a conversion took place
984 int convert_login(char NameToConvert[]) {
988 pw = getpwnam(NameToConvert);
993 strcpy(NameToConvert, pw->pw_gecos);
994 for (a=0; a<strlen(NameToConvert); ++a) {
995 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
1002 * Purge all sessions which have the 'kill_me' flag set.
1003 * This function has code to prevent it from running more than once every
1004 * few seconds, because running it after every single unbind would waste a lot
1005 * of CPU time and keep the context list locked too much. To force it to run
1006 * anyway, set "force" to nonzero.
1008 void dead_session_purge(int force) {
1009 struct CitContext *ptr, *ptr2; /* general-purpose utility pointer */
1010 struct CitContext *rem = NULL; /* list of sessions to be destroyed */
1013 if ( (time(NULL) - last_purge) < 5 ) {
1014 return; /* Too soon, go away */
1019 if (try_critical_section(S_SESSION_TABLE))
1027 if ( (ptr2->state == CON_IDLE) && (ptr2->kill_me) ) {
1028 /* Remove the session from the active list */
1030 ptr2->prev->next = ptr2->next;
1033 ContextList = ptr2->next;
1036 ptr2->next->prev = ptr2->prev;
1040 /* And put it on our to-be-destroyed list */
1045 end_critical_section(S_SESSION_TABLE);
1047 /* Now that we no longer have the session list locked, we can take
1048 * our time and destroy any sessions on the to-be-killed list, which
1049 * is allocated privately on this thread's stack.
1051 while (rem != NULL) {
1052 CtdlLogPrintf(CTDL_DEBUG, "Purging session %d\n", rem->cs_pid);
1065 * masterCC is the context we use when not attached to a session. This
1066 * function initializes it.
1068 void InitializeMasterCC(void) {
1069 memset(&masterCC, 0, sizeof(struct CitContext));
1070 masterCC.internal_pgm = 1;
1071 masterCC.cs_pid = 0;
1078 * Bind a thread to a context. (It's inline merely to speed things up.)
1080 INLINE void become_session(struct CitContext *which_con) {
1081 citthread_setspecific(MyConKey, (void *)which_con );
1087 * This loop just keeps going and going and going...
1091 * This current implimentation of worker_thread creates a bottle neck in several situations
1092 * The first thing to remember is that a single thread can handle more than one connection at a time.
1093 * More threads mean less memory for the system to run in.
1094 * So for efficiency we want every thread to be doing something useful or waiting in the main loop for
1095 * something to happen anywhere.
1096 * This current implimentation requires worker threads to wait in other locations, after it has
1097 * been committed to a single connection which is very wasteful.
1098 * As an extreme case consider this:
1099 * A slow client connects and this slow client sends only one character each second.
1100 * With this current implimentation a single worker thread is dispatched to handle that connection
1101 * until such times as the client timeout expires, an error occurs on the socket or the client
1102 * completes its transmission.
1103 * THIS IS VERY BAD since that thread could have handled a read from many more clients in each one
1104 * second interval between chars.
1106 * It is my intention to re-write this code and the associated client_getln, client_read functions
1107 * to allow any thread to read data on behalf of any connection (context).
1108 * To do this I intend to have this main loop read chars into a buffer stored in the context.
1109 * Once the correct criteria for a full buffer is met then we will dispatch a thread to
1111 * This worker thread loop also needs to be able to handle binary data.
1114 void *worker_thread(void *arg) {
1117 struct CitContext *ptr;
1118 struct CitContext *bind_me = NULL;
1121 struct CitContext *con= NULL; /* Temporary context pointer */
1122 struct ServiceFunctionHook *serviceptr;
1123 int ssock; /* Descriptor for client socket */
1125 int force_purge = 0;
1129 while (!CtdlThreadCheckStop()) {
1131 /* make doubly sure we're not holding any stale db handles
1132 * which might cause a deadlock.
1134 cdb_check_handles();
1135 do_select: force_purge = 0;
1136 bind_me = NULL; /* Which session shall we handle? */
1138 /* Initialize the fdset. */
1142 begin_critical_section(S_SESSION_TABLE);
1143 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1144 if (ptr->state == CON_IDLE) {
1145 FD_SET(ptr->client_socket, &readfds);
1146 if (ptr->client_socket > highest)
1147 highest = ptr->client_socket;
1149 if ((bind_me == NULL) && (ptr->state == CON_READY)) {
1151 ptr->state = CON_EXECUTING;
1154 end_critical_section(S_SESSION_TABLE);
1160 /* If we got this far, it means that there are no sessions
1161 * which a previous thread marked for attention, so we go
1162 * ahead and get ready to select().
1165 /* First, add the various master sockets to the fdset. */
1166 for (serviceptr = ServiceHookTable; serviceptr != NULL;
1167 serviceptr = serviceptr->next ) {
1168 m = serviceptr->msock;
1169 FD_SET(m, &readfds);
1175 if (!CtdlThreadCheckStop()) {
1176 tv.tv_sec = 1; /* wake up every second if no input */
1178 retval = CtdlThreadSelect(highest + 1, &readfds, NULL, NULL, &tv);
1183 /* Now figure out who made this select() unblock.
1184 * First, check for an error or exit condition.
1187 if (errno == EBADF) {
1188 CtdlLogPrintf(CTDL_NOTICE, "select() failed: (%s)\n",
1192 if (errno != EINTR) {
1193 CtdlLogPrintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
1194 CtdlThreadStopAll();
1196 CtdlLogPrintf(CTDL_DEBUG, "Interrupted CtdlThreadSelect.\n");
1197 if (CtdlThreadCheckStop()) return(NULL);
1201 else if(retval == 0) {
1202 if (CtdlThreadCheckStop()) return(NULL);
1205 /* Next, check to see if it's a new client connecting
1206 * on a master socket.
1208 else for (serviceptr = ServiceHookTable; serviceptr != NULL;
1209 serviceptr = serviceptr->next ) {
1211 if (FD_ISSET(serviceptr->msock, &readfds)) {
1212 ssock = accept(serviceptr->msock, NULL, 0);
1214 CtdlLogPrintf(CTDL_DEBUG,
1215 "New client socket %d\n",
1218 /* The master socket is non-blocking but the client
1219 * sockets need to be blocking, otherwise certain
1220 * operations barf on FreeBSD. Not a fatal error.
1222 if (fcntl(ssock, F_SETFL, 0) < 0) {
1223 CtdlLogPrintf(CTDL_EMERG,
1224 "citserver: Can't set socket to blocking: %s\n",
1228 /* New context will be created already
1229 * set up in the CON_EXECUTING state.
1231 con = CreateNewContext();
1233 /* Assign our new socket number to it. */
1234 con->client_socket = ssock;
1235 con->h_command_function =
1236 serviceptr->h_command_function;
1237 con->h_async_function =
1238 serviceptr->h_async_function;
1240 serviceptr->ServiceName;
1242 /* Determine whether it's a local socket */
1243 if (serviceptr->sockpath != NULL)
1244 con->is_local_socket = 1;
1246 /* Set the SO_REUSEADDR socket option */
1248 setsockopt(ssock, SOL_SOCKET,
1252 become_session(con);
1254 serviceptr->h_greeting_function();
1255 become_session(NULL);
1256 con->state = CON_IDLE;
1262 /* It must be a client socket. Find a context that has data
1263 * waiting on its socket *and* is in the CON_IDLE state. Any
1264 * active sockets other than our chosen one are marked as
1265 * CON_READY so the next thread that comes around can just bind
1266 * to one without having to select() again.
1268 begin_critical_section(S_SESSION_TABLE);
1269 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1270 if ( (FD_ISSET(ptr->client_socket, &readfds))
1271 && (ptr->state != CON_EXECUTING) ) {
1272 ptr->input_waiting = 1;
1274 bind_me = ptr; /* I choose you! */
1275 bind_me->state = CON_EXECUTING;
1278 ptr->state = CON_READY;
1282 end_critical_section(S_SESSION_TABLE);
1285 /* We're bound to a session */
1286 if (bind_me != NULL) {
1287 become_session(bind_me);
1289 /* If the client has sent a command, execute it. */
1290 if (CC->input_waiting) {
1291 CC->h_command_function();
1292 CC->input_waiting = 0;
1295 /* If there are asynchronous messages waiting and the
1296 * client supports it, do those now */
1297 if ((CC->is_async) && (CC->async_waiting)
1298 && (CC->h_async_function != NULL)) {
1299 CC->h_async_function();
1300 CC->async_waiting = 0;
1303 force_purge = CC->kill_me;
1304 become_session(NULL);
1305 bind_me->state = CON_IDLE;
1308 dead_session_purge(force_purge);
1311 /* If control reaches this point, the server is shutting down */
1320 * Translate text facility name to syslog.h defined value.
1322 int SyslogFacility(char *name)
1331 { LOG_KERN, "kern" },
1332 { LOG_USER, "user" },
1333 { LOG_MAIL, "mail" },
1334 { LOG_DAEMON, "daemon" },
1335 { LOG_AUTH, "auth" },
1336 { LOG_SYSLOG, "syslog" },
1338 { LOG_NEWS, "news" },
1339 { LOG_UUCP, "uucp" },
1340 { LOG_LOCAL0, "local0" },
1341 { LOG_LOCAL1, "local1" },
1342 { LOG_LOCAL2, "local2" },
1343 { LOG_LOCAL3, "local3" },
1344 { LOG_LOCAL4, "local4" },
1345 { LOG_LOCAL5, "local5" },
1346 { LOG_LOCAL6, "local6" },
1347 { LOG_LOCAL7, "local7" },
1350 for(i = 0; facTbl[i].name != NULL; i++) {
1351 if(!strcasecmp(name, facTbl[i].name))
1352 return facTbl[i].facility;
1359 /********** MEM CHEQQER ***********/
1361 #ifdef DEBUG_MEMORY_LEAKS
1368 void *tracked_malloc(size_t size, char *file, int line) {
1369 struct igheap *thisheap;
1372 block = malloc(size);
1373 if (block == NULL) return(block);
1375 thisheap = malloc(sizeof(struct igheap));
1376 if (thisheap == NULL) {
1381 thisheap->block = block;
1382 strcpy(thisheap->file, file);
1383 thisheap->line = line;
1385 begin_critical_section(S_DEBUGMEMLEAKS);
1386 thisheap->next = igheap;
1388 end_critical_section(S_DEBUGMEMLEAKS);
1394 void *tracked_realloc(void *ptr, size_t size, char *file, int line) {
1395 struct igheap *thisheap;
1398 block = realloc(ptr, size);
1399 if (block == NULL) return(block);
1401 thisheap = malloc(sizeof(struct igheap));
1402 if (thisheap == NULL) {
1407 thisheap->block = block;
1408 strcpy(thisheap->file, file);
1409 thisheap->line = line;
1411 begin_critical_section(S_DEBUGMEMLEAKS);
1412 thisheap->next = igheap;
1414 end_critical_section(S_DEBUGMEMLEAKS);
1421 void tracked_free(void *ptr) {
1422 struct igheap *thisheap;
1423 struct igheap *trash;
1427 if (igheap == NULL) return;
1428 begin_critical_section(S_DEBUGMEMLEAKS);
1429 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1430 if (thisheap->next != NULL) {
1431 if (thisheap->next->block == ptr) {
1432 trash = thisheap->next;
1433 thisheap->next = thisheap->next->next;
1438 if (igheap->block == ptr) {
1440 igheap = igheap->next;
1443 end_critical_section(S_DEBUGMEMLEAKS);
1446 char *tracked_strdup(const char *s, char *file, int line) {
1449 if (s == NULL) return(NULL);
1450 ptr = tracked_malloc(strlen(s) + 1, file, line);
1451 if (ptr == NULL) return(NULL);
1452 strncpy(ptr, s, strlen(s));
1456 void dump_heap(void) {
1457 struct igheap *thisheap;
1459 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1460 CtdlLogPrintf(CTDL_CRIT, "UNFREED: %30s : %d\n",
1461 thisheap->file, thisheap->line);
1465 #endif /* DEBUG_MEMORY_LEAKS */