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.
26 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/syslog.h>
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
37 # include <sys/time.h>
44 #include <sys/resource.h>
45 #include <netinet/in.h>
46 #include <netinet/tcp.h>
47 #include <arpa/inet.h>
60 #include "serv_extensions.h"
61 #include "sysdep_decls.h"
62 #include "citserver.h"
66 #include "housekeeping.h"
68 #include "serv_crypto.h"
70 #ifdef HAVE_SYS_SELECT_H
71 #include <sys/select.h>
79 #ifdef DEBUG_MEMORY_LEAKS
87 struct igheap *igheap = NULL;
91 pthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */
92 pthread_key_t MyConKey; /* TSD key for MyContext() */
94 int verbosity = DEFAULT_VERBOSITY; /* Logging level */
96 struct CitContext masterCC;
97 time_t last_purge = 0; /* Last dead session purge */
98 static int num_threads = 0; /* Current number of threads */
99 int num_sessions = 0; /* Current number of sessions */
101 int syslog_facility = (-1);
102 int enable_syslog = 0;
103 extern int running_as_daemon;
106 * lprintf() ... Write logging information
108 void lprintf(enum LogLevel loglevel, const char *format, ...) {
112 va_start(arg_ptr, format);
113 vsyslog(loglevel, format, arg_ptr);
117 /* stderr output code */
118 if (enable_syslog || running_as_daemon) return;
120 /* if we run in forground and syslog is disabled, log to terminal */
121 if (loglevel <= verbosity) {
126 gettimeofday(&tv, NULL);
127 /* Promote to time_t; types differ on some OSes (like darwin) */
128 unixtime = tv.tv_sec;
129 localtime_r(&unixtime, &tim);
130 if (CC->cs_pid != 0) {
132 "%04d/%02d/%02d %2d:%02d:%02d.%06ld [%3d] ",
133 tim.tm_year + 1900, tim.tm_mon + 1,
134 tim.tm_mday, tim.tm_hour, tim.tm_min,
135 tim.tm_sec, (long)tv.tv_usec,
139 "%04d/%02d/%02d %2d:%02d:%02d.%06ld ",
140 tim.tm_year + 1900, tim.tm_mon + 1,
141 tim.tm_mday, tim.tm_hour, tim.tm_min,
142 tim.tm_sec, (long)tv.tv_usec);
144 va_start(arg_ptr, format);
145 vfprintf(stderr, format, arg_ptr);
154 * Signal handler to shut down the server.
157 volatile int time_to_die = 0;
159 static RETSIGTYPE signal_cleanup(int signum) {
160 lprintf(CTDL_DEBUG, "Caught signal %d; shutting down.\n", signum);
162 master_cleanup(signum);
167 * Some initialization stuff...
169 void init_sysdep(void) {
173 /* Avoid vulnerabilities related to FD_SETSIZE if we can. */
177 getrlimit(RLIMIT_NOFILE, &rl);
178 rl.rlim_cur = FD_SETSIZE;
179 rl.rlim_max = FD_SETSIZE;
180 setrlimit(RLIMIT_NOFILE, &rl);
184 /* If we've got OpenSSL, we're going to use it. */
189 /* Set up a bunch of semaphores to be used for critical sections */
190 for (i=0; i<MAX_SEMAPHORES; ++i) {
191 pthread_mutex_init(&Critters[i], NULL);
195 * Set up a place to put thread-specific data.
196 * We only need a single pointer per thread - it points to the
197 * CitContext structure (in the ContextList linked list) of the
198 * session to which the calling thread is currently bound.
200 if (pthread_key_create(&MyConKey, NULL) != 0) {
201 lprintf(CTDL_CRIT, "Can't create TSD key: %s\n",
206 * The action for unexpected signals and exceptions should be to
207 * call signal_cleanup() to gracefully shut down the server.
210 sigaddset(&set, SIGINT);
211 sigaddset(&set, SIGQUIT);
212 sigaddset(&set, SIGHUP);
213 sigaddset(&set, SIGTERM);
214 // sigaddset(&set, SIGSEGV); commented out because
215 // sigaddset(&set, SIGILL); we want core dumps
216 // sigaddset(&set, SIGBUS);
217 sigprocmask(SIG_UNBLOCK, &set, NULL);
219 signal(SIGINT, signal_cleanup);
220 signal(SIGQUIT, signal_cleanup);
221 signal(SIGHUP, signal_cleanup);
222 signal(SIGTERM, signal_cleanup);
223 // signal(SIGSEGV, signal_cleanup); commented out because
224 // signal(SIGILL, signal_cleanup); we want core dumps
225 // signal(SIGBUS, signal_cleanup);
228 * Do not shut down the server on broken pipe signals, otherwise the
229 * whole Citadel service would come down whenever a single client
232 signal(SIGPIPE, SIG_IGN);
237 * Obtain a semaphore lock to begin a critical section.
239 void begin_critical_section(int which_one)
241 /* lprintf(CTDL_DEBUG, "begin_critical_section(%d)\n", which_one); */
243 /* For all types of critical sections except those listed here,
244 * ensure nobody ever tries to do a critical section within a
245 * transaction; this could lead to deadlock.
247 if ( (which_one != S_FLOORCACHE)
248 #ifdef DEBUG_MEMORY_LEAKS
249 && (which_one != S_DEBUGMEMLEAKS)
254 pthread_mutex_lock(&Critters[which_one]);
258 * Release a semaphore lock to end a critical section.
260 void end_critical_section(int which_one)
262 pthread_mutex_unlock(&Critters[which_one]);
268 * This is a generic function to set up a master socket for listening on
269 * a TCP port. The server shuts down if the bind fails.
272 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
274 struct sockaddr_in sin;
276 int actual_queue_len;
278 actual_queue_len = queue_len;
279 if (actual_queue_len < 5) actual_queue_len = 5;
281 memset(&sin, 0, sizeof(sin));
282 sin.sin_family = AF_INET;
283 sin.sin_port = htons((u_short)port_number);
284 if (ip_addr == NULL) {
285 sin.sin_addr.s_addr = INADDR_ANY;
288 sin.sin_addr.s_addr = inet_addr(ip_addr);
291 if (sin.sin_addr.s_addr == INADDR_NONE) {
292 sin.sin_addr.s_addr = INADDR_ANY;
295 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
298 lprintf(CTDL_EMERG, "citserver: Can't create a socket: %s\n",
304 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
306 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
307 lprintf(CTDL_EMERG, "citserver: Can't bind: %s\n",
313 /* set to nonblock - we need this for some obscure situations */
314 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
316 "citserver: Can't set socket to non-blocking: %s\n",
322 if (listen(s, actual_queue_len) < 0) {
323 lprintf(CTDL_EMERG, "citserver: Can't listen: %s\n",
335 * Create a Unix domain socket and listen on it
337 int ig_uds_server(char *sockpath, int queue_len)
339 struct sockaddr_un addr;
342 int actual_queue_len;
344 actual_queue_len = queue_len;
345 if (actual_queue_len < 5) actual_queue_len = 5;
347 i = unlink(sockpath);
348 if (i != 0) if (errno != ENOENT) {
349 lprintf(CTDL_EMERG, "citserver: can't unlink %s: %s\n",
350 sockpath, strerror(errno));
354 memset(&addr, 0, sizeof(addr));
355 addr.sun_family = AF_UNIX;
356 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
358 s = socket(AF_UNIX, SOCK_STREAM, 0);
360 lprintf(CTDL_EMERG, "citserver: Can't create a socket: %s\n",
365 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
366 lprintf(CTDL_EMERG, "citserver: Can't bind: %s\n",
371 /* set to nonblock - we need this for some obscure situations */
372 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
374 "citserver: Can't set socket to non-blocking: %s\n",
380 if (listen(s, actual_queue_len) < 0) {
381 lprintf(CTDL_EMERG, "citserver: Can't listen: %s\n",
386 chmod(sockpath, 0777);
393 * Return a pointer to the CitContext structure bound to the thread which
394 * called this function. If there's no such binding (for example, if it's
395 * called by the housekeeper thread) then a generic 'master' CC is returned.
397 * This function is used *VERY* frequently and must be kept small.
399 struct CitContext *MyContext(void) {
401 register struct CitContext *c;
403 return ((c = (struct CitContext *) pthread_getspecific(MyConKey),
404 c == NULL) ? &masterCC : c
410 * Initialize a new context and place it in the list. The session number
411 * used to be the PID (which is why it's called cs_pid), but that was when we
412 * had one process per session. Now we just assign them sequentially, starting
413 * at 1 (don't change it to 0 because masterCC uses 0) and re-using them when
414 * sessions terminate.
416 struct CitContext *CreateNewContext(void) {
417 struct CitContext *me;
418 static int next_pid = 0;
420 me = (struct CitContext *) malloc(sizeof(struct CitContext));
422 lprintf(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;
434 * Generate a unique session number and insert this context into
437 begin_critical_section(S_SESSION_TABLE);
438 me->cs_pid = ++next_pid;
440 me->next = ContextList;
442 if (me->next != NULL) {
446 end_critical_section(S_SESSION_TABLE);
452 * The following functions implement output buffering. If the kernel supplies
453 * native TCP buffering (Linux & *BSD), use that; otherwise, emulate it with
454 * user-space buffering.
457 # define HAVE_TCP_BUFFERING
460 # define HAVE_TCP_BUFFERING
461 # define TCP_CORK TCP_NOPUSH
466 #ifdef HAVE_TCP_BUFFERING
467 static unsigned on = 1, off = 0;
468 void buffer_output(void) {
469 struct CitContext *ctx = MyContext();
470 setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
474 void unbuffer_output(void) {
475 struct CitContext *ctx = MyContext();
476 setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
480 void flush_output(void) {
481 struct CitContext *ctx = MyContext();
482 setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
483 setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
486 void buffer_output(void) {
487 if (CC->buffering == 0) {
490 CC->output_buffer = malloc(SIZ);
494 void flush_output(void) {
495 if (CC->buffering == 1) {
496 client_write(CC->output_buffer, CC->buffer_len);
501 void unbuffer_output(void) {
502 if (CC->buffering == 1) {
504 /* We don't call flush_output because we can't. */
505 client_write(CC->output_buffer, CC->buffer_len);
507 free(CC->output_buffer);
508 CC->output_buffer = NULL;
516 * client_write() ... Send binary data to the client.
518 void client_write(char *buf, int nbytes)
520 int bytes_written = 0;
522 #ifndef HAVE_TCP_BUFFERING
523 int old_buffer_len = 0;
526 if (CC->redirect_buffer != NULL) {
527 if ((CC->redirect_len + nbytes + 2) >= CC->redirect_alloc) {
528 CC->redirect_alloc = (CC->redirect_alloc * 2) + nbytes;
529 CC->redirect_buffer = realloc(CC->redirect_buffer,
532 memcpy(&CC->redirect_buffer[CC->redirect_len], buf, nbytes);
533 CC->redirect_len += nbytes;
534 CC->redirect_buffer[CC->redirect_len] = 0;
538 #ifndef HAVE_TCP_BUFFERING
539 /* If we're buffering for later, do that now. */
541 old_buffer_len = CC->buffer_len;
542 CC->buffer_len += nbytes;
543 CC->output_buffer = realloc(CC->output_buffer, CC->buffer_len);
544 memcpy(&CC->output_buffer[old_buffer_len], buf, nbytes);
549 /* Ok, at this point we're not buffering. Go ahead and write. */
552 if (CC->redirect_ssl) {
553 client_write_ssl(buf, nbytes);
558 while (bytes_written < nbytes) {
559 retval = write(CC->client_socket, &buf[bytes_written],
560 nbytes - bytes_written);
562 lprintf(CTDL_ERR, "client_write() failed: %s\n",
567 bytes_written = bytes_written + retval;
573 * cprintf() ... Send formatted printable data to the client. It is
574 * implemented in terms of client_write() but remains in
575 * sysdep.c in case we port to somewhere without va_args...
577 void cprintf(const char *format, ...) {
581 va_start(arg_ptr, format);
582 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
583 buf[sizeof buf - 2] = '\n';
584 client_write(buf, strlen(buf));
590 * Read data from the client socket.
592 * 1 Requested number of bytes has been read.
593 * 0 Request timed out.
594 * -1 The socket is broken.
595 * If the socket breaks, the session will be terminated.
597 int client_read_to(char *buf, int bytes, int timeout)
605 if (CC->redirect_ssl) {
606 return (client_read_ssl(buf, bytes, timeout));
612 FD_SET(CC->client_socket, &rfds);
616 retval = select( (CC->client_socket)+1,
617 &rfds, NULL, NULL, &tv);
619 if (FD_ISSET(CC->client_socket, &rfds) == 0) {
623 rlen = read(CC->client_socket, &buf[len], bytes-len);
625 lprintf(CTDL_ERR, "client_read() failed: %s\n",
636 * Read data from the client socket with default timeout.
637 * (This is implemented in terms of client_read_to() and could be
638 * justifiably moved out of sysdep.c)
640 INLINE int client_read(char *buf, int bytes)
642 return(client_read_to(buf, bytes, config.c_sleeping));
647 * client_getln() ... Get a LF-terminated line of text from the client.
648 * (This is implemented in terms of client_read() and could be
649 * justifiably moved out of sysdep.c)
651 int client_getln(char *buf, int bufsize)
655 /* Read one character at a time.
658 retval = client_read(&buf[i], 1);
659 if (retval != 1 || buf[i] == '\n' || i == (bufsize-1))
663 /* If we got a long line, discard characters until the newline.
665 if (i == (bufsize-1))
666 while (buf[i] != '\n' && retval == 1)
667 retval = client_read(&buf[i], 1);
669 /* Strip the trailing newline and any trailing nonprintables (cr's)
672 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
673 buf[strlen(buf)-1] = 0;
674 if (retval < 0) safestrncpy(buf, "000", bufsize);
681 * The system-dependent part of master_cleanup() - close the master socket.
683 void sysdep_master_cleanup(void) {
684 struct ServiceFunctionHook *serviceptr;
687 * close all protocol master sockets
689 for (serviceptr = ServiceHookTable; serviceptr != NULL;
690 serviceptr = serviceptr->next ) {
692 if (serviceptr->tcp_port > 0)
693 lprintf(CTDL_INFO, "Closing listener on port %d\n",
694 serviceptr->tcp_port);
696 if (serviceptr->sockpath != NULL)
697 lprintf(CTDL_INFO, "Closing listener on '%s'\n",
698 serviceptr->sockpath);
700 close(serviceptr->msock);
702 /* If it's a Unix domain socket, remove the file. */
703 if (serviceptr->sockpath != NULL) {
704 unlink(serviceptr->sockpath);
711 * Terminate another session.
712 * (This could justifiably be moved out of sysdep.c because it
713 * no longer does anything that is system-dependent.)
715 void kill_session(int session_to_kill) {
716 struct CitContext *ptr;
718 begin_critical_section(S_SESSION_TABLE);
719 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
720 if (ptr->cs_pid == session_to_kill) {
724 end_critical_section(S_SESSION_TABLE);
731 * Start running as a daemon.
733 void start_daemon(int unused) {
734 close(0); close(1); close(2);
737 signal(SIGHUP,SIG_IGN);
738 signal(SIGINT,SIG_IGN);
739 signal(SIGQUIT,SIG_IGN);
745 * Generic routine to convert a login name to a full name (gecos)
746 * Returns nonzero if a conversion took place
748 int convert_login(char NameToConvert[]) {
752 pw = getpwnam(NameToConvert);
757 strcpy(NameToConvert, pw->pw_gecos);
758 for (a=0; a<strlen(NameToConvert); ++a) {
759 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
765 struct worker_node *worker_list = NULL;
769 * create a worker thread. this function must always be called from within
770 * an S_WORKER_LIST critical section!
772 void create_worker(void) {
774 struct worker_node *n;
777 n = malloc(sizeof(struct worker_node));
779 lprintf(CTDL_EMERG, "can't allocate worker_node, exiting\n");
784 if ((ret = pthread_attr_init(&attr))) {
785 lprintf(CTDL_EMERG, "pthread_attr_init: %s\n", strerror(ret));
790 /* Our per-thread stacks need to be bigger than the default size, otherwise
791 * the MIME parser crashes on FreeBSD, and the IMAP service crashes on
794 if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
795 lprintf(CTDL_EMERG, "pthread_attr_setstacksize: %s\n", strerror(ret));
797 pthread_attr_destroy(&attr);
801 if ((ret = pthread_create(&n->tid, &attr, worker_thread, NULL) != 0))
804 lprintf(CTDL_ALERT, "Can't create worker thread: %s\n",
808 n->next = worker_list;
810 pthread_attr_destroy(&attr);
816 * Purge all sessions which have the 'kill_me' flag set.
817 * This function has code to prevent it from running more than once every
818 * few seconds, because running it after every single unbind would waste a lot
819 * of CPU time and keep the context list locked too much. To force it to run
820 * anyway, set "force" to nonzero.
823 * After that's done, we raise the size of the worker thread pool
824 * if such an action is appropriate.
826 void dead_session_purge(int force) {
827 struct CitContext *ptr; /* general-purpose utility pointer */
828 struct CitContext *rem = NULL; /* list of sessions to be destroyed */
831 if ( (time(NULL) - last_purge) < 5 ) {
832 return; /* Too soon, go away */
837 begin_critical_section(S_SESSION_TABLE);
838 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
839 if ( (ptr->state == CON_IDLE) && (ptr->kill_me) ) {
841 /* Remove the session from the active list */
843 ptr->prev->next = ptr->next;
846 ContextList = ptr->next;
849 ptr->next->prev = ptr->prev;
854 /* And put it on our to-be-destroyed list */
860 end_critical_section(S_SESSION_TABLE);
862 /* Now that we no longer have the session list locked, we can take
863 * our time and destroy any sessions on the to-be-killed list, which
864 * is allocated privately on this thread's stack.
866 while (rem != NULL) {
867 lprintf(CTDL_DEBUG, "Purging session %d\n", rem->cs_pid);
874 /* Raise the size of the worker thread pool if necessary. */
875 if ( (num_sessions > num_threads)
876 && (num_threads < config.c_max_workers) ) {
877 begin_critical_section(S_WORKER_LIST);
879 end_critical_section(S_WORKER_LIST);
888 * masterCC is the context we use when not attached to a session. This
889 * function initializes it.
891 void InitializeMasterCC(void) {
892 memset(&masterCC, 0, sizeof(struct CitContext));
893 masterCC.internal_pgm = 1;
903 * Bind a thread to a context. (It's inline merely to speed things up.)
905 INLINE void become_session(struct CitContext *which_con) {
906 pthread_setspecific(MyConKey, (void *)which_con );
912 * This loop just keeps going and going and going...
914 void *worker_thread(void *arg) {
917 struct CitContext *ptr;
918 struct CitContext *bind_me = NULL;
921 struct CitContext *con= NULL; /* Temporary context pointer */
922 struct ServiceFunctionHook *serviceptr;
923 int ssock; /* Descriptor for client socket */
932 while (!time_to_die) {
934 /* make doubly sure we're not holding any stale db handles
935 * which might cause a deadlock.
938 do_select: force_purge = 0;
939 bind_me = NULL; /* Which session shall we handle? */
941 /* Initialize the fdset. */
945 begin_critical_section(S_SESSION_TABLE);
946 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
947 if (ptr->state == CON_IDLE) {
948 FD_SET(ptr->client_socket, &readfds);
949 if (ptr->client_socket > highest)
950 highest = ptr->client_socket;
952 if ((bind_me == NULL) && (ptr->state == CON_READY)) {
954 ptr->state = CON_EXECUTING;
957 end_critical_section(S_SESSION_TABLE);
963 /* If we got this far, it means that there are no sessions
964 * which a previous thread marked for attention, so we go
965 * ahead and get ready to select().
968 /* First, add the various master sockets to the fdset. */
969 for (serviceptr = ServiceHookTable; serviceptr != NULL;
970 serviceptr = serviceptr->next ) {
971 m = serviceptr->msock;
979 tv.tv_sec = 1; /* wake up every second if no input */
981 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
984 if (time_to_die) return(NULL);
986 /* Now figure out who made this select() unblock.
987 * First, check for an error or exit condition.
990 if (errno == EBADF) {
991 lprintf(CTDL_NOTICE, "select() failed: (%s)\n",
995 if (errno != EINTR) {
996 lprintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
998 } else if (!time_to_die)
1002 /* Next, check to see if it's a new client connecting
1003 * on a master socket.
1005 else for (serviceptr = ServiceHookTable; serviceptr != NULL;
1006 serviceptr = serviceptr->next ) {
1008 if (FD_ISSET(serviceptr->msock, &readfds)) {
1009 ssock = accept(serviceptr->msock, NULL, 0);
1012 "New client socket %d\n",
1015 /* New context will be created already
1016 * set up in the CON_EXECUTING state.
1018 con = CreateNewContext();
1020 /* Assign new socket number to it. */
1021 con->client_socket = ssock;
1022 con->h_command_function =
1023 serviceptr->h_command_function;
1024 con->h_async_function =
1025 serviceptr->h_async_function;
1027 /* Determine whether local socket */
1028 if (serviceptr->sockpath != NULL)
1029 con->is_local_socket = 1;
1031 /* Set the SO_REUSEADDR socket option */
1033 setsockopt(ssock, SOL_SOCKET,
1037 become_session(con);
1039 serviceptr->h_greeting_function();
1040 become_session(NULL);
1041 con->state = CON_IDLE;
1047 /* It must be a client socket. Find a context that has data
1048 * waiting on its socket *and* is in the CON_IDLE state. Any
1049 * active sockets other than our chosen one are marked as
1050 * CON_READY so the next thread that comes around can just bind
1051 * to one without having to select() again.
1053 begin_critical_section(S_SESSION_TABLE);
1054 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1055 if ( (FD_ISSET(ptr->client_socket, &readfds))
1056 && (ptr->state != CON_EXECUTING) ) {
1057 ptr->input_waiting = 1;
1059 bind_me = ptr; /* I choose you! */
1060 bind_me->state = CON_EXECUTING;
1063 ptr->state = CON_READY;
1067 end_critical_section(S_SESSION_TABLE);
1070 /* We're bound to a session */
1071 if (bind_me != NULL) {
1072 become_session(bind_me);
1074 /* If the client has sent a command, execute it. */
1075 if (CC->input_waiting) {
1076 CC->h_command_function();
1077 CC->input_waiting = 0;
1080 /* If there are asynchronous messages waiting and the
1081 * client supports it, do those now */
1082 if ((CC->is_async) && (CC->async_waiting)
1083 && (CC->h_async_function != NULL)) {
1084 CC->h_async_function();
1085 CC->async_waiting = 0;
1088 force_purge = CC->kill_me;
1089 become_session(NULL);
1090 bind_me->state = CON_IDLE;
1093 dead_session_purge(force_purge);
1095 check_sched_shutdown();
1098 /* If control reaches this point, the server is shutting down */
1107 * Translate text facility name to syslog.h defined value.
1109 int SyslogFacility(char *name)
1118 { LOG_KERN, "kern" },
1119 { LOG_USER, "user" },
1120 { LOG_MAIL, "mail" },
1121 { LOG_DAEMON, "daemon" },
1122 { LOG_AUTH, "auth" },
1123 { LOG_SYSLOG, "syslog" },
1125 { LOG_NEWS, "news" },
1126 { LOG_UUCP, "uucp" },
1127 { LOG_LOCAL0, "local0" },
1128 { LOG_LOCAL1, "local1" },
1129 { LOG_LOCAL2, "local2" },
1130 { LOG_LOCAL3, "local3" },
1131 { LOG_LOCAL4, "local4" },
1132 { LOG_LOCAL5, "local5" },
1133 { LOG_LOCAL6, "local6" },
1134 { LOG_LOCAL7, "local7" },
1137 for(i = 0; facTbl[i].name != NULL; i++) {
1138 if(!strcasecmp(name, facTbl[i].name))
1139 return facTbl[i].facility;
1146 /********** MEM CHEQQER ***********/
1148 #ifdef DEBUG_MEMORY_LEAKS
1155 void *tracked_malloc(size_t size, char *file, int line) {
1156 struct igheap *thisheap;
1159 block = malloc(size);
1160 if (block == NULL) return(block);
1162 thisheap = malloc(sizeof(struct igheap));
1163 if (thisheap == NULL) {
1168 thisheap->block = block;
1169 strcpy(thisheap->file, file);
1170 thisheap->line = line;
1172 begin_critical_section(S_DEBUGMEMLEAKS);
1173 thisheap->next = igheap;
1175 end_critical_section(S_DEBUGMEMLEAKS);
1181 void *tracked_realloc(void *ptr, size_t size, char *file, int line) {
1182 struct igheap *thisheap;
1185 block = realloc(ptr, size);
1186 if (block == NULL) return(block);
1188 thisheap = malloc(sizeof(struct igheap));
1189 if (thisheap == NULL) {
1194 thisheap->block = block;
1195 strcpy(thisheap->file, file);
1196 thisheap->line = line;
1198 begin_critical_section(S_DEBUGMEMLEAKS);
1199 thisheap->next = igheap;
1201 end_critical_section(S_DEBUGMEMLEAKS);
1208 void tracked_free(void *ptr) {
1209 struct igheap *thisheap;
1210 struct igheap *trash;
1214 if (igheap == NULL) return;
1215 begin_critical_section(S_DEBUGMEMLEAKS);
1216 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1217 if (thisheap->next != NULL) {
1218 if (thisheap->next->block == ptr) {
1219 trash = thisheap->next;
1220 thisheap->next = thisheap->next->next;
1225 if (igheap->block == ptr) {
1227 igheap = igheap->next;
1230 end_critical_section(S_DEBUGMEMLEAKS);
1233 char *tracked_strdup(const char *s, char *file, int line) {
1236 if (s == NULL) return(NULL);
1237 ptr = tracked_malloc(strlen(s) + 1, file, line);
1238 if (ptr == NULL) return(NULL);
1239 strncpy(ptr, s, strlen(s));
1243 void dump_heap(void) {
1244 struct igheap *thisheap;
1246 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1247 lprintf(CTDL_CRIT, "UNFREED: %30s : %d\n",
1248 thisheap->file, thisheap->line);
1252 #endif /* DEBUG_MEMORY_LEAKS */