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