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 */
98 pthread_t initial_thread; /* tid for main() thread */
100 int syslog_facility = (-1);
104 * lprintf() ... Write logging information
106 * Note: the variable "buf" below needs to be large enough to handle any
107 * log data sent through this function. BE CAREFUL!
109 void lprintf(enum LogLevel loglevel, const char *format, ...) {
113 va_start(arg_ptr, format);
114 vsnprintf(buf, sizeof(buf), format, arg_ptr);
117 if (syslog_facility >= 0) {
118 if (loglevel <= verbosity) {
120 if (CC && CC->cs_pid) {
121 memmove(buf + 6, buf, sizeof(buf) - 6);
122 snprintf(buf, 6, "[%3d]", CC->cs_pid);
125 syslog(loglevel, buf);
128 else if (loglevel <= verbosity) {
133 gettimeofday(&tv, NULL);
134 /* Promote to time_t; types differ on some OSes (like darwin) */
135 unixtime = tv.tv_sec;
136 tim = localtime(&unixtime);
138 * Log provides millisecond accuracy. If you need
139 * microsecond accuracy and your OS supports it, change
140 * %03ld to %06ld and remove " / 1000" after tv.tv_usec.
142 if (CC && CC->cs_pid) {
144 /* Millisecond display */
146 "%04d/%02d/%02d %2d:%02d:%02d.%03ld [%3d] %s",
147 tim->tm_year + 1900, tim->tm_mon + 1,
148 tim->tm_mday, tim->tm_hour, tim->tm_min,
149 tim->tm_sec, (long)tv.tv_usec / 1000,
152 /* Microsecond display */
154 "%04d/%02d/%02d %2d:%02d:%02d.%06ld [%3d] %s",
155 tim->tm_year + 1900, tim->tm_mon + 1,
156 tim->tm_mday, tim->tm_hour, tim->tm_min,
157 tim->tm_sec, (long)tv.tv_usec,
161 /* Millisecond display */
163 "%04d/%02d/%02d %2d:%02d:%02d.%03ld %s",
164 tim->tm_year + 1900, tim->tm_mon + 1,
165 tim->tm_mday, tim->tm_hour, tim->tm_min,
166 tim->tm_sec, (long)tv.tv_usec / 1000, buf);
168 /* Microsecond display */
170 "%04d/%02d/%02d %2d:%02d:%02d.%06ld %s",
171 tim->tm_year + 1900, tim->tm_mon + 1,
172 tim->tm_mday, tim->tm_hour, tim->tm_min,
173 tim->tm_sec, (long)tv.tv_usec, buf);
178 PerformLogHooks(loglevel, buf);
184 * We used to use master_cleanup() as a signal handler to shut down the server.
185 * however, master_cleanup() and the functions it calls do some things that
186 * aren't such a good idea to do from a signal handler: acquiring mutexes,
187 * playing with signal masks on BSDI systems, etc. so instead we install the
188 * following signal handler to set a global variable to inform the main loop
189 * that it's time to call master_cleanup() and exit.
192 volatile int time_to_die = 0;
194 static RETSIGTYPE signal_cleanup(int signum) {
200 * Some initialization stuff...
202 void init_sysdep(void) {
209 /* Set up a bunch of semaphores to be used for critical sections */
210 for (a=0; a<MAX_SEMAPHORES; ++a) {
211 pthread_mutex_init(&Critters[a], NULL);
215 * Set up a place to put thread-specific data.
216 * We only need a single pointer per thread - it points to the
217 * CitContext structure (in the ContextList linked list) of the
218 * session to which the calling thread is currently bound.
220 if (pthread_key_create(&MyConKey, NULL) != 0) {
221 lprintf(CTDL_CRIT, "Can't create TSD key!! %s\n", strerror(errno));
225 * The action for unexpected signals and exceptions should be to
226 * call signal_cleanup() to gracefully shut down the server.
228 signal(SIGINT, signal_cleanup);
229 signal(SIGQUIT, signal_cleanup);
230 signal(SIGHUP, signal_cleanup);
231 signal(SIGTERM, signal_cleanup);
234 * Do not shut down the server on broken pipe signals, otherwise the
235 * whole Citadel service would come down whenever a single client
238 signal(SIGPIPE, SIG_IGN);
243 * Obtain a semaphore lock to begin a critical section.
245 void begin_critical_section(int which_one)
247 /* lprintf(CTDL_DEBUG, "begin_critical_section(%d)\n", which_one); */
249 /* For all types of critical sections except those listed here,
250 * ensure nobody ever tries to do a critical section within a
251 * transaction; this could lead to deadlock.
253 if ( (which_one != S_FLOORCACHE)
254 #ifdef DEBUG_MEMORY_LEAKS
255 && (which_one != S_DEBUGMEMLEAKS)
260 pthread_mutex_lock(&Critters[which_one]);
264 * Release a semaphore lock to end a critical section.
266 void end_critical_section(int which_one)
268 pthread_mutex_unlock(&Critters[which_one]);
274 * This is a generic function to set up a master socket for listening on
275 * a TCP port. The server shuts down if the bind fails.
278 int ig_tcp_server(int port_number, int queue_len)
280 struct sockaddr_in sin;
282 int actual_queue_len;
284 actual_queue_len = queue_len;
285 if (actual_queue_len < 5) actual_queue_len = 5;
287 memset(&sin, 0, sizeof(sin));
288 sin.sin_family = AF_INET;
289 sin.sin_addr.s_addr = INADDR_ANY;
290 sin.sin_port = htons((u_short)port_number);
292 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
295 lprintf(CTDL_EMERG, "citserver: Can't create a socket: %s\n",
301 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
303 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
304 lprintf(CTDL_EMERG, "citserver: Can't bind: %s\n",
310 if (listen(s, actual_queue_len) < 0) {
311 lprintf(CTDL_EMERG, "citserver: Can't listen: %s\n", strerror(errno));
322 * Create a Unix domain socket and listen on it
324 int ig_uds_server(char *sockpath, int queue_len)
326 struct sockaddr_un addr;
329 int actual_queue_len;
331 actual_queue_len = queue_len;
332 if (actual_queue_len < 5) actual_queue_len = 5;
334 i = unlink(sockpath);
335 if (i != 0) if (errno != ENOENT) {
336 lprintf(CTDL_EMERG, "citserver: can't unlink %s: %s\n",
337 sockpath, strerror(errno));
341 memset(&addr, 0, sizeof(addr));
342 addr.sun_family = AF_UNIX;
343 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
345 s = socket(AF_UNIX, SOCK_STREAM, 0);
347 lprintf(CTDL_EMERG, "citserver: Can't create a socket: %s\n",
352 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
353 lprintf(CTDL_EMERG, "citserver: Can't bind: %s\n",
358 if (listen(s, actual_queue_len) < 0) {
359 lprintf(CTDL_EMERG, "citserver: Can't listen: %s\n", strerror(errno));
363 chmod(sockpath, 0777);
370 * Return a pointer to the CitContext structure bound to the thread which
371 * called this function. If there's no such binding (for example, if it's
372 * called by the housekeeper thread) then a generic 'master' CC is returned.
374 * It's inlined because it's used *VERY* frequently.
376 INLINE struct CitContext *MyContext(void) {
377 return ((pthread_getspecific(MyConKey) == NULL)
379 : (struct CitContext *) pthread_getspecific(MyConKey)
385 * Initialize a new context and place it in the list. The session number
386 * used to be the PID (which is why it's called cs_pid), but that was when we
387 * had one process per session. Now we just assign them sequentially, starting
388 * at 1 (don't change it to 0 because masterCC uses 0) and re-using them when
389 * sessions terminate.
391 struct CitContext *CreateNewContext(void) {
392 struct CitContext *me, *ptr;
394 me = (struct CitContext *) malloc(sizeof(struct CitContext));
396 lprintf(CTDL_ALERT, "citserver: can't allocate memory!!\n");
399 memset(me, 0, sizeof(struct CitContext));
401 /* The new context will be created already in the CON_EXECUTING state
402 * in order to prevent another thread from grabbing it while it's
405 me->state = CON_EXECUTING;
409 * Generate a unique session number and insert this context into
412 begin_critical_section(S_SESSION_TABLE);
414 if (ContextList == NULL) {
420 else if (ContextList->cs_pid > 1) {
421 me->next = ContextList;
427 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
428 if (ptr->next == NULL) {
430 me->cs_pid = ptr->cs_pid + 1;
434 else if (ptr->next->cs_pid > (ptr->cs_pid+1)) {
435 me->next = ptr->next;
437 me->cs_pid = ptr->cs_pid + 1;
443 DONE: ++num_sessions;
444 end_critical_section(S_SESSION_TABLE);
450 * buffer_output() ... tell client_write to buffer all output until
451 * instructed to dump it all out later
453 void buffer_output(void) {
454 if (CC->buffering == 0) {
457 CC->output_buffer = malloc(SIZ);
462 * unbuffer_output() ... dump out all that output we've been buffering.
464 void unbuffer_output(void) {
465 if (CC->buffering == 1) {
467 client_write(CC->output_buffer, CC->buffer_len);
468 free(CC->output_buffer);
469 CC->output_buffer = NULL;
477 * client_write() ... Send binary data to the client.
479 void client_write(char *buf, int nbytes)
481 int bytes_written = 0;
484 int old_buffer_len = 0;
486 if (CC->redirect_fp != NULL) {
487 fwrite(buf, nbytes, 1, CC->redirect_fp);
491 if (CC->redirect_sock > 0) {
492 sock = CC->redirect_sock; /* and continue below... */
495 sock = CC->client_socket;
498 /* If we're buffering for later, do that now. */
500 old_buffer_len = CC->buffer_len;
501 CC->buffer_len += nbytes;
502 CC->output_buffer = realloc(CC->output_buffer, CC->buffer_len);
503 memcpy(&CC->output_buffer[old_buffer_len], buf, nbytes);
507 /* Ok, at this point we're not buffering. Go ahead and write. */
510 if (CC->redirect_ssl) {
511 client_write_ssl(buf, nbytes);
516 while (bytes_written < nbytes) {
517 retval = write(sock, &buf[bytes_written],
518 nbytes - bytes_written);
520 lprintf(CTDL_ERR, "client_write() failed: %s\n",
522 if (sock == CC->client_socket) CC->kill_me = 1;
525 bytes_written = bytes_written + retval;
531 * cprintf() ... Send formatted printable data to the client. It is
532 * implemented in terms of client_write() but remains in
533 * sysdep.c in case we port to somewhere without va_args...
535 void cprintf(const char *format, ...) {
539 va_start(arg_ptr, format);
540 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
541 buf[sizeof buf - 2] = '\n';
542 client_write(buf, strlen(buf));
548 * Read data from the client socket.
550 * 1 Requested number of bytes has been read.
551 * 0 Request timed out.
552 * -1 The socket is broken.
553 * If the socket breaks, the session will be terminated.
555 int client_read_to(char *buf, int bytes, int timeout)
563 if (CC->redirect_ssl) {
564 return (client_read_ssl(buf, bytes, timeout));
570 FD_SET(CC->client_socket, &rfds);
574 retval = select( (CC->client_socket)+1,
575 &rfds, NULL, NULL, &tv);
577 if (FD_ISSET(CC->client_socket, &rfds) == 0) {
581 rlen = read(CC->client_socket, &buf[len], bytes-len);
583 lprintf(CTDL_ERR, "client_read() failed: %s\n",
594 * Read data from the client socket with default timeout.
595 * (This is implemented in terms of client_read_to() and could be
596 * justifiably moved out of sysdep.c)
598 INLINE int client_read(char *buf, int bytes)
600 return(client_read_to(buf, bytes, config.c_sleeping));
605 * client_gets() ... Get a LF-terminated line of text from the client.
606 * (This is implemented in terms of client_read() and could be
607 * justifiably moved out of sysdep.c)
609 int client_gets(char *buf)
613 /* Read one character at a time.
616 retval = client_read(&buf[i], 1);
617 if (retval != 1 || buf[i] == '\n' || i == (SIZ-1))
621 /* If we got a long line, discard characters until the newline.
624 while (buf[i] != '\n' && retval == 1)
625 retval = client_read(&buf[i], 1);
627 /* Strip the trailing newline and any trailing nonprintables (cr's)
630 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
631 buf[strlen(buf)-1] = 0;
632 if (retval < 0) strcpy(buf, "000");
639 * The system-dependent part of master_cleanup() - close the master socket.
641 void sysdep_master_cleanup(void) {
642 struct ServiceFunctionHook *serviceptr;
645 * close all protocol master sockets
647 for (serviceptr = ServiceHookTable; serviceptr != NULL;
648 serviceptr = serviceptr->next ) {
650 if (serviceptr->tcp_port > 0)
651 lprintf(CTDL_INFO, "Closing listener on port %d\n",
652 serviceptr->tcp_port);
654 if (serviceptr->sockpath != NULL)
655 lprintf(CTDL_INFO, "Closing listener on '%s'\n",
656 serviceptr->sockpath);
658 close(serviceptr->msock);
660 /* If it's a Unix domain socket, remove the file. */
661 if (serviceptr->sockpath != NULL) {
662 unlink(serviceptr->sockpath);
669 * Terminate another session.
670 * (This could justifiably be moved out of sysdep.c because it
671 * no longer does anything that is system-dependent.)
673 void kill_session(int session_to_kill) {
674 struct CitContext *ptr;
676 begin_critical_section(S_SESSION_TABLE);
677 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
678 if (ptr->cs_pid == session_to_kill) {
682 end_critical_section(S_SESSION_TABLE);
689 * Start running as a daemon. Only close stdio if do_close_stdio is set.
691 void start_daemon(int do_close_stdio) {
692 if (do_close_stdio) {
697 signal(SIGHUP,SIG_IGN);
698 signal(SIGINT,SIG_IGN);
699 signal(SIGQUIT,SIG_IGN);
700 if (fork()!=0) exit(0);
706 * Generic routine to convert a login name to a full name (gecos)
707 * Returns nonzero if a conversion took place
709 int convert_login(char NameToConvert[]) {
713 pw = getpwnam(NameToConvert);
718 strcpy(NameToConvert, pw->pw_gecos);
719 for (a=0; a<strlen(NameToConvert); ++a) {
720 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
726 struct worker_node *worker_list = NULL;
730 * create a worker thread. this function must always be called from within
731 * an S_WORKER_LIST critical section!
733 void create_worker(void) {
735 struct worker_node *n;
738 n = malloc(sizeof(struct worker_node));
740 lprintf(CTDL_EMERG, "can't allocate worker_node, exiting\n");
745 if ((ret = pthread_attr_init(&attr))) {
746 lprintf(CTDL_EMERG, "pthread_attr_init: %s\n", strerror(ret));
751 /* we seem to need something bigger than FreeBSD's default 64k stack */
753 if ((ret = pthread_attr_setstacksize(&attr, 128 * 1024))) {
754 lprintf(CTDL_EMERG, "pthread_attr_setstacksize: %s\n", strerror(ret));
759 if ((ret = pthread_create(&n->tid, &attr, worker_thread, NULL) != 0))
762 lprintf(CTDL_ALERT, "Can't create worker thread: %s\n",
766 n->next = worker_list;
773 * Purge all sessions which have the 'kill_me' flag set.
774 * This function has code to prevent it from running more than once every
775 * few seconds, because running it after every single unbind would waste a lot
776 * of CPU time and keep the context list locked too much. To force it to run
777 * anyway, set "force" to nonzero.
780 * After that's done, we raise the size of the worker thread pool
781 * if such an action is appropriate.
783 void dead_session_purge(int force) {
784 struct CitContext *ptr, *rem;
787 if ( (time(NULL) - last_purge) < 5 ) {
788 return; /* Too soon, go away */
795 begin_critical_section(S_SESSION_TABLE);
796 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
797 if ( (ptr->state == CON_IDLE) && (ptr->kill_me) ) {
801 end_critical_section(S_SESSION_TABLE);
803 /* RemoveContext() enters its own S_SESSION_TABLE critical
804 * section, so we have to do it like this.
807 lprintf(CTDL_DEBUG, "Purging session %d\n", rem->cs_pid);
811 } while (rem != NULL);
813 /* Raise the size of the worker thread pool if necessary. */
815 if ( (num_sessions > num_threads)
816 && (num_threads < config.c_max_workers) ) {
817 begin_critical_section(S_WORKER_LIST);
819 end_critical_section(S_WORKER_LIST);
828 * Redirect a session's output to a file or socket.
829 * This function may be called with a file handle *or* a socket (but not
830 * both). Call with neither to return output to its normal client socket.
832 void CtdlRedirectOutput(FILE *fp, int sock) {
834 if (fp != NULL) CC->redirect_fp = fp;
835 else CC->redirect_fp = NULL;
837 if (sock > 0) CC->redirect_sock = sock;
838 else CC->redirect_sock = (-1);
844 * masterCC is the context we use when not attached to a session. This
845 * function initializes it.
847 void InitializeMasterCC(void) {
848 memset(&masterCC, 0, sizeof(struct CitContext));
849 masterCC.internal_pgm = 1;
859 * Bind a thread to a context. (It's inline merely to speed things up.)
861 INLINE void become_session(struct CitContext *which_con) {
862 pthread_setspecific(MyConKey, (void *)which_con );
868 * This loop just keeps going and going and going...
870 void *worker_thread(void *arg) {
873 struct CitContext *ptr;
874 struct CitContext *bind_me = NULL;
877 struct CitContext *con= NULL; /* Temporary context pointer */
878 struct ServiceFunctionHook *serviceptr;
879 int ssock; /* Descriptor for client socket */
888 while (!time_to_die) {
891 * A naive implementation would have all idle threads
892 * calling select() and then they'd all wake up at once
893 * (known in computer science as the "thundering herd"
894 * problem). We solve this problem by putting the select()
895 * in a critical section, so only one thread has the
896 * opportunity to wake up. If we wake up on a master
897 * socket, create a new session context; otherwise, just
898 * bind the thread to the context we want and go on our
902 /* make doubly sure we're not holding any stale db handles
903 * which might cause a deadlock.
907 bind_me = NULL; /* Which session shall we handle? */
909 begin_critical_section(S_I_WANNA_SELECT);
911 /* Initialize the fdset. */
915 begin_critical_section(S_SESSION_TABLE);
916 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
917 if (ptr->state == CON_IDLE) {
918 FD_SET(ptr->client_socket, &readfds);
919 if (ptr->client_socket > highest)
920 highest = ptr->client_socket;
922 if ((bind_me == NULL) && (ptr->state == CON_READY)) {
924 ptr->state = CON_EXECUTING;
927 end_critical_section(S_SESSION_TABLE);
929 if (bind_me) goto SKIP_SELECT;
931 do_select: /* Only select() if the server isn't being told to shut down. */
933 /* Add the various master sockets to the fdset. */
934 for (serviceptr = ServiceHookTable; serviceptr != NULL;
935 serviceptr = serviceptr->next ) {
936 m = serviceptr->msock;
944 tv.tv_sec = 1; /* wake up every second if no input */
946 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
949 end_critical_section(S_I_WANNA_SELECT);
953 /* Now figure out who made this select() unblock.
954 * First, check for an error or exit condition.
957 if (errno == EBADF) {
958 lprintf(CTDL_NOTICE, "select() failed: (%s)\n",
962 if (errno != EINTR) {
963 lprintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
965 } else if (!time_to_die)
969 /* Next, check to see if it's a new client connecting
970 * on a master socket.
972 else for (serviceptr = ServiceHookTable; serviceptr != NULL;
973 serviceptr = serviceptr->next ) {
975 if (FD_ISSET(serviceptr->msock, &readfds)) {
976 ssock = accept(serviceptr->msock, NULL, 0);
979 "citserver: accept(): %s\n",
984 "New client socket %d\n",
987 /* New context will be created already
988 * set up in the CON_EXECUTING state.
990 con = CreateNewContext();
992 /* Assign new socket number to it. */
993 con->client_socket = ssock;
994 con->h_command_function =
995 serviceptr->h_command_function;
996 con->h_async_function =
997 serviceptr->h_async_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 /* It must be a client socket. Find a context that has data
1025 * waiting on its socket *and* is in the CON_IDLE state. Any
1026 * active sockets other than our chosen one are marked as
1027 * CON_READY so the next thread that comes around can just bind
1028 * to one without having to select() again.
1030 begin_critical_section(S_SESSION_TABLE);
1031 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1032 if ( (FD_ISSET(ptr->client_socket, &readfds))
1033 && (ptr->state != CON_EXECUTING) ) {
1034 ptr->input_waiting = 1;
1036 bind_me = ptr; /* I choose you! */
1037 bind_me->state = CON_EXECUTING;
1040 ptr->state = CON_READY;
1044 end_critical_section(S_SESSION_TABLE);
1045 SKIP_SELECT: end_critical_section(S_I_WANNA_SELECT);
1047 /* We're bound to a session */
1048 if (bind_me != NULL) {
1049 become_session(bind_me);
1051 /* If the client has sent a command, execute it. */
1052 if (CC->input_waiting) {
1053 CC->h_command_function();
1054 CC->input_waiting = 0;
1057 /* If there are asynchronous messages waiting and the
1058 * client supports it, do those now */
1059 if ((CC->is_async) && (CC->async_waiting)
1060 && (CC->h_async_function != NULL)) {
1061 CC->h_async_function();
1062 CC->async_waiting = 0;
1065 force_purge = CC->kill_me;
1066 become_session(NULL);
1067 bind_me->state = CON_IDLE;
1070 dead_session_purge(force_purge);
1072 check_sched_shutdown();
1075 /* If control reaches this point, the server is shutting down */
1085 * Translate text facility name to syslog.h defined value.
1087 int SyslogFacility(char *name)
1096 { LOG_KERN, "kern" },
1097 { LOG_USER, "user" },
1098 { LOG_MAIL, "mail" },
1099 { LOG_DAEMON, "daemon" },
1100 { LOG_AUTH, "auth" },
1101 { LOG_SYSLOG, "syslog" },
1103 { LOG_NEWS, "news" },
1104 { LOG_UUCP, "uucp" },
1105 { LOG_LOCAL0, "local0" },
1106 { LOG_LOCAL1, "local1" },
1107 { LOG_LOCAL2, "local2" },
1108 { LOG_LOCAL3, "local3" },
1109 { LOG_LOCAL4, "local4" },
1110 { LOG_LOCAL5, "local5" },
1111 { LOG_LOCAL6, "local6" },
1112 { LOG_LOCAL7, "local7" },
1115 for(i = 0; facTbl[i].name != NULL; i++) {
1116 if(!strcasecmp(name, facTbl[i].name))
1117 return facTbl[i].facility;
1123 /********** MEM CHEQQER ***********/
1125 #ifdef DEBUG_MEMORY_LEAKS
1132 void *tracked_malloc(size_t size, char *file, int line) {
1133 struct igheap *thisheap;
1136 block = malloc(size);
1137 if (block == NULL) return(block);
1139 thisheap = malloc(sizeof(struct igheap));
1140 if (thisheap == NULL) {
1145 thisheap->block = block;
1146 strcpy(thisheap->file, file);
1147 thisheap->line = line;
1149 begin_critical_section(S_DEBUGMEMLEAKS);
1150 thisheap->next = igheap;
1152 end_critical_section(S_DEBUGMEMLEAKS);
1158 void *tracked_realloc(void *ptr, size_t size, char *file, int line) {
1159 struct igheap *thisheap;
1162 block = realloc(ptr, size);
1163 if (block == NULL) return(block);
1165 thisheap = malloc(sizeof(struct igheap));
1166 if (thisheap == NULL) {
1171 thisheap->block = block;
1172 strcpy(thisheap->file, file);
1173 thisheap->line = line;
1175 begin_critical_section(S_DEBUGMEMLEAKS);
1176 thisheap->next = igheap;
1178 end_critical_section(S_DEBUGMEMLEAKS);
1185 void tracked_free(void *ptr) {
1186 struct igheap *thisheap;
1187 struct igheap *trash;
1191 if (igheap == NULL) return;
1192 begin_critical_section(S_DEBUGMEMLEAKS);
1193 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1194 if (thisheap->next != NULL) {
1195 if (thisheap->next->block == ptr) {
1196 trash = thisheap->next;
1197 thisheap->next = thisheap->next->next;
1202 if (igheap->block == ptr) {
1204 igheap = igheap->next;
1207 end_critical_section(S_DEBUGMEMLEAKS);
1210 char *tracked_strdup(const char *s, char *file, int line) {
1213 if (s == NULL) return(NULL);
1214 ptr = tracked_malloc(strlen(s) + 1, file, line);
1215 if (ptr == NULL) return(NULL);
1216 strncpy(ptr, s, strlen(s));
1220 void dump_heap(void) {
1221 struct igheap *thisheap;
1223 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1224 lprintf(CTDL_CRIT, "UNFREED: %30s : %d\n",
1225 thisheap->file, thisheap->line);
1229 #endif /* DEBUG_MEMORY_LEAKS */