That didn't work.
[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 #include <libcitadel.h>
53 #include "citadel.h"
54 #include "server.h"
55 #include "sysdep_decls.h"
56 #include "citserver.h"
57 #include "support.h"
58 #include "config.h"
59 #include "database.h"
60 #include "housekeeping.h"
61 #include "modules/crypto/serv_crypto.h" /* Needed for init_ssl, client_write_ssl, client_read_ssl, destruct_ssl */
62 #include "ecrash.h"
63
64 #ifdef HAVE_SYS_SELECT_H
65 #include <sys/select.h>
66 #endif
67
68 #ifndef HAVE_SNPRINTF
69 #include "snprintf.h"
70 #endif
71
72 #include "ctdl_module.h"
73 #include "threads.h"
74 #include "user_ops.h"
75 #include "control.h"
76
77
78 #ifdef DEBUG_MEMORY_LEAKS
79 struct igheap {
80         struct igheap *next;
81         char file[32];
82         int line;
83         void *block;
84 };
85
86 struct igheap *igheap = NULL;
87 #endif
88
89
90 citthread_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 int num_sessions = 0;                           /* Current number of sessions */
97
98 int syslog_facility = LOG_DAEMON;
99 int enable_syslog = 0;
100
101
102 /* Flag for single user mode */
103 static int want_single_user = 0;
104
105 /* Try to go single user */
106
107 int CtdlTrySingleUser(void)
108 {
109         int can_do = 0;
110         
111         begin_critical_section(S_SINGLE_USER);
112         if (want_single_user)
113                 can_do = 0;
114         else
115         {
116                 can_do = 1;
117                 want_single_user = 1;
118         }
119         end_critical_section(S_SINGLE_USER);
120         return can_do;
121 }
122
123 void CtdlEndSingleUser(void)
124 {
125         begin_critical_section(S_SINGLE_USER);
126         want_single_user = 0;
127         end_critical_section(S_SINGLE_USER);
128 }
129
130
131 int CtdlWantSingleUser(void)
132 {
133         return want_single_user;
134 }
135
136 int CtdlIsSingleUser(void)
137 {
138         if (want_single_user)
139         {
140                 /* check for only one context here */
141                 if (num_sessions == 1)
142                         return TRUE;
143         }
144         return FALSE;
145 }
146
147
148 /*
149  * CtdlLogPrintf()  ...   Write logging information
150  */
151 void CtdlLogPrintf(enum LogLevel loglevel, const char *format, ...) {   
152         va_list arg_ptr;
153         va_start(arg_ptr, format);
154         vCtdlLogPrintf(loglevel, format, arg_ptr);
155         va_end(arg_ptr);
156 }
157
158 void vCtdlLogPrintf(enum LogLevel loglevel, const char *format, va_list arg_ptr)
159 {
160         char buf[SIZ], buf2[SIZ];
161
162         if (enable_syslog) {
163                 vsyslog((syslog_facility | loglevel), format, arg_ptr);
164         }
165
166         /* stderr output code */
167         if (enable_syslog || running_as_daemon) return;
168
169         /* if we run in forground and syslog is disabled, log to terminal */
170         if (loglevel <= verbosity) { 
171                 struct timeval tv;
172                 struct tm tim;
173                 time_t unixtime;
174
175                 gettimeofday(&tv, NULL);
176                 /* Promote to time_t; types differ on some OSes (like darwin) */
177                 unixtime = tv.tv_sec;
178                 localtime_r(&unixtime, &tim);
179                 if (CC->cs_pid != 0) {
180                         sprintf(buf,
181                                 "%04d/%02d/%02d %2d:%02d:%02d.%06ld [%3d] ",
182                                 tim.tm_year + 1900, tim.tm_mon + 1,
183                                 tim.tm_mday, tim.tm_hour, tim.tm_min,
184                                 tim.tm_sec, (long)tv.tv_usec,
185                                 CC->cs_pid);
186                 } else {
187                         sprintf(buf,
188                                 "%04d/%02d/%02d %2d:%02d:%02d.%06ld ",
189                                 tim.tm_year + 1900, tim.tm_mon + 1,
190                                 tim.tm_mday, tim.tm_hour, tim.tm_min,
191                                 tim.tm_sec, (long)tv.tv_usec);
192                 }
193                 vsnprintf(buf2, SIZ, format, arg_ptr);   
194
195                 fprintf(stderr, "%s%s", buf, buf2);
196                 fflush(stderr);
197         }
198 }   
199
200
201
202 /*
203  * Signal handler to shut down the server.
204  */
205
206 volatile int exit_signal = 0;
207 volatile int shutdown_and_halt = 0;
208 volatile int restart_server = 0;
209 volatile int running_as_daemon = 0;
210
211 static RETSIGTYPE signal_cleanup(int signum) {
212 #ifdef THREADS_USESIGNALS
213         if (CT)
214                 CT->signal = signum;
215         else
216 #endif
217         {
218                 CtdlLogPrintf(CTDL_DEBUG, "Caught signal %d; shutting down.\n", signum);
219                 exit_signal = signum;
220         }
221 }
222
223
224
225 /*
226  * Some initialization stuff...
227  */
228 void init_sysdep(void) {
229         sigset_t set;
230
231         /* Avoid vulnerabilities related to FD_SETSIZE if we can. */
232 #ifdef FD_SETSIZE
233 #ifdef RLIMIT_NOFILE
234         struct rlimit rl;
235         getrlimit(RLIMIT_NOFILE, &rl);
236         rl.rlim_cur = FD_SETSIZE;
237         rl.rlim_max = FD_SETSIZE;
238         setrlimit(RLIMIT_NOFILE, &rl);
239 #endif
240 #endif
241
242         /* If we've got OpenSSL, we're going to use it. */
243 #ifdef HAVE_OPENSSL
244         init_ssl();
245 #endif
246
247         /*
248          * Set up a place to put thread-specific data.
249          * We only need a single pointer per thread - it points to the
250          * CitContext structure (in the ContextList linked list) of the
251          * session to which the calling thread is currently bound.
252          */
253         if (citthread_key_create(&MyConKey, NULL) != 0) {
254                 CtdlLogPrintf(CTDL_CRIT, "Can't create TSD key: %s\n",
255                         strerror(errno));
256         }
257
258         /*
259          * The action for unexpected signals and exceptions should be to
260          * call signal_cleanup() to gracefully shut down the server.
261          */
262         sigemptyset(&set);
263         sigaddset(&set, SIGINT);
264         sigaddset(&set, SIGQUIT);
265         sigaddset(&set, SIGHUP);
266         sigaddset(&set, SIGTERM);
267         // sigaddset(&set, SIGSEGV);    commented out because
268         // sigaddset(&set, SIGILL);     we want core dumps
269         // sigaddset(&set, SIGBUS);
270         sigprocmask(SIG_UNBLOCK, &set, NULL);
271
272         signal(SIGINT, signal_cleanup);
273         signal(SIGQUIT, signal_cleanup);
274         signal(SIGHUP, signal_cleanup);
275         signal(SIGTERM, signal_cleanup);
276         // signal(SIGSEGV, signal_cleanup);     commented out because
277         // signal(SIGILL, signal_cleanup);      we want core dumps
278         // signal(SIGBUS, signal_cleanup);
279
280         /*
281          * Do not shut down the server on broken pipe signals, otherwise the
282          * whole Citadel service would come down whenever a single client
283          * socket breaks.
284          */
285         signal(SIGPIPE, SIG_IGN);
286 }
287
288
289
290
291 /*
292  * This is a generic function to set up a master socket for listening on
293  * a TCP port.  The server shuts down if the bind fails.
294  *
295  */
296 int ig_tcp_server(char *ip_addr, int port_number, int queue_len, char **errormessage)
297 {
298         struct sockaddr_in sin;
299         int s, i;
300         int actual_queue_len;
301
302         actual_queue_len = queue_len;
303         if (actual_queue_len < 5) actual_queue_len = 5;
304
305         memset(&sin, 0, sizeof(sin));
306         sin.sin_family = AF_INET;
307         sin.sin_port = htons((u_short)port_number);
308         if (ip_addr == NULL) {
309                 sin.sin_addr.s_addr = INADDR_ANY;
310         }
311         else {
312                 sin.sin_addr.s_addr = inet_addr(ip_addr);
313         }
314                                                                                 
315         if (sin.sin_addr.s_addr == !INADDR_ANY) {
316                 sin.sin_addr.s_addr = INADDR_ANY;
317         }
318
319         s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
320
321         if (s < 0) {
322                 *errormessage = (char*) malloc(SIZ + 1);
323                 snprintf(*errormessage, SIZ, 
324                                  "citserver: Can't create a socket: %s",
325                                  strerror(errno));
326                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
327                 return(-1);
328         }
329
330         i = 1;
331         setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
332
333         if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
334                 *errormessage = (char*) malloc(SIZ + 1);
335                 snprintf(*errormessage, SIZ, 
336                                  "citserver: Can't bind: %s",
337                                  strerror(errno));
338                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
339                 close(s);
340                 return(-1);
341         }
342
343         /* set to nonblock - we need this for some obscure situations */
344         if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
345                 *errormessage = (char*) malloc(SIZ + 1);
346                 snprintf(*errormessage, SIZ, 
347                                  "citserver: Can't set socket to non-blocking: %s",
348                                  strerror(errno));
349                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
350                 close(s);
351                 return(-1);
352         }
353
354         if (listen(s, actual_queue_len) < 0) {
355                 *errormessage = (char*) malloc(SIZ + 1);
356                 snprintf(*errormessage, SIZ, 
357                                  "citserver: Can't listen: %s",
358                                  strerror(errno));
359                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
360                 close(s);
361                 return(-1);
362         }
363
364         return(s);
365 }
366
367
368
369 /*
370  * Create a Unix domain socket and listen on it
371  */
372 int ig_uds_server(char *sockpath, int queue_len, char **errormessage)
373 {
374         struct sockaddr_un addr;
375         int s;
376         int i;
377         int actual_queue_len;
378
379         actual_queue_len = queue_len;
380         if (actual_queue_len < 5) actual_queue_len = 5;
381
382         i = unlink(sockpath);
383         if ((i != 0) && (errno != ENOENT)) {
384                 *errormessage = (char*) malloc(SIZ + 1);
385                 snprintf(*errormessage, SIZ, "citserver: can't unlink %s: %s",
386                         sockpath, strerror(errno));
387                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
388                 return(-1);
389         }
390
391         memset(&addr, 0, sizeof(addr));
392         addr.sun_family = AF_UNIX;
393         safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
394
395         s = socket(AF_UNIX, SOCK_STREAM, 0);
396         if (s < 0) {
397                 *errormessage = (char*) malloc(SIZ + 1);
398                 snprintf(*errormessage, SIZ, 
399                          "citserver: Can't create a socket: %s",
400                          strerror(errno));
401                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
402                 return(-1);
403         }
404
405         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
406                 *errormessage = (char*) malloc(SIZ + 1);
407                 snprintf(*errormessage, SIZ, 
408                          "citserver: Can't bind: %s",
409                          strerror(errno));
410                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
411                 return(-1);
412         }
413
414         /* set to nonblock - we need this for some obscure situations */
415         if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
416                 *errormessage = (char*) malloc(SIZ + 1);
417                 snprintf(*errormessage, SIZ, 
418                          "citserver: Can't set socket to non-blocking: %s",
419                          strerror(errno));
420                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
421                 close(s);
422                 return(-1);
423         }
424
425         if (listen(s, actual_queue_len) < 0) {
426                 *errormessage = (char*) malloc(SIZ + 1);
427                 snprintf(*errormessage, SIZ, 
428                          "citserver: Can't listen: %s",
429                          strerror(errno));
430                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
431                 return(-1);
432         }
433
434         chmod(sockpath, S_ISGID|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
435         return(s);
436 }
437
438
439
440 /*
441  * Return a pointer to the CitContext structure bound to the thread which
442  * called this function.  If there's no such binding (for example, if it's
443  * called by the housekeeper thread) then a generic 'master' CC is returned.
444  *
445  * This function is used *VERY* frequently and must be kept small.
446  */
447 struct CitContext *MyContext(void) {
448
449         register struct CitContext *c;
450
451         return ((c = (struct CitContext *) citthread_getspecific(MyConKey),
452                 c == NULL) ? &masterCC : c
453         );
454 }
455
456
457 /*
458  * Initialize a new context and place it in the list.  The session number
459  * used to be the PID (which is why it's called cs_pid), but that was when we
460  * had one process per session.  Now we just assign them sequentially, starting
461  * at 1 (don't change it to 0 because masterCC uses 0).
462  */
463 struct CitContext *CreateNewContext(void) {
464         struct CitContext *me;
465         static int next_pid = 0;
466
467         me = (struct CitContext *) malloc(sizeof(struct CitContext));
468         if (me == NULL) {
469                 CtdlLogPrintf(CTDL_ALERT, "citserver: can't allocate memory!!\n");
470                 return NULL;
471         }
472
473         memset(me, 0, sizeof(struct CitContext));
474         
475         /* Give the contaxt a name. Hopefully makes it easier to track */
476         strcpy (me->user.fullname, "SYS_notauth");
477         
478         /* The new context will be created already in the CON_EXECUTING state
479          * in order to prevent another thread from grabbing it while it's
480          * being set up.
481          */
482         me->state = CON_EXECUTING;
483         /*
484          * Generate a unique session number and insert this context into
485          * the list.
486          */
487         begin_critical_section(S_SESSION_TABLE);
488         me->cs_pid = ++next_pid;
489         me->prev = NULL;
490         me->next = ContextList;
491         ContextList = me;
492         if (me->next != NULL) {
493                 me->next->prev = me;
494         }
495         ++num_sessions;
496         end_critical_section(S_SESSION_TABLE);
497         return (me);
498 }
499
500
501 struct CitContext *CtdlGetContextArray(int *count)
502 {
503         int nContexts, i;
504         struct CitContext *nptr, *cptr;
505         
506         nContexts = num_sessions;
507         nptr = malloc(sizeof(struct CitContext) * nContexts);
508         if (!nptr)
509                 return NULL;
510         begin_critical_section(S_SESSION_TABLE);
511         for (cptr = ContextList, i=0; cptr != NULL && i < nContexts; cptr = cptr->next, i++)
512                 memcpy(&nptr[i], cptr, sizeof (struct CitContext));
513         end_critical_section (S_SESSION_TABLE);
514         
515         *count = i;
516         return nptr;
517 }
518
519
520
521 /**
522  * This function fills in a context and its user field correctly
523  * Then creates/loads that user
524  */
525 void CtdlFillSystemContext(struct CitContext *context, char *name)
526 {
527         char sysname[USERNAME_SIZE];
528
529         memset(context, 0, sizeof(struct CitContext));
530         context->internal_pgm = 1;
531         context->cs_pid = 0;
532         strcpy (sysname, "SYS_");
533         strcat (sysname, name);
534         /* internal_create_user has the side effect of loading the user regardless of wether they
535          * already existed or needed to be created
536          */
537         internal_create_user (sysname, &(context->user), -1) ;
538         
539         /* Check to see if the system user needs upgrading */
540         if (context->user.usernum == 0)
541         {       /* old system user with number 0, upgrade it */
542                 context->user.usernum = get_new_user_number();
543                 CtdlLogPrintf(CTDL_DEBUG, "Upgrading system user \"%s\" from user number 0 to user number %d\n", context->user.fullname, context->user.usernum);
544                 /* add user to the database */
545                 putuser(&(context->user));
546                 cdb_store(CDB_USERSBYNUMBER, &(context->user.usernum), sizeof(long), context->user.fullname, strlen(context->user.fullname)+1);
547         }
548 }
549
550 /*
551  * The following functions implement output buffering. If the kernel supplies
552  * native TCP buffering (Linux & *BSD), use that; otherwise, emulate it with
553  * user-space buffering.
554  */
555 #ifndef HAVE_DARWIN
556 #ifdef TCP_CORK
557 #       define HAVE_TCP_BUFFERING
558 #else
559 #       ifdef TCP_NOPUSH
560 #               define HAVE_TCP_BUFFERING
561 #               define TCP_CORK TCP_NOPUSH
562 #       endif
563 #endif /* TCP_CORK */
564 #endif /* HAVE_DARWIN */
565
566 #ifdef HAVE_TCP_BUFFERING
567 static unsigned on = 1, off = 0;
568 void buffer_output(void) {
569         struct CitContext *ctx = MyContext();
570         setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
571         ctx->buffering = 1;
572 }
573
574 void unbuffer_output(void) {
575         struct CitContext *ctx = MyContext();
576         setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
577         ctx->buffering = 0;
578 }
579
580 void flush_output(void) {
581         struct CitContext *ctx = MyContext();
582         setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
583         setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
584 }
585 #else 
586 #ifdef HAVE_DARWIN
587 /* Stub functions for Darwin/OS X where TCP buffering isn't liked at all */
588 void buffer_output(void) {
589         CC->buffering = 0;
590 }
591 void unbuffer_output(void) {
592         CC->buffering = 0;
593 }
594 void flush_output(void) {
595 }
596 #else
597 void buffer_output(void) {
598         if (CC->buffering == 0) {
599                 CC->buffering = 1;
600                 CC->buffer_len = 0;
601                 CC->output_buffer = malloc(SIZ);
602         }
603 }
604
605 void flush_output(void) {
606         if (CC->buffering == 1) {
607                 client_write(CC->output_buffer, CC->buffer_len);
608                 CC->buffer_len = 0;
609         }
610 }
611
612 void unbuffer_output(void) {
613         if (CC->buffering == 1) {
614                 CC->buffering = 0;
615                 /* We don't call flush_output because we can't. */
616                 client_write(CC->output_buffer, CC->buffer_len);
617                 CC->buffer_len = 0;
618                 free(CC->output_buffer);
619                 CC->output_buffer = NULL;
620         }
621 }
622 #endif /* HAVE_DARWIN */
623 #endif /* HAVE_TCP_BUFFERING */
624
625
626
627 /*
628  * client_write()   ...    Send binary data to the client.
629  */
630 int client_write(char *buf, int nbytes)
631 {
632         int bytes_written = 0;
633         int retval;
634 #ifndef HAVE_TCP_BUFFERING
635         int old_buffer_len = 0;
636 #endif
637         fd_set wset;
638         t_context *Ctx;
639         int fdflags;
640
641         Ctx = CC;
642         if (Ctx->redirect_buffer != NULL) {
643                 if ((Ctx->redirect_len + nbytes + 2) >= Ctx->redirect_alloc) {
644                         Ctx->redirect_alloc = (Ctx->redirect_alloc * 2) + nbytes;
645                         Ctx->redirect_buffer = realloc(Ctx->redirect_buffer,
646                                                 Ctx->redirect_alloc);
647                 }
648                 memcpy(&Ctx->redirect_buffer[Ctx->redirect_len], buf, nbytes);
649                 Ctx->redirect_len += nbytes;
650                 Ctx->redirect_buffer[Ctx->redirect_len] = 0;
651                 return 0;
652         }
653
654 #ifndef HAVE_TCP_BUFFERING
655         /* If we're buffering for later, do that now. */
656         if (Ctx->buffering) {
657                 old_buffer_len = Ctx->buffer_len;
658                 Ctx->buffer_len += nbytes;
659                 Ctx->output_buffer = realloc(Ctx->output_buffer, Ctx->buffer_len);
660                 memcpy(&Ctx->output_buffer[old_buffer_len], buf, nbytes);
661                 return 0;
662         }
663 #endif
664
665         /* Ok, at this point we're not buffering.  Go ahead and write. */
666
667 #ifdef HAVE_OPENSSL
668         if (Ctx->redirect_ssl) {
669                 client_write_ssl(buf, nbytes);
670                 return 0;
671         }
672 #endif
673
674         fdflags = fcntl(Ctx->client_socket, F_GETFL);
675
676         while (bytes_written < nbytes) {
677                 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
678                         FD_ZERO(&wset);
679                         FD_SET(Ctx->client_socket, &wset);
680                         if (select(1, NULL, &wset, NULL, NULL) == -1) {
681                                 CtdlLogPrintf(CTDL_ERR,
682                                         "client_write(%d bytes) select failed: %s (%d)\n",
683                                         nbytes - bytes_written,
684                                         strerror(errno), errno);
685                                 cit_backtrace();
686                                 Ctx->kill_me = 1;
687                                 return -1;
688                         }
689                 }
690
691                 retval = write(Ctx->client_socket, &buf[bytes_written],
692                         nbytes - bytes_written);
693                 if (retval < 1) {
694                         CtdlLogPrintf(CTDL_ERR,
695                                 "client_write(%d bytes) failed: %s (%d)\n",
696                                 nbytes - bytes_written,
697                                 strerror(errno), errno);
698                         cit_backtrace();
699                         // CtdlLogPrintf(CTDL_DEBUG, "Tried to send: %s",  &buf[bytes_written]);
700                         Ctx->kill_me = 1;
701                         return -1;
702                 }
703                 bytes_written = bytes_written + retval;
704         }
705         return 0;
706 }
707
708
709 /*
710  * cprintf()    Send formatted printable data to the client.
711  *              Implemented in terms of client_write() so it's technically not sysdep...
712  */
713 void cprintf(const char *format, ...) {   
714         va_list arg_ptr;   
715         char buf[1024];
716    
717         va_start(arg_ptr, format);   
718         if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
719                 buf[sizeof buf - 2] = '\n';
720         client_write(buf, strlen(buf)); 
721         va_end(arg_ptr);
722 }   
723
724
725 /*
726  * Read data from the client socket.
727  * Return values are:
728  *      1       Requested number of bytes has been read.
729  *      0       Request timed out.
730  *      -1      The socket is broken.
731  * If the socket breaks, the session will be terminated.
732  */
733 int client_read_to(char *buf, int bytes, int timeout)
734 {
735         int len,rlen;
736         fd_set rfds;
737         int fd;
738         struct timeval tv;
739         int retval;
740
741 #ifdef HAVE_OPENSSL
742         if (CC->redirect_ssl) {
743                 return (client_read_ssl(buf, bytes, timeout));
744         }
745 #endif
746         len = 0;
747         fd = CC->client_socket;
748         while(len<bytes) {
749                 FD_ZERO(&rfds);
750                 FD_SET(fd, &rfds);
751                 tv.tv_sec = timeout;
752                 tv.tv_usec = 0;
753
754                 retval = select( (fd)+1, 
755                                  &rfds, NULL, NULL, &tv);
756                 if (retval < 0)
757                 {
758                         if (errno == EINTR)
759                         {
760                                 CtdlLogPrintf(CTDL_DEBUG, "Interrupted select().\n");
761                                 CC->kill_me = 1;
762                                 return (-1);
763                         }
764                 }
765
766                 if (FD_ISSET(fd, &rfds) == 0) {
767                         return(0);
768                 }
769
770                 rlen = read(fd, &buf[len], bytes-len);
771                 if (rlen<1) {
772                         /* The socket has been disconnected! */
773                         CC->kill_me = 1;
774                         return(-1);
775                 }
776                 len = len + rlen;
777         }
778         return(1);
779 }
780
781 /*
782  * Read data from the client socket with default timeout.
783  * (This is implemented in terms of client_read_to() and could be
784  * justifiably moved out of sysdep.c)
785  */
786 INLINE int client_read(char *buf, int bytes)
787 {
788         return(client_read_to(buf, bytes, config.c_sleeping));
789 }
790
791
792 /*
793  * client_getln()   ...   Get a LF-terminated line of text from the client.
794  * (This is implemented in terms of client_read() and could be
795  * justifiably moved out of sysdep.c)
796  */
797 int client_getln(char *buf, int bufsize)
798 {
799         int i, retval;
800
801         /* Read one character at a time.
802          */
803         for (i = 0;;i++) {
804                 retval = client_read(&buf[i], 1);
805                 if (retval != 1 || buf[i] == '\n' || i == (bufsize-1))
806                         break;
807         }
808
809         /* If we got a long line, discard characters until the newline.
810          */
811         if (i == (bufsize-1))
812                 while (buf[i] != '\n' && retval == 1)
813                         retval = client_read(&buf[i], 1);
814
815         /* Strip the trailing LF, and the trailing CR if present.
816          */
817         buf[i] = 0;
818         while ( (i > 0)
819                 && ( (buf[i - 1]==13)
820                      || ( buf[i - 1]==10)) ) {
821                 i--;
822                 buf[i] = 0;
823         }
824         if (retval < 0) safestrncpy(&buf[i], "000", bufsize - i);
825         return(retval);
826 }
827
828
829 /*
830  * Cleanup any contexts that are left lying around
831  */
832 void context_cleanup(void)
833 {
834         struct CitContext *ptr = NULL;
835         struct CitContext *rem = NULL;
836
837         /*
838          * Clean up the contexts.
839          * There are no threads so no critical_section stuff is needed.
840          */
841         ptr = ContextList;
842         
843         /* We need to update the ContextList because some modules may want to itterate it
844          * Question is should we NULL it before iterating here or should we just keep updating it
845          * as we remove items?
846          *
847          * Answer is to NULL it first to prevent modules from doing any actions on the list at all
848          */
849         ContextList=NULL;
850         while (ptr != NULL){
851                 /* Remove the session from the active list */
852                 rem = ptr->next;
853                 --num_sessions;
854                 
855                 CtdlLogPrintf(CTDL_DEBUG, "Purging session %d\n", ptr->cs_pid);
856                 RemoveContext(ptr);
857                 free (ptr);
858                 ptr = rem;
859         }
860 }
861
862
863
864 void close_masters (void)
865 {
866         struct ServiceFunctionHook *serviceptr;
867         
868         /*
869          * close all protocol master sockets
870          */
871         for (serviceptr = ServiceHookTable; serviceptr != NULL;
872             serviceptr = serviceptr->next ) {
873
874                 if (serviceptr->tcp_port > 0)
875                 {
876                         CtdlLogPrintf(CTDL_INFO, "Closing listener on port %d\n",
877                                 serviceptr->tcp_port);
878                         serviceptr->tcp_port = 0;
879                 }
880                 
881                 if (serviceptr->sockpath != NULL)
882                         CtdlLogPrintf(CTDL_INFO, "Closing listener on '%s'\n",
883                                 serviceptr->sockpath);
884
885                 close(serviceptr->msock);
886                 /* If it's a Unix domain socket, remove the file. */
887                 if (serviceptr->sockpath != NULL) {
888                         unlink(serviceptr->sockpath);
889                         serviceptr->sockpath = NULL;
890                 }
891         }
892 }
893
894
895 /*
896  * The system-dependent part of master_cleanup() - close the master socket.
897  */
898 void sysdep_master_cleanup(void) {
899         
900         close_masters();
901         
902         context_cleanup();
903         
904 #ifdef HAVE_OPENSSL
905         destruct_ssl();
906 #endif
907         CtdlDestroyProtoHooks();
908         CtdlDestroyDeleteHooks();
909         CtdlDestroyXmsgHooks();
910         CtdlDestroyNetprocHooks();
911         CtdlDestroyUserHooks();
912         CtdlDestroyMessageHook();
913         CtdlDestroyCleanupHooks();
914         CtdlDestroyFixedOutputHooks();  
915         CtdlDestroySessionHooks();
916         CtdlDestroyServiceHook();
917         CtdlDestroyRoomHooks();
918         CtdlDestroyDirectoryServiceFuncs();
919         #ifdef HAVE_BACKTRACE
920         eCrash_Uninit();
921         #endif
922 }
923
924
925
926 /*
927  * Terminate another session.
928  * (This could justifiably be moved out of sysdep.c because it
929  * no longer does anything that is system-dependent.)
930  */
931 void kill_session(int session_to_kill) {
932         struct CitContext *ptr;
933
934         begin_critical_section(S_SESSION_TABLE);
935         for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
936                 if (ptr->cs_pid == session_to_kill) {
937                         ptr->kill_me = 1;
938                 }
939         }
940         end_critical_section(S_SESSION_TABLE);
941 }
942
943 pid_t current_child;
944 void graceful_shutdown(int signum) {
945         kill(current_child, signum);
946         unlink(file_pid_file);
947         exit(0);
948 }
949
950
951 /*
952  * Start running as a daemon.
953  */
954 void start_daemon(int unused) {
955         int status = 0;
956         pid_t child = 0;
957         FILE *fp;
958         int do_restart = 0;
959
960         current_child = 0;
961
962         /* Close stdin/stdout/stderr and replace them with /dev/null.
963          * We don't just call close() because we don't want these fd's
964          * to be reused for other files.
965          */
966         chdir(ctdl_run_dir);
967
968         child = fork();
969         if (child != 0) {
970                 exit(0);
971         }
972         
973         signal(SIGHUP, SIG_IGN);
974         signal(SIGINT, SIG_IGN);
975         signal(SIGQUIT, SIG_IGN);
976
977         setsid();
978         umask(0);
979         freopen("/dev/null", "r", stdin);
980         freopen("/dev/null", "w", stdout);
981         freopen("/dev/null", "w", stderr);
982
983         do {
984                 current_child = fork();
985
986                 signal(SIGTERM, graceful_shutdown);
987         
988                 if (current_child < 0) {
989                         perror("fork");
990                         exit(errno);
991                 }
992         
993                 else if (current_child == 0) {
994                         return; /* continue starting citadel. */
995                 }
996         
997                 else {
998                         fp = fopen(file_pid_file, "w");
999                         if (fp != NULL) {
1000                                 fprintf(fp, ""F_PID_T"\n", getpid());
1001                                 fclose(fp);
1002                         }
1003                         waitpid(current_child, &status, 0);
1004                 }
1005
1006                 do_restart = 0;
1007
1008                 /* Did the main process exit with an actual exit code? */
1009                 if (WIFEXITED(status)) {
1010
1011                         /* Exit code 0 means the watcher should exit */
1012                         if (WEXITSTATUS(status) == 0) {
1013                                 do_restart = 0;
1014                         }
1015
1016                         /* Exit code 101-109 means the watcher should exit */
1017                         else if ( (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109) ) {
1018                                 do_restart = 0;
1019                         }
1020
1021                         /* Any other exit code means we should restart. */
1022                         else {
1023                                 do_restart = 1;
1024                         }
1025                 }
1026
1027                 /* Any other type of termination (signals, etc.) should also restart. */
1028                 else {
1029                         do_restart = 1;
1030                 }
1031
1032         } while (do_restart);
1033
1034         unlink(file_pid_file);
1035         exit(WEXITSTATUS(status));
1036 }
1037
1038
1039
1040 /*
1041  * Generic routine to convert a login name to a full name (gecos)
1042  * Returns nonzero if a conversion took place
1043  */
1044 int convert_login(char NameToConvert[]) {
1045         struct passwd *pw;
1046         int a;
1047
1048         pw = getpwnam(NameToConvert);
1049         if (pw == NULL) {
1050                 return(0);
1051         }
1052         else {
1053                 strcpy(NameToConvert, pw->pw_gecos);
1054                 for (a=0; a<strlen(NameToConvert); ++a) {
1055                         if (NameToConvert[a] == ',') NameToConvert[a] = 0;
1056                 }
1057                 return(1);
1058         }
1059 }
1060
1061 /*
1062  * Purge all sessions which have the 'kill_me' flag set.
1063  * This function has code to prevent it from running more than once every
1064  * few seconds, because running it after every single unbind would waste a lot
1065  * of CPU time and keep the context list locked too much.  To force it to run
1066  * anyway, set "force" to nonzero.
1067  */
1068 void dead_session_purge(int force) {
1069         struct CitContext *ptr, *ptr2;          /* general-purpose utility pointer */
1070         struct CitContext *rem = NULL;  /* list of sessions to be destroyed */
1071         
1072         if (force == 0) {
1073                 if ( (time(NULL) - last_purge) < 5 ) {
1074                         return; /* Too soon, go away */
1075                 }
1076         }
1077         time(&last_purge);
1078
1079         if (try_critical_section(S_SESSION_TABLE))
1080                 return;
1081                 
1082         ptr = ContextList;
1083         while (ptr) {
1084                 ptr2 = ptr;
1085                 ptr = ptr->next;
1086                 
1087                 if ( (ptr2->state == CON_IDLE) && (ptr2->kill_me) ) {
1088                         /* Remove the session from the active list */
1089                         if (ptr2->prev) {
1090                                 ptr2->prev->next = ptr2->next;
1091                         }
1092                         else {
1093                                 ContextList = ptr2->next;
1094                         }
1095                         if (ptr2->next) {
1096                                 ptr2->next->prev = ptr2->prev;
1097                         }
1098
1099                         --num_sessions;
1100                         /* And put it on our to-be-destroyed list */
1101                         ptr2->next = rem;
1102                         rem = ptr2;
1103                 }
1104         }
1105         end_critical_section(S_SESSION_TABLE);
1106
1107         /* Now that we no longer have the session list locked, we can take
1108          * our time and destroy any sessions on the to-be-killed list, which
1109          * is allocated privately on this thread's stack.
1110          */
1111         while (rem != NULL) {
1112                 CtdlLogPrintf(CTDL_DEBUG, "Purging session %d\n", rem->cs_pid);
1113                 RemoveContext(rem);
1114                 ptr = rem;
1115                 rem = rem->next;
1116                 free(ptr);
1117         }
1118 }
1119
1120
1121
1122
1123
1124 /*
1125  * masterCC is the context we use when not attached to a session.  This
1126  * function initializes it.
1127  */
1128 void InitializeMasterCC(void) {
1129         memset(&masterCC, 0, sizeof(struct CitContext));
1130         masterCC.internal_pgm = 1;
1131         masterCC.cs_pid = 0;
1132 }
1133
1134
1135
1136
1137 /*
1138  * Bind a thread to a context.  (It's inline merely to speed things up.)
1139  */
1140 INLINE void become_session(struct CitContext *which_con) {
1141         citthread_setspecific(MyConKey, (void *)which_con );
1142 }
1143
1144
1145
1146 /* 
1147  * This loop just keeps going and going and going...
1148  */
1149 /*
1150  * FIXME:
1151  * This current implimentation of worker_thread creates a bottle neck in several situations
1152  * The first thing to remember is that a single thread can handle more than one connection at a time.
1153  * More threads mean less memory for the system to run in.
1154  * So for efficiency we want every thread to be doing something useful or waiting in the main loop for
1155  * something to happen anywhere.
1156  * This current implimentation requires worker threads to wait in other locations, after it has
1157  * been committed to a single connection which is very wasteful.
1158  * As an extreme case consider this:
1159  * A slow client connects and this slow client sends only one character each second.
1160  * With this current implimentation a single worker thread is dispatched to handle that connection
1161  * until such times as the client timeout expires, an error occurs on the socket or the client
1162  * completes its transmission.
1163  * THIS IS VERY BAD since that thread could have handled a read from many more clients in each one
1164  * second interval between chars.
1165  *
1166  * It is my intention to re-write this code and the associated client_getln, client_read functions
1167  * to allow any thread to read data on behalf of any connection (context).
1168  * To do this I intend to have this main loop read chars into a buffer stored in the context.
1169  * Once the correct criteria for a full buffer is met then we will dispatch a thread to 
1170  * process it.
1171  * This worker thread loop also needs to be able to handle binary data.
1172  */
1173  
1174 void *worker_thread(void *arg) {
1175         int i;
1176         int highest;
1177         struct CitContext *ptr;
1178         struct CitContext *bind_me = NULL;
1179         fd_set readfds;
1180         int retval = 0;
1181         struct CitContext *con= NULL;   /* Temporary context pointer */
1182         struct ServiceFunctionHook *serviceptr;
1183         int ssock;                      /* Descriptor for client socket */
1184         struct timeval tv;
1185         int force_purge = 0;
1186         int m;
1187         
1188
1189         while (!CtdlThreadCheckStop()) {
1190
1191                 /* make doubly sure we're not holding any stale db handles
1192                  * which might cause a deadlock.
1193                  */
1194                 cdb_check_handles();
1195 do_select:      force_purge = 0;
1196                 bind_me = NULL;         /* Which session shall we handle? */
1197
1198                 /* Initialize the fdset. */
1199                 FD_ZERO(&readfds);
1200                 highest = 0;
1201
1202                 begin_critical_section(S_SESSION_TABLE);
1203                 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1204                         if (ptr->state == CON_IDLE) {
1205                                 FD_SET(ptr->client_socket, &readfds);
1206                                 if (ptr->client_socket > highest)
1207                                         highest = ptr->client_socket;
1208                         }
1209                         if ((bind_me == NULL) && (ptr->state == CON_READY)) {
1210                                 bind_me = ptr;
1211                                 ptr->state = CON_EXECUTING;
1212                         }
1213                 }
1214                 end_critical_section(S_SESSION_TABLE);
1215
1216                 if (bind_me) {
1217                         goto SKIP_SELECT;
1218                 }
1219
1220                 /* If we got this far, it means that there are no sessions
1221                  * which a previous thread marked for attention, so we go
1222                  * ahead and get ready to select().
1223                  */
1224
1225                 /* First, add the various master sockets to the fdset. */
1226                 for (serviceptr = ServiceHookTable; serviceptr != NULL;
1227                 serviceptr = serviceptr->next ) {
1228                         m = serviceptr->msock;
1229                         FD_SET(m, &readfds);
1230                         if (m > highest) {
1231                                 highest = m;
1232                         }
1233                 }
1234
1235                 if (!CtdlThreadCheckStop()) {
1236                         tv.tv_sec = 1;          /* wake up every second if no input */
1237                         tv.tv_usec = 0;
1238                         retval = CtdlThreadSelect(highest + 1, &readfds, NULL, NULL, &tv);
1239                 }
1240                 else
1241                         return NULL;
1242
1243                 /* Now figure out who made this select() unblock.
1244                  * First, check for an error or exit condition.
1245                  */
1246                 if (retval < 0) {
1247                         if (errno == EBADF) {
1248                                 CtdlLogPrintf(CTDL_NOTICE, "select() failed: (%s)\n",
1249                                         strerror(errno));
1250                                 goto do_select;
1251                         }
1252                         if (errno != EINTR) {
1253                                 CtdlLogPrintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
1254                                 CtdlThreadStopAll();
1255                         } else {
1256                                 CtdlLogPrintf(CTDL_DEBUG, "Interrupted CtdlThreadSelect.\n");
1257                                 if (CtdlThreadCheckStop()) return(NULL);
1258                                 goto do_select;
1259                         }
1260                 }
1261                 else if(retval == 0) {
1262                         if (CtdlThreadCheckStop()) return(NULL);
1263                         goto SKIP_SELECT;
1264                 }
1265                 /* Next, check to see if it's a new client connecting
1266                  * on a master socket.
1267                  */
1268                 else for (serviceptr = ServiceHookTable; serviceptr != NULL;
1269                      serviceptr = serviceptr->next ) {
1270
1271                         if (FD_ISSET(serviceptr->msock, &readfds)) {
1272                                 ssock = accept(serviceptr->msock, NULL, 0);
1273                                 if (ssock >= 0) {
1274                                         CtdlLogPrintf(CTDL_DEBUG,
1275                                                 "New client socket %d\n",
1276                                                 ssock);
1277
1278                                         /* The master socket is non-blocking but the client
1279                                          * sockets need to be blocking, otherwise certain
1280                                          * operations barf on FreeBSD.  Not a fatal error.
1281                                          */
1282                                         if (fcntl(ssock, F_SETFL, 0) < 0) {
1283                                                 CtdlLogPrintf(CTDL_EMERG,
1284                                                         "citserver: Can't set socket to blocking: %s\n",
1285                                                         strerror(errno));
1286                                         }
1287
1288                                         /* New context will be created already
1289                                          * set up in the CON_EXECUTING state.
1290                                          */
1291                                         con = CreateNewContext();
1292
1293                                         /* Assign our new socket number to it. */
1294                                         con->client_socket = ssock;
1295                                         con->h_command_function =
1296                                                 serviceptr->h_command_function;
1297                                         con->h_async_function =
1298                                                 serviceptr->h_async_function;
1299                                         con->ServiceName =
1300                                                 serviceptr->ServiceName;
1301                                         
1302                                         /* Determine whether it's a local socket */
1303                                         if (serviceptr->sockpath != NULL)
1304                                                 con->is_local_socket = 1;
1305         
1306                                         /* Set the SO_REUSEADDR socket option */
1307                                         i = 1;
1308                                         setsockopt(ssock, SOL_SOCKET,
1309                                                 SO_REUSEADDR,
1310                                                 &i, sizeof(i));
1311
1312                                         become_session(con);
1313                                         begin_session(con);
1314                                         serviceptr->h_greeting_function();
1315                                         become_session(NULL);
1316                                         con->state = CON_IDLE;
1317                                         goto do_select;
1318                                 }
1319                         }
1320                 }
1321
1322                 /* It must be a client socket.  Find a context that has data
1323                  * waiting on its socket *and* is in the CON_IDLE state.  Any
1324                  * active sockets other than our chosen one are marked as
1325                  * CON_READY so the next thread that comes around can just bind
1326                  * to one without having to select() again.
1327                  */
1328                 begin_critical_section(S_SESSION_TABLE);
1329                 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1330                         if ( (FD_ISSET(ptr->client_socket, &readfds))
1331                            && (ptr->state != CON_EXECUTING) ) {
1332                                 ptr->input_waiting = 1;
1333                                 if (!bind_me) {
1334                                         bind_me = ptr;  /* I choose you! */
1335                                         bind_me->state = CON_EXECUTING;
1336                                 }
1337                                 else {
1338                                         ptr->state = CON_READY;
1339                                 }
1340                         }
1341                 }
1342                 end_critical_section(S_SESSION_TABLE);
1343
1344 SKIP_SELECT:
1345                 /* We're bound to a session */
1346                 if (bind_me != NULL) {
1347                         become_session(bind_me);
1348
1349                         /* If the client has sent a command, execute it. */
1350                         if (CC->input_waiting) {
1351                                 CC->h_command_function();
1352                                 CC->input_waiting = 0;
1353                         }
1354
1355                         /* If there are asynchronous messages waiting and the
1356                          * client supports it, do those now */
1357                         if ((CC->is_async) && (CC->async_waiting)
1358                            && (CC->h_async_function != NULL)) {
1359                                 CC->h_async_function();
1360                                 CC->async_waiting = 0;
1361                         }
1362                         
1363                         force_purge = CC->kill_me;
1364                         become_session(NULL);
1365                         bind_me->state = CON_IDLE;
1366                 }
1367
1368                 dead_session_purge(force_purge);
1369                 do_housekeeping();
1370         }
1371         /* If control reaches this point, the server is shutting down */        
1372         return(NULL);
1373 }
1374
1375
1376
1377
1378 /*
1379  * SyslogFacility()
1380  * Translate text facility name to syslog.h defined value.
1381  */
1382 int SyslogFacility(char *name)
1383 {
1384         int i;
1385         struct
1386         {
1387                 int facility;
1388                 char *name;
1389         }   facTbl[] =
1390         {
1391                 {   LOG_KERN,   "kern"          },
1392                 {   LOG_USER,   "user"          },
1393                 {   LOG_MAIL,   "mail"          },
1394                 {   LOG_DAEMON, "daemon"        },
1395                 {   LOG_AUTH,   "auth"          },
1396                 {   LOG_SYSLOG, "syslog"        },
1397                 {   LOG_LPR,    "lpr"           },
1398                 {   LOG_NEWS,   "news"          },
1399                 {   LOG_UUCP,   "uucp"          },
1400                 {   LOG_LOCAL0, "local0"        },
1401                 {   LOG_LOCAL1, "local1"        },
1402                 {   LOG_LOCAL2, "local2"        },
1403                 {   LOG_LOCAL3, "local3"        },
1404                 {   LOG_LOCAL4, "local4"        },
1405                 {   LOG_LOCAL5, "local5"        },
1406                 {   LOG_LOCAL6, "local6"        },
1407                 {   LOG_LOCAL7, "local7"        },
1408                 {   0,            NULL          }
1409         };
1410         for(i = 0; facTbl[i].name != NULL; i++) {
1411                 if(!strcasecmp(name, facTbl[i].name))
1412                         return facTbl[i].facility;
1413         }
1414         enable_syslog = 0;
1415         return LOG_DAEMON;
1416 }
1417
1418
1419 /********** MEM CHEQQER ***********/
1420
1421 #ifdef DEBUG_MEMORY_LEAKS
1422
1423 #undef malloc
1424 #undef realloc
1425 #undef strdup
1426 #undef free
1427
1428 void *tracked_malloc(size_t size, char *file, int line) {
1429         struct igheap *thisheap;
1430         void *block;
1431
1432         block = malloc(size);
1433         if (block == NULL) return(block);
1434
1435         thisheap = malloc(sizeof(struct igheap));
1436         if (thisheap == NULL) {
1437                 free(block);
1438                 return(NULL);
1439         }
1440
1441         thisheap->block = block;
1442         strcpy(thisheap->file, file);
1443         thisheap->line = line;
1444         
1445         begin_critical_section(S_DEBUGMEMLEAKS);
1446         thisheap->next = igheap;
1447         igheap = thisheap;
1448         end_critical_section(S_DEBUGMEMLEAKS);
1449
1450         return(block);
1451 }
1452
1453
1454 void *tracked_realloc(void *ptr, size_t size, char *file, int line) {
1455         struct igheap *thisheap;
1456         void *block;
1457
1458         block = realloc(ptr, size);
1459         if (block == NULL) return(block);
1460
1461         thisheap = malloc(sizeof(struct igheap));
1462         if (thisheap == NULL) {
1463                 free(block);
1464                 return(NULL);
1465         }
1466
1467         thisheap->block = block;
1468         strcpy(thisheap->file, file);
1469         thisheap->line = line;
1470         
1471         begin_critical_section(S_DEBUGMEMLEAKS);
1472         thisheap->next = igheap;
1473         igheap = thisheap;
1474         end_critical_section(S_DEBUGMEMLEAKS);
1475
1476         return(block);
1477 }
1478
1479
1480
1481 void tracked_free(void *ptr) {
1482         struct igheap *thisheap;
1483         struct igheap *trash;
1484
1485         free(ptr);
1486
1487         if (igheap == NULL) return;
1488         begin_critical_section(S_DEBUGMEMLEAKS);
1489         for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1490                 if (thisheap->next != NULL) {
1491                         if (thisheap->next->block == ptr) {
1492                                 trash = thisheap->next;
1493                                 thisheap->next = thisheap->next->next;
1494                                 free(trash);
1495                         }
1496                 }
1497         }
1498         if (igheap->block == ptr) {
1499                 trash = igheap;
1500                 igheap = igheap->next;
1501                 free(trash);
1502         }
1503         end_critical_section(S_DEBUGMEMLEAKS);
1504 }
1505
1506 char *tracked_strdup(const char *s, char *file, int line) {
1507         char *ptr;
1508
1509         if (s == NULL) return(NULL);
1510         ptr = tracked_malloc(strlen(s) + 1, file, line);
1511         if (ptr == NULL) return(NULL);
1512         strncpy(ptr, s, strlen(s));
1513         return(ptr);
1514 }
1515
1516 void dump_heap(void) {
1517         struct igheap *thisheap;
1518
1519         for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1520                 CtdlLogPrintf(CTDL_CRIT, "UNFREED: %30s : %d\n",
1521                         thisheap->file, thisheap->line);
1522         }
1523 }
1524
1525 #endif /*  DEBUG_MEMORY_LEAKS */