4 * Citadel "system dependent" stuff.
5 * See copyright.txt for copyright information.
7 * Here's where we (hopefully) have most parts of the Citadel server that
8 * would need to be altered to run the server in a non-POSIX environment.
10 * If we ever port to a different platform and either have multiple
11 * variants of this file or simply load it up with #ifdefs.
26 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/syslog.h>
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
37 # include <sys/time.h>
44 #include <sys/resource.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
59 #include "serv_extensions.h"
60 #include "sysdep_decls.h"
61 #include "citserver.h"
65 #include "housekeeping.h"
67 #include "serv_crypto.h"
69 #ifdef HAVE_SYS_SELECT_H
70 #include <sys/select.h>
78 #ifdef DEBUG_MEMORY_LEAKS
86 struct igheap *igheap = NULL;
90 pthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */
91 pthread_key_t MyConKey; /* TSD key for MyContext() */
93 int verbosity = DEFAULT_VERBOSITY; /* Logging level */
95 struct CitContext masterCC;
96 time_t last_purge = 0; /* Last dead session purge */
97 static int num_threads = 0; /* Current number of threads */
98 int num_sessions = 0; /* Current number of sessions */
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 extern int running_as_daemon;
110 static int enable_syslog = 1;
111 void lprintf(enum LogLevel loglevel, const char *format, ...) {
115 va_start(arg_ptr, format);
116 vsyslog(loglevel, format, arg_ptr);
120 if (enable_syslog && LogHookTable == 0) return;
122 /* legacy output code; hooks get processed first */
124 va_start(arg_ptr, format);
125 vsnprintf(buf, sizeof(buf), format, arg_ptr);
127 PerformLogHooks(loglevel, buf);
129 if (enable_syslog || running_as_daemon) return;
131 /* if we run in forground and syslog is disabled, log to terminal */
132 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 localtime_r(&unixtime, &tim);
141 if (CC->cs_pid != 0) {
143 "%04d/%02d/%02d %2d:%02d:%02d.%06ld [%3d] %s",
144 tim.tm_year + 1900, tim.tm_mon + 1,
145 tim.tm_mday, tim.tm_hour, tim.tm_min,
146 tim.tm_sec, (long)tv.tv_usec,
150 "%04d/%02d/%02d %2d:%02d:%02d.%06ld %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, buf);
162 * We used to use master_cleanup() as a signal handler to shut down the server.
163 * however, master_cleanup() and the functions it calls do some things that
164 * aren't such a good idea to do from a signal handler: acquiring mutexes,
165 * playing with signal masks on BSDI systems, etc. so instead we install the
166 * following signal handler to set a global variable to inform the main loop
167 * that it's time to call master_cleanup() and exit.
170 volatile int time_to_die = 0;
172 static RETSIGTYPE signal_cleanup(int signum) {
178 * Some initialization stuff...
180 void init_sysdep(void) {
183 /* Avoid vulnerabilities related to FD_SETSIZE if we can. */
187 getrlimit(RLIMIT_NOFILE, &rl);
188 rl.rlim_cur = FD_SETSIZE;
189 rl.rlim_max = FD_SETSIZE;
190 setrlimit(RLIMIT_NOFILE, &rl);
194 /* If we've got OpenSSL, we're going to use it. */
199 /* Set up a bunch of semaphores to be used for critical sections */
200 for (i=0; i<MAX_SEMAPHORES; ++i) {
201 pthread_mutex_init(&Critters[i], NULL);
205 * Set up a place to put thread-specific data.
206 * We only need a single pointer per thread - it points to the
207 * CitContext structure (in the ContextList linked list) of the
208 * session to which the calling thread is currently bound.
210 if (pthread_key_create(&MyConKey, NULL) != 0) {
211 lprintf(CTDL_CRIT, "Can't create TSD key!! %s\n", strerror(errno));
215 * The action for unexpected signals and exceptions should be to
216 * call signal_cleanup() to gracefully shut down the server.
218 signal(SIGINT, signal_cleanup);
219 signal(SIGQUIT, signal_cleanup);
220 signal(SIGHUP, signal_cleanup);
221 signal(SIGTERM, signal_cleanup);
224 * Do not shut down the server on broken pipe signals, otherwise the
225 * whole Citadel service would come down whenever a single client
228 signal(SIGPIPE, SIG_IGN);
233 * Obtain a semaphore lock to begin a critical section.
235 void begin_critical_section(int which_one)
237 /* lprintf(CTDL_DEBUG, "begin_critical_section(%d)\n", which_one); */
239 /* For all types of critical sections except those listed here,
240 * ensure nobody ever tries to do a critical section within a
241 * transaction; this could lead to deadlock.
243 if ( (which_one != S_FLOORCACHE)
244 #ifdef DEBUG_MEMORY_LEAKS
245 && (which_one != S_DEBUGMEMLEAKS)
250 pthread_mutex_lock(&Critters[which_one]);
254 * Release a semaphore lock to end a critical section.
256 void end_critical_section(int which_one)
258 pthread_mutex_unlock(&Critters[which_one]);
264 * This is a generic function to set up a master socket for listening on
265 * a TCP port. The server shuts down if the bind fails.
268 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
270 struct sockaddr_in sin;
272 int actual_queue_len;
274 actual_queue_len = queue_len;
275 if (actual_queue_len < 5) actual_queue_len = 5;
277 memset(&sin, 0, sizeof(sin));
278 sin.sin_family = AF_INET;
279 sin.sin_port = htons((u_short)port_number);
280 if (ip_addr == NULL) {
281 sin.sin_addr.s_addr = INADDR_ANY;
284 sin.sin_addr.s_addr = inet_addr(ip_addr);
287 if (sin.sin_addr.s_addr == INADDR_NONE) {
288 sin.sin_addr.s_addr = INADDR_ANY;
291 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
294 lprintf(CTDL_EMERG, "citserver: Can't create a socket: %s\n",
300 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
302 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
303 lprintf(CTDL_EMERG, "citserver: Can't bind: %s\n",
309 if (listen(s, actual_queue_len) < 0) {
310 lprintf(CTDL_EMERG, "citserver: Can't listen: %s\n", strerror(errno));
321 * Create a Unix domain socket and listen on it
323 int ig_uds_server(char *sockpath, int queue_len)
325 struct sockaddr_un addr;
328 int actual_queue_len;
330 actual_queue_len = queue_len;
331 if (actual_queue_len < 5) actual_queue_len = 5;
333 i = unlink(sockpath);
334 if (i != 0) if (errno != ENOENT) {
335 lprintf(CTDL_EMERG, "citserver: can't unlink %s: %s\n",
336 sockpath, strerror(errno));
340 memset(&addr, 0, sizeof(addr));
341 addr.sun_family = AF_UNIX;
342 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
344 s = socket(AF_UNIX, SOCK_STREAM, 0);
346 lprintf(CTDL_EMERG, "citserver: Can't create a socket: %s\n",
351 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
352 lprintf(CTDL_EMERG, "citserver: Can't bind: %s\n",
357 if (listen(s, actual_queue_len) < 0) {
358 lprintf(CTDL_EMERG, "citserver: Can't listen: %s\n", strerror(errno));
362 chmod(sockpath, 0777);
369 * Return a pointer to the CitContext structure bound to the thread which
370 * called this function. If there's no such binding (for example, if it's
371 * called by the housekeeper thread) then a generic 'master' CC is returned.
373 * This function is used *VERY* frequently and must be kept small.
375 struct CitContext *MyContext(void) {
377 register struct CitContext *c;
379 return ((c = (struct CitContext *) pthread_getspecific(MyConKey),
380 c == NULL) ? &masterCC : c
386 * Initialize a new context and place it in the list. The session number
387 * used to be the PID (which is why it's called cs_pid), but that was when we
388 * had one process per session. Now we just assign them sequentially, starting
389 * at 1 (don't change it to 0 because masterCC uses 0) and re-using them when
390 * sessions terminate.
392 struct CitContext *CreateNewContext(void) {
393 struct CitContext *me, *ptr;
395 me = (struct CitContext *) malloc(sizeof(struct CitContext));
397 lprintf(CTDL_ALERT, "citserver: can't allocate memory!!\n");
400 memset(me, 0, sizeof(struct CitContext));
402 /* The new context will be created already in the CON_EXECUTING state
403 * in order to prevent another thread from grabbing it while it's
406 me->state = CON_EXECUTING;
410 * Generate a unique session number and insert this context into
413 begin_critical_section(S_SESSION_TABLE);
415 if (ContextList == NULL) {
421 else if (ContextList->cs_pid > 1) {
422 me->next = ContextList;
428 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
429 if (ptr->next == NULL) {
431 me->cs_pid = ptr->cs_pid + 1;
435 else if (ptr->next->cs_pid > (ptr->cs_pid+1)) {
436 me->next = ptr->next;
438 me->cs_pid = ptr->cs_pid + 1;
444 DONE: ++num_sessions;
445 end_critical_section(S_SESSION_TABLE);
451 * buffer_output() ... tell client_write to buffer all output until
452 * instructed to dump it all out later
454 void buffer_output(void) {
455 if (CC->buffering == 0) {
458 CC->output_buffer = malloc(SIZ);
463 * flush_output() ... dump out all that output we've been buffering.
465 void flush_output(void) {
466 if (CC->buffering == 1) {
467 client_write(CC->output_buffer, CC->buffer_len);
473 * unbuffer_output() ... stop buffering output.
475 void unbuffer_output(void) {
476 if (CC->buffering == 1) {
478 /* We don't call flush_output because we can't. */
479 client_write(CC->output_buffer, CC->buffer_len);
481 free(CC->output_buffer);
482 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, (size_t)nbytes, (size_t)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_getln() ... 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_getln(char *buf, int bufsize)
625 /* Read one character at a time.
628 retval = client_read(&buf[i], 1);
629 if (retval != 1 || buf[i] == '\n' || i == (bufsize-1))
633 /* If we got a long line, discard characters until the newline.
635 if (i == (bufsize-1))
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.
703 void start_daemon(int unused) {
704 close(0); close(1); close(2);
707 signal(SIGHUP,SIG_IGN);
708 signal(SIGINT,SIG_IGN);
709 signal(SIGQUIT,SIG_IGN);
715 * Generic routine to convert a login name to a full name (gecos)
716 * Returns nonzero if a conversion took place
718 int convert_login(char NameToConvert[]) {
722 pw = getpwnam(NameToConvert);
727 strcpy(NameToConvert, pw->pw_gecos);
728 for (a=0; a<strlen(NameToConvert); ++a) {
729 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
735 struct worker_node *worker_list = NULL;
739 * create a worker thread. this function must always be called from within
740 * an S_WORKER_LIST critical section!
742 void create_worker(void) {
744 struct worker_node *n;
747 n = malloc(sizeof(struct worker_node));
749 lprintf(CTDL_EMERG, "can't allocate worker_node, exiting\n");
754 if ((ret = pthread_attr_init(&attr))) {
755 lprintf(CTDL_EMERG, "pthread_attr_init: %s\n", strerror(ret));
760 /* Our per-thread stacks need to be bigger than the default size, otherwise
761 * the MIME parser crashes on FreeBSD, and the IMAP service crashes on
764 if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
765 lprintf(CTDL_EMERG, "pthread_attr_setstacksize: %s\n", strerror(ret));
767 pthread_attr_destroy(&attr);
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;
780 pthread_attr_destroy(&attr);
786 * Purge all sessions which have the 'kill_me' flag set.
787 * This function has code to prevent it from running more than once every
788 * few seconds, because running it after every single unbind would waste a lot
789 * of CPU time and keep the context list locked too much. To force it to run
790 * anyway, set "force" to nonzero.
793 * After that's done, we raise the size of the worker thread pool
794 * if such an action is appropriate.
796 void dead_session_purge(int force) {
797 struct CitContext *ptr, *rem;
800 if ( (time(NULL) - last_purge) < 5 ) {
801 return; /* Too soon, go away */
808 begin_critical_section(S_SESSION_TABLE);
809 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
810 if ( (ptr->state == CON_IDLE) && (ptr->kill_me) ) {
814 end_critical_section(S_SESSION_TABLE);
816 /* RemoveContext() enters its own S_SESSION_TABLE critical
817 * section, so we have to do it like this.
820 lprintf(CTDL_DEBUG, "Purging session %d\n", rem->cs_pid);
824 } while (rem != NULL);
826 /* Raise the size of the worker thread pool if necessary. */
828 if ( (num_sessions > num_threads)
829 && (num_threads < config.c_max_workers) ) {
830 begin_critical_section(S_WORKER_LIST);
832 end_critical_section(S_WORKER_LIST);
841 * Redirect a session's output to a file or socket.
842 * This function may be called with a file handle *or* a socket (but not
843 * both). Call with neither to return output to its normal client socket.
845 void CtdlRedirectOutput(FILE *fp, int sock) {
847 if (fp != NULL) CC->redirect_fp = fp;
848 else CC->redirect_fp = NULL;
850 if (sock > 0) CC->redirect_sock = sock;
851 else CC->redirect_sock = (-1);
857 * masterCC is the context we use when not attached to a session. This
858 * function initializes it.
860 void InitializeMasterCC(void) {
861 memset(&masterCC, 0, sizeof(struct CitContext));
862 masterCC.internal_pgm = 1;
872 * Bind a thread to a context. (It's inline merely to speed things up.)
874 INLINE void become_session(struct CitContext *which_con) {
875 pthread_setspecific(MyConKey, (void *)which_con );
881 * This loop just keeps going and going and going...
883 void *worker_thread(void *arg) {
886 struct CitContext *ptr;
887 struct CitContext *bind_me = NULL;
890 struct CitContext *con= NULL; /* Temporary context pointer */
891 struct ServiceFunctionHook *serviceptr;
892 int ssock; /* Descriptor for client socket */
901 while (!time_to_die) {
903 /* make doubly sure we're not holding any stale db handles
904 * which might cause a deadlock.
907 do_select: force_purge = 0;
908 bind_me = NULL; /* Which session shall we handle? */
910 /* Initialize the fdset. */
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 */
948 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
951 if (time_to_die) return(NULL);
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;
1019 /* It must be a client socket. Find a context that has data
1020 * waiting on its socket *and* is in the CON_IDLE state. Any
1021 * active sockets other than our chosen one are marked as
1022 * CON_READY so the next thread that comes around can just bind
1023 * to one without having to select() again.
1025 begin_critical_section(S_SESSION_TABLE);
1026 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1027 if ( (FD_ISSET(ptr->client_socket, &readfds))
1028 && (ptr->state != CON_EXECUTING) ) {
1029 ptr->input_waiting = 1;
1031 bind_me = ptr; /* I choose you! */
1032 bind_me->state = CON_EXECUTING;
1035 ptr->state = CON_READY;
1039 end_critical_section(S_SESSION_TABLE);
1042 /* We're bound to a session */
1043 if (bind_me != NULL) {
1044 become_session(bind_me);
1046 /* If the client has sent a command, execute it. */
1047 if (CC->input_waiting) {
1048 CC->h_command_function();
1049 CC->input_waiting = 0;
1052 /* If there are asynchronous messages waiting and the
1053 * client supports it, do those now */
1054 if ((CC->is_async) && (CC->async_waiting)
1055 && (CC->h_async_function != NULL)) {
1056 CC->h_async_function();
1057 CC->async_waiting = 0;
1060 force_purge = CC->kill_me;
1061 become_session(NULL);
1062 bind_me->state = CON_IDLE;
1065 dead_session_purge(force_purge);
1067 check_sched_shutdown();
1070 /* If control reaches this point, the server is shutting down */
1079 * Translate text facility name to syslog.h defined value.
1081 int SyslogFacility(char *name)
1090 { LOG_KERN, "kern" },
1091 { LOG_USER, "user" },
1092 { LOG_MAIL, "mail" },
1093 { LOG_DAEMON, "daemon" },
1094 { LOG_AUTH, "auth" },
1095 { LOG_SYSLOG, "syslog" },
1097 { LOG_NEWS, "news" },
1098 { LOG_UUCP, "uucp" },
1099 { LOG_LOCAL0, "local0" },
1100 { LOG_LOCAL1, "local1" },
1101 { LOG_LOCAL2, "local2" },
1102 { LOG_LOCAL3, "local3" },
1103 { LOG_LOCAL4, "local4" },
1104 { LOG_LOCAL5, "local5" },
1105 { LOG_LOCAL6, "local6" },
1106 { LOG_LOCAL7, "local7" },
1109 for(i = 0; facTbl[i].name != NULL; i++) {
1110 if(!strcasecmp(name, facTbl[i].name))
1111 return facTbl[i].facility;
1118 /********** MEM CHEQQER ***********/
1120 #ifdef DEBUG_MEMORY_LEAKS
1127 void *tracked_malloc(size_t size, char *file, int line) {
1128 struct igheap *thisheap;
1131 block = malloc(size);
1132 if (block == NULL) return(block);
1134 thisheap = malloc(sizeof(struct igheap));
1135 if (thisheap == NULL) {
1140 thisheap->block = block;
1141 strcpy(thisheap->file, file);
1142 thisheap->line = line;
1144 begin_critical_section(S_DEBUGMEMLEAKS);
1145 thisheap->next = igheap;
1147 end_critical_section(S_DEBUGMEMLEAKS);
1153 void *tracked_realloc(void *ptr, size_t size, char *file, int line) {
1154 struct igheap *thisheap;
1157 block = realloc(ptr, size);
1158 if (block == NULL) return(block);
1160 thisheap = malloc(sizeof(struct igheap));
1161 if (thisheap == NULL) {
1166 thisheap->block = block;
1167 strcpy(thisheap->file, file);
1168 thisheap->line = line;
1170 begin_critical_section(S_DEBUGMEMLEAKS);
1171 thisheap->next = igheap;
1173 end_critical_section(S_DEBUGMEMLEAKS);
1180 void tracked_free(void *ptr) {
1181 struct igheap *thisheap;
1182 struct igheap *trash;
1186 if (igheap == NULL) return;
1187 begin_critical_section(S_DEBUGMEMLEAKS);
1188 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1189 if (thisheap->next != NULL) {
1190 if (thisheap->next->block == ptr) {
1191 trash = thisheap->next;
1192 thisheap->next = thisheap->next->next;
1197 if (igheap->block == ptr) {
1199 igheap = igheap->next;
1202 end_critical_section(S_DEBUGMEMLEAKS);
1205 char *tracked_strdup(const char *s, char *file, int line) {
1208 if (s == NULL) return(NULL);
1209 ptr = tracked_malloc(strlen(s) + 1, file, line);
1210 if (ptr == NULL) return(NULL);
1211 strncpy(ptr, s, strlen(s));
1215 void dump_heap(void) {
1216 struct igheap *thisheap;
1218 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1219 lprintf(CTDL_CRIT, "UNFREED: %30s : %d\n",
1220 thisheap->file, thisheap->line);
1224 #endif /* DEBUG_MEMORY_LEAKS */