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 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->cs_pid != 0) {
121 memmove(&buf[6], buf, sizeof(buf) - 6);
122 snprintf(buf, 6, "[%3d]", CC->cs_pid);
125 syslog(loglevel, "%s", 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 localtime_r(&unixtime, &tim);
137 if (CC->cs_pid != 0) {
139 "%04d/%02d/%02d %2d:%02d:%02d.%06ld [%3d] %s",
140 tim.tm_year + 1900, tim.tm_mon + 1,
141 tim.tm_mday, tim.tm_hour, tim.tm_min,
142 tim.tm_sec, (long)tv.tv_usec,
146 "%04d/%02d/%02d %2d:%02d:%02d.%06ld %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, buf);
154 PerformLogHooks(loglevel, buf);
160 * We used to use master_cleanup() as a signal handler to shut down the server.
161 * however, master_cleanup() and the functions it calls do some things that
162 * aren't such a good idea to do from a signal handler: acquiring mutexes,
163 * playing with signal masks on BSDI systems, etc. so instead we install the
164 * following signal handler to set a global variable to inform the main loop
165 * that it's time to call master_cleanup() and exit.
168 volatile int time_to_die = 0;
170 static RETSIGTYPE signal_cleanup(int signum) {
176 * Some initialization stuff...
178 void init_sysdep(void) {
181 /* Avoid vulnerabilities related to FD_SETSIZE if we can. */
185 getrlimit(RLIMIT_NOFILE, &rl);
186 rl.rlim_cur = FD_SETSIZE;
187 rl.rlim_max = FD_SETSIZE;
188 setrlimit(RLIMIT_NOFILE, &rl);
192 /* If we've got OpenSSL, we're going to use it. */
197 /* Set up a bunch of semaphores to be used for critical sections */
198 for (i=0; i<MAX_SEMAPHORES; ++i) {
199 pthread_mutex_init(&Critters[i], NULL);
203 * Set up a place to put thread-specific data.
204 * We only need a single pointer per thread - it points to the
205 * CitContext structure (in the ContextList linked list) of the
206 * session to which the calling thread is currently bound.
208 if (pthread_key_create(&MyConKey, NULL) != 0) {
209 lprintf(CTDL_CRIT, "Can't create TSD key!! %s\n", strerror(errno));
213 * The action for unexpected signals and exceptions should be to
214 * call signal_cleanup() to gracefully shut down the server.
216 signal(SIGINT, signal_cleanup);
217 signal(SIGQUIT, signal_cleanup);
218 signal(SIGHUP, signal_cleanup);
219 signal(SIGTERM, signal_cleanup);
222 * Do not shut down the server on broken pipe signals, otherwise the
223 * whole Citadel service would come down whenever a single client
226 signal(SIGPIPE, SIG_IGN);
231 * Obtain a semaphore lock to begin a critical section.
233 void begin_critical_section(int which_one)
235 /* lprintf(CTDL_DEBUG, "begin_critical_section(%d)\n", which_one); */
237 /* For all types of critical sections except those listed here,
238 * ensure nobody ever tries to do a critical section within a
239 * transaction; this could lead to deadlock.
241 if ( (which_one != S_FLOORCACHE)
242 #ifdef DEBUG_MEMORY_LEAKS
243 && (which_one != S_DEBUGMEMLEAKS)
248 pthread_mutex_lock(&Critters[which_one]);
252 * Release a semaphore lock to end a critical section.
254 void end_critical_section(int which_one)
256 pthread_mutex_unlock(&Critters[which_one]);
262 * This is a generic function to set up a master socket for listening on
263 * a TCP port. The server shuts down if the bind fails.
266 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
268 struct sockaddr_in sin;
270 int actual_queue_len;
272 actual_queue_len = queue_len;
273 if (actual_queue_len < 5) actual_queue_len = 5;
275 memset(&sin, 0, sizeof(sin));
276 sin.sin_family = AF_INET;
277 sin.sin_port = htons((u_short)port_number);
278 if (ip_addr == NULL) {
279 sin.sin_addr.s_addr = INADDR_ANY;
282 sin.sin_addr.s_addr = inet_addr(ip_addr);
285 if (sin.sin_addr.s_addr == INADDR_NONE) {
286 sin.sin_addr.s_addr = INADDR_ANY;
289 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
292 lprintf(CTDL_EMERG, "citserver: Can't create a socket: %s\n",
298 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
300 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
301 lprintf(CTDL_EMERG, "citserver: Can't bind: %s\n",
307 if (listen(s, actual_queue_len) < 0) {
308 lprintf(CTDL_EMERG, "citserver: Can't listen: %s\n", strerror(errno));
319 * Create a Unix domain socket and listen on it
321 int ig_uds_server(char *sockpath, int queue_len)
323 struct sockaddr_un addr;
326 int actual_queue_len;
328 actual_queue_len = queue_len;
329 if (actual_queue_len < 5) actual_queue_len = 5;
331 i = unlink(sockpath);
332 if (i != 0) if (errno != ENOENT) {
333 lprintf(CTDL_EMERG, "citserver: can't unlink %s: %s\n",
334 sockpath, strerror(errno));
338 memset(&addr, 0, sizeof(addr));
339 addr.sun_family = AF_UNIX;
340 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
342 s = socket(AF_UNIX, SOCK_STREAM, 0);
344 lprintf(CTDL_EMERG, "citserver: Can't create a socket: %s\n",
349 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
350 lprintf(CTDL_EMERG, "citserver: Can't bind: %s\n",
355 if (listen(s, actual_queue_len) < 0) {
356 lprintf(CTDL_EMERG, "citserver: Can't listen: %s\n", strerror(errno));
360 chmod(sockpath, 0777);
367 * Return a pointer to the CitContext structure bound to the thread which
368 * called this function. If there's no such binding (for example, if it's
369 * called by the housekeeper thread) then a generic 'master' CC is returned.
371 * This function is used *VERY* frequently and must be kept small.
373 struct CitContext *MyContext(void) {
375 register struct CitContext *c;
377 return ((c = (struct CitContext *) pthread_getspecific(MyConKey),
378 c == NULL) ? &masterCC : c
384 * Initialize a new context and place it in the list. The session number
385 * used to be the PID (which is why it's called cs_pid), but that was when we
386 * had one process per session. Now we just assign them sequentially, starting
387 * at 1 (don't change it to 0 because masterCC uses 0) and re-using them when
388 * sessions terminate.
390 struct CitContext *CreateNewContext(void) {
391 struct CitContext *me, *ptr;
393 me = (struct CitContext *) malloc(sizeof(struct CitContext));
395 lprintf(CTDL_ALERT, "citserver: can't allocate memory!!\n");
398 memset(me, 0, sizeof(struct CitContext));
400 /* The new context will be created already in the CON_EXECUTING state
401 * in order to prevent another thread from grabbing it while it's
404 me->state = CON_EXECUTING;
408 * Generate a unique session number and insert this context into
411 begin_critical_section(S_SESSION_TABLE);
413 if (ContextList == NULL) {
419 else if (ContextList->cs_pid > 1) {
420 me->next = ContextList;
426 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
427 if (ptr->next == NULL) {
429 me->cs_pid = ptr->cs_pid + 1;
433 else if (ptr->next->cs_pid > (ptr->cs_pid+1)) {
434 me->next = ptr->next;
436 me->cs_pid = ptr->cs_pid + 1;
442 DONE: ++num_sessions;
443 end_critical_section(S_SESSION_TABLE);
449 * buffer_output() ... tell client_write to buffer all output until
450 * instructed to dump it all out later
452 void buffer_output(void) {
453 if (CC->buffering == 0) {
456 CC->output_buffer = malloc(SIZ);
461 * flush_output() ... dump out all that output we've been buffering.
463 void flush_output(void) {
464 if (CC->buffering == 1) {
465 client_write(CC->output_buffer, CC->buffer_len);
471 * unbuffer_output() ... stop buffering output.
473 void unbuffer_output(void) {
474 if (CC->buffering == 1) {
476 /* We don't call flush_output because we can't. */
477 client_write(CC->output_buffer, CC->buffer_len);
479 free(CC->output_buffer);
480 CC->output_buffer = NULL;
487 * client_write() ... Send binary data to the client.
489 void client_write(char *buf, int nbytes)
491 int bytes_written = 0;
494 int old_buffer_len = 0;
496 if (CC->redirect_fp != NULL) {
497 fwrite(buf, nbytes, 1, CC->redirect_fp);
501 if (CC->redirect_sock > 0) {
502 sock = CC->redirect_sock; /* and continue below... */
505 sock = CC->client_socket;
508 /* If we're buffering for later, do that now. */
510 old_buffer_len = CC->buffer_len;
511 CC->buffer_len += nbytes;
512 CC->output_buffer = realloc(CC->output_buffer, CC->buffer_len);
513 memcpy(&CC->output_buffer[old_buffer_len], buf, nbytes);
517 /* Ok, at this point we're not buffering. Go ahead and write. */
520 if (CC->redirect_ssl) {
521 client_write_ssl(buf, nbytes);
526 while (bytes_written < nbytes) {
527 retval = write(sock, &buf[bytes_written],
528 nbytes - bytes_written);
530 lprintf(CTDL_ERR, "client_write() failed: %s\n",
532 if (sock == CC->client_socket) CC->kill_me = 1;
535 bytes_written = bytes_written + retval;
541 * cprintf() ... Send formatted printable data to the client. It is
542 * implemented in terms of client_write() but remains in
543 * sysdep.c in case we port to somewhere without va_args...
545 void cprintf(const char *format, ...) {
549 va_start(arg_ptr, format);
550 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
551 buf[sizeof buf - 2] = '\n';
552 client_write(buf, strlen(buf));
558 * Read data from the client socket.
560 * 1 Requested number of bytes has been read.
561 * 0 Request timed out.
562 * -1 The socket is broken.
563 * If the socket breaks, the session will be terminated.
565 int client_read_to(char *buf, int bytes, int timeout)
573 if (CC->redirect_ssl) {
574 return (client_read_ssl(buf, bytes, timeout));
580 FD_SET(CC->client_socket, &rfds);
584 retval = select( (CC->client_socket)+1,
585 &rfds, NULL, NULL, &tv);
587 if (FD_ISSET(CC->client_socket, &rfds) == 0) {
591 rlen = read(CC->client_socket, &buf[len], bytes-len);
593 lprintf(CTDL_ERR, "client_read() failed: %s\n",
604 * Read data from the client socket with default timeout.
605 * (This is implemented in terms of client_read_to() and could be
606 * justifiably moved out of sysdep.c)
608 INLINE int client_read(char *buf, int bytes)
610 return(client_read_to(buf, bytes, config.c_sleeping));
615 * client_gets() ... Get a LF-terminated line of text from the client.
616 * (This is implemented in terms of client_read() and could be
617 * justifiably moved out of sysdep.c)
619 int client_gets(char *buf)
623 /* Read one character at a time.
626 retval = client_read(&buf[i], 1);
627 if (retval != 1 || buf[i] == '\n' || i == (SIZ-1))
631 /* If we got a long line, discard characters until the newline.
634 while (buf[i] != '\n' && retval == 1)
635 retval = client_read(&buf[i], 1);
637 /* Strip the trailing newline and any trailing nonprintables (cr's)
640 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
641 buf[strlen(buf)-1] = 0;
642 if (retval < 0) strcpy(buf, "000");
649 * The system-dependent part of master_cleanup() - close the master socket.
651 void sysdep_master_cleanup(void) {
652 struct ServiceFunctionHook *serviceptr;
655 * close all protocol master sockets
657 for (serviceptr = ServiceHookTable; serviceptr != NULL;
658 serviceptr = serviceptr->next ) {
660 if (serviceptr->tcp_port > 0)
661 lprintf(CTDL_INFO, "Closing listener on port %d\n",
662 serviceptr->tcp_port);
664 if (serviceptr->sockpath != NULL)
665 lprintf(CTDL_INFO, "Closing listener on '%s'\n",
666 serviceptr->sockpath);
668 close(serviceptr->msock);
670 /* If it's a Unix domain socket, remove the file. */
671 if (serviceptr->sockpath != NULL) {
672 unlink(serviceptr->sockpath);
679 * Terminate another session.
680 * (This could justifiably be moved out of sysdep.c because it
681 * no longer does anything that is system-dependent.)
683 void kill_session(int session_to_kill) {
684 struct CitContext *ptr;
686 begin_critical_section(S_SESSION_TABLE);
687 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
688 if (ptr->cs_pid == session_to_kill) {
692 end_critical_section(S_SESSION_TABLE);
699 * Start running as a daemon. Only close stdio if do_close_stdio is set.
701 void start_daemon(int do_close_stdio) {
702 if (do_close_stdio) {
707 signal(SIGHUP,SIG_IGN);
708 signal(SIGINT,SIG_IGN);
709 signal(SIGQUIT,SIG_IGN);
710 if (fork()!=0) exit(0);
716 * Generic routine to convert a login name to a full name (gecos)
717 * Returns nonzero if a conversion took place
719 int convert_login(char NameToConvert[]) {
723 pw = getpwnam(NameToConvert);
728 strcpy(NameToConvert, pw->pw_gecos);
729 for (a=0; a<strlen(NameToConvert); ++a) {
730 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
736 struct worker_node *worker_list = NULL;
740 * create a worker thread. this function must always be called from within
741 * an S_WORKER_LIST critical section!
743 void create_worker(void) {
745 struct worker_node *n;
748 n = malloc(sizeof(struct worker_node));
750 lprintf(CTDL_EMERG, "can't allocate worker_node, exiting\n");
755 if ((ret = pthread_attr_init(&attr))) {
756 lprintf(CTDL_EMERG, "pthread_attr_init: %s\n", strerror(ret));
761 /* we seem to need something bigger than FreeBSD's default 64k stack */
763 if ((ret = pthread_attr_setstacksize(&attr, 128 * 1024))) {
764 lprintf(CTDL_EMERG, "pthread_attr_setstacksize: %s\n", strerror(ret));
766 pthread_attr_destroy(&attr);
770 if ((ret = pthread_create(&n->tid, &attr, worker_thread, NULL) != 0))
773 lprintf(CTDL_ALERT, "Can't create worker thread: %s\n",
777 n->next = worker_list;
779 pthread_attr_destroy(&attr);
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. */
913 begin_critical_section(S_SESSION_TABLE);
914 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
915 if (ptr->state == CON_IDLE) {
916 FD_SET(ptr->client_socket, &readfds);
917 if (ptr->client_socket > highest)
918 highest = ptr->client_socket;
920 if ((bind_me == NULL) && (ptr->state == CON_READY)) {
922 ptr->state = CON_EXECUTING;
925 end_critical_section(S_SESSION_TABLE);
927 if (bind_me) goto SKIP_SELECT;
929 /* If we got this far, it means that there are no sessions
930 * which a previous thread marked for attention, so we go
931 * ahead and get ready to select().
934 /* First, add the various master sockets to the fdset. */
935 for (serviceptr = ServiceHookTable; serviceptr != NULL;
936 serviceptr = serviceptr->next ) {
937 m = serviceptr->msock;
945 tv.tv_sec = 1; /* wake up every second if no input */
947 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
950 if (time_to_die) return(NULL);
952 /* Now figure out who made this select() unblock.
953 * First, check for an error or exit condition.
956 if (errno == EBADF) {
957 lprintf(CTDL_NOTICE, "select() failed: (%s)\n",
961 if (errno != EINTR) {
962 lprintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
964 } else if (!time_to_die)
968 /* Next, check to see if it's a new client connecting
969 * on a master socket.
971 else for (serviceptr = ServiceHookTable; serviceptr != NULL;
972 serviceptr = serviceptr->next ) {
974 if (FD_ISSET(serviceptr->msock, &readfds)) {
975 ssock = accept(serviceptr->msock, NULL, 0);
978 "citserver: accept(): %s\n",
983 "New client socket %d\n",
986 /* New context will be created already
987 * set up in the CON_EXECUTING state.
989 con = CreateNewContext();
991 /* Assign new socket number to it. */
992 con->client_socket = ssock;
993 con->h_command_function =
994 serviceptr->h_command_function;
995 con->h_async_function =
996 serviceptr->h_async_function;
998 /* Determine whether local socket */
999 if (serviceptr->sockpath != NULL)
1000 con->is_local_socket = 1;
1002 /* Set the SO_REUSEADDR socket option */
1004 setsockopt(ssock, SOL_SOCKET,
1008 become_session(con);
1010 serviceptr->h_greeting_function();
1011 become_session(NULL);
1012 con->state = CON_IDLE;
1018 /* It must be a client socket. Find a context that has data
1019 * waiting on its socket *and* is in the CON_IDLE state. Any
1020 * active sockets other than our chosen one are marked as
1021 * CON_READY so the next thread that comes around can just bind
1022 * to one without having to select() again.
1024 begin_critical_section(S_SESSION_TABLE);
1025 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1026 if ( (FD_ISSET(ptr->client_socket, &readfds))
1027 && (ptr->state != CON_EXECUTING) ) {
1028 ptr->input_waiting = 1;
1030 bind_me = ptr; /* I choose you! */
1031 bind_me->state = CON_EXECUTING;
1034 ptr->state = CON_READY;
1038 end_critical_section(S_SESSION_TABLE);
1041 /* We're bound to a session */
1042 if (bind_me != NULL) {
1043 become_session(bind_me);
1045 /* If the client has sent a command, execute it. */
1046 if (CC->input_waiting) {
1047 CC->h_command_function();
1048 CC->input_waiting = 0;
1051 /* If there are asynchronous messages waiting and the
1052 * client supports it, do those now */
1053 if ((CC->is_async) && (CC->async_waiting)
1054 && (CC->h_async_function != NULL)) {
1055 CC->h_async_function();
1056 CC->async_waiting = 0;
1059 force_purge = CC->kill_me;
1060 become_session(NULL);
1061 bind_me->state = CON_IDLE;
1064 dead_session_purge(force_purge);
1066 check_sched_shutdown();
1069 /* If control reaches this point, the server is shutting down */
1078 * Translate text facility name to syslog.h defined value.
1080 int SyslogFacility(char *name)
1089 { LOG_KERN, "kern" },
1090 { LOG_USER, "user" },
1091 { LOG_MAIL, "mail" },
1092 { LOG_DAEMON, "daemon" },
1093 { LOG_AUTH, "auth" },
1094 { LOG_SYSLOG, "syslog" },
1096 { LOG_NEWS, "news" },
1097 { LOG_UUCP, "uucp" },
1098 { LOG_LOCAL0, "local0" },
1099 { LOG_LOCAL1, "local1" },
1100 { LOG_LOCAL2, "local2" },
1101 { LOG_LOCAL3, "local3" },
1102 { LOG_LOCAL4, "local4" },
1103 { LOG_LOCAL5, "local5" },
1104 { LOG_LOCAL6, "local6" },
1105 { LOG_LOCAL7, "local7" },
1108 for(i = 0; facTbl[i].name != NULL; i++) {
1109 if(!strcasecmp(name, facTbl[i].name))
1110 return facTbl[i].facility;
1116 /********** MEM CHEQQER ***********/
1118 #ifdef DEBUG_MEMORY_LEAKS
1125 void *tracked_malloc(size_t size, char *file, int line) {
1126 struct igheap *thisheap;
1129 block = malloc(size);
1130 if (block == NULL) return(block);
1132 thisheap = malloc(sizeof(struct igheap));
1133 if (thisheap == NULL) {
1138 thisheap->block = block;
1139 strcpy(thisheap->file, file);
1140 thisheap->line = line;
1142 begin_critical_section(S_DEBUGMEMLEAKS);
1143 thisheap->next = igheap;
1145 end_critical_section(S_DEBUGMEMLEAKS);
1151 void *tracked_realloc(void *ptr, size_t size, char *file, int line) {
1152 struct igheap *thisheap;
1155 block = realloc(ptr, size);
1156 if (block == NULL) return(block);
1158 thisheap = malloc(sizeof(struct igheap));
1159 if (thisheap == NULL) {
1164 thisheap->block = block;
1165 strcpy(thisheap->file, file);
1166 thisheap->line = line;
1168 begin_critical_section(S_DEBUGMEMLEAKS);
1169 thisheap->next = igheap;
1171 end_critical_section(S_DEBUGMEMLEAKS);
1178 void tracked_free(void *ptr) {
1179 struct igheap *thisheap;
1180 struct igheap *trash;
1184 if (igheap == NULL) return;
1185 begin_critical_section(S_DEBUGMEMLEAKS);
1186 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1187 if (thisheap->next != NULL) {
1188 if (thisheap->next->block == ptr) {
1189 trash = thisheap->next;
1190 thisheap->next = thisheap->next->next;
1195 if (igheap->block == ptr) {
1197 igheap = igheap->next;
1200 end_critical_section(S_DEBUGMEMLEAKS);
1203 char *tracked_strdup(const char *s, char *file, int line) {
1206 if (s == NULL) return(NULL);
1207 ptr = tracked_malloc(strlen(s) + 1, file, line);
1208 if (ptr == NULL) return(NULL);
1209 strncpy(ptr, s, strlen(s));
1213 void dump_heap(void) {
1214 struct igheap *thisheap;
1216 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1217 lprintf(CTDL_CRIT, "UNFREED: %30s : %d\n",
1218 thisheap->file, thisheap->line);
1222 #endif /* DEBUG_MEMORY_LEAKS */