4 * Citadel/UX "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 <netinet/in.h>
57 #include "serv_extensions.h"
58 #include "sysdep_decls.h"
59 #include "citserver.h"
63 #include "housekeeping.h"
65 #include "serv_crypto.h"
67 #ifdef HAVE_SYS_SELECT_H
68 #include <sys/select.h>
76 #ifdef DEBUG_MEMORY_LEAKS
84 struct igheap *igheap = NULL;
88 pthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */
89 pthread_key_t MyConKey; /* TSD key for MyContext() */
91 int verbosity = DEFAULT_VERBOSITY; /* Logging level */
93 struct CitContext masterCC;
94 int rescan[2]; /* The Rescan Pipe */
95 time_t last_purge = 0; /* Last dead session purge */
96 static int num_threads = 0; /* Current number of threads */
97 int num_sessions = 0; /* Current number of sessions */
99 fd_set masterfds; /* Master sockets etc. */
102 pthread_t initial_thread; /* tid for main() thread */
104 int syslog_facility = (-1);
106 /* This is synchronized below; it helps implement round robin mode */
107 extern struct CitContext* next_session;
110 * lprintf() ... Write logging information
112 * Note: the variable "buf" below needs to be large enough to handle any
113 * log data sent through this function. BE CAREFUL!
115 void lprintf(enum LogLevel loglevel, const char *format, ...) {
119 va_start(arg_ptr, format);
120 vsnprintf(buf, sizeof(buf), format, arg_ptr);
123 if (syslog_facility >= 0) {
124 if (loglevel <= verbosity) {
126 if (CC && CC->cs_pid) {
127 memmove(buf + 6, buf, sizeof(buf) - 6);
128 snprintf(buf, 6, "[%3d]", CC->cs_pid);
131 syslog(loglevel, buf);
134 else if (loglevel <= verbosity) {
139 gettimeofday(&tv, NULL);
140 /* Promote to time_t; types differ on some OSes (like darwin) */
141 unixtime = tv.tv_sec;
142 tim = localtime(&unixtime);
144 * Log provides millisecond accuracy. If you need
145 * microsecond accuracy and your OS supports it, change
146 * %03ld to %06ld and remove " / 1000" after tv.tv_usec.
148 if (CC && CC->cs_pid) {
150 /* Millisecond display */
152 "%04d/%02d/%02d %2d:%02d:%02d.%03ld [%3d] %s",
153 tim->tm_year + 1900, tim->tm_mon + 1,
154 tim->tm_mday, tim->tm_hour, tim->tm_min,
155 tim->tm_sec, (long)tv.tv_usec / 1000,
158 /* Microsecond display */
160 "%04d/%02d/%02d %2d:%02d:%02d.%06ld [%3d] %s",
161 tim->tm_year + 1900, tim->tm_mon + 1,
162 tim->tm_mday, tim->tm_hour, tim->tm_min,
163 tim->tm_sec, (long)tv.tv_usec,
167 /* Millisecond display */
169 "%04d/%02d/%02d %2d:%02d:%02d.%03ld %s",
170 tim->tm_year + 1900, tim->tm_mon + 1,
171 tim->tm_mday, tim->tm_hour, tim->tm_min,
172 tim->tm_sec, (long)tv.tv_usec / 1000, buf);
174 /* Microsecond display */
176 "%04d/%02d/%02d %2d:%02d:%02d.%06ld %s",
177 tim->tm_year + 1900, tim->tm_mon + 1,
178 tim->tm_mday, tim->tm_hour, tim->tm_min,
179 tim->tm_sec, (long)tv.tv_usec, buf);
184 PerformLogHooks(loglevel, buf);
190 * We used to use master_cleanup() as a signal handler to shut down the server.
191 * however, master_cleanup() and the functions it calls do some things that
192 * aren't such a good idea to do from a signal handler: acquiring mutexes,
193 * playing with signal masks on BSDI systems, etc. so instead we install the
194 * following signal handler to set a global variable to inform the main loop
195 * that it's time to call master_cleanup() and exit.
198 volatile int time_to_die = 0;
200 static RETSIGTYPE signal_cleanup(int signum) {
206 * Some initialization stuff...
208 void init_sysdep(void) {
215 /* Set up a bunch of semaphores to be used for critical sections */
216 for (a=0; a<MAX_SEMAPHORES; ++a) {
217 pthread_mutex_init(&Critters[a], NULL);
221 * Set up a place to put thread-specific data.
222 * We only need a single pointer per thread - it points to the
223 * CitContext structure (in the ContextList linked list) of the
224 * session to which the calling thread is currently bound.
226 if (pthread_key_create(&MyConKey, NULL) != 0) {
227 lprintf(CTDL_CRIT, "Can't create TSD key!! %s\n", strerror(errno));
231 * The action for unexpected signals and exceptions should be to
232 * call signal_cleanup() to gracefully shut down the server.
234 signal(SIGINT, signal_cleanup);
235 signal(SIGQUIT, signal_cleanup);
236 signal(SIGHUP, signal_cleanup);
237 signal(SIGTERM, signal_cleanup);
240 * Do not shut down the server on broken pipe signals, otherwise the
241 * whole Citadel service would come down whenever a single client
244 signal(SIGPIPE, SIG_IGN);
249 * Obtain a semaphore lock to begin a critical section.
251 void begin_critical_section(int which_one)
253 /* lprintf(CTDL_DEBUG, "begin_critical_section(%d)\n", which_one); */
255 /* For all types of critical sections except those listed here,
256 * ensure nobody ever tries to do a critical section within a
257 * transaction; this could lead to deadlock.
259 if ( (which_one != S_FLOORCACHE)
260 #ifdef DEBUG_MEMORY_LEAKS
261 && (which_one != S_DEBUGMEMLEAKS)
266 pthread_mutex_lock(&Critters[which_one]);
270 * Release a semaphore lock to end a critical section.
272 void end_critical_section(int which_one)
274 pthread_mutex_unlock(&Critters[which_one]);
280 * This is a generic function to set up a master socket for listening on
281 * a TCP port. The server shuts down if the bind fails.
284 int ig_tcp_server(int port_number, int queue_len)
286 struct sockaddr_in sin;
288 int actual_queue_len;
290 actual_queue_len = queue_len;
291 if (actual_queue_len < 5) actual_queue_len = 5;
293 memset(&sin, 0, sizeof(sin));
294 sin.sin_family = AF_INET;
295 sin.sin_addr.s_addr = INADDR_ANY;
296 sin.sin_port = htons((u_short)port_number);
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 * It's inlined because it's used *VERY* frequently.
382 INLINE struct CitContext *MyContext(void) {
383 return ((pthread_getspecific(MyConKey) == NULL)
385 : (struct CitContext *) pthread_getspecific(MyConKey)
391 * Initialize a new context and place it in the list. The session number
392 * used to be the PID (which is why it's called cs_pid), but that was when we
393 * had one process per session. Now we just assign them sequentially, starting
394 * at 1 (don't change it to 0 because masterCC uses 0) and re-using them when
395 * sessions terminate.
397 struct CitContext *CreateNewContext(void) {
398 struct CitContext *me, *ptr;
400 me = (struct CitContext *) malloc(sizeof(struct CitContext));
402 lprintf(CTDL_ALERT, "citserver: can't allocate memory!!\n");
405 memset(me, 0, sizeof(struct CitContext));
407 /* The new context will be created already in the CON_EXECUTING state
408 * in order to prevent another thread from grabbing it while it's
411 me->state = CON_EXECUTING;
415 * Generate a unique session number and insert this context into
418 begin_critical_section(S_SESSION_TABLE);
420 if (ContextList == NULL) {
426 else if (ContextList->cs_pid > 1) {
427 me->next = ContextList;
433 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
434 if (ptr->next == NULL) {
436 me->cs_pid = ptr->cs_pid + 1;
440 else if (ptr->next->cs_pid > (ptr->cs_pid+1)) {
441 me->next = ptr->next;
443 me->cs_pid = ptr->cs_pid + 1;
449 DONE: ++num_sessions;
450 end_critical_section(S_SESSION_TABLE);
456 * buffer_output() ... tell client_write to buffer all output until
457 * instructed to dump it all out later
459 void buffer_output(void) {
460 if (CC->buffering == 0) {
463 CC->output_buffer = malloc(SIZ);
468 * unbuffer_output() ... dump out all that output we've been buffering.
470 void unbuffer_output(void) {
471 if (CC->buffering == 1) {
473 client_write(CC->output_buffer, CC->buffer_len);
474 free(CC->output_buffer);
475 CC->output_buffer = NULL;
483 * client_write() ... Send binary data to the client.
485 void client_write(char *buf, int nbytes)
487 int bytes_written = 0;
490 int old_buffer_len = 0;
492 if (CC->redirect_fp != NULL) {
493 fwrite(buf, nbytes, 1, CC->redirect_fp);
497 if (CC->redirect_sock > 0) {
498 sock = CC->redirect_sock; /* and continue below... */
501 sock = CC->client_socket;
504 /* If we're buffering for later, do that now. */
506 old_buffer_len = CC->buffer_len;
507 CC->buffer_len += nbytes;
508 CC->output_buffer = realloc(CC->output_buffer, CC->buffer_len);
509 memcpy(&CC->output_buffer[old_buffer_len], buf, nbytes);
513 /* Ok, at this point we're not buffering. Go ahead and write. */
516 if (CC->redirect_ssl) {
517 client_write_ssl(buf, nbytes);
522 while (bytes_written < nbytes) {
523 retval = write(sock, &buf[bytes_written],
524 nbytes - bytes_written);
526 lprintf(CTDL_ERR, "client_write() failed: %s\n",
528 if (sock == CC->client_socket) CC->kill_me = 1;
531 bytes_written = bytes_written + retval;
537 * cprintf() ... Send formatted printable data to the client. It is
538 * implemented in terms of client_write() but remains in
539 * sysdep.c in case we port to somewhere without va_args...
541 void cprintf(const char *format, ...) {
545 va_start(arg_ptr, format);
546 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
547 buf[sizeof buf - 2] = '\n';
548 client_write(buf, strlen(buf));
554 * Read data from the client socket.
556 * 1 Requested number of bytes has been read.
557 * 0 Request timed out.
558 * -1 The socket is broken.
559 * If the socket breaks, the session will be terminated.
561 int client_read_to(char *buf, int bytes, int timeout)
569 if (CC->redirect_ssl) {
570 return (client_read_ssl(buf, bytes, timeout));
576 FD_SET(CC->client_socket, &rfds);
580 retval = select( (CC->client_socket)+1,
581 &rfds, NULL, NULL, &tv);
583 if (FD_ISSET(CC->client_socket, &rfds) == 0) {
587 rlen = read(CC->client_socket, &buf[len], bytes-len);
589 lprintf(CTDL_ERR, "client_read() failed: %s\n",
600 * Read data from the client socket with default timeout.
601 * (This is implemented in terms of client_read_to() and could be
602 * justifiably moved out of sysdep.c)
604 INLINE int client_read(char *buf, int bytes)
606 return(client_read_to(buf, bytes, config.c_sleeping));
611 * client_gets() ... Get a LF-terminated line of text from the client.
612 * (This is implemented in terms of client_read() and could be
613 * justifiably moved out of sysdep.c)
615 int client_gets(char *buf)
619 /* Read one character at a time.
622 retval = client_read(&buf[i], 1);
623 if (retval != 1 || buf[i] == '\n' || i == (SIZ-1))
627 /* If we got a long line, discard characters until the newline.
630 while (buf[i] != '\n' && retval == 1)
631 retval = client_read(&buf[i], 1);
633 /* Strip the trailing newline and any trailing nonprintables (cr's)
636 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
637 buf[strlen(buf)-1] = 0;
638 if (retval < 0) strcpy(buf, "000");
645 * The system-dependent part of master_cleanup() - close the master socket.
647 void sysdep_master_cleanup(void) {
648 struct ServiceFunctionHook *serviceptr;
651 * close all protocol master sockets
653 for (serviceptr = ServiceHookTable; serviceptr != NULL;
654 serviceptr = serviceptr->next ) {
656 if (serviceptr->tcp_port > 0)
657 lprintf(CTDL_INFO, "Closing listener on port %d\n",
658 serviceptr->tcp_port);
660 if (serviceptr->sockpath != NULL)
661 lprintf(CTDL_INFO, "Closing listener on '%s'\n",
662 serviceptr->sockpath);
664 close(serviceptr->msock);
666 /* If it's a Unix domain socket, remove the file. */
667 if (serviceptr->sockpath != NULL) {
668 unlink(serviceptr->sockpath);
675 * Terminate another session.
676 * (This could justifiably be moved out of sysdep.c because it
677 * no longer does anything that is system-dependent.)
679 void kill_session(int session_to_kill) {
680 struct CitContext *ptr;
682 begin_critical_section(S_SESSION_TABLE);
683 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
684 if (ptr->cs_pid == session_to_kill) {
688 end_critical_section(S_SESSION_TABLE);
695 * Start running as a daemon. Only close stdio if do_close_stdio is set.
697 void start_daemon(int do_close_stdio) {
698 if (do_close_stdio) {
703 signal(SIGHUP,SIG_IGN);
704 signal(SIGINT,SIG_IGN);
705 signal(SIGQUIT,SIG_IGN);
706 if (fork()!=0) exit(0);
712 * Generic routine to convert a login name to a full name (gecos)
713 * Returns nonzero if a conversion took place
715 int convert_login(char NameToConvert[]) {
719 pw = getpwnam(NameToConvert);
724 strcpy(NameToConvert, pw->pw_gecos);
725 for (a=0; a<strlen(NameToConvert); ++a) {
726 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
732 struct worker_node *worker_list = NULL;
736 * create a worker thread. this function must always be called from within
737 * an S_WORKER_LIST critical section!
739 void create_worker(void) {
741 struct worker_node *n;
744 n = malloc(sizeof(struct worker_node));
746 lprintf(CTDL_EMERG, "can't allocate worker_node, exiting\n");
751 if ((ret = pthread_attr_init(&attr))) {
752 lprintf(CTDL_EMERG, "pthread_attr_init: %s\n", strerror(ret));
757 /* we seem to need something bigger than FreeBSD's default 64k stack */
759 if ((ret = pthread_attr_setstacksize(&attr, 128 * 1024))) {
760 lprintf(CTDL_EMERG, "pthread_attr_setstacksize: %s\n", strerror(ret));
765 if ((ret = pthread_create(&n->tid, &attr, worker_thread, NULL) != 0))
768 lprintf(CTDL_ALERT, "Can't create worker thread: %s\n",
772 n->next = worker_list;
779 * Purge all sessions which have the 'kill_me' flag set.
780 * This function has code to prevent it from running more than once every
781 * few seconds, because running it after every single unbind would waste a lot
782 * of CPU time and keep the context list locked too much. To force it to run
783 * anyway, set "force" to nonzero.
786 * After that's done, we raise the size of the worker thread pool
787 * if such an action is appropriate.
789 void dead_session_purge(int force) {
790 struct CitContext *ptr, *rem;
793 if ( (time(NULL) - last_purge) < 5 ) {
794 return; /* Too soon, go away */
801 begin_critical_section(S_SESSION_TABLE);
802 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
803 if ( (ptr->state == CON_IDLE) && (ptr->kill_me) ) {
807 end_critical_section(S_SESSION_TABLE);
809 /* RemoveContext() enters its own S_SESSION_TABLE critical
810 * section, so we have to do it like this.
813 lprintf(CTDL_DEBUG, "Purging session %d\n", rem->cs_pid);
817 } while (rem != NULL);
819 /* Raise the size of the worker thread pool if necessary. */
821 if ( (num_sessions > num_threads)
822 && (num_threads < config.c_max_workers) ) {
823 begin_critical_section(S_WORKER_LIST);
825 end_critical_section(S_WORKER_LIST);
834 * Redirect a session's output to a file or socket.
835 * This function may be called with a file handle *or* a socket (but not
836 * both). Call with neither to return output to its normal client socket.
838 void CtdlRedirectOutput(FILE *fp, int sock) {
840 if (fp != NULL) CC->redirect_fp = fp;
841 else CC->redirect_fp = NULL;
843 if (sock > 0) CC->redirect_sock = sock;
844 else CC->redirect_sock = (-1);
850 * masterCC is the context we use when not attached to a session. This
851 * function initializes it.
853 void InitializeMasterCC(void) {
854 memset(&masterCC, 0, sizeof(struct CitContext));
855 masterCC.internal_pgm = 1;
862 * Set up a fd_set containing all the master sockets to which we
863 * always listen. It's computationally less expensive to just copy
864 * this to a local fd_set when starting a new select() and then add
865 * the client sockets than it is to initialize a new one and then
866 * figure out what to put there.
868 void init_master_fdset(void) {
869 struct ServiceFunctionHook *serviceptr;
872 lprintf(CTDL_DEBUG, "Initializing master fdset\n");
877 lprintf(CTDL_DEBUG, "Will listen on rescan pipe %d\n", rescan[0]);
878 FD_SET(rescan[0], &masterfds);
879 if (rescan[0] > masterhighest) masterhighest = rescan[0];
881 for (serviceptr = ServiceHookTable; serviceptr != NULL;
882 serviceptr = serviceptr->next ) {
883 m = serviceptr->msock;
884 lprintf(CTDL_DEBUG, "Will listen on master socket %d\n", m);
885 FD_SET(m, &masterfds);
886 if (m > masterhighest) {
890 lprintf(CTDL_DEBUG, "masterhighest = %d\n", masterhighest);
895 * Bind a thread to a context. (It's inline merely to speed things up.)
897 INLINE void become_session(struct CitContext *which_con) {
898 pthread_setspecific(MyConKey, (void *)which_con );
904 * This loop just keeps going and going and going...
906 void *worker_thread(void *arg) {
910 struct CitContext *ptr;
911 struct CitContext *bind_me = NULL;
914 struct CitContext *con= NULL; /* Temporary context pointer */
915 struct ServiceFunctionHook *serviceptr;
916 int ssock; /* Descriptor for client socket */
924 while (!time_to_die) {
927 * A naive implementation would have all idle threads
928 * calling select() and then they'd all wake up at once
929 * (known in computer science as the "thundering herd"
930 * problem). We solve this problem by putting the select()
931 * in a critical section, so only one thread has the
932 * opportunity to wake up. If we wake up on a master
933 * socket, create a new session context; otherwise, just
934 * bind the thread to the context we want and go on our
938 /* make doubly sure we're not holding any stale db handles
939 * which might cause a deadlock.
944 begin_critical_section(S_I_WANNA_SELECT);
945 SETUP_FD: memcpy(&readfds, &masterfds, sizeof masterfds);
946 highest = masterhighest;
947 begin_critical_section(S_SESSION_TABLE);
948 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
949 if (ptr->state == CON_IDLE) {
950 FD_SET(ptr->client_socket, &readfds);
951 if (ptr->client_socket > highest)
952 highest = ptr->client_socket;
955 end_critical_section(S_SESSION_TABLE);
957 tv.tv_sec = 1; /* wake up every second if no input */
962 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
964 end_critical_section(S_I_WANNA_SELECT);
968 /* Now figure out who made this select() unblock.
969 * First, check for an error or exit condition.
972 if (errno == EBADF) {
973 lprintf(CTDL_NOTICE, "select() failed: (%s)\n",
977 if (errno != EINTR) {
978 lprintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
980 } else if (!time_to_die)
984 /* Next, check to see if it's a new client connecting
985 * on a master socket.
987 else for (serviceptr = ServiceHookTable; serviceptr != NULL;
988 serviceptr = serviceptr->next ) {
990 if (FD_ISSET(serviceptr->msock, &readfds)) {
991 ssock = accept(serviceptr->msock, NULL, 0);
994 "citserver: accept(): %s\n",
999 "New client socket %d\n",
1002 /* New context will be created already
1003 * set up in the CON_EXECUTING state.
1005 con = CreateNewContext();
1007 /* Assign new socket number to it. */
1008 con->client_socket = ssock;
1009 con->h_command_function =
1010 serviceptr->h_command_function;
1012 /* Determine whether local socket */
1013 if (serviceptr->sockpath != NULL)
1014 con->is_local_socket = 1;
1016 /* Set the SO_REUSEADDR socket option */
1018 setsockopt(ssock, SOL_SOCKET,
1022 become_session(con);
1024 serviceptr->h_greeting_function();
1025 become_session(NULL);
1026 con->state = CON_IDLE;
1033 end_critical_section(S_I_WANNA_SELECT);
1037 /* If the rescan pipe went active, someone is telling this
1038 * thread that the &readfds needs to be refreshed with more
1041 if (FD_ISSET(rescan[0], &readfds)) {
1042 read(rescan[0], &junk, 1);
1046 /* It must be a client socket. Find a context that has data
1047 * waiting on its socket *and* is in the CON_IDLE state.
1051 begin_critical_section(S_SESSION_TABLE);
1053 * We start where we left off. If we get to the end
1054 * we'll start from the beginning again, then give up
1055 * if we still don't find anything. This ensures
1056 * that all contexts get a more-or-less equal chance
1057 * to run. And yes, I did add a goto to the code. -IO
1059 find_session: if (next_session == NULL)
1060 next_session = ContextList;
1061 for (ptr = next_session;
1062 ( (ptr != NULL) && (bind_me == NULL) );
1064 if ( (FD_ISSET(ptr->client_socket, &readfds))
1065 && (ptr->state == CON_IDLE) ) {
1069 if (bind_me != NULL) {
1070 /* Found one. Stake a claim to it before
1071 * letting anyone else touch the context list.
1073 bind_me->state = CON_EXECUTING;
1074 next_session = bind_me->next;
1075 } else if (next_session == ContextList) {
1076 next_session = NULL;
1078 if (bind_me == NULL && next_session != NULL) {
1079 next_session = NULL;
1083 end_critical_section(S_SESSION_TABLE);
1084 end_critical_section(S_I_WANNA_SELECT);
1086 /* We're bound to a session, now do *one* command */
1087 if (bind_me != NULL) {
1088 become_session(bind_me);
1089 CC->h_command_function();
1090 force_purge = CC->kill_me;
1091 become_session(NULL);
1092 bind_me->state = CON_IDLE;
1093 write(rescan[1], &junk, 1);
1097 dead_session_purge(force_purge);
1099 check_sched_shutdown();
1102 /* If control reaches this point, the server is shutting down */
1112 * Translate text facility name to syslog.h defined value.
1114 int SyslogFacility(char *name)
1123 { LOG_KERN, "kern" },
1124 { LOG_USER, "user" },
1125 { LOG_MAIL, "mail" },
1126 { LOG_DAEMON, "daemon" },
1127 { LOG_AUTH, "auth" },
1128 { LOG_SYSLOG, "syslog" },
1130 { LOG_NEWS, "news" },
1131 { LOG_UUCP, "uucp" },
1132 { LOG_LOCAL0, "local0" },
1133 { LOG_LOCAL1, "local1" },
1134 { LOG_LOCAL2, "local2" },
1135 { LOG_LOCAL3, "local3" },
1136 { LOG_LOCAL4, "local4" },
1137 { LOG_LOCAL5, "local5" },
1138 { LOG_LOCAL6, "local6" },
1139 { LOG_LOCAL7, "local7" },
1142 for(i = 0; facTbl[i].name != NULL; i++) {
1143 if(!strcasecmp(name, facTbl[i].name))
1144 return facTbl[i].facility;
1150 /********** MEM CHEQQER ***********/
1152 #ifdef DEBUG_MEMORY_LEAKS
1159 void *tracked_malloc(size_t size, char *file, int line) {
1160 struct igheap *thisheap;
1163 block = malloc(size);
1164 if (block == NULL) return(block);
1166 thisheap = malloc(sizeof(struct igheap));
1167 if (thisheap == NULL) {
1172 thisheap->block = block;
1173 strcpy(thisheap->file, file);
1174 thisheap->line = line;
1176 begin_critical_section(S_DEBUGMEMLEAKS);
1177 thisheap->next = igheap;
1179 end_critical_section(S_DEBUGMEMLEAKS);
1185 void *tracked_realloc(void *ptr, size_t size, char *file, int line) {
1186 struct igheap *thisheap;
1189 block = realloc(ptr, size);
1190 if (block == NULL) return(block);
1192 thisheap = malloc(sizeof(struct igheap));
1193 if (thisheap == NULL) {
1198 thisheap->block = block;
1199 strcpy(thisheap->file, file);
1200 thisheap->line = line;
1202 begin_critical_section(S_DEBUGMEMLEAKS);
1203 thisheap->next = igheap;
1205 end_critical_section(S_DEBUGMEMLEAKS);
1212 void tracked_free(void *ptr) {
1213 struct igheap *thisheap;
1214 struct igheap *trash;
1218 if (igheap == NULL) return;
1219 begin_critical_section(S_DEBUGMEMLEAKS);
1220 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1221 if (thisheap->next != NULL) {
1222 if (thisheap->next->block == ptr) {
1223 trash = thisheap->next;
1224 thisheap->next = thisheap->next->next;
1229 if (igheap->block == ptr) {
1231 igheap = igheap->next;
1234 end_critical_section(S_DEBUGMEMLEAKS);
1237 char *tracked_strdup(const char *s, char *file, int line) {
1240 if (s == NULL) return(NULL);
1241 ptr = tracked_malloc(strlen(s) + 1, file, line);
1242 if (ptr == NULL) return(NULL);
1243 strncpy(ptr, s, strlen(s));
1247 void dump_heap(void) {
1248 struct igheap *thisheap;
1250 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1251 lprintf(CTDL_CRIT, "UNFREED: %30s : %d\n",
1252 thisheap->file, thisheap->line);
1256 #endif /* DEBUG_MEMORY_LEAKS */