]> code.citadel.org Git - citadel.git/blob - citadel/sysdep.c
Prevent CtdlThreadCancel on garbage collector.
[citadel.git] / citadel / sysdep.c
1 /*
2  * $Id$
3  *
4  * Citadel "system dependent" stuff.
5  * See copyright.txt for copyright information.
6  *
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.
9  * 
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.
12  *
13  */
14
15 #include "sysdep.h"
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <fcntl.h>
20 #include <ctype.h>
21 #include <signal.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/wait.h>
25 #include <sys/socket.h>
26 #include <syslog.h>
27 #include <sys/syslog.h>
28
29 #if TIME_WITH_SYS_TIME
30 # include <sys/time.h>
31 # include <time.h>
32 #else
33 # if HAVE_SYS_TIME_H
34 #  include <sys/time.h>
35 # else
36 #  include <time.h>
37 # endif
38 #endif
39
40 #include <limits.h>
41 #include <sys/resource.h>
42 #include <netinet/in.h>
43 #include <netinet/tcp.h>
44 #include <arpa/inet.h>
45 #include <netdb.h>
46 #include <sys/un.h>
47 #include <string.h>
48 #include <pwd.h>
49 #include <errno.h>
50 #include <stdarg.h>
51 #include <grp.h>
52 #ifdef HAVE_PTHREAD_H
53 #include <pthread.h>
54 #endif
55 #include <libcitadel.h>
56 #include "citadel.h"
57 #include "server.h"
58 #include "sysdep_decls.h"
59 #include "citserver.h"
60 #include "support.h"
61 #include "config.h"
62 #include "database.h"
63 #include "housekeeping.h"
64 #include "modules/crypto/serv_crypto.h" /* Needed for init_ssl, client_write_ssl, client_read_ssl, destruct_ssl */
65 #include "ecrash.h"
66
67 #ifdef HAVE_SYS_SELECT_H
68 #include <sys/select.h>
69 #endif
70
71 #ifndef HAVE_SNPRINTF
72 #include "snprintf.h"
73 #endif
74
75 #include "ctdl_module.h"
76
77 #ifdef DEBUG_MEMORY_LEAKS
78 struct igheap {
79         struct igheap *next;
80         char file[32];
81         int line;
82         void *block;
83 };
84
85 struct igheap *igheap = NULL;
86 #endif
87
88
89 pthread_mutex_t Critters[MAX_SEMAPHORES];       /* Things needing locking */
90 pthread_key_t MyConKey;                         /* TSD key for MyContext() */
91
92 int verbosity = DEFAULT_VERBOSITY;              /* Logging level */
93
94 struct CitContext masterCC;
95 time_t last_purge = 0;                          /* Last dead session purge */
96 static int num_threads = 0;                     /* Current number of threads */
97 static int num_workers = 0;                     /* Current number of worker threads */
98 int num_sessions = 0;                           /* Current number of sessions */
99
100 int syslog_facility = LOG_DAEMON;
101 int enable_syslog = 0;
102
103
104 /*
105  * Create an interface to lprintf that follows the coding convention.
106  * This is here until such time as we have replaced all calls to lprintf with CtdlLogPrintf
107  */
108  
109 void CtdlLogPrintf(enum LogLevel loglevel, const char *format, ...)
110 {
111         va_list arg_ptr;
112         va_start(arg_ptr, format);
113         vlprintf(loglevel, format, arg_ptr);
114         va_end(arg_ptr);
115 }
116
117
118 /*
119  * lprintf()  ...   Write logging information
120  */
121 void lprintf(enum LogLevel loglevel, const char *format, ...) {   
122         va_list arg_ptr;
123         va_start(arg_ptr, format);
124         vlprintf(loglevel, format, arg_ptr);
125         va_end(arg_ptr);
126 }
127
128 void vlprintf(enum LogLevel loglevel, const char *format, va_list arg_ptr)
129 {
130         char buf[SIZ], buf2[SIZ];
131
132         if (enable_syslog) {
133                 vsyslog((syslog_facility | loglevel), format, arg_ptr);
134         }
135
136         /* stderr output code */
137         if (enable_syslog || running_as_daemon) return;
138
139         /* if we run in forground and syslog is disabled, log to terminal */
140         if (loglevel <= verbosity) { 
141                 struct timeval tv;
142                 struct tm tim;
143                 time_t unixtime;
144
145                 gettimeofday(&tv, NULL);
146                 /* Promote to time_t; types differ on some OSes (like darwin) */
147                 unixtime = tv.tv_sec;
148                 localtime_r(&unixtime, &tim);
149                 if (CC->cs_pid != 0) {
150                         sprintf(buf,
151                                 "%04d/%02d/%02d %2d:%02d:%02d.%06ld [%3d] ",
152                                 tim.tm_year + 1900, tim.tm_mon + 1,
153                                 tim.tm_mday, tim.tm_hour, tim.tm_min,
154                                 tim.tm_sec, (long)tv.tv_usec,
155                                 CC->cs_pid);
156                 } else {
157                         sprintf(buf,
158                                 "%04d/%02d/%02d %2d:%02d:%02d.%06ld ",
159                                 tim.tm_year + 1900, tim.tm_mon + 1,
160                                 tim.tm_mday, tim.tm_hour, tim.tm_min,
161                                 tim.tm_sec, (long)tv.tv_usec);
162                 }
163                 vsprintf(buf2, format, arg_ptr);   
164
165                 fprintf(stderr, "%s%s", buf, buf2);
166                 fflush(stderr);
167         }
168 }   
169
170
171
172 /*
173  * Signal handler to shut down the server.
174  */
175
176 volatile int exit_signal = 0;
177 volatile int shutdown_and_halt = 0;
178 volatile int restart_server = 0;
179 volatile int running_as_daemon = 0;
180
181 static RETSIGTYPE signal_cleanup(int signum) {
182         CtdlLogPrintf(CTDL_DEBUG, "Caught signal %d; shutting down.\n", signum);
183         CtdlThreadStopAll();
184         exit_signal = signum;
185 }
186
187
188
189
190 void InitialiseSemaphores(void)
191 {
192         int i;
193
194         /* Set up a bunch of semaphores to be used for critical sections */
195         for (i=0; i<MAX_SEMAPHORES; ++i) {
196                 pthread_mutex_init(&Critters[i], NULL);
197         }
198 }
199
200
201
202 /*
203  * Some initialization stuff...
204  */
205 void init_sysdep(void) {
206         sigset_t set;
207
208         /* Avoid vulnerabilities related to FD_SETSIZE if we can. */
209 #ifdef FD_SETSIZE
210 #ifdef RLIMIT_NOFILE
211         struct rlimit rl;
212         getrlimit(RLIMIT_NOFILE, &rl);
213         rl.rlim_cur = FD_SETSIZE;
214         rl.rlim_max = FD_SETSIZE;
215         setrlimit(RLIMIT_NOFILE, &rl);
216 #endif
217 #endif
218
219         /* If we've got OpenSSL, we're going to use it. */
220 #ifdef HAVE_OPENSSL
221         init_ssl();
222 #endif
223
224         /*
225          * Set up a place to put thread-specific data.
226          * We only need a single pointer per thread - it points to the
227          * CitContext structure (in the ContextList linked list) of the
228          * session to which the calling thread is currently bound.
229          */
230         if (pthread_key_create(&MyConKey, NULL) != 0) {
231                 CtdlLogPrintf(CTDL_CRIT, "Can't create TSD key: %s\n",
232                         strerror(errno));
233         }
234
235         /*
236          * The action for unexpected signals and exceptions should be to
237          * call signal_cleanup() to gracefully shut down the server.
238          */
239         sigemptyset(&set);
240         sigaddset(&set, SIGINT);
241         sigaddset(&set, SIGQUIT);
242         sigaddset(&set, SIGHUP);
243         sigaddset(&set, SIGTERM);
244         // sigaddset(&set, SIGSEGV);    commented out because
245         // sigaddset(&set, SIGILL);     we want core dumps
246         // sigaddset(&set, SIGBUS);
247         sigprocmask(SIG_UNBLOCK, &set, NULL);
248
249         signal(SIGINT, signal_cleanup);
250         signal(SIGQUIT, signal_cleanup);
251         signal(SIGHUP, signal_cleanup);
252         signal(SIGTERM, signal_cleanup);
253         // signal(SIGSEGV, signal_cleanup);     commented out because
254         // signal(SIGILL, signal_cleanup);      we want core dumps
255         // signal(SIGBUS, signal_cleanup);
256
257         /*
258          * Do not shut down the server on broken pipe signals, otherwise the
259          * whole Citadel service would come down whenever a single client
260          * socket breaks.
261          */
262         //signal(SIGPIPE, SIG_IGN);
263         signal(SIGPIPE, signal_cleanup);
264 }
265
266
267 /*
268  * Obtain a semaphore lock to begin a critical section.
269  */
270 void begin_critical_section(int which_one)
271 {
272         /* CtdlLogPrintf(CTDL_DEBUG, "begin_critical_section(%d)\n", which_one); */
273
274         /* For all types of critical sections except those listed here,
275          * ensure nobody ever tries to do a critical section within a
276          * transaction; this could lead to deadlock.
277          */
278         if (    (which_one != S_FLOORCACHE)
279 #ifdef DEBUG_MEMORY_LEAKS
280                 && (which_one != S_DEBUGMEMLEAKS)
281 #endif
282                 && (which_one != S_RPLIST)
283         ) {
284                 cdb_check_handles();
285         }
286         pthread_mutex_lock(&Critters[which_one]);
287 }
288
289 /*
290  * Release a semaphore lock to end a critical section.
291  */
292 void end_critical_section(int which_one)
293 {
294         pthread_mutex_unlock(&Critters[which_one]);
295 }
296
297
298
299 /*
300  * This is a generic function to set up a master socket for listening on
301  * a TCP port.  The server shuts down if the bind fails.
302  *
303  */
304 int ig_tcp_server(char *ip_addr, int port_number, int queue_len, char **errormessage)
305 {
306         struct sockaddr_in sin;
307         int s, i;
308         int actual_queue_len;
309
310         actual_queue_len = queue_len;
311         if (actual_queue_len < 5) actual_queue_len = 5;
312
313         memset(&sin, 0, sizeof(sin));
314         sin.sin_family = AF_INET;
315         sin.sin_port = htons((u_short)port_number);
316         if (ip_addr == NULL) {
317                 sin.sin_addr.s_addr = INADDR_ANY;
318         }
319         else {
320                 sin.sin_addr.s_addr = inet_addr(ip_addr);
321         }
322                                                                                 
323         if (sin.sin_addr.s_addr == !INADDR_ANY) {
324                 sin.sin_addr.s_addr = INADDR_ANY;
325         }
326
327         s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
328
329         if (s < 0) {
330                 *errormessage = (char*) malloc(SIZ + 1);
331                 snprintf(*errormessage, SIZ, 
332                                  "citserver: Can't create a socket: %s",
333                                  strerror(errno));
334                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
335                 return(-1);
336         }
337
338         i = 1;
339         setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
340
341         if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
342                 *errormessage = (char*) malloc(SIZ + 1);
343                 snprintf(*errormessage, SIZ, 
344                                  "citserver: Can't bind: %s",
345                                  strerror(errno));
346                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
347                 close(s);
348                 return(-1);
349         }
350
351         /* set to nonblock - we need this for some obscure situations */
352         if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
353                 *errormessage = (char*) malloc(SIZ + 1);
354                 snprintf(*errormessage, SIZ, 
355                                  "citserver: Can't set socket to non-blocking: %s",
356                                  strerror(errno));
357                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
358                 close(s);
359                 return(-1);
360         }
361
362         if (listen(s, actual_queue_len) < 0) {
363                 *errormessage = (char*) malloc(SIZ + 1);
364                 snprintf(*errormessage, SIZ, 
365                                  "citserver: Can't listen: %s",
366                                  strerror(errno));
367                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
368                 close(s);
369                 return(-1);
370         }
371
372         return(s);
373 }
374
375
376
377 /*
378  * Create a Unix domain socket and listen on it
379  */
380 int ig_uds_server(char *sockpath, int queue_len, char **errormessage)
381 {
382         struct sockaddr_un addr;
383         int s;
384         int i;
385         int actual_queue_len;
386
387         actual_queue_len = queue_len;
388         if (actual_queue_len < 5) actual_queue_len = 5;
389
390         i = unlink(sockpath);
391         if (i != 0) if (errno != ENOENT) {
392                 *errormessage = (char*) malloc(SIZ + 1);
393                 snprintf(*errormessage, SIZ, "citserver: can't unlink %s: %s",
394                         sockpath, strerror(errno));
395                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
396                 return(-1);
397         }
398
399         memset(&addr, 0, sizeof(addr));
400         addr.sun_family = AF_UNIX;
401         safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
402
403         s = socket(AF_UNIX, SOCK_STREAM, 0);
404         if (s < 0) {
405                 *errormessage = (char*) malloc(SIZ + 1);
406                 snprintf(*errormessage, SIZ, 
407                          "citserver: Can't create a socket: %s",
408                          strerror(errno));
409                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
410                 return(-1);
411         }
412
413         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
414                 *errormessage = (char*) malloc(SIZ + 1);
415                 snprintf(*errormessage, SIZ, 
416                          "citserver: Can't bind: %s",
417                          strerror(errno));
418                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
419                 return(-1);
420         }
421
422         /* set to nonblock - we need this for some obscure situations */
423         if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
424                 *errormessage = (char*) malloc(SIZ + 1);
425                 snprintf(*errormessage, SIZ, 
426                          "citserver: Can't set socket to non-blocking: %s",
427                          strerror(errno));
428                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
429                 close(s);
430                 return(-1);
431         }
432
433         if (listen(s, actual_queue_len) < 0) {
434                 *errormessage = (char*) malloc(SIZ + 1);
435                 snprintf(*errormessage, SIZ, 
436                          "citserver: Can't listen: %s",
437                          strerror(errno));
438                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
439                 return(-1);
440         }
441
442         chmod(sockpath, S_ISGID|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
443         return(s);
444 }
445
446
447
448 /*
449  * Return a pointer to the CitContext structure bound to the thread which
450  * called this function.  If there's no such binding (for example, if it's
451  * called by the housekeeper thread) then a generic 'master' CC is returned.
452  *
453  * This function is used *VERY* frequently and must be kept small.
454  */
455 struct CitContext *MyContext(void) {
456
457         register struct CitContext *c;
458
459         return ((c = (struct CitContext *) pthread_getspecific(MyConKey),
460                 c == NULL) ? &masterCC : c
461         );
462 }
463
464
465 /*
466  * Initialize a new context and place it in the list.  The session number
467  * used to be the PID (which is why it's called cs_pid), but that was when we
468  * had one process per session.  Now we just assign them sequentially, starting
469  * at 1 (don't change it to 0 because masterCC uses 0).
470  */
471 struct CitContext *CreateNewContext(void) {
472         struct CitContext *me;
473         static int next_pid = 0;
474
475         me = (struct CitContext *) malloc(sizeof(struct CitContext));
476         if (me == NULL) {
477                 CtdlLogPrintf(CTDL_ALERT, "citserver: can't allocate memory!!\n");
478                 return NULL;
479         }
480         memset(me, 0, sizeof(struct CitContext));
481
482         /* The new context will be created already in the CON_EXECUTING state
483          * in order to prevent another thread from grabbing it while it's
484          * being set up.
485          */
486         me->state = CON_EXECUTING;
487
488         /*
489          * Generate a unique session number and insert this context into
490          * the list.
491          */
492         begin_critical_section(S_SESSION_TABLE);
493         me->cs_pid = ++next_pid;
494         me->prev = NULL;
495         me->next = ContextList;
496         ContextList = me;
497         if (me->next != NULL) {
498                 me->next->prev = me;
499         }
500         ++num_sessions;
501         end_critical_section(S_SESSION_TABLE);
502         return(me);
503 }
504
505
506 /*
507  * The following functions implement output buffering. If the kernel supplies
508  * native TCP buffering (Linux & *BSD), use that; otherwise, emulate it with
509  * user-space buffering.
510  */
511 #ifndef HAVE_DARWIN
512 #ifdef TCP_CORK
513 #       define HAVE_TCP_BUFFERING
514 #else
515 #       ifdef TCP_NOPUSH
516 #               define HAVE_TCP_BUFFERING
517 #               define TCP_CORK TCP_NOPUSH
518 #       endif
519 #endif /* TCP_CORK */
520 #endif /* HAVE_DARWIN */
521
522 #ifdef HAVE_TCP_BUFFERING
523 static unsigned on = 1, off = 0;
524 void buffer_output(void) {
525         struct CitContext *ctx = MyContext();
526         setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
527         ctx->buffering = 1;
528 }
529
530 void unbuffer_output(void) {
531         struct CitContext *ctx = MyContext();
532         setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
533         ctx->buffering = 0;
534 }
535
536 void flush_output(void) {
537         struct CitContext *ctx = MyContext();
538         setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
539         setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
540 }
541 #else 
542 #ifdef HAVE_DARWIN
543 /* Stub functions for Darwin/OS X where TCP buffering isn't liked at all */
544 void buffer_output(void) {
545         CC->buffering = 0;
546 }
547 void unbuffer_output(void) {
548         CC->buffering = 0;
549 }
550 void flush_output(void) {
551 }
552 #else
553 void buffer_output(void) {
554         if (CC->buffering == 0) {
555                 CC->buffering = 1;
556                 CC->buffer_len = 0;
557                 CC->output_buffer = malloc(SIZ);
558         }
559 }
560
561 void flush_output(void) {
562         if (CC->buffering == 1) {
563                 client_write(CC->output_buffer, CC->buffer_len);
564                 CC->buffer_len = 0;
565         }
566 }
567
568 void unbuffer_output(void) {
569         if (CC->buffering == 1) {
570                 CC->buffering = 0;
571                 /* We don't call flush_output because we can't. */
572                 client_write(CC->output_buffer, CC->buffer_len);
573                 CC->buffer_len = 0;
574                 free(CC->output_buffer);
575                 CC->output_buffer = NULL;
576         }
577 }
578 #endif /* HAVE_DARWIN */
579 #endif /* HAVE_TCP_BUFFERING */
580
581
582
583 /*
584  * client_write()   ...    Send binary data to the client.
585  */
586 void client_write(char *buf, int nbytes)
587 {
588         int bytes_written = 0;
589         int retval;
590 #ifndef HAVE_TCP_BUFFERING
591         int old_buffer_len = 0;
592 #endif
593         t_context *Ctx;
594
595         Ctx = CC;
596         if (Ctx->redirect_buffer != NULL) {
597                 if ((Ctx->redirect_len + nbytes + 2) >= Ctx->redirect_alloc) {
598                         Ctx->redirect_alloc = (Ctx->redirect_alloc * 2) + nbytes;
599                         Ctx->redirect_buffer = realloc(Ctx->redirect_buffer,
600                                                 Ctx->redirect_alloc);
601                 }
602                 memcpy(&Ctx->redirect_buffer[Ctx->redirect_len], buf, nbytes);
603                 Ctx->redirect_len += nbytes;
604                 Ctx->redirect_buffer[Ctx->redirect_len] = 0;
605                 return;
606         }
607
608 #ifndef HAVE_TCP_BUFFERING
609         /* If we're buffering for later, do that now. */
610         if (Ctx->buffering) {
611                 old_buffer_len = Ctx->buffer_len;
612                 Ctx->buffer_len += nbytes;
613                 Ctx->output_buffer = realloc(Ctx->output_buffer, Ctx->buffer_len);
614                 memcpy(&Ctx->output_buffer[old_buffer_len], buf, nbytes);
615                 return;
616         }
617 #endif
618
619         /* Ok, at this point we're not buffering.  Go ahead and write. */
620
621 #ifdef HAVE_OPENSSL
622         if (Ctx->redirect_ssl) {
623                 client_write_ssl(buf, nbytes);
624                 return;
625         }
626 #endif
627
628         while (bytes_written < nbytes) {
629                 retval = write(Ctx->client_socket, &buf[bytes_written],
630                         nbytes - bytes_written);
631                 if (retval < 1) {
632                         CtdlLogPrintf(CTDL_ERR,
633                                 "client_write(%d bytes) failed: %s (%d)\n",
634                                 nbytes - bytes_written,
635                                 strerror(errno), errno);
636                         cit_backtrace();
637                         // CtdlLogPrintf(CTDL_DEBUG, "Tried to send: %s",  &buf[bytes_written]);
638                         Ctx->kill_me = 1;
639                         return;
640                 }
641                 bytes_written = bytes_written + retval;
642         }
643 }
644
645
646 /*
647  * cprintf()  ...   Send formatted printable data to the client.   It is
648  *                implemented in terms of client_write() but remains in
649  *                sysdep.c in case we port to somewhere without va_args...
650  */
651 void cprintf(const char *format, ...) {   
652         va_list arg_ptr;   
653         char buf[1024];   
654    
655         va_start(arg_ptr, format);   
656         if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
657                 buf[sizeof buf - 2] = '\n';
658         client_write(buf, strlen(buf)); 
659         va_end(arg_ptr);
660 }   
661
662
663 /*
664  * Read data from the client socket.
665  * Return values are:
666  *      1       Requested number of bytes has been read.
667  *      0       Request timed out.
668  *      -1      The socket is broken.
669  * If the socket breaks, the session will be terminated.
670  */
671 int client_read_to(char *buf, int bytes, int timeout)
672 {
673         int len,rlen;
674         fd_set rfds;
675         int fd;
676         struct timeval tv;
677         int retval;
678
679 #ifdef HAVE_OPENSSL
680         if (CC->redirect_ssl) {
681                 return (client_read_ssl(buf, bytes, timeout));
682         }
683 #endif
684         len = 0;
685         fd = CC->client_socket;
686         while(len<bytes) {
687                 FD_ZERO(&rfds);
688                 FD_SET(fd, &rfds);
689                 tv.tv_sec = timeout;
690                 tv.tv_usec = 0;
691
692                 retval = select( (fd)+1, 
693                                  &rfds, NULL, NULL, &tv);
694
695                 if (FD_ISSET(fd, &rfds) == 0) {
696                         return(0);
697                 }
698
699                 rlen = read(fd, &buf[len], bytes-len);
700                 if (rlen<1) {
701                         /* The socket has been disconnected! */
702                         CC->kill_me = 1;
703                         return(-1);
704                 }
705                 len = len + rlen;
706         }
707         return(1);
708 }
709
710 /*
711  * Read data from the client socket with default timeout.
712  * (This is implemented in terms of client_read_to() and could be
713  * justifiably moved out of sysdep.c)
714  */
715 INLINE int client_read(char *buf, int bytes)
716 {
717         return(client_read_to(buf, bytes, config.c_sleeping));
718 }
719
720
721 /*
722  * client_getln()   ...   Get a LF-terminated line of text from the client.
723  * (This is implemented in terms of client_read() and could be
724  * justifiably moved out of sysdep.c)
725  */
726 int client_getln(char *buf, int bufsize)
727 {
728         int i, retval;
729
730         /* Read one character at a time.
731          */
732         for (i = 0;;i++) {
733                 retval = client_read(&buf[i], 1);
734                 if (retval != 1 || buf[i] == '\n' || i == (bufsize-1))
735                         break;
736         }
737
738         /* If we got a long line, discard characters until the newline.
739          */
740         if (i == (bufsize-1))
741                 while (buf[i] != '\n' && retval == 1)
742                         retval = client_read(&buf[i], 1);
743
744         /* Strip the trailing LF, and the trailing CR if present.
745          */
746         buf[i] = 0;
747         while ( (i > 0)
748                 && ( (buf[i - 1]==13)
749                      || ( buf[i - 1]==10)) ) {
750                 i--;
751                 buf[i] = 0;
752         }
753         if (retval < 0) safestrncpy(&buf[i], "000", bufsize - i);
754         return(retval);
755 }
756
757
758 /*
759  * Cleanup any contexts that are left lying around
760  */
761 void context_cleanup(void)
762 {
763         struct CitContext *ptr = NULL;
764         struct CitContext *rem = NULL;
765
766         /*
767          * Clean up the contexts.
768          * There are no threads so no critical_section stuff is needed.
769          */
770         ptr = ContextList;
771         while (ptr != NULL){
772                 /* Remove the session from the active list */
773                 rem = ptr->next;
774                 --num_sessions;
775                 
776                 lprintf(CTDL_DEBUG, "Purging session %d\n", ptr->cs_pid);
777                 RemoveContext(ptr);
778                 free (ptr);
779                 ptr = rem;
780         }
781         
782 }
783
784
785 /*
786  * The system-dependent part of master_cleanup() - close the master socket.
787  */
788 void sysdep_master_cleanup(void) {
789         struct ServiceFunctionHook *serviceptr;
790         
791         /*
792          * close all protocol master sockets
793          */
794         for (serviceptr = ServiceHookTable; serviceptr != NULL;
795             serviceptr = serviceptr->next ) {
796
797                 if (serviceptr->tcp_port > 0)
798                         CtdlLogPrintf(CTDL_INFO, "Closing listener on port %d\n",
799                                 serviceptr->tcp_port);
800
801                 if (serviceptr->sockpath != NULL)
802                         CtdlLogPrintf(CTDL_INFO, "Closing listener on '%s'\n",
803                                 serviceptr->sockpath);
804
805                 close(serviceptr->msock);
806
807                 /* If it's a Unix domain socket, remove the file. */
808                 if (serviceptr->sockpath != NULL) {
809                         unlink(serviceptr->sockpath);
810                 }
811         }
812         
813         context_cleanup();
814         
815 #ifdef HAVE_OPENSSL
816         destruct_ssl();
817 #endif
818         CtdlDestroyProtoHooks();
819         CtdlDestroyDeleteHooks();
820         CtdlDestroyXmsgHooks();
821         CtdlDestroyNetprocHooks();
822         CtdlDestroyUserHooks();
823         CtdlDestroyMessageHook();
824         CtdlDestroyCleanupHooks();
825         CtdlDestroyFixedOutputHooks();  
826         CtdlDestroySessionHooks();
827         CtdlDestroyServiceHook();
828         #ifdef HAVE_BACKTRACE
829         eCrash_Uninit();
830         #endif
831 }
832
833
834
835 /*
836  * Terminate another session.
837  * (This could justifiably be moved out of sysdep.c because it
838  * no longer does anything that is system-dependent.)
839  */
840 void kill_session(int session_to_kill) {
841         struct CitContext *ptr;
842
843         begin_critical_section(S_SESSION_TABLE);
844         for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
845                 if (ptr->cs_pid == session_to_kill) {
846                         ptr->kill_me = 1;
847                 }
848         }
849         end_critical_section(S_SESSION_TABLE);
850 }
851
852 pid_t current_child;
853 void graceful_shutdown(int signum) {
854         kill(current_child, signum);
855         unlink(file_pid_file);
856         exit(0);
857 }
858
859
860 /*
861  * Start running as a daemon.
862  */
863 void start_daemon(int unused) {
864         int status = 0;
865         pid_t child = 0;
866         FILE *fp;
867         int do_restart = 0;
868
869         current_child = 0;
870
871         /* Close stdin/stdout/stderr and replace them with /dev/null.
872          * We don't just call close() because we don't want these fd's
873          * to be reused for other files.
874          */
875         chdir(ctdl_run_dir);
876
877         child = fork();
878         if (child != 0) {
879                 exit(0);
880         }
881         
882         signal(SIGHUP, SIG_IGN);
883         signal(SIGINT, SIG_IGN);
884         signal(SIGQUIT, SIG_IGN);
885
886         setsid();
887         umask(0);
888         freopen("/dev/null", "r", stdin);
889         freopen("/dev/null", "w", stdout);
890         freopen("/dev/null", "w", stderr);
891
892         do {
893                 current_child = fork();
894
895                 signal(SIGTERM, graceful_shutdown);
896         
897                 if (current_child < 0) {
898                         perror("fork");
899                         exit(errno);
900                 }
901         
902                 else if (current_child == 0) {
903                         return; /* continue starting citadel. */
904                 }
905         
906                 else {
907                         fp = fopen(file_pid_file, "w");
908                         if (fp != NULL) {
909                 /*
910                  * NB.. The pid file contains the pid of the actual server.
911                  * This is not the pid of the watcher process
912                  */
913                                 fprintf(fp, ""F_PID_T"\n", current_child);
914                                 fclose(fp);
915                         }
916                         waitpid(current_child, &status, 0);
917                 }
918
919                 do_restart = 0;
920
921                 /* Did the main process exit with an actual exit code? */
922                 if (WIFEXITED(status)) {
923
924                         /* Exit code 0 means the watcher should exit */
925                         if (WEXITSTATUS(status) == 0) {
926                                 do_restart = 0;
927                         }
928
929                         /* Exit code 101-109 means the watcher should exit */
930                         else if ( (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109) ) {
931                                 do_restart = 0;
932                         }
933
934                         /* Any other exit code means we should restart. */
935                         else {
936                                 do_restart = 1;
937                         }
938                 }
939
940                 /* Any other type of termination (signals, etc.) should also restart. */
941                 else {
942                         do_restart = 1;
943                 }
944
945         } while (do_restart);
946
947         unlink(file_pid_file);
948         exit(WEXITSTATUS(status));
949 }
950
951
952
953 /*
954  * Generic routine to convert a login name to a full name (gecos)
955  * Returns nonzero if a conversion took place
956  */
957 int convert_login(char NameToConvert[]) {
958         struct passwd *pw;
959         int a;
960
961         pw = getpwnam(NameToConvert);
962         if (pw == NULL) {
963                 return(0);
964         }
965         else {
966                 strcpy(NameToConvert, pw->pw_gecos);
967                 for (a=0; a<strlen(NameToConvert); ++a) {
968                         if (NameToConvert[a] == ',') NameToConvert[a] = 0;
969                 }
970                 return(1);
971         }
972 }
973
974
975
976 /*
977  * New thread interface.
978  * To create a thread you must call one of the create thread functions.
979  * You must pass it the address of (a pointer to a CtdlThreadNode initialised to NULL) like this
980  * struct CtdlThreadNode *node = NULL;
981  * pass in &node
982  * If the thread is created *node will point to the thread control structure for the created thread.
983  * If the thread creation fails *node remains NULL
984  * Do not free the memory pointed to by *node, it doesn't belong to you.
985  * If your thread function returns it will be started again without creating a new thread.
986  * If your thread function wants to exit it should call CtdlThreadExit(ret_code);
987  * This new interface duplicates much of the eCrash stuff. We should go for closer integration since that would
988  * remove the need for the calls to eCrashRegisterThread and friends
989  */
990
991
992 struct CtdlThreadNode *CtdlThreadList = NULL;
993
994 /*
995  * Condition variable and Mutex for thread garbage collection
996  */
997 /*static pthread_mutex_t thread_gc_mutex = PTHREAD_MUTEX_INITIALIZER;
998 static pthread_cond_t thread_gc_cond = PTHREAD_COND_INITIALIZER;
999 */static pthread_t GC_thread;
1000 static char *CtdlThreadStates[CTDL_THREAD_LAST_STATE];
1001 /*
1002  * Pinched the following bits regarding signals from Kannel.org
1003  */
1004  
1005 /*
1006  * Change this thread's signal mask to block user-visible signals
1007  * (HUP, TERM, QUIT, INT), and store the old signal mask in
1008  * *old_set_storage.
1009  * Return 0 for success, or -1 if an error occurred.
1010  */
1011  
1012  /* 
1013   * This does not work in Darwin alias MacOS X alias Mach kernel,
1014   * however. So we define a dummy function doing nothing.
1015   */
1016 #if defined(DARWIN_OLD)
1017     static int pthread_sigmask();
1018 #endif
1019   
1020 static int ctdl_thread_internal_block_signals(sigset_t *old_set_storage)
1021 {
1022     int ret;
1023     sigset_t block_signals;
1024
1025     ret = sigemptyset(&block_signals);
1026     if (ret != 0) {
1027         CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC. Couldn't initialize signal set\n");
1028             return -1;
1029     }
1030     ret = sigaddset(&block_signals, SIGHUP);
1031     ret |= sigaddset(&block_signals, SIGTERM);
1032     ret |= sigaddset(&block_signals, SIGQUIT);
1033     ret |= sigaddset(&block_signals, SIGINT);
1034     if (ret != 0) {
1035         CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC. Couldn't add signal to signal set.\n");
1036             return -1;
1037     }
1038     ret = pthread_sigmask(SIG_BLOCK, &block_signals, old_set_storage);
1039     if (ret != 0) {
1040         CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC. Couldn't disable signals for thread creation\n");
1041         return -1;
1042     }
1043     return 0;
1044 }
1045
1046 static void ctdl_thread_internal_restore_signals(sigset_t *old_set)
1047 {
1048     int ret;
1049
1050     ret = pthread_sigmask(SIG_SETMASK, old_set, NULL);
1051     if (ret != 0) {
1052         CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC. Couldn't restore signal set.\n");
1053     }
1054 }
1055
1056
1057 void ctdl_thread_internal_init(void)
1058 {
1059         struct CtdlThreadNode *this_thread;
1060         int ret = 0;
1061         
1062         GC_thread = pthread_self();
1063         CtdlThreadStates[CTDL_THREAD_INVALID] = strdup ("Invalid Thread");
1064         CtdlThreadStates[CTDL_THREAD_VALID] = strdup("Valid Thread");
1065         CtdlThreadStates[CTDL_THREAD_CREATE] = strdup("Thread being Created");
1066         CtdlThreadStates[CTDL_THREAD_CANCELLED] = strdup("Thread Cancelled");
1067         CtdlThreadStates[CTDL_THREAD_EXITED] = strdup("Thread Exited");
1068         CtdlThreadStates[CTDL_THREAD_STOPPING] = strdup("Thread Stopping");
1069         CtdlThreadStates[CTDL_THREAD_STOP_REQ] = strdup("Thread Stop Requested");
1070         CtdlThreadStates[CTDL_THREAD_SLEEPING] = strdup("Thread Sleeping");
1071         CtdlThreadStates[CTDL_THREAD_RUNNING] = strdup("Thread Running");
1072         
1073         
1074         /* Get ourself a thread entry */
1075         this_thread = malloc(sizeof(struct CtdlThreadNode));
1076         if (this_thread == NULL) {
1077                 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
1078                 return;
1079         }
1080         // Ensuring this is zero'd means we make sure the thread doesn't start doing its thing until we are ready.
1081         memset (this_thread, 0, sizeof(struct CtdlThreadNode));
1082         
1083         /* We are garbage collector so create us as running */
1084         this_thread->state = CTDL_THREAD_RUNNING;
1085         
1086         if ((ret = pthread_attr_init(&this_thread->attr))) {
1087                 CtdlLogPrintf(CTDL_EMERG, "Thread system, pthread_attr_init: %s\n", strerror(ret));
1088                 free(this_thread);
1089                 return;
1090         }
1091
1092         this_thread->name = strdup("Garbage Collection Thread");
1093         
1094         pthread_mutex_init (&(this_thread->ThreadMutex), NULL);
1095         pthread_cond_init (&(this_thread->ThreadCond), NULL);
1096         
1097         this_thread->tid = GC_thread;
1098         
1099         num_threads++;  // Increase the count of threads in the system.
1100
1101         this_thread->next = CtdlThreadList;
1102         CtdlThreadList = this_thread;
1103         if (this_thread->next)
1104                 this_thread->next->prev = this_thread;
1105 }
1106
1107 /*
1108  * A function to tell all threads to exit
1109  */
1110 void CtdlThreadStopAll(void)
1111 {
1112         struct CtdlThreadNode *this_thread;
1113         
1114         begin_critical_section(S_THREAD_LIST);
1115         this_thread = CtdlThreadList;
1116         while(this_thread)
1117         {
1118                 if (this_thread->thread_func) // Don't tell garbage collector to stop
1119                 {
1120                         pthread_mutex_lock(&this_thread->ThreadMutex); /* To prevent race condition of a sleeping thread */
1121                         if (this_thread->state > CTDL_THREAD_STOP_REQ)
1122                                 this_thread->state = CTDL_THREAD_STOP_REQ;
1123                         pthread_mutex_unlock(&this_thread->ThreadMutex);
1124                         pthread_cond_signal(&this_thread->ThreadCond);
1125                         CtdlLogPrintf(CTDL_DEBUG, "Thread system stopping thread \"%s\" (%ld).\n", this_thread->name, this_thread->tid);
1126                 }
1127                 this_thread = this_thread->next;
1128         }
1129         end_critical_section(S_THREAD_LIST);
1130 }
1131
1132
1133 /*
1134  * A function to signal that we need to do garbage collection on the thread list
1135  */
1136 void CtdlThreadGC(void)
1137 {
1138         struct CtdlThreadNode *this_thread;
1139         
1140         CtdlLogPrintf(CTDL_DEBUG, "Thread system signalling garbage collection.\n");
1141         
1142         begin_critical_section(S_THREAD_LIST);
1143         this_thread = CtdlThreadList;
1144         while(this_thread)
1145         {
1146                 if (!this_thread->thread_func)
1147                         pthread_cond_signal(&this_thread->ThreadCond);
1148                         
1149                 this_thread = this_thread->next;
1150         }
1151         end_critical_section(S_THREAD_LIST);
1152 }
1153
1154
1155 /*
1156  * A function to return the number of threads running in the system
1157  */
1158 int CtdlThreadGetCount(void)
1159 {
1160         return num_threads;
1161 }
1162
1163 /*
1164  * A function to find the thread structure for this thread
1165  */
1166 struct CtdlThreadNode *CtdlThreadSelf(void)
1167 {
1168         pthread_t self_tid;
1169         struct CtdlThreadNode *this_thread;
1170         
1171         self_tid = pthread_self();
1172         
1173         begin_critical_section(S_THREAD_LIST);
1174         this_thread = CtdlThreadList;
1175         while(this_thread)
1176         {
1177                 if (pthread_equal(self_tid, this_thread->tid))
1178                 {
1179                         end_critical_section(S_THREAD_LIST);
1180                         return this_thread;
1181                 }
1182                 this_thread = this_thread->next;
1183         }
1184         end_critical_section(S_THREAD_LIST);
1185         return NULL;
1186 }
1187
1188
1189
1190
1191 /*
1192  * A function to rename a thread
1193  * Returns a char * and the caller owns the memory and should free it
1194  */
1195 char *CtdlThreadName(struct CtdlThreadNode *thread, char *name)
1196 {
1197         struct CtdlThreadNode *this_thread;
1198         char *old_name;
1199         
1200         if (!thread)
1201                 this_thread = CtdlThreadSelf();
1202         else
1203                 this_thread = thread;
1204         if (!this_thread)
1205         {
1206                 CtdlLogPrintf(CTDL_WARNING, "Thread system WARNING. Attempt to CtdlThreadRename() a non thread.\n");
1207                 return NULL;
1208         }
1209         begin_critical_section(S_THREAD_LIST);
1210         if (name)
1211         {
1212                 old_name = this_thread->name;
1213                 this_thread->name = strdup (name);
1214                 free(old_name);
1215         }
1216         old_name = strdup(this_thread->name);
1217         end_critical_section (S_THREAD_LIST);
1218         return (old_name);
1219 }       
1220
1221
1222 /*
1223  * A function to force a thread to exit
1224  */
1225 void CtdlThreadCancel(struct CtdlThreadNode *thread)
1226 {
1227         struct CtdlThreadNode *this_thread;
1228         
1229         if (!thread)
1230                 this_thread = CtdlThreadSelf();
1231         else
1232                 this_thread = thread;
1233         if (!this_thread)
1234         {
1235                 CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC. Attempt to CtdlThreadCancel() a non thread.\n");
1236                 CtdlThreadStopAll();
1237                 return;
1238         }
1239         
1240         if (!this_thread->thread_func)
1241         {
1242                 CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC. Attempt to CtdlThreadCancel() the garbage collector.\n");
1243                 CtdlThreadStopAll();
1244                 return;
1245         }
1246         
1247         begin_critical_section(S_THREAD_LIST);
1248         this_thread->state = CTDL_THREAD_CANCELLED;
1249         pthread_cancel(this_thread->tid);
1250         end_critical_section (S_THREAD_LIST);
1251 }
1252
1253
1254
1255 /*
1256  * A function for a thread to check if it has been asked to stop
1257  */
1258 int CtdlThreadCheckStop(void)
1259 {
1260         struct CtdlThreadNode *this_thread;
1261         
1262         this_thread = CtdlThreadSelf();
1263         if (!this_thread)
1264         {
1265                 CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, CtdlThreadCheckStop() called by a non thread.\n");
1266                 CtdlThreadStopAll();
1267                 return -1;
1268         }
1269         if(this_thread->state == CTDL_THREAD_STOP_REQ)
1270         {
1271                 this_thread->state = CTDL_THREAD_STOPPING;
1272                 return -1;
1273         }
1274         else if(this_thread->state < CTDL_THREAD_STOP_REQ)
1275                 return -1;
1276                 
1277         return 0;
1278 }
1279
1280
1281 /*
1282  * A function to ask a thread to exit
1283  * The thread must call CtdlThreadCheckStop() periodically to determine if it should exit
1284  */
1285 void CtdlThreadStop(struct CtdlThreadNode *thread)
1286 {
1287         struct CtdlThreadNode *this_thread;
1288         
1289         if (!thread)
1290                 this_thread = CtdlThreadSelf();
1291         else
1292                 this_thread = thread;
1293         if (!this_thread)
1294                 return;
1295         if (!(this_thread->thread_func))
1296                 return;         // Don't stop garbage collector
1297                 
1298         begin_critical_section (S_THREAD_LIST);
1299         pthread_mutex_lock(&this_thread->ThreadMutex); /* To prevent race condition of a sleeping thread */
1300         if (this_thread->state > CTDL_THREAD_STOP_REQ)
1301                 this_thread->state = CTDL_THREAD_STOP_REQ;
1302         pthread_mutex_unlock(&this_thread->ThreadMutex);
1303         pthread_cond_signal(&this_thread->ThreadCond);
1304         end_critical_section(S_THREAD_LIST);
1305 }
1306
1307 /*
1308  * So we now have a sleep command that works with threads but it is in seconds
1309  */
1310 void CtdlThreadSleep(int secs)
1311 {
1312         struct timespec wake_time;
1313         struct timeval time_now;
1314         struct CtdlThreadNode *self;
1315         int state;
1316         
1317         
1318         self = CtdlThreadSelf();
1319         if (!self)
1320         {
1321                 CtdlLogPrintf(CTDL_WARNING, "CtdlThreadSleep() called by something that is not a thread. Should we die?\n");
1322                 return;
1323         }
1324         
1325         begin_critical_section(S_THREAD_LIST);
1326         pthread_mutex_lock(&self->ThreadMutex); /* Prevent something asking us to awaken before we've gone to sleep */
1327         state = self->state;
1328         if (state == CTDL_THREAD_RUNNING)
1329                 self->state = CTDL_THREAD_SLEEPING;
1330         end_critical_section(S_THREAD_LIST);
1331         
1332         if(state != CTDL_THREAD_RUNNING)
1333         {
1334                 CtdlLogPrintf(CTDL_DEBUG, "CtdlThreadSleep() called by a thread that is not running.\n");
1335                 pthread_mutex_unlock(&self->ThreadMutex);
1336                 return;
1337         }
1338         
1339         memset (&wake_time, 0, sizeof(struct timespec));
1340         gettimeofday(&time_now, NULL);
1341         wake_time.tv_sec = time_now.tv_sec + secs;
1342         wake_time.tv_nsec = time_now.tv_usec * 10;
1343         pthread_cond_timedwait(&self->ThreadCond, &self->ThreadMutex, &wake_time);
1344         begin_critical_section(S_THREAD_LIST);
1345         if (self->state == CTDL_THREAD_SLEEPING) /* Don't change state if something else changed it while we were asleep */
1346                 self->state = state;
1347         pthread_mutex_unlock(&self->ThreadMutex);
1348         end_critical_section(S_THREAD_LIST);
1349 }
1350
1351
1352 /*
1353  * Routine to clean up our thread function on exit
1354  */
1355 static void ctdl_internal_thread_cleanup(void *arg)
1356 {
1357         struct CtdlThreadNode *this_thread;
1358         this_thread = CtdlThreadSelf();
1359         /*
1360          * In here we were called by the current thread because it is exiting
1361          * NB. WE ARE THE CURRENT THREAD
1362          */
1363         CtdlLogPrintf(CTDL_NOTICE, "Thread \"%s\" (%ld) exited.\n", this_thread->name, this_thread->tid);
1364         begin_critical_section(S_THREAD_LIST);
1365         #ifdef HAVE_BACKTRACE
1366         eCrash_UnregisterThread();
1367         #endif
1368         this_thread->state = CTDL_THREAD_EXITED;        // needs to be last thing else house keeping will unlink us too early
1369         end_critical_section(S_THREAD_LIST);
1370         CtdlThreadGC();
1371 }
1372
1373
1374
1375
1376 /*
1377  * Garbage collection routine.
1378  * Gets called by main() in a loop to clean up the thread list periodically.
1379  */
1380 void ctdl_internal_thread_gc (void)
1381 {
1382         struct CtdlThreadNode *this_thread, *that_thread = NULL;
1383         int workers = 0;
1384         
1385         /* 
1386          * Wait on the condition variable that tells us garbage collection is needed
1387          * We wake up every 10 seconds just in case someone forgot to inform us of a thread exiting
1388          */
1389         
1390         CtdlThreadSleep(10);
1391         
1392         /* Handle exiting of garbage collector thread */
1393         if(num_threads == 1)
1394                 CtdlThreadList->state = CTDL_THREAD_EXITED;
1395         
1396         CtdlLogPrintf(CTDL_DEBUG, "Thread system running garbage collection.\n");
1397         /*
1398          * Woke up to do garbage collection
1399          */
1400         begin_critical_section(S_THREAD_LIST);
1401         this_thread = CtdlThreadList;
1402         while(this_thread)
1403         {
1404                 that_thread = this_thread;
1405                 this_thread = this_thread->next;
1406                 
1407                 CtdlLogPrintf(CTDL_DEBUG, "CtdlThread, \"%s\" (%ld) \"%s\".\n", that_thread->name, that_thread->tid, CtdlThreadStates[that_thread->state]);
1408                 /* Do we need to clean up this thread? */
1409                 if (that_thread->state != CTDL_THREAD_EXITED)
1410                 {
1411                         if(that_thread->flags & CTDLTHREAD_WORKER)
1412                                 workers++;      /* Sanity check on number of worker threads */
1413                         continue;
1414                 }
1415                 
1416                 if (pthread_equal(that_thread->tid, pthread_self()) && that_thread->thread_func)
1417                 {       /* Sanity check */
1418                         end_critical_section(S_THREAD_LIST);
1419                         CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, a thread is trying to clean up after itself.\n");
1420                         CtdlThreadStopAll();
1421                         return;
1422                 }
1423                 
1424                 if (num_threads <= 0)
1425                 {       /* Sanity check */
1426                         end_critical_section (S_THREAD_LIST);
1427                         CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, num_threads <= 0 and trying to do Garbage Collection.\n");
1428                         CtdlThreadStopAll();
1429                         return;
1430                 }
1431
1432                 /* If we are unlinking the list head then the next becomes the list head */
1433                 if (that_thread == CtdlThreadList)
1434                         CtdlThreadList = that_thread->next;
1435                 if(that_thread->prev)
1436                         that_thread->prev->next = that_thread->next;
1437                 if(that_thread->next)
1438                         that_thread->next->prev = that_thread->next;
1439                 num_threads--;
1440                 if(that_thread->flags & CTDLTHREAD_WORKER)
1441                         num_workers--;  /* This is a wroker thread so reduce the count. */
1442                 
1443                 /*
1444                  * Join on the thread to do clean up and prevent memory leaks
1445                  * Also makes sure the thread has cleaned up after itself before we remove it from the list
1446                  * If that thread has no function it must be the garbage collector
1447                  */
1448                 if (that_thread->thread_func)
1449                         pthread_join (that_thread->tid, NULL);
1450                 
1451                 /*
1452                  * Now we own that thread entry
1453                  */
1454                 CtdlLogPrintf(CTDL_INFO, "Garbage Collection for thread \"%s\" (%ld).\n", that_thread->name, that_thread->tid);
1455                 if(that_thread->name)
1456                         free(that_thread->name);
1457                 pthread_mutex_destroy(&that_thread->ThreadMutex);
1458                 pthread_cond_destroy(&that_thread->ThreadCond);
1459                 pthread_attr_destroy(&that_thread->attr);
1460                 free(that_thread);
1461                 that_thread = NULL;
1462         }
1463         
1464         /* Sanity check number of worker threads */
1465         if (workers != num_workers)
1466         {
1467                 end_critical_section(S_THREAD_LIST);
1468                 CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, discrepancy in number of worker threads. Counted %d, should be %d.\n", workers, num_workers);
1469                 return;
1470         }
1471         
1472         end_critical_section(S_THREAD_LIST);
1473 }
1474
1475
1476
1477  
1478 /*
1479  * Runtime function for a Citadel Thread.
1480  * This initialises the threads environment and then calls the user supplied thread function
1481  * Note that this is the REAL thread function and wraps the users thread function.
1482  */ 
1483 static void *ctdl_internal_thread_func (void *arg)
1484 {
1485         struct CtdlThreadNode *this_thread;
1486         void *ret = NULL;
1487
1488         /* lock and unlock the thread list.
1489          * This causes this thread to wait until all its creation stuff has finished before it
1490          * can continue its execution.
1491          */
1492         begin_critical_section(S_THREAD_LIST);
1493         // Get our thread data structure
1494         this_thread = (struct CtdlThreadNode *) arg;
1495         this_thread->state = CTDL_THREAD_RUNNING;
1496         this_thread->pid = getpid();
1497         end_critical_section(S_THREAD_LIST);
1498                 
1499         // Tell the world we are here
1500         CtdlLogPrintf(CTDL_NOTICE, "Created a new thread \"%s\" (%ld). \n", this_thread->name, this_thread->tid);
1501
1502         // Register the cleanup function to take care of when we exit.
1503         pthread_cleanup_push(ctdl_internal_thread_cleanup, NULL);
1504         
1505         
1506         /*
1507          * run the thread to do the work
1508          */
1509         ret = (this_thread->thread_func)(this_thread->user_args);
1510         
1511         /*
1512          * Our thread is exiting either because it wanted to end or because the server is stopping
1513          * We need to clean up
1514          */
1515         pthread_cleanup_pop(1); // Execute our cleanup routine and remove it
1516         
1517         return(ret);
1518 }
1519
1520
1521  
1522 /*
1523  * Internal function to create a thread.
1524  * Must be called from within a S_THREAD_LIST critical section
1525  */ 
1526 struct CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thread_func) (void *arg), void *args)
1527 {
1528         int ret = 0;
1529         struct CtdlThreadNode *this_thread;
1530         int sigtrick = 0;
1531         sigset_t old_signal_set;
1532
1533         if (num_threads >= 32767)
1534         {
1535                 CtdlLogPrintf(CTDL_EMERG, "Thread system. Thread list full.\n");
1536                 return NULL;
1537         }
1538                 
1539         this_thread = malloc(sizeof(struct CtdlThreadNode));
1540         if (this_thread == NULL) {
1541                 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
1542                 return NULL;
1543         }
1544         // Ensuring this is zero'd means we make sure the thread doesn't start doing its thing until we are ready.
1545         memset (this_thread, 0, sizeof(struct CtdlThreadNode));
1546         
1547         this_thread->state = CTDL_THREAD_CREATE;
1548         
1549         if ((ret = pthread_attr_init(&this_thread->attr))) {
1550                 CtdlLogPrintf(CTDL_EMERG, "Thread system, pthread_attr_init: %s\n", strerror(ret));
1551                 free(this_thread);
1552                 return NULL;
1553         }
1554
1555         /* Our per-thread stacks need to be bigger than the default size,
1556          * otherwise the MIME parser crashes on FreeBSD, and the IMAP service
1557          * crashes on 64-bit Linux.
1558          */
1559         if (flags & CTDLTHREAD_BIGSTACK)
1560         {
1561                 CtdlLogPrintf(CTDL_INFO, "Thread system. Creating BIG STACK thread.\n");
1562                 if ((ret = pthread_attr_setstacksize(&this_thread->attr, THREADSTACKSIZE))) {
1563                         CtdlLogPrintf(CTDL_EMERG, "Thread system, pthread_attr_setstacksize: %s\n",
1564                                 strerror(ret));
1565                         pthread_attr_destroy(&this_thread->attr);
1566                         free(this_thread);
1567                         return NULL;
1568                 }
1569         }
1570
1571         /*
1572          * If we got here we are going to create the thread so we must initilise the structure
1573          * first because most implimentations of threading can't create it in a stopped state
1574          * and it might want to do things with its structure that aren't initialised otherwise.
1575          */
1576         if(name)
1577         {
1578                 this_thread->name = strdup(name);
1579         }
1580         else
1581         {
1582                 this_thread->name = strdup("Un-named Thread");
1583         }
1584         
1585         this_thread->flags = flags;
1586         this_thread->thread_func = thread_func;
1587         this_thread->user_args = args;
1588         pthread_mutex_init (&(this_thread->ThreadMutex), NULL);
1589         pthread_cond_init (&(this_thread->ThreadCond), NULL);
1590         
1591         /*
1592          * We want to make sure that only the main thread handles signals,
1593          * so that each signal is handled exactly once.  To do this, we
1594          * make sure that each new thread has all the signals that we
1595          * handle blocked.  To avoid race conditions, we block them in 
1596          * the spawning thread first, then create the new thread (which
1597          * inherits the settings), and then restore the old settings in
1598          * the spawning thread.  This means that there is a brief period
1599          * when no signals will be processed, but during that time they
1600          * should be queued by the operating system.
1601          */
1602         if (pthread_equal(GC_thread, pthread_self())) 
1603             sigtrick = ctdl_thread_internal_block_signals(&old_signal_set) == 0;
1604
1605         /*
1606          * We pass this_thread into the thread as its args so that it can find out information
1607          * about itself and it has a bit of storage space for itself, not to mention that the REAL
1608          * thread function needs to finish off the setup of the structure
1609          */
1610         if ((ret = pthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0))
1611         {
1612
1613                 CtdlLogPrintf(CTDL_ALERT, "Thread system, Can't create thread: %s\n",
1614                         strerror(ret));
1615                 if (this_thread->name)
1616                         free (this_thread->name);
1617                 pthread_mutex_destroy(&(this_thread->ThreadMutex));
1618                 pthread_cond_destroy(&(this_thread->ThreadCond));
1619                 pthread_attr_destroy(&this_thread->attr);
1620                 free(this_thread);
1621                 if (sigtrick)
1622                         ctdl_thread_internal_restore_signals(&old_signal_set);
1623                 return NULL;
1624         }
1625         
1626         if (sigtrick)
1627                 ctdl_thread_internal_restore_signals(&old_signal_set);
1628         
1629         num_threads++;  // Increase the count of threads in the system.
1630         if(this_thread->flags & CTDLTHREAD_WORKER)
1631                 num_workers++;
1632
1633         this_thread->next = CtdlThreadList;
1634         CtdlThreadList = this_thread;
1635         if (this_thread->next)
1636                 this_thread->next->prev = this_thread;
1637         // Register for tracing
1638         #ifdef HAVE_BACKTRACE
1639         eCrash_RegisterThread(this_thread->name, 0);
1640         #endif
1641         return this_thread;
1642 }
1643
1644 /*
1645  * Wrapper function to create a thread
1646  * ensures the critical section and other protections are in place.
1647  * char *name = name to give to thread, if NULL, use generic name
1648  * int flags = flags to determine type of thread and standard facilities
1649  */
1650 struct CtdlThreadNode *CtdlThreadCreate(char *name, long flags, void *(*thread_func) (void *arg), void *args)
1651 {
1652         struct CtdlThreadNode *ret = NULL;
1653         
1654         begin_critical_section(S_THREAD_LIST);
1655         ret = ctdl_internal_create_thread(name, flags, thread_func, args);
1656         end_critical_section(S_THREAD_LIST);
1657         return ret;
1658 }
1659
1660
1661
1662
1663 /*
1664  * Purge all sessions which have the 'kill_me' flag set.
1665  * This function has code to prevent it from running more than once every
1666  * few seconds, because running it after every single unbind would waste a lot
1667  * of CPU time and keep the context list locked too much.  To force it to run
1668  * anyway, set "force" to nonzero.
1669  *
1670  *
1671  * After that's done, we raise the size of the worker thread pool
1672  * if such an action is appropriate.
1673  */
1674 void dead_session_purge(int force) {
1675         struct CitContext *ptr, *ptr2;          /* general-purpose utility pointer */
1676         struct CitContext *rem = NULL;  /* list of sessions to be destroyed */
1677
1678         if (force == 0) {
1679                 if ( (time(NULL) - last_purge) < 5 ) {
1680                         return; /* Too soon, go away */
1681                 }
1682         }
1683         time(&last_purge);
1684
1685         begin_critical_section(S_SESSION_TABLE);
1686         ptr = ContextList;
1687         while (ptr) {
1688                 ptr2 = ptr;
1689                 ptr = ptr->next;
1690                 
1691                 if ( (ptr2->state == CON_IDLE) && (ptr2->kill_me) ) {
1692                         /* Remove the session from the active list */
1693                         if (ptr2->prev) {
1694                                 ptr2->prev->next = ptr2->next;
1695                         }
1696                         else {
1697                                 ContextList = ptr2->next;
1698                         }
1699                         if (ptr2->next) {
1700                                 ptr2->next->prev = ptr2->prev;
1701                         }
1702
1703                         --num_sessions;
1704
1705                         /* And put it on our to-be-destroyed list */
1706                         ptr2->next = rem;
1707                         rem = ptr2;
1708
1709                 }
1710         }
1711         end_critical_section(S_SESSION_TABLE);
1712
1713         /* Now that we no longer have the session list locked, we can take
1714          * our time and destroy any sessions on the to-be-killed list, which
1715          * is allocated privately on this thread's stack.
1716          */
1717         while (rem != NULL) {
1718                 CtdlLogPrintf(CTDL_DEBUG, "Purging session %d\n", rem->cs_pid);
1719                 RemoveContext(rem);
1720                 ptr = rem;
1721                 rem = rem->next;
1722                 free(ptr);
1723         }
1724
1725         /* Raise the size of the worker thread pool if necessary. */
1726         begin_critical_section(S_THREAD_LIST);
1727         if ( (num_sessions > num_workers)
1728            && (num_workers < config.c_max_workers) ) {
1729                 ctdl_internal_create_thread(NULL, CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER, worker_thread, NULL);
1730         }
1731         end_critical_section(S_THREAD_LIST);
1732         // FIXME: reduce the number of worker threads too
1733 }
1734
1735
1736
1737
1738
1739 /*
1740  * masterCC is the context we use when not attached to a session.  This
1741  * function initializes it.
1742  */
1743 void InitializeMasterCC(void) {
1744         memset(&masterCC, 0, sizeof(struct CitContext));
1745         masterCC.internal_pgm = 1;
1746         masterCC.cs_pid = 0;
1747 }
1748
1749
1750
1751
1752
1753
1754 /*
1755  * Bind a thread to a context.  (It's inline merely to speed things up.)
1756  */
1757 INLINE void become_session(struct CitContext *which_con) {
1758         pthread_setspecific(MyConKey, (void *)which_con );
1759 }
1760
1761
1762
1763 /* 
1764  * This loop just keeps going and going and going...
1765  */     
1766 void *worker_thread(void *arg) {
1767         int i;
1768         int highest;
1769         struct CitContext *ptr;
1770         struct CitContext *bind_me = NULL;
1771         fd_set readfds;
1772         int retval = 0;
1773         struct CitContext *con= NULL;   /* Temporary context pointer */
1774         struct ServiceFunctionHook *serviceptr;
1775         int ssock;                      /* Descriptor for client socket */
1776         struct timeval tv;
1777         int force_purge = 0;
1778         int m;
1779
1780         cdb_allocate_tsd();
1781
1782         while (!CtdlThreadCheckStop()) {
1783
1784                 /* make doubly sure we're not holding any stale db handles
1785                  * which might cause a deadlock.
1786                  */
1787                 cdb_check_handles();
1788 do_select:      force_purge = 0;
1789                 bind_me = NULL;         /* Which session shall we handle? */
1790
1791                 /* Initialize the fdset. */
1792                 FD_ZERO(&readfds);
1793                 highest = 0;
1794
1795                 begin_critical_section(S_SESSION_TABLE);
1796                 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1797                         if (ptr->state == CON_IDLE) {
1798                                 FD_SET(ptr->client_socket, &readfds);
1799                                 if (ptr->client_socket > highest)
1800                                         highest = ptr->client_socket;
1801                         }
1802                         if ((bind_me == NULL) && (ptr->state == CON_READY)) {
1803                                 bind_me = ptr;
1804                                 ptr->state = CON_EXECUTING;
1805                         }
1806                 }
1807                 end_critical_section(S_SESSION_TABLE);
1808
1809                 if (bind_me) {
1810                         goto SKIP_SELECT;
1811                 }
1812
1813                 /* If we got this far, it means that there are no sessions
1814                  * which a previous thread marked for attention, so we go
1815                  * ahead and get ready to select().
1816                  */
1817
1818                 /* First, add the various master sockets to the fdset. */
1819                 for (serviceptr = ServiceHookTable; serviceptr != NULL;
1820                 serviceptr = serviceptr->next ) {
1821                         m = serviceptr->msock;
1822                         FD_SET(m, &readfds);
1823                         if (m > highest) {
1824                                 highest = m;
1825                         }
1826                 }
1827
1828                 if (!CtdlThreadCheckStop()) {
1829                         tv.tv_sec = 1;          /* wake up every second if no input */
1830                         tv.tv_usec = 0;
1831                         retval = select(highest + 1, &readfds, NULL, NULL, &tv);
1832                 }
1833
1834                 if (CtdlThreadCheckStop()) return(NULL);
1835
1836                 /* Now figure out who made this select() unblock.
1837                  * First, check for an error or exit condition.
1838                  */
1839                 if (retval < 0) {
1840                         if (errno == EBADF) {
1841                                 CtdlLogPrintf(CTDL_NOTICE, "select() failed: (%s)\n",
1842                                         strerror(errno));
1843                                 goto do_select;
1844                         }
1845                         if (errno != EINTR) {
1846                                 CtdlLogPrintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
1847                                 CtdlThreadStopAll();
1848                         } else if (!CtdlThreadCheckStop()) {
1849                                 CtdlLogPrintf(CTDL_DEBUG, "Un handled select failure.\n");
1850                                 goto do_select;
1851                         }
1852                 }
1853                 else if(retval == 0) {
1854                         goto SKIP_SELECT;
1855                 }
1856                 /* Next, check to see if it's a new client connecting
1857                  * on a master socket.
1858                  */
1859                 else for (serviceptr = ServiceHookTable; serviceptr != NULL;
1860                      serviceptr = serviceptr->next ) {
1861
1862                         if (FD_ISSET(serviceptr->msock, &readfds)) {
1863                                 ssock = accept(serviceptr->msock, NULL, 0);
1864                                 if (ssock >= 0) {
1865                                         CtdlLogPrintf(CTDL_DEBUG,
1866                                                 "New client socket %d\n",
1867                                                 ssock);
1868
1869                                         /* The master socket is non-blocking but the client
1870                                          * sockets need to be blocking, otherwise certain
1871                                          * operations barf on FreeBSD.  Not a fatal error.
1872                                          */
1873                                         if (fcntl(ssock, F_SETFL, 0) < 0) {
1874                                                 CtdlLogPrintf(CTDL_EMERG,
1875                                                         "citserver: Can't set socket to blocking: %s\n",
1876                                                         strerror(errno));
1877                                         }
1878
1879                                         /* New context will be created already
1880                                          * set up in the CON_EXECUTING state.
1881                                          */
1882                                         con = CreateNewContext();
1883
1884                                         /* Assign our new socket number to it. */
1885                                         con->client_socket = ssock;
1886                                         con->h_command_function =
1887                                                 serviceptr->h_command_function;
1888                                         con->h_async_function =
1889                                                 serviceptr->h_async_function;
1890                                         con->ServiceName =
1891                                                 serviceptr->ServiceName;
1892                                         
1893                                         /* Determine whether it's a local socket */
1894                                         if (serviceptr->sockpath != NULL)
1895                                                 con->is_local_socket = 1;
1896         
1897                                         /* Set the SO_REUSEADDR socket option */
1898                                         i = 1;
1899                                         setsockopt(ssock, SOL_SOCKET,
1900                                                 SO_REUSEADDR,
1901                                                 &i, sizeof(i));
1902
1903                                         become_session(con);
1904                                         begin_session(con);
1905                                         serviceptr->h_greeting_function();
1906                                         become_session(NULL);
1907                                         con->state = CON_IDLE;
1908                                         goto do_select;
1909                                 }
1910                         }
1911                 }
1912
1913                 /* It must be a client socket.  Find a context that has data
1914                  * waiting on its socket *and* is in the CON_IDLE state.  Any
1915                  * active sockets other than our chosen one are marked as
1916                  * CON_READY so the next thread that comes around can just bind
1917                  * to one without having to select() again.
1918                  */
1919                 begin_critical_section(S_SESSION_TABLE);
1920                 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1921                         if ( (FD_ISSET(ptr->client_socket, &readfds))
1922                            && (ptr->state != CON_EXECUTING) ) {
1923                                 ptr->input_waiting = 1;
1924                                 if (!bind_me) {
1925                                         bind_me = ptr;  /* I choose you! */
1926                                         bind_me->state = CON_EXECUTING;
1927                                 }
1928                                 else {
1929                                         ptr->state = CON_READY;
1930                                 }
1931                         }
1932                 }
1933                 end_critical_section(S_SESSION_TABLE);
1934
1935 SKIP_SELECT:
1936                 /* We're bound to a session */
1937                 if (bind_me != NULL) {
1938                         become_session(bind_me);
1939
1940                         /* If the client has sent a command, execute it. */
1941                         if (CC->input_waiting) {
1942                                 CC->h_command_function();
1943                                 CC->input_waiting = 0;
1944                         }
1945
1946                         /* If there are asynchronous messages waiting and the
1947                          * client supports it, do those now */
1948                         if ((CC->is_async) && (CC->async_waiting)
1949                            && (CC->h_async_function != NULL)) {
1950                                 CC->h_async_function();
1951                                 CC->async_waiting = 0;
1952                         }
1953                         
1954                         force_purge = CC->kill_me;
1955                         become_session(NULL);
1956                         bind_me->state = CON_IDLE;
1957                 }
1958
1959                 dead_session_purge(force_purge);
1960                 do_housekeeping();
1961                 check_sched_shutdown();
1962         }
1963         /* If control reaches this point, the server is shutting down */        
1964         return(NULL);
1965 }
1966
1967
1968
1969
1970 /*
1971  * SyslogFacility()
1972  * Translate text facility name to syslog.h defined value.
1973  */
1974 int SyslogFacility(char *name)
1975 {
1976         int i;
1977         struct
1978         {
1979                 int facility;
1980                 char *name;
1981         }   facTbl[] =
1982         {
1983                 {   LOG_KERN,   "kern"          },
1984                 {   LOG_USER,   "user"          },
1985                 {   LOG_MAIL,   "mail"          },
1986                 {   LOG_DAEMON, "daemon"        },
1987                 {   LOG_AUTH,   "auth"          },
1988                 {   LOG_SYSLOG, "syslog"        },
1989                 {   LOG_LPR,    "lpr"           },
1990                 {   LOG_NEWS,   "news"          },
1991                 {   LOG_UUCP,   "uucp"          },
1992                 {   LOG_LOCAL0, "local0"        },
1993                 {   LOG_LOCAL1, "local1"        },
1994                 {   LOG_LOCAL2, "local2"        },
1995                 {   LOG_LOCAL3, "local3"        },
1996                 {   LOG_LOCAL4, "local4"        },
1997                 {   LOG_LOCAL5, "local5"        },
1998                 {   LOG_LOCAL6, "local6"        },
1999                 {   LOG_LOCAL7, "local7"        },
2000                 {   0,            NULL          }
2001         };
2002         for(i = 0; facTbl[i].name != NULL; i++) {
2003                 if(!strcasecmp(name, facTbl[i].name))
2004                         return facTbl[i].facility;
2005         }
2006         enable_syslog = 0;
2007         return LOG_DAEMON;
2008 }
2009
2010
2011 /********** MEM CHEQQER ***********/
2012
2013 #ifdef DEBUG_MEMORY_LEAKS
2014
2015 #undef malloc
2016 #undef realloc
2017 #undef strdup
2018 #undef free
2019
2020 void *tracked_malloc(size_t size, char *file, int line) {
2021         struct igheap *thisheap;
2022         void *block;
2023
2024         block = malloc(size);
2025         if (block == NULL) return(block);
2026
2027         thisheap = malloc(sizeof(struct igheap));
2028         if (thisheap == NULL) {
2029                 free(block);
2030                 return(NULL);
2031         }
2032
2033         thisheap->block = block;
2034         strcpy(thisheap->file, file);
2035         thisheap->line = line;
2036         
2037         begin_critical_section(S_DEBUGMEMLEAKS);
2038         thisheap->next = igheap;
2039         igheap = thisheap;
2040         end_critical_section(S_DEBUGMEMLEAKS);
2041
2042         return(block);
2043 }
2044
2045
2046 void *tracked_realloc(void *ptr, size_t size, char *file, int line) {
2047         struct igheap *thisheap;
2048         void *block;
2049
2050         block = realloc(ptr, size);
2051         if (block == NULL) return(block);
2052
2053         thisheap = malloc(sizeof(struct igheap));
2054         if (thisheap == NULL) {
2055                 free(block);
2056                 return(NULL);
2057         }
2058
2059         thisheap->block = block;
2060         strcpy(thisheap->file, file);
2061         thisheap->line = line;
2062         
2063         begin_critical_section(S_DEBUGMEMLEAKS);
2064         thisheap->next = igheap;
2065         igheap = thisheap;
2066         end_critical_section(S_DEBUGMEMLEAKS);
2067
2068         return(block);
2069 }
2070
2071
2072
2073 void tracked_free(void *ptr) {
2074         struct igheap *thisheap;
2075         struct igheap *trash;
2076
2077         free(ptr);
2078
2079         if (igheap == NULL) return;
2080         begin_critical_section(S_DEBUGMEMLEAKS);
2081         for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
2082                 if (thisheap->next != NULL) {
2083                         if (thisheap->next->block == ptr) {
2084                                 trash = thisheap->next;
2085                                 thisheap->next = thisheap->next->next;
2086                                 free(trash);
2087                         }
2088                 }
2089         }
2090         if (igheap->block == ptr) {
2091                 trash = igheap;
2092                 igheap = igheap->next;
2093                 free(trash);
2094         }
2095         end_critical_section(S_DEBUGMEMLEAKS);
2096 }
2097
2098 char *tracked_strdup(const char *s, char *file, int line) {
2099         char *ptr;
2100
2101         if (s == NULL) return(NULL);
2102         ptr = tracked_malloc(strlen(s) + 1, file, line);
2103         if (ptr == NULL) return(NULL);
2104         strncpy(ptr, s, strlen(s));
2105         return(ptr);
2106 }
2107
2108 void dump_heap(void) {
2109         struct igheap *thisheap;
2110
2111         for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
2112                 CtdlLogPrintf(CTDL_CRIT, "UNFREED: %30s : %d\n",
2113                         thisheap->file, thisheap->line);
2114         }
2115 }
2116
2117 #endif /*  DEBUG_MEMORY_LEAKS */