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 time_t last_purge = 0; /* Last dead session purge */
95 static int num_threads = 0; /* Current number of threads */
96 int num_sessions = 0; /* Current number of sessions */
97 static int rescan[2]; /* The rescan pipe */
99 pthread_t initial_thread; /* tid for main() thread */
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 void lprintf(enum LogLevel loglevel, const char *format, ...) {
114 va_start(arg_ptr, format);
115 vsnprintf(buf, sizeof(buf), format, arg_ptr);
118 if (syslog_facility >= 0) {
119 if (loglevel <= verbosity) {
121 if (CC && CC->cs_pid) {
122 memmove(buf + 6, buf, sizeof(buf) - 6);
123 snprintf(buf, 6, "[%3d]", CC->cs_pid);
126 syslog(loglevel, buf);
129 else if (loglevel <= verbosity) {
134 gettimeofday(&tv, NULL);
135 /* Promote to time_t; types differ on some OSes (like darwin) */
136 unixtime = tv.tv_sec;
137 tim = localtime(&unixtime);
139 * Log provides millisecond accuracy. If you need
140 * microsecond accuracy and your OS supports it, change
141 * %03ld to %06ld and remove " / 1000" after tv.tv_usec.
143 if (CC && CC->cs_pid) {
145 /* Millisecond display */
147 "%04d/%02d/%02d %2d:%02d:%02d.%03ld [%3d] %s",
148 tim->tm_year + 1900, tim->tm_mon + 1,
149 tim->tm_mday, tim->tm_hour, tim->tm_min,
150 tim->tm_sec, (long)tv.tv_usec / 1000,
153 /* Microsecond display */
155 "%04d/%02d/%02d %2d:%02d:%02d.%06ld [%3d] %s",
156 tim->tm_year + 1900, tim->tm_mon + 1,
157 tim->tm_mday, tim->tm_hour, tim->tm_min,
158 tim->tm_sec, (long)tv.tv_usec,
162 /* Millisecond display */
164 "%04d/%02d/%02d %2d:%02d:%02d.%03ld %s",
165 tim->tm_year + 1900, tim->tm_mon + 1,
166 tim->tm_mday, tim->tm_hour, tim->tm_min,
167 tim->tm_sec, (long)tv.tv_usec / 1000, buf);
169 /* Microsecond display */
171 "%04d/%02d/%02d %2d:%02d:%02d.%06ld %s",
172 tim->tm_year + 1900, tim->tm_mon + 1,
173 tim->tm_mday, tim->tm_hour, tim->tm_min,
174 tim->tm_sec, (long)tv.tv_usec, buf);
179 PerformLogHooks(loglevel, buf);
185 * We used to use master_cleanup() as a signal handler to shut down the server.
186 * however, master_cleanup() and the functions it calls do some things that
187 * aren't such a good idea to do from a signal handler: acquiring mutexes,
188 * playing with signal masks on BSDI systems, etc. so instead we install the
189 * following signal handler to set a global variable to inform the main loop
190 * that it's time to call master_cleanup() and exit.
193 volatile int time_to_die = 0;
195 static RETSIGTYPE signal_cleanup(int signum) {
201 * Some initialization stuff...
203 void init_sysdep(void) {
210 /* Set up a bunch of semaphores to be used for critical sections */
211 for (a=0; a<MAX_SEMAPHORES; ++a) {
212 pthread_mutex_init(&Critters[a], NULL);
216 * Set up a place to put thread-specific data.
217 * We only need a single pointer per thread - it points to the
218 * CitContext structure (in the ContextList linked list) of the
219 * session to which the calling thread is currently bound.
221 if (pthread_key_create(&MyConKey, NULL) != 0) {
222 lprintf(CTDL_CRIT, "Can't create TSD key!! %s\n", strerror(errno));
226 * The action for unexpected signals and exceptions should be to
227 * call signal_cleanup() to gracefully shut down the server.
229 signal(SIGINT, signal_cleanup);
230 signal(SIGQUIT, signal_cleanup);
231 signal(SIGHUP, signal_cleanup);
232 signal(SIGTERM, signal_cleanup);
235 * Do not shut down the server on broken pipe signals, otherwise the
236 * whole Citadel service would come down whenever a single client
239 signal(SIGPIPE, SIG_IGN);
242 * Set up the rescan pipe. When a session goes idle, it writes a
243 * single byte to this pipe to wake up the thread calling select(),
244 * which tells it to rescan the list of session sockets.
246 if (pipe(rescan) != 0) {
247 lprintf(CTDL_EMERG, "Cannot create rescan pipe: %s\n",
255 * Obtain a semaphore lock to begin a critical section.
257 void begin_critical_section(int which_one)
259 /* lprintf(CTDL_DEBUG, "begin_critical_section(%d)\n", which_one); */
261 /* For all types of critical sections except those listed here,
262 * ensure nobody ever tries to do a critical section within a
263 * transaction; this could lead to deadlock.
265 if ( (which_one != S_FLOORCACHE)
266 #ifdef DEBUG_MEMORY_LEAKS
267 && (which_one != S_DEBUGMEMLEAKS)
272 pthread_mutex_lock(&Critters[which_one]);
276 * Release a semaphore lock to end a critical section.
278 void end_critical_section(int which_one)
280 pthread_mutex_unlock(&Critters[which_one]);
286 * This is a generic function to set up a master socket for listening on
287 * a TCP port. The server shuts down if the bind fails.
290 int ig_tcp_server(int port_number, int queue_len)
292 struct sockaddr_in sin;
294 int actual_queue_len;
296 actual_queue_len = queue_len;
297 if (actual_queue_len < 5) actual_queue_len = 5;
299 memset(&sin, 0, sizeof(sin));
300 sin.sin_family = AF_INET;
301 sin.sin_addr.s_addr = INADDR_ANY;
302 sin.sin_port = htons((u_short)port_number);
304 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
307 lprintf(CTDL_EMERG, "citserver: Can't create a socket: %s\n",
313 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
315 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
316 lprintf(CTDL_EMERG, "citserver: Can't bind: %s\n",
322 if (listen(s, actual_queue_len) < 0) {
323 lprintf(CTDL_EMERG, "citserver: Can't listen: %s\n", strerror(errno));
334 * Create a Unix domain socket and listen on it
336 int ig_uds_server(char *sockpath, int queue_len)
338 struct sockaddr_un addr;
341 int actual_queue_len;
343 actual_queue_len = queue_len;
344 if (actual_queue_len < 5) actual_queue_len = 5;
346 i = unlink(sockpath);
347 if (i != 0) if (errno != ENOENT) {
348 lprintf(CTDL_EMERG, "citserver: can't unlink %s: %s\n",
349 sockpath, strerror(errno));
353 memset(&addr, 0, sizeof(addr));
354 addr.sun_family = AF_UNIX;
355 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
357 s = socket(AF_UNIX, SOCK_STREAM, 0);
359 lprintf(CTDL_EMERG, "citserver: Can't create a socket: %s\n",
364 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
365 lprintf(CTDL_EMERG, "citserver: Can't bind: %s\n",
370 if (listen(s, actual_queue_len) < 0) {
371 lprintf(CTDL_EMERG, "citserver: Can't listen: %s\n", strerror(errno));
375 chmod(sockpath, 0777);
382 * Return a pointer to the CitContext structure bound to the thread which
383 * called this function. If there's no such binding (for example, if it's
384 * called by the housekeeper thread) then a generic 'master' CC is returned.
386 * It's inlined because it's used *VERY* frequently.
388 INLINE struct CitContext *MyContext(void) {
389 return ((pthread_getspecific(MyConKey) == NULL)
391 : (struct CitContext *) pthread_getspecific(MyConKey)
397 * Initialize a new context and place it in the list. The session number
398 * used to be the PID (which is why it's called cs_pid), but that was when we
399 * had one process per session. Now we just assign them sequentially, starting
400 * at 1 (don't change it to 0 because masterCC uses 0) and re-using them when
401 * sessions terminate.
403 struct CitContext *CreateNewContext(void) {
404 struct CitContext *me, *ptr;
406 me = (struct CitContext *) malloc(sizeof(struct CitContext));
408 lprintf(CTDL_ALERT, "citserver: can't allocate memory!!\n");
411 memset(me, 0, sizeof(struct CitContext));
413 /* The new context will be created already in the CON_EXECUTING state
414 * in order to prevent another thread from grabbing it while it's
417 me->state = CON_EXECUTING;
421 * Generate a unique session number and insert this context into
424 begin_critical_section(S_SESSION_TABLE);
426 if (ContextList == NULL) {
432 else if (ContextList->cs_pid > 1) {
433 me->next = ContextList;
439 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
440 if (ptr->next == NULL) {
442 me->cs_pid = ptr->cs_pid + 1;
446 else if (ptr->next->cs_pid > (ptr->cs_pid+1)) {
447 me->next = ptr->next;
449 me->cs_pid = ptr->cs_pid + 1;
455 DONE: ++num_sessions;
456 end_critical_section(S_SESSION_TABLE);
462 * buffer_output() ... tell client_write to buffer all output until
463 * instructed to dump it all out later
465 void buffer_output(void) {
466 if (CC->buffering == 0) {
469 CC->output_buffer = malloc(SIZ);
474 * unbuffer_output() ... dump out all that output we've been buffering.
476 void unbuffer_output(void) {
477 if (CC->buffering == 1) {
479 client_write(CC->output_buffer, CC->buffer_len);
480 free(CC->output_buffer);
481 CC->output_buffer = NULL;
489 * client_write() ... Send binary data to the client.
491 void client_write(char *buf, int nbytes)
493 int bytes_written = 0;
496 int old_buffer_len = 0;
498 if (CC->redirect_fp != NULL) {
499 fwrite(buf, nbytes, 1, CC->redirect_fp);
503 if (CC->redirect_sock > 0) {
504 sock = CC->redirect_sock; /* and continue below... */
507 sock = CC->client_socket;
510 /* If we're buffering for later, do that now. */
512 old_buffer_len = CC->buffer_len;
513 CC->buffer_len += nbytes;
514 CC->output_buffer = realloc(CC->output_buffer, CC->buffer_len);
515 memcpy(&CC->output_buffer[old_buffer_len], buf, nbytes);
519 /* Ok, at this point we're not buffering. Go ahead and write. */
522 if (CC->redirect_ssl) {
523 client_write_ssl(buf, nbytes);
528 while (bytes_written < nbytes) {
529 retval = write(sock, &buf[bytes_written],
530 nbytes - bytes_written);
532 lprintf(CTDL_ERR, "client_write() failed: %s\n",
534 if (sock == CC->client_socket) CC->kill_me = 1;
537 bytes_written = bytes_written + retval;
543 * cprintf() ... Send formatted printable data to the client. It is
544 * implemented in terms of client_write() but remains in
545 * sysdep.c in case we port to somewhere without va_args...
547 void cprintf(const char *format, ...) {
551 va_start(arg_ptr, format);
552 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
553 buf[sizeof buf - 2] = '\n';
554 client_write(buf, strlen(buf));
560 * Read data from the client socket.
562 * 1 Requested number of bytes has been read.
563 * 0 Request timed out.
564 * -1 The socket is broken.
565 * If the socket breaks, the session will be terminated.
567 int client_read_to(char *buf, int bytes, int timeout)
575 if (CC->redirect_ssl) {
576 return (client_read_ssl(buf, bytes, timeout));
582 FD_SET(CC->client_socket, &rfds);
586 retval = select( (CC->client_socket)+1,
587 &rfds, NULL, NULL, &tv);
589 if (FD_ISSET(CC->client_socket, &rfds) == 0) {
593 rlen = read(CC->client_socket, &buf[len], bytes-len);
595 lprintf(CTDL_ERR, "client_read() failed: %s\n",
606 * Read data from the client socket with default timeout.
607 * (This is implemented in terms of client_read_to() and could be
608 * justifiably moved out of sysdep.c)
610 INLINE int client_read(char *buf, int bytes)
612 return(client_read_to(buf, bytes, config.c_sleeping));
617 * client_gets() ... Get a LF-terminated line of text from the client.
618 * (This is implemented in terms of client_read() and could be
619 * justifiably moved out of sysdep.c)
621 int client_gets(char *buf)
625 /* Read one character at a time.
628 retval = client_read(&buf[i], 1);
629 if (retval != 1 || buf[i] == '\n' || i == (SIZ-1))
633 /* If we got a long line, discard characters until the newline.
636 while (buf[i] != '\n' && retval == 1)
637 retval = client_read(&buf[i], 1);
639 /* Strip the trailing newline and any trailing nonprintables (cr's)
642 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
643 buf[strlen(buf)-1] = 0;
644 if (retval < 0) strcpy(buf, "000");
651 * The system-dependent part of master_cleanup() - close the master socket.
653 void sysdep_master_cleanup(void) {
654 struct ServiceFunctionHook *serviceptr;
657 * close all protocol master sockets
659 for (serviceptr = ServiceHookTable; serviceptr != NULL;
660 serviceptr = serviceptr->next ) {
662 if (serviceptr->tcp_port > 0)
663 lprintf(CTDL_INFO, "Closing listener on port %d\n",
664 serviceptr->tcp_port);
666 if (serviceptr->sockpath != NULL)
667 lprintf(CTDL_INFO, "Closing listener on '%s'\n",
668 serviceptr->sockpath);
670 close(serviceptr->msock);
672 /* If it's a Unix domain socket, remove the file. */
673 if (serviceptr->sockpath != NULL) {
674 unlink(serviceptr->sockpath);
681 * Terminate another session.
682 * (This could justifiably be moved out of sysdep.c because it
683 * no longer does anything that is system-dependent.)
685 void kill_session(int session_to_kill) {
686 struct CitContext *ptr;
688 begin_critical_section(S_SESSION_TABLE);
689 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
690 if (ptr->cs_pid == session_to_kill) {
694 end_critical_section(S_SESSION_TABLE);
701 * Start running as a daemon. Only close stdio if do_close_stdio is set.
703 void start_daemon(int do_close_stdio) {
704 if (do_close_stdio) {
709 signal(SIGHUP,SIG_IGN);
710 signal(SIGINT,SIG_IGN);
711 signal(SIGQUIT,SIG_IGN);
712 if (fork()!=0) exit(0);
718 * Generic routine to convert a login name to a full name (gecos)
719 * Returns nonzero if a conversion took place
721 int convert_login(char NameToConvert[]) {
725 pw = getpwnam(NameToConvert);
730 strcpy(NameToConvert, pw->pw_gecos);
731 for (a=0; a<strlen(NameToConvert); ++a) {
732 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
738 struct worker_node *worker_list = NULL;
742 * create a worker thread. this function must always be called from within
743 * an S_WORKER_LIST critical section!
745 void create_worker(void) {
747 struct worker_node *n;
750 n = malloc(sizeof(struct worker_node));
752 lprintf(CTDL_EMERG, "can't allocate worker_node, exiting\n");
757 if ((ret = pthread_attr_init(&attr))) {
758 lprintf(CTDL_EMERG, "pthread_attr_init: %s\n", strerror(ret));
763 /* we seem to need something bigger than FreeBSD's default 64k stack */
765 if ((ret = pthread_attr_setstacksize(&attr, 128 * 1024))) {
766 lprintf(CTDL_EMERG, "pthread_attr_setstacksize: %s\n", strerror(ret));
771 if ((ret = pthread_create(&n->tid, &attr, worker_thread, NULL) != 0))
774 lprintf(CTDL_ALERT, "Can't create worker thread: %s\n",
778 n->next = worker_list;
785 * Purge all sessions which have the 'kill_me' flag set.
786 * This function has code to prevent it from running more than once every
787 * few seconds, because running it after every single unbind would waste a lot
788 * of CPU time and keep the context list locked too much. To force it to run
789 * anyway, set "force" to nonzero.
792 * After that's done, we raise the size of the worker thread pool
793 * if such an action is appropriate.
795 void dead_session_purge(int force) {
796 struct CitContext *ptr, *rem;
799 if ( (time(NULL) - last_purge) < 5 ) {
800 return; /* Too soon, go away */
807 begin_critical_section(S_SESSION_TABLE);
808 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
809 if ( (ptr->state == CON_IDLE) && (ptr->kill_me) ) {
813 end_critical_section(S_SESSION_TABLE);
815 /* RemoveContext() enters its own S_SESSION_TABLE critical
816 * section, so we have to do it like this.
819 lprintf(CTDL_DEBUG, "Purging session %d\n", rem->cs_pid);
823 } while (rem != NULL);
825 /* Raise the size of the worker thread pool if necessary. */
827 if ( (num_sessions > num_threads)
828 && (num_threads < config.c_max_workers) ) {
829 begin_critical_section(S_WORKER_LIST);
831 end_critical_section(S_WORKER_LIST);
840 * Redirect a session's output to a file or socket.
841 * This function may be called with a file handle *or* a socket (but not
842 * both). Call with neither to return output to its normal client socket.
844 void CtdlRedirectOutput(FILE *fp, int sock) {
846 if (fp != NULL) CC->redirect_fp = fp;
847 else CC->redirect_fp = NULL;
849 if (sock > 0) CC->redirect_sock = sock;
850 else CC->redirect_sock = (-1);
856 * masterCC is the context we use when not attached to a session. This
857 * function initializes it.
859 void InitializeMasterCC(void) {
860 memset(&masterCC, 0, sizeof(struct CitContext));
861 masterCC.internal_pgm = 1;
871 * Bind a thread to a context. (It's inline merely to speed things up.)
873 INLINE void become_session(struct CitContext *which_con) {
874 pthread_setspecific(MyConKey, (void *)which_con );
880 * This loop just keeps going and going and going...
882 void *worker_thread(void *arg) {
885 struct CitContext *ptr;
886 struct CitContext *bind_me = NULL;
889 struct CitContext *con= NULL; /* Temporary context pointer */
890 struct ServiceFunctionHook *serviceptr;
891 int ssock; /* Descriptor for client socket */
900 while (!time_to_die) {
902 /* make doubly sure we're not holding any stale db handles
903 * which might cause a deadlock.
906 do_select: force_purge = 0;
907 bind_me = NULL; /* Which session shall we handle? */
909 /* Initialize the fdset. Start with the rescan pipe. */
911 FD_SET(rescan[0], &readfds);
912 highest = rescan[0] + 1;
914 begin_critical_section(S_SESSION_TABLE);
915 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
916 if (ptr->state == CON_IDLE) {
917 FD_SET(ptr->client_socket, &readfds);
918 if (ptr->client_socket > highest)
919 highest = ptr->client_socket;
921 if ((bind_me == NULL) && (ptr->state == CON_READY)) {
923 ptr->state = CON_EXECUTING;
926 end_critical_section(S_SESSION_TABLE);
928 if (bind_me) goto SKIP_SELECT;
930 /* If we got this far, it means that there are no sessions
931 * which a previous thread marked for attention, so we go
932 * ahead and get ready to select().
935 /* First, add the various master sockets to the fdset. */
936 for (serviceptr = ServiceHookTable; serviceptr != NULL;
937 serviceptr = serviceptr->next ) {
938 m = serviceptr->msock;
946 tv.tv_sec = 1; /* wake up every second if no input */
949 * Avoid the "thundering herd" problem by letting only
950 * one thread select() at a time.
952 begin_critical_section(S_I_WANNA_SELECT);
953 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
954 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 == EBADF) {
965 lprintf(CTDL_NOTICE, "select() failed: (%s)\n",
969 if (errno != EINTR) {
970 lprintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
972 } else if (!time_to_die)
976 /* Next, check to see if it's a new client connecting
977 * on a master socket.
979 else for (serviceptr = ServiceHookTable; serviceptr != NULL;
980 serviceptr = serviceptr->next ) {
982 if (FD_ISSET(serviceptr->msock, &readfds)) {
983 ssock = accept(serviceptr->msock, NULL, 0);
986 "citserver: accept(): %s\n",
991 "New client socket %d\n",
994 /* New context will be created already
995 * set up in the CON_EXECUTING state.
997 con = CreateNewContext();
999 /* Assign new socket number to it. */
1000 con->client_socket = ssock;
1001 con->h_command_function =
1002 serviceptr->h_command_function;
1003 con->h_async_function =
1004 serviceptr->h_async_function;
1006 /* Determine whether local socket */
1007 if (serviceptr->sockpath != NULL)
1008 con->is_local_socket = 1;
1010 /* Set the SO_REUSEADDR socket option */
1012 setsockopt(ssock, SOL_SOCKET,
1016 become_session(con);
1018 serviceptr->h_greeting_function();
1019 become_session(NULL);
1020 con->state = CON_IDLE;
1022 /* Wake up the currently blocking select() by
1023 * writing a dummy byte to the rescan pipe.
1025 write(rescan[1], &i, 1);
1032 end_critical_section(S_I_WANNA_SELECT);
1036 /* If the rescan pipe went active, read a dummy byte from it */
1037 if (FD_ISSET(rescan[0], &readfds)) {
1038 read(rescan[0], &i, 1);
1042 /* It must be a client socket. Find a context that has data
1043 * waiting on its socket *and* is in the CON_IDLE state. Any
1044 * active sockets other than our chosen one are marked as
1045 * CON_READY so the next thread that comes around can just bind
1046 * to one without having to select() again.
1048 begin_critical_section(S_SESSION_TABLE);
1049 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1050 if ( (FD_ISSET(ptr->client_socket, &readfds))
1051 && (ptr->state != CON_EXECUTING) ) {
1052 ptr->input_waiting = 1;
1054 bind_me = ptr; /* I choose you! */
1055 bind_me->state = CON_EXECUTING;
1058 ptr->state = CON_READY;
1062 end_critical_section(S_SESSION_TABLE);
1065 /* We're bound to a session */
1066 if (bind_me != NULL) {
1067 become_session(bind_me);
1069 /* If the client has sent a command, execute it. */
1070 if (CC->input_waiting) {
1071 CC->h_command_function();
1072 CC->input_waiting = 0;
1075 /* If there are asynchronous messages waiting and the
1076 * client supports it, do those now */
1077 if ((CC->is_async) && (CC->async_waiting)
1078 && (CC->h_async_function != NULL)) {
1079 CC->h_async_function();
1080 CC->async_waiting = 0;
1083 force_purge = CC->kill_me;
1084 become_session(NULL);
1085 bind_me->state = CON_IDLE;
1087 /* Wake up the currently blocking select() by writing
1088 * a dummy byte to the rescan pipe.
1090 write(rescan[1], &i, 1);
1093 dead_session_purge(force_purge);
1095 check_sched_shutdown();
1098 /* If control reaches this point, the server is shutting down */
1108 * Translate text facility name to syslog.h defined value.
1110 int SyslogFacility(char *name)
1119 { LOG_KERN, "kern" },
1120 { LOG_USER, "user" },
1121 { LOG_MAIL, "mail" },
1122 { LOG_DAEMON, "daemon" },
1123 { LOG_AUTH, "auth" },
1124 { LOG_SYSLOG, "syslog" },
1126 { LOG_NEWS, "news" },
1127 { LOG_UUCP, "uucp" },
1128 { LOG_LOCAL0, "local0" },
1129 { LOG_LOCAL1, "local1" },
1130 { LOG_LOCAL2, "local2" },
1131 { LOG_LOCAL3, "local3" },
1132 { LOG_LOCAL4, "local4" },
1133 { LOG_LOCAL5, "local5" },
1134 { LOG_LOCAL6, "local6" },
1135 { LOG_LOCAL7, "local7" },
1138 for(i = 0; facTbl[i].name != NULL; i++) {
1139 if(!strcasecmp(name, facTbl[i].name))
1140 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 */