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