* Initial hack of worker-thread rearchitecture. Right now it is successfully
[citadel.git] / citadel / sysdep.c
1 /*
2  * Citadel/UX "system dependent" stuff.
3  * See copyright.txt for copyright information.
4  *
5  * $Id$
6  *
7  * Here's where we (hopefully) have all the parts of the Citadel server that
8  * would need to be altered to run the server in a non-POSIX environment.
9  * Wherever possible, we use function wrappers and type definitions to create
10  * abstractions that are platform-independent from the calling side.
11  * 
12  * Eventually we'll try porting to a different platform and either have
13  * multiple variants of this file or simply load it up with #ifdefs.
14  */
15
16
17 #include "sysdep.h"
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <ctype.h>
23 #include <signal.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <sys/socket.h>
27 #include <sys/time.h>
28 #include <limits.h>
29 #include <netinet/in.h>
30 #include <netdb.h>
31 #include <string.h>
32 #include <pwd.h>
33 #include <errno.h>
34 #include <stdarg.h>
35 #include <syslog.h>
36 #include <grp.h>
37 #ifdef __GNUC__
38 #include <malloc.h>
39 #endif
40 #ifdef HAVE_PTHREAD_H
41 #include <pthread.h>
42 #endif
43 #include "citadel.h"
44 #include "server.h"
45 #include "sysdep_decls.h"
46 #include "citserver.h"
47 #include "support.h"
48 #include "config.h"
49 #include "database.h"
50 #include "housekeeping.h"
51 #include "dynloader.h"
52 #include "tools.h"
53
54 #ifdef HAVE_SYS_SELECT_H
55 #include <sys/select.h>
56 #endif
57
58 #ifndef HAVE_SNPRINTF
59 #include "snprintf.h"
60 #endif
61
62 #ifdef DEBUG_MEMORY_LEAKS
63 struct TheHeap *heap = NULL;
64 #endif
65
66 pthread_mutex_t Critters[MAX_SEMAPHORES];       /* Things needing locking */
67 pthread_key_t MyConKey;                         /* TSD key for MyContext() */
68
69 int msock;                                      /* master listening socket */
70 int verbosity = 9;                              /* Logging level */
71
72 struct CitContext masterCC;
73 int rescan[2];                                  /* The Rescan Pipe */
74
75 /*
76  * lprintf()  ...   Write logging information
77  */
78 void lprintf(int loglevel, const char *format, ...) {   
79         va_list arg_ptr;
80         char buf[512];
81   
82         va_start(arg_ptr, format);   
83         vsprintf(buf, format, arg_ptr);   
84         va_end(arg_ptr);   
85
86         if (loglevel <= verbosity) { 
87                 fprintf(stderr, "%s", buf);
88                 fflush(stderr);
89                 }
90
91         PerformLogHooks(loglevel, buf);
92         }   
93
94
95
96 #ifdef DEBUG_MEMORY_LEAKS
97 void *tracked_malloc(size_t tsize, char *tfile, int tline) {
98         void *ptr;
99         struct TheHeap *hptr;
100
101         ptr = malloc(tsize);
102         if (ptr == NULL) {
103                 lprintf(3, "DANGER!  mallok(%d) at %s:%d failed!\n",
104                         tsize, tfile, tline);
105                 return(NULL);
106         }
107
108         hptr = (struct TheHeap *) malloc(sizeof(struct TheHeap));
109         strcpy(hptr->h_file, tfile);
110         hptr->h_line = tline;
111         hptr->next = heap;
112         hptr->h_ptr = ptr;
113         heap = hptr;
114         return ptr;
115         }
116
117 char *tracked_strdup(const char *orig, char *tfile, int tline) {
118         char *s;
119
120         s = tracked_malloc( (strlen(orig)+1), tfile, tline);
121         if (s == NULL) return NULL;
122
123         strcpy(s, orig);
124         return s;
125 }
126
127 void tracked_free(void *ptr) {
128         struct TheHeap *hptr, *freeme;
129
130         if (heap->h_ptr == ptr) {
131                 hptr = heap->next;
132                 free(heap);
133                 heap = hptr;
134                 }
135         else {
136                 for (hptr=heap; hptr->next!=NULL; hptr=hptr->next) {
137                         if (hptr->next->h_ptr == ptr) {
138                                 freeme = hptr->next;
139                                 hptr->next = hptr->next->next;
140                                 free(freeme);
141                                 }
142                         }
143                 }
144
145         free(ptr);
146         }
147
148 void *tracked_realloc(void *ptr, size_t size) {
149         void *newptr;
150         struct TheHeap *hptr;
151         
152         newptr = realloc(ptr, size);
153
154         for (hptr=heap; hptr!=NULL; hptr=hptr->next) {
155                 if (hptr->h_ptr == ptr) hptr->h_ptr = newptr;
156                 }
157
158         return newptr;
159         }
160
161
162 void dump_tracked() {
163         struct TheHeap *hptr;
164
165         cprintf("%d Here's what's allocated...\n", LISTING_FOLLOWS);    
166         for (hptr=heap; hptr!=NULL; hptr=hptr->next) {
167                 cprintf("%20s %5d\n",
168                         hptr->h_file, hptr->h_line);
169                 }
170 #ifdef __GNUC__
171         malloc_stats();
172 #endif
173
174         cprintf("000\n");
175         }
176 #endif
177
178 static pthread_t main_thread_id;
179
180 #ifndef HAVE_PTHREAD_CANCEL
181 /*
182  * signal handler to fake thread cancellation; only required on BSDI as far
183  * as I know.
184  */
185 static RETSIGTYPE cancel_thread(int signum) {
186         pthread_exit(NULL);
187         }
188 #endif
189
190 /*
191  * we used to use master_cleanup() as a signal handler to shut down the server.
192  * however, master_cleanup() and the functions it calls do some things that
193  * aren't such a good idea to do from a signal handler: acquiring mutexes,
194  * playing with signal masks on BSDI systems, etc. so instead we install the
195  * following signal handler to set a global variable to inform the main loop
196  * that it's time to call master_cleanup() and exit.
197  */
198
199 static volatile int time_to_die = 0;
200
201 static RETSIGTYPE signal_cleanup(int signum) {
202         time_to_die = 1;
203         }
204
205
206 /*
207  * Some initialization stuff...
208  */
209 void init_sysdep(void) {
210         int a;
211
212         /* Set up a bunch of semaphores to be used for critical sections */
213         for (a=0; a<MAX_SEMAPHORES; ++a) {
214                 pthread_mutex_init(&Critters[a], NULL);
215                 }
216
217         /*
218          * Set up a place to put thread-specific data.
219          * We only need a single pointer per thread - it points to the
220          * thread's CitContext structure in the ContextList linked list.
221          */
222         if (pthread_key_create(&MyConKey, NULL) != 0) {
223                 lprintf(1, "Can't create TSD key!!  %s\n", strerror(errno));
224                 }
225
226         /*
227          * The action for unexpected signals and exceptions should be to
228          * call signal_cleanup() to gracefully shut down the server.
229          */
230         signal(SIGINT, signal_cleanup);
231         signal(SIGQUIT, signal_cleanup);
232         signal(SIGHUP, signal_cleanup);
233         signal(SIGTERM, signal_cleanup);
234         signal(SIGPIPE, SIG_IGN);
235         main_thread_id = pthread_self();
236 #ifndef HAVE_PTHREAD_CANCEL /* fake it - only BSDI afaik */
237         signal(SIGUSR1, cancel_thread);
238 #endif
239         }
240
241
242 /*
243  * Obtain a semaphore lock to begin a critical section.
244  */
245 void begin_critical_section(int which_one)
246 {
247         pthread_mutex_lock(&Critters[which_one]);
248 }
249
250 /*
251  * Release a semaphore lock to end a critical section.
252  */
253 void end_critical_section(int which_one)
254 {
255         pthread_mutex_unlock(&Critters[which_one]);
256 }
257
258
259
260 /*
261  * This is a generic function to set up a master socket for listening on
262  * a TCP port.  The server shuts down if the bind fails.
263  */
264 int ig_tcp_server(int port_number, int queue_len)
265 {
266         struct sockaddr_in sin;
267         int s, i;
268
269         memset(&sin, 0, sizeof(sin));
270         sin.sin_family = AF_INET;
271         sin.sin_addr.s_addr = INADDR_ANY;
272
273         if (port_number == 0) {
274                 lprintf(1,
275                         "citserver: No port number specified.  Run setup.\n");
276                 exit(1);
277                 }
278         
279         sin.sin_port = htons((u_short)port_number);
280
281         s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
282         if (s < 0) {
283                 lprintf(1, "citserver: Can't create a socket: %s\n",
284                         strerror(errno));
285                 exit(errno);
286                 }
287
288         /* Set the SO_REUSEADDR socket option, because it makes sense. */
289         i = 1;
290         setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
291
292         if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
293                 lprintf(1, "citserver: Can't bind: %s\n", strerror(errno));
294                 exit(errno);
295                 }
296
297         if (listen(s, queue_len) < 0) {
298                 lprintf(1, "citserver: Can't listen: %s\n", strerror(errno));
299                 exit(errno);
300                 }
301
302         return(s);
303         }
304
305
306
307 /*
308  * Return a pointer to a thread's own CitContext structure (new)
309  */
310 struct CitContext *MyContext(void) {
311         struct CitContext *retCC;
312         retCC = (struct CitContext *) pthread_getspecific(MyConKey);
313         if (retCC == NULL) retCC = &masterCC;
314         return(retCC);
315         }
316
317
318 /*
319  * Wedge our way into the context list.
320  */
321 struct CitContext *CreateNewContext(void) {
322         struct CitContext *me;
323
324         me = (struct CitContext *) mallok(sizeof(struct CitContext));
325         if (me == NULL) {
326                 lprintf(1, "citserver: can't allocate memory!!\n");
327                 return NULL;
328                 }
329         memset(me, 0, sizeof(struct CitContext));
330
331         /* The new context will be created already in the CON_EXECUTING state
332          * in order to prevent another thread from grabbing it while it's
333          * being set up.
334          */
335         me->state = CON_EXECUTING;
336
337         begin_critical_section(S_SESSION_TABLE);
338         me->next = ContextList;
339         ContextList = me;
340         end_critical_section(S_SESSION_TABLE);
341         return(me);
342         }
343
344 /*
345  * Remove a context from the context list.
346  */
347 void RemoveContext(struct CitContext *con)
348 {
349         struct CitContext *ptr = NULL;
350         struct CitContext *ToFree = NULL;
351
352         lprintf(7, "Starting RemoveContext()\n");
353         if (con==NULL) {
354                 lprintf(5, "WARNING: RemoveContext() called with NULL!\n");
355                 return;
356                 }
357
358         /*
359          * session_count() starts its own S_SESSION_TABLE critical section;
360          * so do not call it from within this loop.
361          */
362         begin_critical_section(S_SESSION_TABLE);
363
364         if (ContextList == con) {
365                 ToFree = ContextList;
366                 ContextList = ContextList->next;
367                 }
368         else {
369                 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
370                         if (ptr->next == con) {
371                                 ToFree = ptr->next;
372                                 ptr->next = ptr->next->next;
373                                 }
374                         }
375                 }
376
377
378         end_critical_section(S_SESSION_TABLE);
379
380         lprintf(7, "Closing socket %d\n", ToFree->client_socket);
381         close(ToFree->client_socket);
382
383         /* Tell the housekeeping thread to check to see if this is the time
384          * to initiate a scheduled shutdown event.
385          */
386         enter_housekeeping_cmd("SCHED_SHUTDOWN");
387
388         /* Free up the memory used by this context */
389         phree(ToFree);
390
391         lprintf(7, "Done with RemoveContext\n");
392         }
393
394
395 /*
396  * Return the number of sessions currently running.
397  * (This should probably be moved out of sysdep.c)
398  */
399 int session_count(void) {
400         struct CitContext *ptr;
401         int TheCount = 0;
402
403         begin_critical_section(S_SESSION_TABLE);
404         for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
405                 ++TheCount;
406                 }
407         end_critical_section(S_SESSION_TABLE);
408
409         return(TheCount);
410         }
411
412
413 /*
414  * client_write()   ...    Send binary data to the client.
415  */
416 void client_write(char *buf, int nbytes)
417 {
418         int bytes_written = 0;
419         int retval;
420         while (bytes_written < nbytes) {
421                 retval = write(CC->client_socket, &buf[bytes_written],
422                         nbytes - bytes_written);
423                 if (retval < 1) {
424                         lprintf(2, "client_write() failed: %s\n",
425                                 strerror(errno));
426                         cleanup(errno);
427                         }
428                 bytes_written = bytes_written + retval;
429                 }
430         }
431
432
433 /*
434  * cprintf()  ...   Send formatted printable data to the client.   It is
435  *                  implemented in terms of client_write() but remains in
436  *                  sysdep.c in case we port to somewhere without va_args...
437  */
438 void cprintf(const char *format, ...) {   
439         va_list arg_ptr;   
440         char buf[256];   
441    
442         va_start(arg_ptr, format);   
443         if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
444                 buf[sizeof buf - 2] = '\n';
445         client_write(buf, strlen(buf)); 
446         va_end(arg_ptr);
447         }   
448
449
450 /*
451  * Read data from the client socket.
452  * Return values are:
453  *      1       Requested number of bytes has been read.
454  *      0       Request timed out.
455  * If the socket breaks, the session is immediately terminated.
456  */
457 int client_read_to(char *buf, int bytes, int timeout)
458 {
459         int len,rlen;
460         fd_set rfds;
461         struct timeval tv;
462         int retval;
463
464         len = 0;
465         while(len<bytes) {
466                 FD_ZERO(&rfds);
467                 FD_SET(CC->client_socket, &rfds);
468                 tv.tv_sec = 1;
469                 tv.tv_usec = 0;
470
471                 retval = select( (CC->client_socket)+1, 
472                                         &rfds, NULL, NULL, &tv);
473
474                 if (FD_ISSET(CC->client_socket, &rfds) == 0) {
475                         return(0);
476                         }
477
478                 rlen = read(CC->client_socket, &buf[len], bytes-len);
479                 if (rlen<1) {
480                         lprintf(2, "client_read() failed: %s\n",
481                                 strerror(errno));
482                         cleanup(errno);
483                         }
484                 len = len + rlen;
485                 }
486         return(1);
487         }
488
489 /*
490  * Read data from the client socket with default timeout.
491  * (This is implemented in terms of client_read_to() and could be
492  * justifiably moved out of sysdep.c)
493  */
494 int client_read(char *buf, int bytes)
495 {
496         return(client_read_to(buf, bytes, config.c_sleeping));
497         }
498
499
500 /*
501  * client_gets()   ...   Get a LF-terminated line of text from the client.
502  * (This is implemented in terms of client_read() and could be
503  * justifiably moved out of sysdep.c)
504  */
505 int client_gets(char *buf)
506 {
507         int i, retval;
508
509         /* Read one character at a time.
510          */
511         for (i = 0;;i++) {
512                 retval = client_read(&buf[i], 1);
513                 if (retval != 1 || buf[i] == '\n' || i == 255)
514                         break;
515                 }
516
517         /* If we got a long line, discard characters until the newline.
518          */
519         if (i == 255)
520                 while (buf[i] != '\n' && retval == 1)
521                         retval = client_read(&buf[i], 1);
522
523         /* Strip the trailing newline and any trailing nonprintables (cr's)
524          */
525         buf[i] = 0;
526         while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
527                 buf[strlen(buf)-1] = 0;
528         return(retval);
529         }
530
531
532
533 /*
534  * The system-dependent part of master_cleanup() - close the master socket.
535  */
536 void sysdep_master_cleanup(void) {
537         lprintf(7, "Closing master socket %d\n", msock);
538         close(msock);
539         }
540
541
542 /*
543  * Terminate another session.
544  */
545 void kill_session(int session_to_kill) {
546         struct CitContext *ptr;
547
548         begin_critical_section(S_SESSION_TABLE);
549         for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
550                 if (ptr->cs_pid == session_to_kill) {
551                         ptr->state = CON_DYING;
552                         }
553                 }
554         end_critical_section(S_SESSION_TABLE);
555         }
556
557
558
559
560 /*
561  * Start running as a daemon.  Only close stdio if do_close_stdio is set.
562  */
563 void start_daemon(int do_close_stdio) {
564         if (do_close_stdio) {
565                 /* close(0); */
566                 close(1);
567                 close(2);
568                 }
569         signal(SIGHUP,SIG_IGN);
570         signal(SIGINT,SIG_IGN);
571         signal(SIGQUIT,SIG_IGN);
572         if (fork()!=0) exit(0);
573         }
574
575
576
577 /*
578  * Tie in to the 'netsetup' program.
579  *
580  * (We're going to hope that netsetup never feeds more than 4096 bytes back.)
581  */
582 void cmd_nset(char *cmdbuf)
583 {
584         int retcode;
585         char fbuf[4096];
586         FILE *netsetup;
587         int ch;
588         int a, b;
589         char netsetup_args[3][256];
590
591         if (CC->usersupp.axlevel < 6) {
592                 cprintf("%d Higher access required.\n", 
593                         ERROR + HIGHER_ACCESS_REQUIRED);
594                 return;
595                 }
596
597         for (a=1; a<=3; ++a) {
598                 if (num_parms(cmdbuf) >= a) {
599                         extract(netsetup_args[a-1], cmdbuf, a-1);
600                         for (b=0; b<strlen(netsetup_args[a-1]); ++b) {
601                                 if (netsetup_args[a-1][b] == 34) {
602                                         netsetup_args[a-1][b] = '_';
603                                         }
604                                 }
605                         }
606                 else {
607                         netsetup_args[a-1][0] = 0;
608                         }
609                 }
610
611         sprintf(fbuf, "./netsetup \"%s\" \"%s\" \"%s\" </dev/null 2>&1",
612                 netsetup_args[0], netsetup_args[1], netsetup_args[2]);
613         netsetup = popen(fbuf, "r");
614         if (netsetup == NULL) {
615                 cprintf("%d %s\n", ERROR, strerror(errno));
616                 return;
617                 }
618
619         fbuf[0] = 0;
620         while (ch = getc(netsetup), (ch > 0)) {
621                 fbuf[strlen(fbuf)+1] = 0;
622                 fbuf[strlen(fbuf)] = ch;
623                 }
624
625         retcode = pclose(netsetup);
626
627         if (retcode != 0) {
628                 for (a=0; a<strlen(fbuf); ++a) {
629                         if (fbuf[a] < 32) fbuf[a] = 32;
630                         }
631                 fbuf[245] = 0;
632                 cprintf("%d %s\n", ERROR, fbuf);
633                 return;
634                 }
635
636         cprintf("%d Command succeeded.  Output follows:\n", LISTING_FOLLOWS);
637         cprintf("%s", fbuf);
638         if (fbuf[strlen(fbuf)-1] != 10) cprintf("\n");
639         cprintf("000\n");
640         }
641
642
643
644 /*
645  * Generic routine to convert a login name to a full name (gecos)
646  * Returns nonzero if a conversion took place
647  */
648 int convert_login(char NameToConvert[]) {
649         struct passwd *pw;
650         int a;
651
652         pw = getpwnam(NameToConvert);
653         if (pw == NULL) {
654                 return(0);
655                 }
656         else {
657                 strcpy(NameToConvert, pw->pw_gecos);
658                 for (a=0; a<strlen(NameToConvert); ++a) {
659                         if (NameToConvert[a] == ',') NameToConvert[a] = 0;
660                         }
661                 return(1);
662                 }
663         }
664
665
666
667
668         
669
670 /*
671  * Here's where it all begins.
672  */
673 int main(int argc, char **argv)
674 {
675         THREAD HousekeepingThread;      /* Thread descriptor */
676         pthread_attr_t attr;            /* Thread attributes */
677         char tracefile[128];            /* Name of file to log traces to */
678         int a, i;                       /* General-purpose variables */
679         struct passwd *pw;
680         int drop_root_perms = 1;
681         char *moddir;
682         
683         /* specify default port name and trace file */
684         strcpy(tracefile, "");
685
686         /* parse command-line arguments */
687         for (a=1; a<argc; ++a) {
688
689                 /* -t specifies where to log trace messages to */
690                 if (!strncmp(argv[a], "-t", 2)) {
691                         strcpy(tracefile, argv[a]);
692                         strcpy(tracefile, &tracefile[2]);
693                         freopen(tracefile, "r", stdin);
694                         freopen(tracefile, "w", stdout);
695                         freopen(tracefile, "w", stderr);
696                         }
697
698                 /* run in the background if -d was specified */
699                 else if (!strcmp(argv[a], "-d")) {
700                         start_daemon( (strlen(tracefile) > 0) ? 0 : 1 ) ;
701                         }
702
703                 /* -x specifies the desired logging level */
704                 else if (!strncmp(argv[a], "-x", 2)) {
705                         verbosity = atoi(&argv[a][2]);
706                         }
707
708                 else if (!strncmp(argv[a], "-h", 2)) {
709                         safestrncpy(bbs_home_directory, &argv[a][2],
710                                     sizeof bbs_home_directory);
711                         home_specified = 1;
712                         }
713
714                 else if (!strncmp(argv[a], "-f", 2)) {
715                         do_defrag = 1;
716                         }
717
718                 /* -r tells the server not to drop root permissions. don't use
719                  * this unless you know what you're doing. this should be
720                  * removed in the next release if it proves unnecessary. */
721                 else if (!strcmp(argv[a], "-r"))
722                         drop_root_perms = 0;
723
724                 /* any other parameter makes it crash and burn */
725                 else {
726                         lprintf(1,      "citserver: usage: "
727                                         "citserver [-tTraceFile] [-d] [-f]"
728                                         " [-xLogLevel] [-hHomeDir]\n");
729                         exit(1);
730                         }
731
732                 }
733
734         /* Tell 'em who's in da house */
735         lprintf(1,
736 "\nMultithreaded message server for Citadel/UX\n"
737 "Copyright (C) 1987-1999 by the Citadel/UX development team.\n"
738 "Citadel/UX is free software, covered by the GNU General Public License, and\n"
739 "you are welcome to change it and/or distribute copies of it under certain\n"
740 "conditions.  There is absolutely no warranty for this software.  Please\n"
741 "read the 'COPYING.txt' file for details.\n\n");
742
743         /* Initialize... */
744         init_sysdep();
745         openlog("citserver",LOG_PID,LOG_USER);
746         /* Load site-specific parameters */
747         lprintf(7, "Loading citadel.config\n");
748         get_config();
749
750         /*
751          * Bind the server to our favourite port.
752          * There is no need to check for errors, because ig_tcp_server()
753          * exits if it doesn't succeed.
754          */
755         lprintf(7, "Attempting to bind to port %d...\n", config.c_port_number);
756         msock = ig_tcp_server(config.c_port_number, 5);
757         lprintf(7, "Listening on socket %d\n", msock);
758
759         /*
760          * Now that we've bound the socket, change to the BBS user id and its
761          * corresponding group ids
762          */
763         if (drop_root_perms) {
764                 if ((pw = getpwuid(BBSUID)) == NULL)
765                         lprintf(1, "WARNING: getpwuid(%d): %s\n"
766                                    "Group IDs will be incorrect.\n", BBSUID,
767                                 strerror(errno));
768                 else {
769                         initgroups(pw->pw_name, pw->pw_gid);
770                         if (setgid(pw->pw_gid))
771                                 lprintf(3, "setgid(%d): %s\n", pw->pw_gid,
772                                         strerror(errno));
773                         }
774                 lprintf(7, "Changing uid to %d\n", BBSUID);
775                 if (setuid(BBSUID) != 0) {
776                         lprintf(3, "setuid() failed: %s\n", strerror(errno));
777                         }
778                 }
779
780         /*
781          * Do non system dependent startup functions.
782          */
783         master_startup();
784
785         /*
786          * Load any server-side modules (plugins) available here.
787          */
788         lprintf(7, "Initializing loadable modules\n");
789         if ((moddir = malloc(strlen(bbs_home_directory) + 9)) != NULL) {
790                 sprintf(moddir, "%s/modules", bbs_home_directory);
791                 DLoader_Init(moddir);
792                 free(moddir);
793                 }
794
795         lprintf(7, "Starting housekeeper thread\n");
796         pthread_attr_init(&attr);
797         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
798         if (pthread_create(&HousekeepingThread, &attr,
799            (void* (*)(void*)) housekeeping_loop, NULL) != 0) {
800                 lprintf(1, "Can't create housekeeping thead: %s\n",
801                         strerror(errno));
802         }
803
804
805         /*
806          * The rescan pipe exists so that worker threads can be woken up and
807          * told to re-scan the context list for fd's to listen on.  This is
808          * necessary, for example, when a context is about to go idle and needs
809          * to get back on that list.
810          */
811         if (pipe(rescan)) {
812                 lprintf(1, "Can't create rescan pipe!\n");
813                 exit(errno);
814         }
815
816         /*
817          * Now create a bunch of worker threads.
818          */
819         for (i=0; i<(NUM_WORKER_THREADS-1); ++i) {
820                 pthread_attr_init(&attr);
821                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
822                 if (pthread_create(&HousekeepingThread, &attr,
823                    (void* (*)(void*)) worker_thread, NULL) != 0) {
824                         lprintf(1, "Can't create worker thead: %s\n",
825                         strerror(errno));
826                 }
827         }
828
829         /* Now this thread can become a worker as well. */
830         worker_thread();
831
832         return(0);
833 }
834
835
836
837
838
839
840
841 /* 
842  * This loop just keeps going and going and going...
843  */     
844 void worker_thread(void) {
845         int i;
846         char junk;
847         int numselect = 0;
848         int highest;
849         struct CitContext *ptr;
850         struct CitContext *bind_me = NULL;
851         fd_set readfds;
852         int retval;
853         struct CitContext *con= NULL;   /* Temporary context pointer */
854         struct sockaddr_in fsin;        /* Data for master socket */
855         int alen;                       /* Data for master socket */
856         int ssock;                      /* Descriptor for master socket */
857
858         while (!time_to_die) {
859
860                 /* 
861                  * In a stupid environment, we would have all idle threads
862                  * calling select() and then they'd all wake up at once.  We
863                  * solve this problem by putting the select() in a critical
864                  * section, so only one thread has the opportunity to wake
865                  * up.  If we wake up on the master socket, create a new
866                  * session context; otherwise, just bind the thread to the
867                  * context we want and go on our merry way.
868                  */
869
870                 begin_critical_section(S_I_WANNA_SELECT);
871                 lprintf(9, "Worker thread %2d woke up\n", getpid());
872
873 SETUP_FD:
874                 FD_ZERO(&readfds);
875                 FD_SET(msock, &readfds);
876                 highest = msock;
877                 FD_SET(rescan[0], &readfds);
878                 if (rescan[0] > highest) highest = rescan[0];
879                 numselect = 2;
880
881                 begin_critical_section(S_SESSION_TABLE);
882                 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
883                         if (ptr->state == CON_IDLE) {
884                                 FD_SET(ptr->client_socket, &readfds);
885                                 if (ptr->client_socket > highest)
886                                         highest = ptr->client_socket;
887                                 ++numselect;
888                         }
889                 }
890                 end_critical_section(S_SESSION_TABLE);
891
892                 lprintf(9, "Thread %2d can wake up on %d different fd's\n",
893                         getpid(), numselect);
894                 retval = select(highest + 1, &readfds, NULL, NULL, NULL);
895                 lprintf(9, "select() returned %d\n", retval);
896
897                 /* Now figure out who made this select() unblock.
898                  * First, check for an error or exit condition.
899                  */
900                 if (retval < 0) {
901                         end_critical_section(S_I_WANNA_SELECT);
902                         lprintf(9, "Exiting (%s)\n", errno);
903                         time_to_die = 1;
904                 }
905
906                 /* Next, check to see if it's a new client connecting
907                  * on the master socket.
908                  */
909                 else if (FD_ISSET(msock, &readfds)) {
910                         lprintf(9, "It's the master socket!\n");
911                         alen = sizeof fsin;
912                         ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
913                         if (ssock < 0) {
914                                 lprintf(2, "citserver: accept() failed: %s\n",
915                                         strerror(errno));
916                         }
917                         else {
918                                 lprintf(7, "citserver: New client socket %d\n",
919                                         ssock);
920
921                                 /* New context will be created already set up
922                                  * in the CON_EXECUTING state.
923                                  */
924                                 con = CreateNewContext();
925
926                                 /* Assign our new socket number to it. */
927                                 con->client_socket = ssock;
928         
929                                 /* Set the SO_REUSEADDR socket option */
930                                 i = 1;
931                                 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
932                                         &i, sizeof(i));
933
934                                 pthread_setspecific(MyConKey, (void *)con);
935                                 begin_session(con);
936                                 /* do_command_loop(); */
937                                 pthread_setspecific(MyConKey, (void *)NULL);
938                                 con->state = CON_IDLE;
939                                 goto SETUP_FD;
940                         }
941                 }
942
943                 /* If the rescan pipe went active, someone is telling this
944                  * thread that the &readfds needs to be refreshed with more
945                  * current data.
946                  */
947                 else if (FD_ISSET(rescan[0], &readfds)) {
948                         lprintf(9, "rescanning\n");
949                         read(rescan[0], &junk, 1);
950                         goto SETUP_FD;
951                 }
952
953                 /* It must be a client socket.  Find a context that has data
954                  * waiting on its socket *and* is in the CON_IDLE state.
955                  */
956                 else {
957                         bind_me = NULL;
958                         begin_critical_section(S_SESSION_TABLE);
959                         for (ptr = ContextList;
960                             ( (ptr != NULL) && (bind_me == NULL) );
961                             ptr = ptr->next) {
962                                 if ( (FD_ISSET(ptr->client_socket, &readfds))
963                                    && (ptr->state == CON_IDLE) ) {
964                                         bind_me = con;
965                                 }
966                         }
967                         if (bind_me != NULL) {
968                                 /* Found one.  Stake a claim to it before
969                                  * letting anyone else touch the context list.
970                                  */
971                                 bind_me->state = CON_EXECUTING;
972                         }
973
974                         end_critical_section(S_SESSION_TABLE);
975                         end_critical_section(S_I_WANNA_SELECT);
976
977                         /* We're bound to a session, now do *one* command */
978                         if (bind_me != NULL) {
979                                 lprintf(9, "Binding thread to session %d\n",
980                                         bind_me->client_socket);
981                                 pthread_setspecific(MyConKey, (void *)bind_me);
982                                 do_command_loop();
983                                 pthread_setspecific(MyConKey, (void *)NULL);
984                                 if (bind_me->state == CON_DYING) {
985                                         RemoveContext(bind_me);
986                                 } 
987                                 else {
988                                         bind_me->state = CON_IDLE;
989                                 }
990                                 write(rescan[1], &junk, 1);
991                         }
992                         else {
993                                 lprintf(9, "Thread %02d found nothing to do!\n",
994                                         getpid());
995                         }
996
997                 }
998
999         }
1000
1001         pthread_exit(NULL);
1002 }
1003