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);
108 * lprintf() ... Write logging information
110 * Note: the variable "buf" below needs to be large enough to handle any
111 * log data sent through this function. BE CAREFUL!
113 void lprintf(enum LogLevel loglevel, const char *format, ...) {
117 va_start(arg_ptr, format);
118 vsnprintf(buf, sizeof(buf), format, arg_ptr);
121 if (syslog_facility >= 0) {
122 if (loglevel <= verbosity) {
124 if (CC && CC->cs_pid) {
125 memmove(buf + 6, buf, sizeof(buf) - 6);
126 snprintf(buf, 6, "[%3d]", CC->cs_pid);
129 syslog(loglevel, buf);
132 else if (loglevel <= verbosity) {
137 gettimeofday(&tv, NULL);
138 /* Promote to time_t; types differ on some OSes (like darwin) */
139 unixtime = tv.tv_sec;
140 tim = localtime(&unixtime);
142 * Log provides millisecond accuracy. If you need
143 * microsecond accuracy and your OS supports it, change
144 * %03ld to %06ld and remove " / 1000" after tv.tv_usec.
146 if (CC && CC->cs_pid) {
148 /* Millisecond display */
150 "%04d/%02d/%02d %2d:%02d:%02d.%03ld [%3d] %s",
151 tim->tm_year + 1900, tim->tm_mon + 1,
152 tim->tm_mday, tim->tm_hour, tim->tm_min,
153 tim->tm_sec, (long)tv.tv_usec / 1000,
156 /* Microsecond display */
158 "%04d/%02d/%02d %2d:%02d:%02d.%06ld [%3d] %s",
159 tim->tm_year + 1900, tim->tm_mon + 1,
160 tim->tm_mday, tim->tm_hour, tim->tm_min,
161 tim->tm_sec, (long)tv.tv_usec,
165 /* Millisecond display */
167 "%04d/%02d/%02d %2d:%02d:%02d.%03ld %s",
168 tim->tm_year + 1900, tim->tm_mon + 1,
169 tim->tm_mday, tim->tm_hour, tim->tm_min,
170 tim->tm_sec, (long)tv.tv_usec / 1000, buf);
172 /* Microsecond display */
174 "%04d/%02d/%02d %2d:%02d:%02d.%06ld %s",
175 tim->tm_year + 1900, tim->tm_mon + 1,
176 tim->tm_mday, tim->tm_hour, tim->tm_min,
177 tim->tm_sec, (long)tv.tv_usec, buf);
182 PerformLogHooks(loglevel, buf);
188 * We used to use master_cleanup() as a signal handler to shut down the server.
189 * however, master_cleanup() and the functions it calls do some things that
190 * aren't such a good idea to do from a signal handler: acquiring mutexes,
191 * playing with signal masks on BSDI systems, etc. so instead we install the
192 * following signal handler to set a global variable to inform the main loop
193 * that it's time to call master_cleanup() and exit.
196 volatile int time_to_die = 0;
198 static RETSIGTYPE signal_cleanup(int signum) {
204 * Some initialization stuff...
206 void init_sysdep(void) {
213 /* Set up a bunch of semaphores to be used for critical sections */
214 for (a=0; a<MAX_SEMAPHORES; ++a) {
215 pthread_mutex_init(&Critters[a], NULL);
219 * Set up a place to put thread-specific data.
220 * We only need a single pointer per thread - it points to the
221 * CitContext structure (in the ContextList linked list) of the
222 * session to which the calling thread is currently bound.
224 if (pthread_key_create(&MyConKey, NULL) != 0) {
225 lprintf(CTDL_CRIT, "Can't create TSD key!! %s\n", strerror(errno));
229 * The action for unexpected signals and exceptions should be to
230 * call signal_cleanup() to gracefully shut down the server.
232 signal(SIGINT, signal_cleanup);
233 signal(SIGQUIT, signal_cleanup);
234 signal(SIGHUP, signal_cleanup);
235 signal(SIGTERM, signal_cleanup);
238 * Do not shut down the server on broken pipe signals, otherwise the
239 * whole Citadel service would come down whenever a single client
242 signal(SIGPIPE, SIG_IGN);
247 * Obtain a semaphore lock to begin a critical section.
249 void begin_critical_section(int which_one)
251 /* lprintf(CTDL_DEBUG, "begin_critical_section(%d)\n", which_one); */
253 /* For all types of critical sections except those listed here,
254 * ensure nobody ever tries to do a critical section within a
255 * transaction; this could lead to deadlock.
257 if ( (which_one != S_FLOORCACHE)
258 #ifdef DEBUG_MEMORY_LEAKS
259 && (which_one != S_DEBUGMEMLEAKS)
264 pthread_mutex_lock(&Critters[which_one]);
268 * Release a semaphore lock to end a critical section.
270 void end_critical_section(int which_one)
272 pthread_mutex_unlock(&Critters[which_one]);
278 * This is a generic function to set up a master socket for listening on
279 * a TCP port. The server shuts down if the bind fails.
282 int ig_tcp_server(int port_number, int queue_len)
284 struct sockaddr_in sin;
286 int actual_queue_len;
288 actual_queue_len = queue_len;
289 if (actual_queue_len < 5) actual_queue_len = 5;
291 memset(&sin, 0, sizeof(sin));
292 sin.sin_family = AF_INET;
293 sin.sin_addr.s_addr = INADDR_ANY;
294 sin.sin_port = htons((u_short)port_number);
296 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
299 lprintf(CTDL_EMERG, "citserver: Can't create a socket: %s\n",
305 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
307 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
308 lprintf(CTDL_EMERG, "citserver: Can't bind: %s\n",
314 if (listen(s, actual_queue_len) < 0) {
315 lprintf(CTDL_EMERG, "citserver: Can't listen: %s\n", strerror(errno));
326 * Create a Unix domain socket and listen on it
328 int ig_uds_server(char *sockpath, int queue_len)
330 struct sockaddr_un addr;
333 int actual_queue_len;
335 actual_queue_len = queue_len;
336 if (actual_queue_len < 5) actual_queue_len = 5;
338 i = unlink(sockpath);
339 if (i != 0) if (errno != ENOENT) {
340 lprintf(CTDL_EMERG, "citserver: can't unlink %s: %s\n",
341 sockpath, strerror(errno));
345 memset(&addr, 0, sizeof(addr));
346 addr.sun_family = AF_UNIX;
347 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
349 s = socket(AF_UNIX, SOCK_STREAM, 0);
351 lprintf(CTDL_EMERG, "citserver: Can't create a socket: %s\n",
356 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
357 lprintf(CTDL_EMERG, "citserver: Can't bind: %s\n",
362 if (listen(s, actual_queue_len) < 0) {
363 lprintf(CTDL_EMERG, "citserver: Can't listen: %s\n", strerror(errno));
367 chmod(sockpath, 0777);
374 * Return a pointer to the CitContext structure bound to the thread which
375 * called this function. If there's no such binding (for example, if it's
376 * called by the housekeeper thread) then a generic 'master' CC is returned.
378 * It's inlined because it's used *VERY* frequently.
380 INLINE struct CitContext *MyContext(void) {
381 return ((pthread_getspecific(MyConKey) == NULL)
383 : (struct CitContext *) pthread_getspecific(MyConKey)
389 * Initialize a new context and place it in the list. The session number
390 * used to be the PID (which is why it's called cs_pid), but that was when we
391 * had one process per session. Now we just assign them sequentially, starting
392 * at 1 (don't change it to 0 because masterCC uses 0) and re-using them when
393 * sessions terminate.
395 struct CitContext *CreateNewContext(void) {
396 struct CitContext *me, *ptr;
398 me = (struct CitContext *) malloc(sizeof(struct CitContext));
400 lprintf(CTDL_ALERT, "citserver: can't allocate memory!!\n");
403 memset(me, 0, sizeof(struct CitContext));
405 /* The new context will be created already in the CON_EXECUTING state
406 * in order to prevent another thread from grabbing it while it's
409 me->state = CON_EXECUTING;
413 * Generate a unique session number and insert this context into
416 begin_critical_section(S_SESSION_TABLE);
418 if (ContextList == NULL) {
424 else if (ContextList->cs_pid > 1) {
425 me->next = ContextList;
431 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
432 if (ptr->next == NULL) {
434 me->cs_pid = ptr->cs_pid + 1;
438 else if (ptr->next->cs_pid > (ptr->cs_pid+1)) {
439 me->next = ptr->next;
441 me->cs_pid = ptr->cs_pid + 1;
447 DONE: ++num_sessions;
448 end_critical_section(S_SESSION_TABLE);
454 * buffer_output() ... tell client_write to buffer all output until
455 * instructed to dump it all out later
457 void buffer_output(void) {
458 if (CC->buffering == 0) {
461 CC->output_buffer = malloc(SIZ);
466 * unbuffer_output() ... dump out all that output we've been buffering.
468 void unbuffer_output(void) {
469 if (CC->buffering == 1) {
471 client_write(CC->output_buffer, CC->buffer_len);
472 free(CC->output_buffer);
473 CC->output_buffer = NULL;
481 * client_write() ... Send binary data to the client.
483 void client_write(char *buf, int nbytes)
485 int bytes_written = 0;
488 int old_buffer_len = 0;
490 if (CC->redirect_fp != NULL) {
491 fwrite(buf, nbytes, 1, CC->redirect_fp);
495 if (CC->redirect_sock > 0) {
496 sock = CC->redirect_sock; /* and continue below... */
499 sock = CC->client_socket;
502 /* If we're buffering for later, do that now. */
504 old_buffer_len = CC->buffer_len;
505 CC->buffer_len += nbytes;
506 CC->output_buffer = realloc(CC->output_buffer, CC->buffer_len);
507 memcpy(&CC->output_buffer[old_buffer_len], buf, nbytes);
511 /* Ok, at this point we're not buffering. Go ahead and write. */
514 if (CC->redirect_ssl) {
515 client_write_ssl(buf, nbytes);
520 while (bytes_written < nbytes) {
521 retval = write(sock, &buf[bytes_written],
522 nbytes - bytes_written);
524 lprintf(CTDL_ERR, "client_write() failed: %s\n",
526 if (sock == CC->client_socket) CC->kill_me = 1;
529 bytes_written = bytes_written + retval;
535 * cprintf() ... Send formatted printable data to the client. It is
536 * implemented in terms of client_write() but remains in
537 * sysdep.c in case we port to somewhere without va_args...
539 void cprintf(const char *format, ...) {
543 va_start(arg_ptr, format);
544 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
545 buf[sizeof buf - 2] = '\n';
546 client_write(buf, strlen(buf));
552 * Read data from the client socket.
554 * 1 Requested number of bytes has been read.
555 * 0 Request timed out.
556 * -1 The socket is broken.
557 * If the socket breaks, the session will be terminated.
559 int client_read_to(char *buf, int bytes, int timeout)
567 if (CC->redirect_ssl) {
568 return (client_read_ssl(buf, bytes, timeout));
574 FD_SET(CC->client_socket, &rfds);
578 retval = select( (CC->client_socket)+1,
579 &rfds, NULL, NULL, &tv);
581 if (FD_ISSET(CC->client_socket, &rfds) == 0) {
585 rlen = read(CC->client_socket, &buf[len], bytes-len);
587 lprintf(CTDL_ERR, "client_read() failed: %s\n",
598 * Read data from the client socket with default timeout.
599 * (This is implemented in terms of client_read_to() and could be
600 * justifiably moved out of sysdep.c)
602 INLINE int client_read(char *buf, int bytes)
604 return(client_read_to(buf, bytes, config.c_sleeping));
609 * client_gets() ... Get a LF-terminated line of text from the client.
610 * (This is implemented in terms of client_read() and could be
611 * justifiably moved out of sysdep.c)
613 int client_gets(char *buf)
617 /* Read one character at a time.
620 retval = client_read(&buf[i], 1);
621 if (retval != 1 || buf[i] == '\n' || i == (SIZ-1))
625 /* If we got a long line, discard characters until the newline.
628 while (buf[i] != '\n' && retval == 1)
629 retval = client_read(&buf[i], 1);
631 /* Strip the trailing newline and any trailing nonprintables (cr's)
634 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
635 buf[strlen(buf)-1] = 0;
636 if (retval < 0) strcpy(buf, "000");
643 * The system-dependent part of master_cleanup() - close the master socket.
645 void sysdep_master_cleanup(void) {
646 struct ServiceFunctionHook *serviceptr;
649 * close all protocol master sockets
651 for (serviceptr = ServiceHookTable; serviceptr != NULL;
652 serviceptr = serviceptr->next ) {
654 if (serviceptr->tcp_port > 0)
655 lprintf(CTDL_INFO, "Closing listener on port %d\n",
656 serviceptr->tcp_port);
658 if (serviceptr->sockpath != NULL)
659 lprintf(CTDL_INFO, "Closing listener on '%s'\n",
660 serviceptr->sockpath);
662 close(serviceptr->msock);
664 /* If it's a Unix domain socket, remove the file. */
665 if (serviceptr->sockpath != NULL) {
666 unlink(serviceptr->sockpath);
673 * Terminate another session.
674 * (This could justifiably be moved out of sysdep.c because it
675 * no longer does anything that is system-dependent.)
677 void kill_session(int session_to_kill) {
678 struct CitContext *ptr;
680 begin_critical_section(S_SESSION_TABLE);
681 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
682 if (ptr->cs_pid == session_to_kill) {
686 end_critical_section(S_SESSION_TABLE);
693 * Start running as a daemon. Only close stdio if do_close_stdio is set.
695 void start_daemon(int do_close_stdio) {
696 if (do_close_stdio) {
701 signal(SIGHUP,SIG_IGN);
702 signal(SIGINT,SIG_IGN);
703 signal(SIGQUIT,SIG_IGN);
704 if (fork()!=0) exit(0);
710 * Generic routine to convert a login name to a full name (gecos)
711 * Returns nonzero if a conversion took place
713 int convert_login(char NameToConvert[]) {
717 pw = getpwnam(NameToConvert);
722 strcpy(NameToConvert, pw->pw_gecos);
723 for (a=0; a<strlen(NameToConvert); ++a) {
724 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
730 struct worker_node *worker_list = NULL;
734 * create a worker thread. this function must always be called from within
735 * an S_WORKER_LIST critical section!
737 void create_worker(void) {
739 struct worker_node *n;
742 n = malloc(sizeof(struct worker_node));
744 lprintf(CTDL_EMERG, "can't allocate worker_node, exiting\n");
749 if ((ret = pthread_attr_init(&attr))) {
750 lprintf(CTDL_EMERG, "pthread_attr_init: %s\n", strerror(ret));
755 /* we seem to need something bigger than FreeBSD's default 64k stack */
757 if ((ret = pthread_attr_setstacksize(&attr, 128 * 1024))) {
758 lprintf(CTDL_EMERG, "pthread_attr_setstacksize: %s\n", strerror(ret));
763 if ((ret = pthread_create(&n->tid, &attr, worker_thread, NULL) != 0))
766 lprintf(CTDL_ALERT, "Can't create worker thread: %s\n",
770 n->next = worker_list;
777 * Purge all sessions which have the 'kill_me' flag set.
778 * This function has code to prevent it from running more than once every
779 * few seconds, because running it after every single unbind would waste a lot
780 * of CPU time and keep the context list locked too much.
782 * After that's done, we raise or lower the size of the worker thread pool
783 * if such an action is appropriate.
785 void dead_session_purge(void) {
786 struct CitContext *ptr, *rem;
788 if ( (time(NULL) - last_purge) < 5 ) return; /* Too soon, go away */
793 begin_critical_section(S_SESSION_TABLE);
794 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
795 if ( (ptr->state == CON_IDLE) && (ptr->kill_me) ) {
799 end_critical_section(S_SESSION_TABLE);
801 /* RemoveContext() enters its own S_SESSION_TABLE critical
802 * section, so we have to do it like this.
805 lprintf(CTDL_DEBUG, "Purging session %d\n", rem->cs_pid);
809 } while (rem != NULL);
811 /* Raise the size of the worker thread pool if necessary. */
813 if ( (num_sessions > num_threads)
814 && (num_threads < config.c_max_workers) ) {
815 begin_critical_section(S_WORKER_LIST);
817 end_critical_section(S_WORKER_LIST);
826 * Redirect a session's output to a file or socket.
827 * This function may be called with a file handle *or* a socket (but not
828 * both). Call with neither to return output to its normal client socket.
830 void CtdlRedirectOutput(FILE *fp, int sock) {
832 if (fp != NULL) CC->redirect_fp = fp;
833 else CC->redirect_fp = NULL;
835 if (sock > 0) CC->redirect_sock = sock;
836 else CC->redirect_sock = (-1);
842 * masterCC is the context we use when not attached to a session. This
843 * function initializes it.
845 void InitializeMasterCC(void) {
846 memset(&masterCC, 0, sizeof(struct CitContext));
847 masterCC.internal_pgm = 1;
854 * Set up a fd_set containing all the master sockets to which we
855 * always listen. It's computationally less expensive to just copy
856 * this to a local fd_set when starting a new select() and then add
857 * the client sockets than it is to initialize a new one and then
858 * figure out what to put there.
860 void init_master_fdset(void) {
861 struct ServiceFunctionHook *serviceptr;
864 lprintf(CTDL_DEBUG, "Initializing master fdset\n");
869 lprintf(CTDL_DEBUG, "Will listen on rescan pipe %d\n", rescan[0]);
870 FD_SET(rescan[0], &masterfds);
871 if (rescan[0] > masterhighest) masterhighest = rescan[0];
873 for (serviceptr = ServiceHookTable; serviceptr != NULL;
874 serviceptr = serviceptr->next ) {
875 m = serviceptr->msock;
876 lprintf(CTDL_DEBUG, "Will listen on master socket %d\n", m);
877 FD_SET(m, &masterfds);
878 if (m > masterhighest) {
882 lprintf(CTDL_DEBUG, "masterhighest = %d\n", masterhighest);
887 * Bind a thread to a context. (It's inline merely to speed things up.)
889 INLINE void become_session(struct CitContext *which_con) {
890 pthread_setspecific(MyConKey, (void *)which_con );
896 * This loop just keeps going and going and going...
898 void *worker_thread(void *arg) {
902 /* This is synchronized below; it helps implement round robin mode */
903 static struct CitContext* next_session = NULL;
904 struct CitContext *ptr;
905 struct CitContext *bind_me = NULL;
908 struct CitContext *con= NULL; /* Temporary context pointer */
909 struct ServiceFunctionHook *serviceptr;
910 int ssock; /* Descriptor for client socket */
917 while (!time_to_die) {
920 * A naive implementation would have all idle threads
921 * calling select() and then they'd all wake up at once
922 * (known in computer science as the "thundering herd"
923 * problem). We solve this problem by putting the select()
924 * in a critical section, so only one thread has the
925 * opportunity to wake up. If we wake up on a master
926 * socket, create a new session context; otherwise, just
927 * bind the thread to the context we want and go on our
931 /* make doubly sure we're not holding any stale db handles
932 * which might cause a deadlock.
936 begin_critical_section(S_I_WANNA_SELECT);
937 SETUP_FD: memcpy(&readfds, &masterfds, sizeof masterfds);
938 highest = masterhighest;
939 begin_critical_section(S_SESSION_TABLE);
940 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
941 if (ptr->state == CON_IDLE) {
942 FD_SET(ptr->client_socket, &readfds);
943 if (ptr->client_socket > highest)
944 highest = ptr->client_socket;
947 end_critical_section(S_SESSION_TABLE);
949 tv.tv_sec = 1; /* wake up every second if no input */
954 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
956 end_critical_section(S_I_WANNA_SELECT);
960 /* Now figure out who made this select() unblock.
961 * First, check for an error or exit condition.
964 if (errno != EINTR) {
965 lprintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
967 } else if (!time_to_die)
971 /* Next, check to see if it's a new client connecting
972 * on a master socket.
974 else for (serviceptr = ServiceHookTable; serviceptr != NULL;
975 serviceptr = serviceptr->next ) {
977 if (FD_ISSET(serviceptr->msock, &readfds)) {
978 ssock = accept(serviceptr->msock, NULL, 0);
981 "citserver: accept(): %s\n",
986 "New client socket %d\n",
989 /* New context will be created already
990 * set up in the CON_EXECUTING state.
992 con = CreateNewContext();
994 /* Assign new socket number to it. */
995 con->client_socket = ssock;
996 con->h_command_function =
997 serviceptr->h_command_function;
999 /* Determine whether local socket */
1000 if (serviceptr->sockpath != NULL)
1001 con->is_local_socket = 1;
1003 /* Set the SO_REUSEADDR socket option */
1005 setsockopt(ssock, SOL_SOCKET,
1009 become_session(con);
1011 serviceptr->h_greeting_function();
1012 become_session(NULL);
1013 con->state = CON_IDLE;
1020 end_critical_section(S_I_WANNA_SELECT);
1024 /* If the rescan pipe went active, someone is telling this
1025 * thread that the &readfds needs to be refreshed with more
1028 if (FD_ISSET(rescan[0], &readfds)) {
1029 read(rescan[0], &junk, 1);
1033 /* It must be a client socket. Find a context that has data
1034 * waiting on its socket *and* is in the CON_IDLE state.
1038 begin_critical_section(S_SESSION_TABLE);
1040 * We start where we left off. If we get to the end
1041 * we'll start from the beginning again, then give up
1042 * if we still don't find anything. This ensures
1043 * that all contexts get a more-or-less equal chance
1044 * to run. And yes, I did add a goto to the code. -IO
1046 find_session: if (next_session == NULL)
1047 next_session = ContextList;
1048 for (ptr = next_session;
1049 ( (ptr != NULL) && (bind_me == NULL) );
1051 if ( (FD_ISSET(ptr->client_socket, &readfds))
1052 && (ptr->state == CON_IDLE) ) {
1056 if (bind_me != NULL) {
1057 /* Found one. Stake a claim to it before
1058 * letting anyone else touch the context list.
1060 bind_me->state = CON_EXECUTING;
1061 next_session = bind_me->next;
1062 } else if (next_session == ContextList) {
1063 next_session = NULL;
1065 if (bind_me == NULL && next_session != NULL) {
1066 next_session = NULL;
1070 end_critical_section(S_SESSION_TABLE);
1071 end_critical_section(S_I_WANNA_SELECT);
1073 /* We're bound to a session, now do *one* command */
1074 if (bind_me != NULL) {
1075 become_session(bind_me);
1076 CC->h_command_function();
1077 become_session(NULL);
1078 bind_me->state = CON_IDLE;
1079 write(rescan[1], &junk, 1);
1083 dead_session_purge();
1085 check_sched_shutdown();
1088 /* If control reaches this point, the server is shutting down */
1098 * Translate text facility name to syslog.h defined value.
1100 int SyslogFacility(char *name)
1109 { LOG_KERN, "kern" },
1110 { LOG_USER, "user" },
1111 { LOG_MAIL, "mail" },
1112 { LOG_DAEMON, "daemon" },
1113 { LOG_AUTH, "auth" },
1114 { LOG_SYSLOG, "syslog" },
1116 { LOG_NEWS, "news" },
1117 { LOG_UUCP, "uucp" },
1118 { LOG_LOCAL0, "local0" },
1119 { LOG_LOCAL1, "local1" },
1120 { LOG_LOCAL2, "local2" },
1121 { LOG_LOCAL3, "local3" },
1122 { LOG_LOCAL4, "local4" },
1123 { LOG_LOCAL5, "local5" },
1124 { LOG_LOCAL6, "local6" },
1125 { LOG_LOCAL7, "local7" },
1128 for(i = 0; facTbl[i].name != NULL; i++) {
1129 if(!strcasecmp(name, facTbl[i].name))
1130 return facTbl[i].facility;
1136 /********** MEM CHEQQER ***********/
1138 #ifdef DEBUG_MEMORY_LEAKS
1145 void *tracked_malloc(size_t size, char *file, int line) {
1146 struct igheap *thisheap;
1149 block = malloc(size);
1150 if (block == NULL) return(block);
1152 thisheap = malloc(sizeof(struct igheap));
1153 if (thisheap == NULL) {
1158 thisheap->block = block;
1159 strcpy(thisheap->file, file);
1160 thisheap->line = line;
1162 begin_critical_section(S_DEBUGMEMLEAKS);
1163 thisheap->next = igheap;
1165 end_critical_section(S_DEBUGMEMLEAKS);
1171 void *tracked_realloc(void *ptr, size_t size, char *file, int line) {
1172 struct igheap *thisheap;
1175 block = realloc(ptr, size);
1176 if (block == NULL) return(block);
1178 thisheap = malloc(sizeof(struct igheap));
1179 if (thisheap == NULL) {
1184 thisheap->block = block;
1185 strcpy(thisheap->file, file);
1186 thisheap->line = line;
1188 begin_critical_section(S_DEBUGMEMLEAKS);
1189 thisheap->next = igheap;
1191 end_critical_section(S_DEBUGMEMLEAKS);
1198 void tracked_free(void *ptr) {
1199 struct igheap *thisheap;
1200 struct igheap *trash;
1204 if (igheap == NULL) return;
1205 begin_critical_section(S_DEBUGMEMLEAKS);
1206 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1207 if (thisheap->next != NULL) {
1208 if (thisheap->next->block == ptr) {
1209 trash = thisheap->next;
1210 thisheap->next = thisheap->next->next;
1215 if (igheap->block == ptr) {
1217 igheap = igheap->next;
1220 end_critical_section(S_DEBUGMEMLEAKS);
1223 char *tracked_strdup(const char *s, char *file, int line) {
1226 if (s == NULL) return(NULL);
1227 ptr = tracked_malloc(strlen(s) + 1, file, line);
1228 if (ptr == NULL) return(NULL);
1229 strncpy(ptr, s, strlen(s));
1233 void dump_heap(void) {
1234 struct igheap *thisheap;
1236 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1237 lprintf(CTDL_CRIT, "UNFREED: %30s : %d\n",
1238 thisheap->file, thisheap->line);
1242 #endif /* DEBUG_MEMORY_LEAKS */