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 <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(char *ip_addr, 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_port = htons((u_short)port_number);
290 if (ip_addr == NULL) {
291 sin.sin_addr.s_addr = INADDR_ANY;
294 sin.sin_addr.s_addr = inet_addr(ip_addr);
297 if (sin.sin_addr.s_addr == INADDR_NONE) {
298 sin.sin_addr.s_addr = INADDR_ANY;
301 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
304 lprintf(CTDL_EMERG, "citserver: Can't create a socket: %s\n",
310 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
312 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
313 lprintf(CTDL_EMERG, "citserver: Can't bind: %s\n",
319 if (listen(s, actual_queue_len) < 0) {
320 lprintf(CTDL_EMERG, "citserver: Can't listen: %s\n", strerror(errno));
331 * Create a Unix domain socket and listen on it
333 int ig_uds_server(char *sockpath, int queue_len)
335 struct sockaddr_un addr;
338 int actual_queue_len;
340 actual_queue_len = queue_len;
341 if (actual_queue_len < 5) actual_queue_len = 5;
343 i = unlink(sockpath);
344 if (i != 0) if (errno != ENOENT) {
345 lprintf(CTDL_EMERG, "citserver: can't unlink %s: %s\n",
346 sockpath, strerror(errno));
350 memset(&addr, 0, sizeof(addr));
351 addr.sun_family = AF_UNIX;
352 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
354 s = socket(AF_UNIX, SOCK_STREAM, 0);
356 lprintf(CTDL_EMERG, "citserver: Can't create a socket: %s\n",
361 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
362 lprintf(CTDL_EMERG, "citserver: Can't bind: %s\n",
367 if (listen(s, actual_queue_len) < 0) {
368 lprintf(CTDL_EMERG, "citserver: Can't listen: %s\n", strerror(errno));
372 chmod(sockpath, 0777);
379 * Return a pointer to the CitContext structure bound to the thread which
380 * called this function. If there's no such binding (for example, if it's
381 * called by the housekeeper thread) then a generic 'master' CC is returned.
383 * It's inlined because it's used *VERY* frequently.
385 INLINE struct CitContext *MyContext(void) {
386 return ((pthread_getspecific(MyConKey) == NULL)
388 : (struct CitContext *) pthread_getspecific(MyConKey)
394 * Initialize a new context and place it in the list. The session number
395 * used to be the PID (which is why it's called cs_pid), but that was when we
396 * had one process per session. Now we just assign them sequentially, starting
397 * at 1 (don't change it to 0 because masterCC uses 0) and re-using them when
398 * sessions terminate.
400 struct CitContext *CreateNewContext(void) {
401 struct CitContext *me, *ptr;
403 me = (struct CitContext *) malloc(sizeof(struct CitContext));
405 lprintf(CTDL_ALERT, "citserver: can't allocate memory!!\n");
408 memset(me, 0, sizeof(struct CitContext));
410 /* The new context will be created already in the CON_EXECUTING state
411 * in order to prevent another thread from grabbing it while it's
414 me->state = CON_EXECUTING;
418 * Generate a unique session number and insert this context into
421 begin_critical_section(S_SESSION_TABLE);
423 if (ContextList == NULL) {
429 else if (ContextList->cs_pid > 1) {
430 me->next = ContextList;
436 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
437 if (ptr->next == NULL) {
439 me->cs_pid = ptr->cs_pid + 1;
443 else if (ptr->next->cs_pid > (ptr->cs_pid+1)) {
444 me->next = ptr->next;
446 me->cs_pid = ptr->cs_pid + 1;
452 DONE: ++num_sessions;
453 end_critical_section(S_SESSION_TABLE);
459 * buffer_output() ... tell client_write to buffer all output until
460 * instructed to dump it all out later
462 void buffer_output(void) {
463 if (CC->buffering == 0) {
466 CC->output_buffer = malloc(SIZ);
471 * flush_output() ... dump out all that output we've been buffering.
473 void flush_output(void) {
474 if (CC->buffering == 1) {
475 client_write(CC->output_buffer, CC->buffer_len);
481 * unbuffer_output() ... stop buffering output.
483 void unbuffer_output(void) {
484 if (CC->buffering == 1) {
486 /* We don't call flush_output because we can't. */
487 client_write(CC->output_buffer, CC->buffer_len);
489 free(CC->output_buffer);
490 CC->output_buffer = NULL;
497 * client_write() ... Send binary data to the client.
499 void client_write(char *buf, int nbytes)
501 int bytes_written = 0;
504 int old_buffer_len = 0;
506 if (CC->redirect_fp != NULL) {
507 fwrite(buf, nbytes, 1, CC->redirect_fp);
511 if (CC->redirect_sock > 0) {
512 sock = CC->redirect_sock; /* and continue below... */
515 sock = CC->client_socket;
518 /* If we're buffering for later, do that now. */
520 old_buffer_len = CC->buffer_len;
521 CC->buffer_len += nbytes;
522 CC->output_buffer = realloc(CC->output_buffer, CC->buffer_len);
523 memcpy(&CC->output_buffer[old_buffer_len], buf, nbytes);
527 /* Ok, at this point we're not buffering. Go ahead and write. */
530 if (CC->redirect_ssl) {
531 client_write_ssl(buf, nbytes);
536 while (bytes_written < nbytes) {
537 retval = write(sock, &buf[bytes_written],
538 nbytes - bytes_written);
540 lprintf(CTDL_ERR, "client_write() failed: %s\n",
542 if (sock == CC->client_socket) CC->kill_me = 1;
545 bytes_written = bytes_written + retval;
551 * cprintf() ... Send formatted printable data to the client. It is
552 * implemented in terms of client_write() but remains in
553 * sysdep.c in case we port to somewhere without va_args...
555 void cprintf(const char *format, ...) {
559 va_start(arg_ptr, format);
560 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
561 buf[sizeof buf - 2] = '\n';
562 client_write(buf, strlen(buf));
568 * Read data from the client socket.
570 * 1 Requested number of bytes has been read.
571 * 0 Request timed out.
572 * -1 The socket is broken.
573 * If the socket breaks, the session will be terminated.
575 int client_read_to(char *buf, int bytes, int timeout)
583 if (CC->redirect_ssl) {
584 return (client_read_ssl(buf, bytes, timeout));
590 FD_SET(CC->client_socket, &rfds);
594 retval = select( (CC->client_socket)+1,
595 &rfds, NULL, NULL, &tv);
597 if (FD_ISSET(CC->client_socket, &rfds) == 0) {
601 rlen = read(CC->client_socket, &buf[len], bytes-len);
603 lprintf(CTDL_ERR, "client_read() failed: %s\n",
614 * Read data from the client socket with default timeout.
615 * (This is implemented in terms of client_read_to() and could be
616 * justifiably moved out of sysdep.c)
618 INLINE int client_read(char *buf, int bytes)
620 return(client_read_to(buf, bytes, config.c_sleeping));
625 * client_gets() ... Get a LF-terminated line of text from the client.
626 * (This is implemented in terms of client_read() and could be
627 * justifiably moved out of sysdep.c)
629 int client_gets(char *buf)
633 /* Read one character at a time.
636 retval = client_read(&buf[i], 1);
637 if (retval != 1 || buf[i] == '\n' || i == (SIZ-1))
641 /* If we got a long line, discard characters until the newline.
644 while (buf[i] != '\n' && retval == 1)
645 retval = client_read(&buf[i], 1);
647 /* Strip the trailing newline and any trailing nonprintables (cr's)
650 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
651 buf[strlen(buf)-1] = 0;
652 if (retval < 0) strcpy(buf, "000");
659 * The system-dependent part of master_cleanup() - close the master socket.
661 void sysdep_master_cleanup(void) {
662 struct ServiceFunctionHook *serviceptr;
665 * close all protocol master sockets
667 for (serviceptr = ServiceHookTable; serviceptr != NULL;
668 serviceptr = serviceptr->next ) {
670 if (serviceptr->tcp_port > 0)
671 lprintf(CTDL_INFO, "Closing listener on port %d\n",
672 serviceptr->tcp_port);
674 if (serviceptr->sockpath != NULL)
675 lprintf(CTDL_INFO, "Closing listener on '%s'\n",
676 serviceptr->sockpath);
678 close(serviceptr->msock);
680 /* If it's a Unix domain socket, remove the file. */
681 if (serviceptr->sockpath != NULL) {
682 unlink(serviceptr->sockpath);
689 * Terminate another session.
690 * (This could justifiably be moved out of sysdep.c because it
691 * no longer does anything that is system-dependent.)
693 void kill_session(int session_to_kill) {
694 struct CitContext *ptr;
696 begin_critical_section(S_SESSION_TABLE);
697 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
698 if (ptr->cs_pid == session_to_kill) {
702 end_critical_section(S_SESSION_TABLE);
709 * Start running as a daemon. Only close stdio if do_close_stdio is set.
711 void start_daemon(int do_close_stdio) {
712 if (do_close_stdio) {
717 signal(SIGHUP,SIG_IGN);
718 signal(SIGINT,SIG_IGN);
719 signal(SIGQUIT,SIG_IGN);
720 if (fork()!=0) exit(0);
726 * Generic routine to convert a login name to a full name (gecos)
727 * Returns nonzero if a conversion took place
729 int convert_login(char NameToConvert[]) {
733 pw = getpwnam(NameToConvert);
738 strcpy(NameToConvert, pw->pw_gecos);
739 for (a=0; a<strlen(NameToConvert); ++a) {
740 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
746 struct worker_node *worker_list = NULL;
750 * create a worker thread. this function must always be called from within
751 * an S_WORKER_LIST critical section!
753 void create_worker(void) {
755 struct worker_node *n;
758 n = malloc(sizeof(struct worker_node));
760 lprintf(CTDL_EMERG, "can't allocate worker_node, exiting\n");
765 if ((ret = pthread_attr_init(&attr))) {
766 lprintf(CTDL_EMERG, "pthread_attr_init: %s\n", strerror(ret));
771 /* we seem to need something bigger than FreeBSD's default 64k stack */
773 if ((ret = pthread_attr_setstacksize(&attr, 128 * 1024))) {
774 lprintf(CTDL_EMERG, "pthread_attr_setstacksize: %s\n", strerror(ret));
779 if ((ret = pthread_create(&n->tid, &attr, worker_thread, NULL) != 0))
782 lprintf(CTDL_ALERT, "Can't create worker thread: %s\n",
786 n->next = worker_list;
793 * Purge all sessions which have the 'kill_me' flag set.
794 * This function has code to prevent it from running more than once every
795 * few seconds, because running it after every single unbind would waste a lot
796 * of CPU time and keep the context list locked too much. To force it to run
797 * anyway, set "force" to nonzero.
800 * After that's done, we raise the size of the worker thread pool
801 * if such an action is appropriate.
803 void dead_session_purge(int force) {
804 struct CitContext *ptr, *rem;
807 if ( (time(NULL) - last_purge) < 5 ) {
808 return; /* Too soon, go away */
815 begin_critical_section(S_SESSION_TABLE);
816 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
817 if ( (ptr->state == CON_IDLE) && (ptr->kill_me) ) {
821 end_critical_section(S_SESSION_TABLE);
823 /* RemoveContext() enters its own S_SESSION_TABLE critical
824 * section, so we have to do it like this.
827 lprintf(CTDL_DEBUG, "Purging session %d\n", rem->cs_pid);
831 } while (rem != NULL);
833 /* Raise the size of the worker thread pool if necessary. */
835 if ( (num_sessions > num_threads)
836 && (num_threads < config.c_max_workers) ) {
837 begin_critical_section(S_WORKER_LIST);
839 end_critical_section(S_WORKER_LIST);
848 * Redirect a session's output to a file or socket.
849 * This function may be called with a file handle *or* a socket (but not
850 * both). Call with neither to return output to its normal client socket.
852 void CtdlRedirectOutput(FILE *fp, int sock) {
854 if (fp != NULL) CC->redirect_fp = fp;
855 else CC->redirect_fp = NULL;
857 if (sock > 0) CC->redirect_sock = sock;
858 else CC->redirect_sock = (-1);
864 * masterCC is the context we use when not attached to a session. This
865 * function initializes it.
867 void InitializeMasterCC(void) {
868 memset(&masterCC, 0, sizeof(struct CitContext));
869 masterCC.internal_pgm = 1;
879 * Bind a thread to a context. (It's inline merely to speed things up.)
881 INLINE void become_session(struct CitContext *which_con) {
882 pthread_setspecific(MyConKey, (void *)which_con );
888 * This loop just keeps going and going and going...
890 void *worker_thread(void *arg) {
893 struct CitContext *ptr;
894 struct CitContext *bind_me = NULL;
897 struct CitContext *con= NULL; /* Temporary context pointer */
898 struct ServiceFunctionHook *serviceptr;
899 int ssock; /* Descriptor for client socket */
908 while (!time_to_die) {
910 /* make doubly sure we're not holding any stale db handles
911 * which might cause a deadlock.
914 do_select: force_purge = 0;
915 bind_me = NULL; /* Which session shall we handle? */
917 /* Initialize the fdset. */
921 begin_critical_section(S_SESSION_TABLE);
922 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
923 if (ptr->state == CON_IDLE) {
924 FD_SET(ptr->client_socket, &readfds);
925 if (ptr->client_socket > highest)
926 highest = ptr->client_socket;
928 if ((bind_me == NULL) && (ptr->state == CON_READY)) {
930 ptr->state = CON_EXECUTING;
933 end_critical_section(S_SESSION_TABLE);
935 if (bind_me) goto SKIP_SELECT;
937 /* If we got this far, it means that there are no sessions
938 * which a previous thread marked for attention, so we go
939 * ahead and get ready to select().
942 /* First, add the various master sockets to the fdset. */
943 for (serviceptr = ServiceHookTable; serviceptr != NULL;
944 serviceptr = serviceptr->next ) {
945 m = serviceptr->msock;
953 tv.tv_sec = 1; /* wake up every second if no input */
955 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
961 /* Now figure out who made this select() unblock.
962 * First, check for an error or exit condition.
965 if (errno == EBADF) {
966 lprintf(CTDL_NOTICE, "select() failed: (%s)\n",
970 if (errno != EINTR) {
971 lprintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
973 } else if (!time_to_die)
977 /* Next, check to see if it's a new client connecting
978 * on a master socket.
980 else for (serviceptr = ServiceHookTable; serviceptr != NULL;
981 serviceptr = serviceptr->next ) {
983 if (FD_ISSET(serviceptr->msock, &readfds)) {
984 ssock = accept(serviceptr->msock, NULL, 0);
987 "citserver: accept(): %s\n",
992 "New client socket %d\n",
995 /* New context will be created already
996 * set up in the CON_EXECUTING state.
998 con = CreateNewContext();
1000 /* Assign new socket number to it. */
1001 con->client_socket = ssock;
1002 con->h_command_function =
1003 serviceptr->h_command_function;
1004 con->h_async_function =
1005 serviceptr->h_async_function;
1007 /* Determine whether local socket */
1008 if (serviceptr->sockpath != NULL)
1009 con->is_local_socket = 1;
1011 /* Set the SO_REUSEADDR socket option */
1013 setsockopt(ssock, SOL_SOCKET,
1017 become_session(con);
1019 serviceptr->h_greeting_function();
1020 become_session(NULL);
1021 con->state = CON_IDLE;
1031 /* It must be a client socket. Find a context that has data
1032 * waiting on its socket *and* is in the CON_IDLE state. Any
1033 * active sockets other than our chosen one are marked as
1034 * CON_READY so the next thread that comes around can just bind
1035 * to one without having to select() again.
1037 begin_critical_section(S_SESSION_TABLE);
1038 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1039 if ( (FD_ISSET(ptr->client_socket, &readfds))
1040 && (ptr->state != CON_EXECUTING) ) {
1041 ptr->input_waiting = 1;
1043 bind_me = ptr; /* I choose you! */
1044 bind_me->state = CON_EXECUTING;
1047 ptr->state = CON_READY;
1051 end_critical_section(S_SESSION_TABLE);
1054 /* We're bound to a session */
1055 if (bind_me != NULL) {
1056 become_session(bind_me);
1058 /* If the client has sent a command, execute it. */
1059 if (CC->input_waiting) {
1060 CC->h_command_function();
1061 CC->input_waiting = 0;
1064 /* If there are asynchronous messages waiting and the
1065 * client supports it, do those now */
1066 if ((CC->is_async) && (CC->async_waiting)
1067 && (CC->h_async_function != NULL)) {
1068 CC->h_async_function();
1069 CC->async_waiting = 0;
1072 force_purge = CC->kill_me;
1073 become_session(NULL);
1074 bind_me->state = CON_IDLE;
1077 dead_session_purge(force_purge);
1079 check_sched_shutdown();
1082 /* If control reaches this point, the server is shutting down */
1092 * Translate text facility name to syslog.h defined value.
1094 int SyslogFacility(char *name)
1103 { LOG_KERN, "kern" },
1104 { LOG_USER, "user" },
1105 { LOG_MAIL, "mail" },
1106 { LOG_DAEMON, "daemon" },
1107 { LOG_AUTH, "auth" },
1108 { LOG_SYSLOG, "syslog" },
1110 { LOG_NEWS, "news" },
1111 { LOG_UUCP, "uucp" },
1112 { LOG_LOCAL0, "local0" },
1113 { LOG_LOCAL1, "local1" },
1114 { LOG_LOCAL2, "local2" },
1115 { LOG_LOCAL3, "local3" },
1116 { LOG_LOCAL4, "local4" },
1117 { LOG_LOCAL5, "local5" },
1118 { LOG_LOCAL6, "local6" },
1119 { LOG_LOCAL7, "local7" },
1122 for(i = 0; facTbl[i].name != NULL; i++) {
1123 if(!strcasecmp(name, facTbl[i].name))
1124 return facTbl[i].facility;
1130 /********** MEM CHEQQER ***********/
1132 #ifdef DEBUG_MEMORY_LEAKS
1139 void *tracked_malloc(size_t size, char *file, int line) {
1140 struct igheap *thisheap;
1143 block = malloc(size);
1144 if (block == NULL) return(block);
1146 thisheap = malloc(sizeof(struct igheap));
1147 if (thisheap == NULL) {
1152 thisheap->block = block;
1153 strcpy(thisheap->file, file);
1154 thisheap->line = line;
1156 begin_critical_section(S_DEBUGMEMLEAKS);
1157 thisheap->next = igheap;
1159 end_critical_section(S_DEBUGMEMLEAKS);
1165 void *tracked_realloc(void *ptr, size_t size, char *file, int line) {
1166 struct igheap *thisheap;
1169 block = realloc(ptr, size);
1170 if (block == NULL) return(block);
1172 thisheap = malloc(sizeof(struct igheap));
1173 if (thisheap == NULL) {
1178 thisheap->block = block;
1179 strcpy(thisheap->file, file);
1180 thisheap->line = line;
1182 begin_critical_section(S_DEBUGMEMLEAKS);
1183 thisheap->next = igheap;
1185 end_critical_section(S_DEBUGMEMLEAKS);
1192 void tracked_free(void *ptr) {
1193 struct igheap *thisheap;
1194 struct igheap *trash;
1198 if (igheap == NULL) return;
1199 begin_critical_section(S_DEBUGMEMLEAKS);
1200 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1201 if (thisheap->next != NULL) {
1202 if (thisheap->next->block == ptr) {
1203 trash = thisheap->next;
1204 thisheap->next = thisheap->next->next;
1209 if (igheap->block == ptr) {
1211 igheap = igheap->next;
1214 end_critical_section(S_DEBUGMEMLEAKS);
1217 char *tracked_strdup(const char *s, char *file, int line) {
1220 if (s == NULL) return(NULL);
1221 ptr = tracked_malloc(strlen(s) + 1, file, line);
1222 if (ptr == NULL) return(NULL);
1223 strncpy(ptr, s, strlen(s));
1227 void dump_heap(void) {
1228 struct igheap *thisheap;
1230 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1231 lprintf(CTDL_CRIT, "UNFREED: %30s : %d\n",
1232 thisheap->file, thisheap->line);
1236 #endif /* DEBUG_MEMORY_LEAKS */