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);
105 * lprintf() ... Write logging information
107 * Note: the variable "buf" below needs to be large enough to handle any
108 * log data sent through this function. BE CAREFUL!
110 extern int running_as_daemon;
111 static int enable_syslog = 1;
112 void lprintf(enum LogLevel loglevel, const char *format, ...) {
116 va_start(arg_ptr, format);
117 vsyslog(loglevel, format, arg_ptr);
121 if (enable_syslog && LogHookTable == 0) return;
123 /* legacy output code; hooks get processed first */
125 va_start(arg_ptr, format);
126 vsnprintf(buf, sizeof(buf), format, arg_ptr);
128 PerformLogHooks(loglevel, buf);
130 if (enable_syslog || running_as_daemon) return;
132 /* if we run in forground and syslog is disabled, log to terminal */
133 if (loglevel <= verbosity) {
138 gettimeofday(&tv, NULL);
139 /* Promote to time_t; types differ on some OSes (like darwin) */
140 unixtime = tv.tv_sec;
141 localtime_r(&unixtime, &tim);
142 if (CC->cs_pid != 0) {
144 "%04d/%02d/%02d %2d:%02d:%02d.%06ld [%3d] %s",
145 tim.tm_year + 1900, tim.tm_mon + 1,
146 tim.tm_mday, tim.tm_hour, tim.tm_min,
147 tim.tm_sec, (long)tv.tv_usec,
151 "%04d/%02d/%02d %2d:%02d:%02d.%06ld %s",
152 tim.tm_year + 1900, tim.tm_mon + 1,
153 tim.tm_mday, tim.tm_hour, tim.tm_min,
154 tim.tm_sec, (long)tv.tv_usec, buf);
163 * Signal handler to shut down the server.
166 volatile int time_to_die = 0;
168 static RETSIGTYPE signal_cleanup(int signum) {
169 lprintf(CTDL_DEBUG, "Caught signal %d; shutting down.\n", signum);
171 master_cleanup(signum);
176 * Some initialization stuff...
178 void init_sysdep(void) {
182 /* Avoid vulnerabilities related to FD_SETSIZE if we can. */
186 getrlimit(RLIMIT_NOFILE, &rl);
187 rl.rlim_cur = FD_SETSIZE;
188 rl.rlim_max = FD_SETSIZE;
189 setrlimit(RLIMIT_NOFILE, &rl);
193 /* If we've got OpenSSL, we're going to use it. */
198 /* Set up a bunch of semaphores to be used for critical sections */
199 for (i=0; i<MAX_SEMAPHORES; ++i) {
200 pthread_mutex_init(&Critters[i], NULL);
204 * Set up a place to put thread-specific data.
205 * We only need a single pointer per thread - it points to the
206 * CitContext structure (in the ContextList linked list) of the
207 * session to which the calling thread is currently bound.
209 if (pthread_key_create(&MyConKey, NULL) != 0) {
210 lprintf(CTDL_CRIT, "Can't create TSD key!! %s\n", strerror(errno));
214 * The action for unexpected signals and exceptions should be to
215 * call signal_cleanup() to gracefully shut down the server.
218 sigaddset(&set, SIGINT);
219 sigaddset(&set, SIGQUIT);
220 sigaddset(&set, SIGHUP);
221 sigaddset(&set, SIGTERM);
222 sigaddset(&set, SIGSEGV);
223 sigprocmask(SIG_UNBLOCK, &set, NULL);
224 signal(SIGINT, signal_cleanup);
225 signal(SIGQUIT, signal_cleanup);
226 signal(SIGHUP, signal_cleanup);
227 signal(SIGTERM, signal_cleanup);
228 signal(SIGSEGV, signal_cleanup);
231 * Do not shut down the server on broken pipe signals, otherwise the
232 * whole Citadel service would come down whenever a single client
235 signal(SIGPIPE, SIG_IGN);
240 * Obtain a semaphore lock to begin a critical section.
242 void begin_critical_section(int which_one)
244 /* lprintf(CTDL_DEBUG, "begin_critical_section(%d)\n", which_one); */
246 /* For all types of critical sections except those listed here,
247 * ensure nobody ever tries to do a critical section within a
248 * transaction; this could lead to deadlock.
250 if ( (which_one != S_FLOORCACHE)
251 #ifdef DEBUG_MEMORY_LEAKS
252 && (which_one != S_DEBUGMEMLEAKS)
257 pthread_mutex_lock(&Critters[which_one]);
261 * Release a semaphore lock to end a critical section.
263 void end_critical_section(int which_one)
265 pthread_mutex_unlock(&Critters[which_one]);
271 * This is a generic function to set up a master socket for listening on
272 * a TCP port. The server shuts down if the bind fails.
275 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
277 struct sockaddr_in sin;
279 int actual_queue_len;
281 actual_queue_len = queue_len;
282 if (actual_queue_len < 5) actual_queue_len = 5;
284 memset(&sin, 0, sizeof(sin));
285 sin.sin_family = AF_INET;
286 sin.sin_port = htons((u_short)port_number);
287 if (ip_addr == NULL) {
288 sin.sin_addr.s_addr = INADDR_ANY;
291 sin.sin_addr.s_addr = inet_addr(ip_addr);
294 if (sin.sin_addr.s_addr == INADDR_NONE) {
295 sin.sin_addr.s_addr = INADDR_ANY;
298 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
301 lprintf(CTDL_EMERG, "citserver: Can't create a socket: %s\n",
307 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
309 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
310 lprintf(CTDL_EMERG, "citserver: Can't bind: %s\n",
316 if (listen(s, actual_queue_len) < 0) {
317 lprintf(CTDL_EMERG, "citserver: Can't listen: %s\n", strerror(errno));
328 * Create a Unix domain socket and listen on it
330 int ig_uds_server(char *sockpath, int queue_len)
332 struct sockaddr_un addr;
335 int actual_queue_len;
337 actual_queue_len = queue_len;
338 if (actual_queue_len < 5) actual_queue_len = 5;
340 i = unlink(sockpath);
341 if (i != 0) if (errno != ENOENT) {
342 lprintf(CTDL_EMERG, "citserver: can't unlink %s: %s\n",
343 sockpath, strerror(errno));
347 memset(&addr, 0, sizeof(addr));
348 addr.sun_family = AF_UNIX;
349 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
351 s = socket(AF_UNIX, SOCK_STREAM, 0);
353 lprintf(CTDL_EMERG, "citserver: Can't create a socket: %s\n",
358 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
359 lprintf(CTDL_EMERG, "citserver: Can't bind: %s\n",
364 if (listen(s, actual_queue_len) < 0) {
365 lprintf(CTDL_EMERG, "citserver: Can't listen: %s\n", strerror(errno));
369 chmod(sockpath, 0777);
376 * Return a pointer to the CitContext structure bound to the thread which
377 * called this function. If there's no such binding (for example, if it's
378 * called by the housekeeper thread) then a generic 'master' CC is returned.
380 * This function is used *VERY* frequently and must be kept small.
382 struct CitContext *MyContext(void) {
384 register struct CitContext *c;
386 return ((c = (struct CitContext *) pthread_getspecific(MyConKey),
387 c == NULL) ? &masterCC : c
393 * Initialize a new context and place it in the list. The session number
394 * used to be the PID (which is why it's called cs_pid), but that was when we
395 * had one process per session. Now we just assign them sequentially, starting
396 * at 1 (don't change it to 0 because masterCC uses 0) and re-using them when
397 * sessions terminate.
399 struct CitContext *CreateNewContext(void) {
400 struct CitContext *me, *ptr;
402 me = (struct CitContext *) malloc(sizeof(struct CitContext));
404 lprintf(CTDL_ALERT, "citserver: can't allocate memory!!\n");
407 memset(me, 0, sizeof(struct CitContext));
409 /* The new context will be created already in the CON_EXECUTING state
410 * in order to prevent another thread from grabbing it while it's
413 me->state = CON_EXECUTING;
417 * Generate a unique session number and insert this context into
420 begin_critical_section(S_SESSION_TABLE);
422 if (ContextList == NULL) {
429 else if (ContextList->cs_pid > 1) {
431 me->next = ContextList;
437 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
438 if (ptr->next == NULL) {
440 me->cs_pid = ptr->cs_pid + 1;
445 else if (ptr->next->cs_pid > (ptr->cs_pid+1)) {
447 me->next = ptr->next;
448 ptr->next->prev = me;
450 me->cs_pid = ptr->cs_pid + 1;
456 DONE: ++num_sessions;
457 end_critical_section(S_SESSION_TABLE);
463 * The following functions implement output buffering. If the kernel supplies
464 * native TCP buffering (Linux & *BSD), use that; otherwise, emulate it with
465 * user-space buffering.
468 # define HAVE_TCP_BUFFERING
471 # define HAVE_TCP_BUFFERING
472 # define TCP_CORK TCP_NOPUSH
477 #ifdef HAVE_TCP_BUFFERING
478 static unsigned on = 1, off = 0;
479 void buffer_output(void) {
480 struct CitContext *ctx = MyContext();
481 setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
485 void unbuffer_output(void) {
486 struct CitContext *ctx = MyContext();
487 setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
491 void flush_output(void) {
492 struct CitContext *ctx = MyContext();
493 setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
494 setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
497 void buffer_output(void) {
498 if (CC->buffering == 0) {
501 CC->output_buffer = malloc(SIZ);
505 void flush_output(void) {
506 if (CC->buffering == 1) {
507 client_write(CC->output_buffer, CC->buffer_len);
512 void unbuffer_output(void) {
513 if (CC->buffering == 1) {
515 /* We don't call flush_output because we can't. */
516 client_write(CC->output_buffer, CC->buffer_len);
518 free(CC->output_buffer);
519 CC->output_buffer = NULL;
527 * client_write() ... Send binary data to the client.
529 void client_write(char *buf, int nbytes)
531 int bytes_written = 0;
534 #ifndef HAVE_TCP_BUFFERING
535 int old_buffer_len = 0;
538 if (CC->redirect_fp != NULL) {
539 fwrite(buf, (size_t)nbytes, (size_t)1, CC->redirect_fp);
543 if (CC->redirect_sock > 0) {
544 sock = CC->redirect_sock; /* and continue below... */
547 sock = CC->client_socket;
550 #ifndef HAVE_TCP_BUFFERING
551 /* If we're buffering for later, do that now. */
553 old_buffer_len = CC->buffer_len;
554 CC->buffer_len += nbytes;
555 CC->output_buffer = realloc(CC->output_buffer, CC->buffer_len);
556 memcpy(&CC->output_buffer[old_buffer_len], buf, nbytes);
561 /* Ok, at this point we're not buffering. Go ahead and write. */
564 if (CC->redirect_ssl) {
565 client_write_ssl(buf, nbytes);
570 while (bytes_written < nbytes) {
571 retval = write(sock, &buf[bytes_written],
572 nbytes - bytes_written);
574 lprintf(CTDL_ERR, "client_write() failed: %s\n",
576 if (sock == CC->client_socket) CC->kill_me = 1;
579 bytes_written = bytes_written + retval;
585 * cprintf() ... Send formatted printable data to the client. It is
586 * implemented in terms of client_write() but remains in
587 * sysdep.c in case we port to somewhere without va_args...
589 void cprintf(const char *format, ...) {
593 va_start(arg_ptr, format);
594 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
595 buf[sizeof buf - 2] = '\n';
596 client_write(buf, strlen(buf));
602 * Read data from the client socket.
604 * 1 Requested number of bytes has been read.
605 * 0 Request timed out.
606 * -1 The socket is broken.
607 * If the socket breaks, the session will be terminated.
609 int client_read_to(char *buf, int bytes, int timeout)
617 if (CC->redirect_ssl) {
618 return (client_read_ssl(buf, bytes, timeout));
624 FD_SET(CC->client_socket, &rfds);
628 retval = select( (CC->client_socket)+1,
629 &rfds, NULL, NULL, &tv);
631 if (FD_ISSET(CC->client_socket, &rfds) == 0) {
635 rlen = read(CC->client_socket, &buf[len], bytes-len);
637 lprintf(CTDL_ERR, "client_read() failed: %s\n",
648 * Read data from the client socket with default timeout.
649 * (This is implemented in terms of client_read_to() and could be
650 * justifiably moved out of sysdep.c)
652 INLINE int client_read(char *buf, int bytes)
654 return(client_read_to(buf, bytes, config.c_sleeping));
659 * client_getln() ... Get a LF-terminated line of text from the client.
660 * (This is implemented in terms of client_read() and could be
661 * justifiably moved out of sysdep.c)
663 int client_getln(char *buf, int bufsize)
667 /* Read one character at a time.
670 retval = client_read(&buf[i], 1);
671 if (retval != 1 || buf[i] == '\n' || i == (bufsize-1))
675 /* If we got a long line, discard characters until the newline.
677 if (i == (bufsize-1))
678 while (buf[i] != '\n' && retval == 1)
679 retval = client_read(&buf[i], 1);
681 /* Strip the trailing newline and any trailing nonprintables (cr's)
684 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
685 buf[strlen(buf)-1] = 0;
686 if (retval < 0) strcpy(buf, "000");
693 * The system-dependent part of master_cleanup() - close the master socket.
695 void sysdep_master_cleanup(void) {
696 struct ServiceFunctionHook *serviceptr;
699 * close all protocol master sockets
701 for (serviceptr = ServiceHookTable; serviceptr != NULL;
702 serviceptr = serviceptr->next ) {
704 if (serviceptr->tcp_port > 0)
705 lprintf(CTDL_INFO, "Closing listener on port %d\n",
706 serviceptr->tcp_port);
708 if (serviceptr->sockpath != NULL)
709 lprintf(CTDL_INFO, "Closing listener on '%s'\n",
710 serviceptr->sockpath);
712 close(serviceptr->msock);
714 /* If it's a Unix domain socket, remove the file. */
715 if (serviceptr->sockpath != NULL) {
716 unlink(serviceptr->sockpath);
723 * Terminate another session.
724 * (This could justifiably be moved out of sysdep.c because it
725 * no longer does anything that is system-dependent.)
727 void kill_session(int session_to_kill) {
728 struct CitContext *ptr;
730 begin_critical_section(S_SESSION_TABLE);
731 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
732 if (ptr->cs_pid == session_to_kill) {
736 end_critical_section(S_SESSION_TABLE);
743 * Start running as a daemon.
745 void start_daemon(int unused) {
746 close(0); close(1); close(2);
749 signal(SIGHUP,SIG_IGN);
750 signal(SIGINT,SIG_IGN);
751 signal(SIGQUIT,SIG_IGN);
757 * Generic routine to convert a login name to a full name (gecos)
758 * Returns nonzero if a conversion took place
760 int convert_login(char NameToConvert[]) {
764 pw = getpwnam(NameToConvert);
769 strcpy(NameToConvert, pw->pw_gecos);
770 for (a=0; a<strlen(NameToConvert); ++a) {
771 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
777 struct worker_node *worker_list = NULL;
781 * create a worker thread. this function must always be called from within
782 * an S_WORKER_LIST critical section!
784 void create_worker(void) {
786 struct worker_node *n;
789 n = malloc(sizeof(struct worker_node));
791 lprintf(CTDL_EMERG, "can't allocate worker_node, exiting\n");
796 if ((ret = pthread_attr_init(&attr))) {
797 lprintf(CTDL_EMERG, "pthread_attr_init: %s\n", strerror(ret));
802 /* Our per-thread stacks need to be bigger than the default size, otherwise
803 * the MIME parser crashes on FreeBSD, and the IMAP service crashes on
806 if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
807 lprintf(CTDL_EMERG, "pthread_attr_setstacksize: %s\n", strerror(ret));
809 pthread_attr_destroy(&attr);
813 if ((ret = pthread_create(&n->tid, &attr, worker_thread, NULL) != 0))
816 lprintf(CTDL_ALERT, "Can't create worker thread: %s\n",
820 n->next = worker_list;
822 pthread_attr_destroy(&attr);
828 * Purge all sessions which have the 'kill_me' flag set.
829 * This function has code to prevent it from running more than once every
830 * few seconds, because running it after every single unbind would waste a lot
831 * of CPU time and keep the context list locked too much. To force it to run
832 * anyway, set "force" to nonzero.
835 * After that's done, we raise the size of the worker thread pool
836 * if such an action is appropriate.
838 void dead_session_purge(int force) {
839 struct CitContext *ptr, *rem;
842 if ( (time(NULL) - last_purge) < 5 ) {
843 return; /* Too soon, go away */
850 begin_critical_section(S_SESSION_TABLE);
851 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
852 if ( (ptr->state == CON_IDLE) && (ptr->kill_me) ) {
856 end_critical_section(S_SESSION_TABLE);
858 /* RemoveContext() enters its own S_SESSION_TABLE critical
859 * section, so we have to do it like this.
862 lprintf(CTDL_DEBUG, "Purging session %d\n", rem->cs_pid);
866 } while (rem != NULL);
868 /* Raise the size of the worker thread pool if necessary. */
870 if ( (num_sessions > num_threads)
871 && (num_threads < config.c_max_workers) ) {
872 begin_critical_section(S_WORKER_LIST);
874 end_critical_section(S_WORKER_LIST);
883 * Redirect a session's output to a file or socket.
884 * This function may be called with a file handle *or* a socket (but not
885 * both). Call with neither to return output to its normal client socket.
887 void CtdlRedirectOutput(FILE *fp, int sock) {
889 if (fp != NULL) CC->redirect_fp = fp;
890 else CC->redirect_fp = NULL;
892 if (sock > 0) CC->redirect_sock = sock;
893 else CC->redirect_sock = (-1);
899 * masterCC is the context we use when not attached to a session. This
900 * function initializes it.
902 void InitializeMasterCC(void) {
903 memset(&masterCC, 0, sizeof(struct CitContext));
904 masterCC.internal_pgm = 1;
914 * Bind a thread to a context. (It's inline merely to speed things up.)
916 INLINE void become_session(struct CitContext *which_con) {
917 pthread_setspecific(MyConKey, (void *)which_con );
923 * This loop just keeps going and going and going...
925 void *worker_thread(void *arg) {
928 struct CitContext *ptr;
929 struct CitContext *bind_me = NULL;
932 struct CitContext *con= NULL; /* Temporary context pointer */
933 struct ServiceFunctionHook *serviceptr;
934 int ssock; /* Descriptor for client socket */
943 while (!time_to_die) {
945 /* make doubly sure we're not holding any stale db handles
946 * which might cause a deadlock.
949 do_select: force_purge = 0;
950 bind_me = NULL; /* Which session shall we handle? */
952 /* Initialize the fdset. */
956 begin_critical_section(S_SESSION_TABLE);
957 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
958 if (ptr->state == CON_IDLE) {
959 FD_SET(ptr->client_socket, &readfds);
960 if (ptr->client_socket > highest)
961 highest = ptr->client_socket;
963 if ((bind_me == NULL) && (ptr->state == CON_READY)) {
965 ptr->state = CON_EXECUTING;
968 end_critical_section(S_SESSION_TABLE);
970 if (bind_me) goto SKIP_SELECT;
972 /* If we got this far, it means that there are no sessions
973 * which a previous thread marked for attention, so we go
974 * ahead and get ready to select().
977 /* First, add the various master sockets to the fdset. */
978 for (serviceptr = ServiceHookTable; serviceptr != NULL;
979 serviceptr = serviceptr->next ) {
980 m = serviceptr->msock;
988 tv.tv_sec = 1; /* wake up every second if no input */
990 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
993 if (time_to_die) return(NULL);
995 /* Now figure out who made this select() unblock.
996 * First, check for an error or exit condition.
999 if (errno == EBADF) {
1000 lprintf(CTDL_NOTICE, "select() failed: (%s)\n",
1004 if (errno != EINTR) {
1005 lprintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
1007 } else if (!time_to_die)
1011 /* Next, check to see if it's a new client connecting
1012 * on a master socket.
1014 else for (serviceptr = ServiceHookTable; serviceptr != NULL;
1015 serviceptr = serviceptr->next ) {
1017 if (FD_ISSET(serviceptr->msock, &readfds)) {
1018 ssock = accept(serviceptr->msock, NULL, 0);
1021 "citserver: accept(): %s\n",
1026 "New client socket %d\n",
1029 /* New context will be created already
1030 * set up in the CON_EXECUTING state.
1032 con = CreateNewContext();
1034 /* Assign new socket number to it. */
1035 con->client_socket = ssock;
1036 con->h_command_function =
1037 serviceptr->h_command_function;
1038 con->h_async_function =
1039 serviceptr->h_async_function;
1041 /* Determine whether local socket */
1042 if (serviceptr->sockpath != NULL)
1043 con->is_local_socket = 1;
1045 /* Set the SO_REUSEADDR socket option */
1047 setsockopt(ssock, SOL_SOCKET,
1051 become_session(con);
1053 serviceptr->h_greeting_function();
1054 become_session(NULL);
1055 con->state = CON_IDLE;
1061 /* It must be a client socket. Find a context that has data
1062 * waiting on its socket *and* is in the CON_IDLE state. Any
1063 * active sockets other than our chosen one are marked as
1064 * CON_READY so the next thread that comes around can just bind
1065 * to one without having to select() again.
1067 begin_critical_section(S_SESSION_TABLE);
1068 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1069 if ( (FD_ISSET(ptr->client_socket, &readfds))
1070 && (ptr->state != CON_EXECUTING) ) {
1071 ptr->input_waiting = 1;
1073 bind_me = ptr; /* I choose you! */
1074 bind_me->state = CON_EXECUTING;
1077 ptr->state = CON_READY;
1081 end_critical_section(S_SESSION_TABLE);
1084 /* We're bound to a session */
1085 if (bind_me != NULL) {
1086 become_session(bind_me);
1088 /* If the client has sent a command, execute it. */
1089 if (CC->input_waiting) {
1090 CC->h_command_function();
1091 CC->input_waiting = 0;
1094 /* If there are asynchronous messages waiting and the
1095 * client supports it, do those now */
1096 if ((CC->is_async) && (CC->async_waiting)
1097 && (CC->h_async_function != NULL)) {
1098 CC->h_async_function();
1099 CC->async_waiting = 0;
1102 force_purge = CC->kill_me;
1103 become_session(NULL);
1104 bind_me->state = CON_IDLE;
1107 dead_session_purge(force_purge);
1109 check_sched_shutdown();
1112 /* If control reaches this point, the server is shutting down */
1121 * Translate text facility name to syslog.h defined value.
1123 int SyslogFacility(char *name)
1132 { LOG_KERN, "kern" },
1133 { LOG_USER, "user" },
1134 { LOG_MAIL, "mail" },
1135 { LOG_DAEMON, "daemon" },
1136 { LOG_AUTH, "auth" },
1137 { LOG_SYSLOG, "syslog" },
1139 { LOG_NEWS, "news" },
1140 { LOG_UUCP, "uucp" },
1141 { LOG_LOCAL0, "local0" },
1142 { LOG_LOCAL1, "local1" },
1143 { LOG_LOCAL2, "local2" },
1144 { LOG_LOCAL3, "local3" },
1145 { LOG_LOCAL4, "local4" },
1146 { LOG_LOCAL5, "local5" },
1147 { LOG_LOCAL6, "local6" },
1148 { LOG_LOCAL7, "local7" },
1151 for(i = 0; facTbl[i].name != NULL; i++) {
1152 if(!strcasecmp(name, facTbl[i].name))
1153 return facTbl[i].facility;
1160 /********** MEM CHEQQER ***********/
1162 #ifdef DEBUG_MEMORY_LEAKS
1169 void *tracked_malloc(size_t size, char *file, int line) {
1170 struct igheap *thisheap;
1173 block = malloc(size);
1174 if (block == NULL) return(block);
1176 thisheap = malloc(sizeof(struct igheap));
1177 if (thisheap == NULL) {
1182 thisheap->block = block;
1183 strcpy(thisheap->file, file);
1184 thisheap->line = line;
1186 begin_critical_section(S_DEBUGMEMLEAKS);
1187 thisheap->next = igheap;
1189 end_critical_section(S_DEBUGMEMLEAKS);
1195 void *tracked_realloc(void *ptr, size_t size, char *file, int line) {
1196 struct igheap *thisheap;
1199 block = realloc(ptr, size);
1200 if (block == NULL) return(block);
1202 thisheap = malloc(sizeof(struct igheap));
1203 if (thisheap == NULL) {
1208 thisheap->block = block;
1209 strcpy(thisheap->file, file);
1210 thisheap->line = line;
1212 begin_critical_section(S_DEBUGMEMLEAKS);
1213 thisheap->next = igheap;
1215 end_critical_section(S_DEBUGMEMLEAKS);
1222 void tracked_free(void *ptr) {
1223 struct igheap *thisheap;
1224 struct igheap *trash;
1228 if (igheap == NULL) return;
1229 begin_critical_section(S_DEBUGMEMLEAKS);
1230 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1231 if (thisheap->next != NULL) {
1232 if (thisheap->next->block == ptr) {
1233 trash = thisheap->next;
1234 thisheap->next = thisheap->next->next;
1239 if (igheap->block == ptr) {
1241 igheap = igheap->next;
1244 end_critical_section(S_DEBUGMEMLEAKS);
1247 char *tracked_strdup(const char *s, char *file, int line) {
1250 if (s == NULL) return(NULL);
1251 ptr = tracked_malloc(strlen(s) + 1, file, line);
1252 if (ptr == NULL) return(NULL);
1253 strncpy(ptr, s, strlen(s));
1257 void dump_heap(void) {
1258 struct igheap *thisheap;
1260 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1261 lprintf(CTDL_CRIT, "UNFREED: %30s : %d\n",
1262 thisheap->file, thisheap->line);
1266 #endif /* DEBUG_MEMORY_LEAKS */