4 * Citadel/UX "system dependent" stuff.
5 * See copyright.txt for copyright information.
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.
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.
23 #include <sys/types.h>
26 #include <sys/socket.h>
29 #include <netinet/in.h>
46 #include "sysdep_decls.h"
47 #include "citserver.h"
51 #include "housekeeping.h"
52 #include "dynloader.h"
55 #ifdef HAVE_SYS_SELECT_H
56 #include <sys/select.h>
63 #ifdef DEBUG_MEMORY_LEAKS
64 struct TheHeap *heap = NULL;
67 pthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */
68 pthread_key_t MyConKey; /* TSD key for MyContext() */
70 int verbosity = DEFAULT_VERBOSITY; /* Logging level */
72 struct CitContext masterCC;
73 int rescan[2]; /* The Rescan Pipe */
74 time_t last_purge = 0; /* Last dead session purge */
75 int num_threads = 0; /* Current number of threads */
76 int num_sessions = 0; /* Current number of sessions */
78 fd_set masterfds; /* Master sockets etc. */
81 time_t last_timer = 0L; /* Last timer hook processing */
85 * lprintf() ... Write logging information
87 void lprintf(int loglevel, const char *format, ...) {
91 va_start(arg_ptr, format);
92 vsprintf(buf, format, arg_ptr);
95 if (loglevel <= verbosity) {
96 fprintf(stderr, "%s", buf);
100 PerformLogHooks(loglevel, buf);
105 #ifdef DEBUG_MEMORY_LEAKS
106 void *tracked_malloc(size_t tsize, char *tfile, int tline) {
108 struct TheHeap *hptr;
112 lprintf(3, "DANGER! mallok(%d) at %s:%d failed!\n",
113 tsize, tfile, tline);
117 hptr = (struct TheHeap *) malloc(sizeof(struct TheHeap));
118 strcpy(hptr->h_file, tfile);
119 hptr->h_line = tline;
126 char *tracked_strdup(const char *orig, char *tfile, int tline) {
129 s = tracked_malloc( (strlen(orig)+1), tfile, tline);
130 if (s == NULL) return NULL;
136 void tracked_free(void *ptr) {
137 struct TheHeap *hptr, *freeme;
139 if (heap->h_ptr == ptr) {
145 for (hptr=heap; hptr->next!=NULL; hptr=hptr->next) {
146 if (hptr->next->h_ptr == ptr) {
148 hptr->next = hptr->next->next;
157 void *tracked_realloc(void *ptr, size_t size) {
159 struct TheHeap *hptr;
161 newptr = realloc(ptr, size);
163 for (hptr=heap; hptr!=NULL; hptr=hptr->next) {
164 if (hptr->h_ptr == ptr) hptr->h_ptr = newptr;
171 void dump_tracked() {
172 struct TheHeap *hptr;
174 cprintf("%d Here's what's allocated...\n", LISTING_FOLLOWS);
175 for (hptr=heap; hptr!=NULL; hptr=hptr->next) {
176 cprintf("%20s %5d\n",
177 hptr->h_file, hptr->h_line);
189 * we used to use master_cleanup() as a signal handler to shut down the server.
190 * however, master_cleanup() and the functions it calls do some things that
191 * aren't such a good idea to do from a signal handler: acquiring mutexes,
192 * playing with signal masks on BSDI systems, etc. so instead we install the
193 * following signal handler to set a global variable to inform the main loop
194 * that it's time to call master_cleanup() and exit.
197 static volatile int time_to_die = 0;
199 static RETSIGTYPE signal_cleanup(int signum) {
205 * Some initialization stuff...
207 void init_sysdep(void) {
210 /* Set up a bunch of semaphores to be used for critical sections */
211 for (a=0; a<MAX_SEMAPHORES; ++a) {
212 pthread_mutex_init(&Critters[a], NULL);
216 * Set up a place to put thread-specific data.
217 * We only need a single pointer per thread - it points to the
218 * thread's CitContext structure in the ContextList linked list.
220 if (pthread_key_create(&MyConKey, NULL) != 0) {
221 lprintf(1, "Can't create TSD key!! %s\n", strerror(errno));
225 * The action for unexpected signals and exceptions should be to
226 * call signal_cleanup() to gracefully shut down the server.
228 signal(SIGINT, signal_cleanup);
229 signal(SIGQUIT, signal_cleanup);
230 signal(SIGHUP, signal_cleanup);
231 signal(SIGTERM, signal_cleanup);
234 * Do not shut down the server on broken pipe signals, otherwise the
235 * whole Citadel service would come down whenever a single client
238 signal(SIGPIPE, SIG_IGN);
243 * Obtain a semaphore lock to begin a critical section.
245 void begin_critical_section(int which_one)
247 /* lprintf(9, "begin_critical_section(%d)\n", which_one); */
248 pthread_mutex_lock(&Critters[which_one]);
252 * Release a semaphore lock to end a critical section.
254 void end_critical_section(int which_one)
256 /* lprintf(9, "end_critical_section(%d)\n", which_one); */
257 pthread_mutex_unlock(&Critters[which_one]);
263 * This is a generic function to set up a master socket for listening on
264 * a TCP port. The server shuts down if the bind fails.
267 int ig_tcp_server(int port_number, int queue_len)
269 struct sockaddr_in sin;
272 memset(&sin, 0, sizeof(sin));
273 sin.sin_family = AF_INET;
274 sin.sin_addr.s_addr = INADDR_ANY;
275 sin.sin_port = htons((u_short)port_number);
277 s = socket(PF_INET, SOCK_STREAM,
278 (getprotobyname("tcp")->p_proto));
281 lprintf(1, "citserver: Can't create a socket: %s\n",
287 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
289 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
290 lprintf(1, "citserver: Can't bind: %s\n",
295 if (listen(s, queue_len) < 0) {
296 lprintf(1, "citserver: Can't listen: %s\n", strerror(errno));
306 * Create a Unix domain socket and listen on it
308 int ig_uds_server(char *sockpath, int queue_len)
310 struct sockaddr_un addr;
315 memset(&addr, 0, sizeof(addr));
316 addr.sun_family = AF_UNIX;
317 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
319 s = socket(AF_UNIX, SOCK_STREAM, 0);
321 lprintf(1, "citserver: Can't create a socket: %s\n",
326 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
327 lprintf(1, "citserver: Can't bind: %s\n",
332 if (listen(s, queue_len) < 0) {
333 lprintf(1, "citserver: Can't listen: %s\n", strerror(errno));
337 chmod(sockpath, 0777);
344 * Return a pointer to the CitContext structure bound to the thread which
345 * called this function. If there's no such binding (for example, if it's
346 * called by the housekeeper thread) then a generic 'master' CC is returned.
348 struct CitContext *MyContext(void) {
349 struct CitContext *retCC;
350 retCC = (struct CitContext *) pthread_getspecific(MyConKey);
351 if (retCC == NULL) retCC = &masterCC;
357 * Initialize a new context and place it in the list.
359 struct CitContext *CreateNewContext(void) {
360 struct CitContext *me, *ptr;
364 me = (struct CitContext *) mallok(sizeof(struct CitContext));
366 lprintf(1, "citserver: can't allocate memory!!\n");
369 memset(me, 0, sizeof(struct CitContext));
371 /* The new context will be created already in the CON_EXECUTING state
372 * in order to prevent another thread from grabbing it while it's
375 me->state = CON_EXECUTING;
377 begin_critical_section(S_SESSION_TABLE);
379 /* obtain a unique session number */
382 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
383 if (ptr->cs_pid == num) {
388 } while (startover == 1);
391 me->next = ContextList;
395 end_critical_section(S_SESSION_TABLE);
402 * client_write() ... Send binary data to the client.
404 void client_write(char *buf, int nbytes)
406 int bytes_written = 0;
410 if (CC->redirect_fp != NULL) {
411 fwrite(buf, nbytes, 1, CC->redirect_fp);
415 if (CC->redirect_sock > 0) {
416 sock = CC->redirect_sock; /* and continue below... */
419 sock = CC->client_socket;
422 while (bytes_written < nbytes) {
423 retval = write(sock, &buf[bytes_written],
424 nbytes - bytes_written);
426 lprintf(2, "client_write() failed: %s\n",
428 if (sock == CC->client_socket) CC->kill_me = 1;
431 bytes_written = bytes_written + retval;
437 * cprintf() ... Send formatted printable data to the client. It is
438 * implemented in terms of client_write() but remains in
439 * sysdep.c in case we port to somewhere without va_args...
441 void cprintf(const char *format, ...) {
445 va_start(arg_ptr, format);
446 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
447 buf[sizeof buf - 2] = '\n';
448 client_write(buf, strlen(buf));
454 * Read data from the client socket.
456 * 1 Requested number of bytes has been read.
457 * 0 Request timed out.
458 * -1 The socket is broken.
459 * If the socket breaks, the session will be terminated.
461 int client_read_to(char *buf, int bytes, int timeout)
471 FD_SET(CC->client_socket, &rfds);
475 retval = select( (CC->client_socket)+1,
476 &rfds, NULL, NULL, &tv);
478 if (FD_ISSET(CC->client_socket, &rfds) == 0) {
482 rlen = read(CC->client_socket, &buf[len], bytes-len);
484 lprintf(2, "client_read() failed: %s\n",
495 * Read data from the client socket with default timeout.
496 * (This is implemented in terms of client_read_to() and could be
497 * justifiably moved out of sysdep.c)
499 int client_read(char *buf, int bytes)
501 return(client_read_to(buf, bytes, config.c_sleeping));
506 * client_gets() ... Get a LF-terminated line of text from the client.
507 * (This is implemented in terms of client_read() and could be
508 * justifiably moved out of sysdep.c)
510 int client_gets(char *buf)
514 /* Read one character at a time.
517 retval = client_read(&buf[i], 1);
518 if (retval != 1 || buf[i] == '\n' || i == 255)
522 /* If we got a long line, discard characters until the newline.
525 while (buf[i] != '\n' && retval == 1)
526 retval = client_read(&buf[i], 1);
528 /* Strip the trailing newline and any trailing nonprintables (cr's)
531 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
532 buf[strlen(buf)-1] = 0;
539 * The system-dependent part of master_cleanup() - close the master socket.
541 void sysdep_master_cleanup(void) {
542 struct ServiceFunctionHook *serviceptr;
545 * close all protocol master sockets
547 for (serviceptr = ServiceHookTable; serviceptr != NULL;
548 serviceptr = serviceptr->next ) {
549 lprintf(3, "Closing listener on port %d\n",
550 serviceptr->tcp_port);
551 close(serviceptr->msock);
553 /* If it's a Unix domain socket, remove the file. */
554 if (serviceptr->sockpath != NULL) {
555 unlink(serviceptr->sockpath);
562 * Terminate another session.
563 * (This could justifiably be moved out of sysdep.c because it
564 * no longer does anything that is system-dependent.)
566 void kill_session(int session_to_kill) {
567 struct CitContext *ptr;
569 begin_critical_section(S_SESSION_TABLE);
570 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
571 if (ptr->cs_pid == session_to_kill) {
575 end_critical_section(S_SESSION_TABLE);
582 * Start running as a daemon. Only close stdio if do_close_stdio is set.
584 void start_daemon(int do_close_stdio) {
585 if (do_close_stdio) {
590 signal(SIGHUP,SIG_IGN);
591 signal(SIGINT,SIG_IGN);
592 signal(SIGQUIT,SIG_IGN);
593 if (fork()!=0) exit(0);
599 * Tie in to the 'netsetup' program.
601 * (We're going to hope that netsetup never feeds more than 4096 bytes back.)
603 void cmd_nset(char *cmdbuf)
610 char netsetup_args[3][256];
612 if (CC->usersupp.axlevel < 6) {
613 cprintf("%d Higher access required.\n",
614 ERROR + HIGHER_ACCESS_REQUIRED);
618 for (a=1; a<=3; ++a) {
619 if (num_parms(cmdbuf) >= a) {
620 extract(netsetup_args[a-1], cmdbuf, a-1);
621 for (b=0; b<strlen(netsetup_args[a-1]); ++b) {
622 if (netsetup_args[a-1][b] == 34) {
623 netsetup_args[a-1][b] = '_';
628 netsetup_args[a-1][0] = 0;
632 sprintf(fbuf, "./netsetup \"%s\" \"%s\" \"%s\" </dev/null 2>&1",
633 netsetup_args[0], netsetup_args[1], netsetup_args[2]);
634 netsetup = popen(fbuf, "r");
635 if (netsetup == NULL) {
636 cprintf("%d %s\n", ERROR, strerror(errno));
641 while (ch = getc(netsetup), (ch > 0)) {
642 fbuf[strlen(fbuf)+1] = 0;
643 fbuf[strlen(fbuf)] = ch;
646 retcode = pclose(netsetup);
649 for (a=0; a<strlen(fbuf); ++a) {
650 if (fbuf[a] < 32) fbuf[a] = 32;
653 cprintf("%d %s\n", ERROR, fbuf);
657 cprintf("%d Command succeeded. Output follows:\n", LISTING_FOLLOWS);
659 if (fbuf[strlen(fbuf)-1] != 10) cprintf("\n");
666 * Generic routine to convert a login name to a full name (gecos)
667 * Returns nonzero if a conversion took place
669 int convert_login(char NameToConvert[]) {
673 pw = getpwnam(NameToConvert);
678 strcpy(NameToConvert, pw->pw_gecos);
679 for (a=0; a<strlen(NameToConvert); ++a) {
680 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
689 * Purge all sessions which have the 'kill_me' flag set.
690 * This function has code to prevent it from running more than once every
691 * few seconds, because running it after every single unbind would waste a lot
692 * of CPU time and keep the context list locked too much.
694 * After that's done, we raise or lower the size of the worker thread pool
695 * if such an action is appropriate.
697 void dead_session_purge(void) {
698 struct CitContext *ptr, *rem;
702 if ( (time(NULL) - last_purge) < 5 ) return; /* Too soon, go away */
707 begin_critical_section(S_SESSION_TABLE);
708 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
709 if ( (ptr->state == CON_IDLE) && (ptr->kill_me) ) {
713 end_critical_section(S_SESSION_TABLE);
715 /* RemoveContext() enters its own S_SESSION_TABLE critical
716 * section, so we have to do it like this.
719 lprintf(9, "Purging session %d\n", rem->cs_pid);
723 } while (rem != NULL);
726 /* Raise or lower the size of the worker thread pool if such
727 * an action is appropriate.
730 if ( (num_sessions > num_threads)
731 && (num_threads < config.c_max_workers) ) {
733 pthread_attr_init(&attr);
734 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
735 if (pthread_create(&newthread, &attr,
736 (void* (*)(void*)) worker_thread, NULL) != 0) {
737 lprintf(1, "Can't create worker thead: %s\n",
743 else if ( (num_sessions < num_threads)
744 && (num_threads > config.c_min_workers) ) {
756 * Redirect a session's output to a file or socket.
757 * This function may be called with a file handle *or* a socket (but not
758 * both). Call with neither to return output to its normal client socket.
760 void CtdlRedirectOutput(FILE *fp, int sock) {
762 if (fp != NULL) CC->redirect_fp = fp;
763 else CC->redirect_fp = NULL;
765 if (sock > 0) CC->redirect_sock = sock;
766 else CC->redirect_sock = (-1);
772 * masterCC is the context we use when not attached to a session. This
773 * function initializes it.
775 void InitializeMasterCC(void) {
776 memset(&masterCC, 0, sizeof(struct CitContext));
777 masterCC.internal_pgm = 1;
783 * Here's where it all begins.
785 int main(int argc, char **argv)
787 pthread_t HousekeepingThread; /* Thread descriptor */
788 pthread_attr_t attr; /* Thread attributes */
789 char tracefile[128]; /* Name of file to log traces to */
790 int a, i; /* General-purpose variables */
792 int drop_root_perms = 1;
794 struct ServiceFunctionHook *serviceptr;
796 /* specify default port name and trace file */
797 strcpy(tracefile, "");
799 /* initialize the master context */
800 InitializeMasterCC();
802 /* parse command-line arguments */
803 for (a=1; a<argc; ++a) {
805 /* -t specifies where to log trace messages to */
806 if (!strncmp(argv[a], "-t", 2)) {
807 strcpy(tracefile, argv[a]);
808 strcpy(tracefile, &tracefile[2]);
809 freopen(tracefile, "r", stdin);
810 freopen(tracefile, "w", stdout);
811 freopen(tracefile, "w", stderr);
814 /* run in the background if -d was specified */
815 else if (!strcmp(argv[a], "-d")) {
816 start_daemon( (strlen(tracefile) > 0) ? 0 : 1 ) ;
819 /* -x specifies the desired logging level */
820 else if (!strncmp(argv[a], "-x", 2)) {
821 verbosity = atoi(&argv[a][2]);
824 else if (!strncmp(argv[a], "-h", 2)) {
825 safestrncpy(bbs_home_directory, &argv[a][2],
826 sizeof bbs_home_directory);
830 else if (!strncmp(argv[a], "-f", 2)) {
834 /* -r tells the server not to drop root permissions. don't use
835 * this unless you know what you're doing. this should be
836 * removed in the next release if it proves unnecessary. */
837 else if (!strcmp(argv[a], "-r"))
840 /* any other parameter makes it crash and burn */
842 lprintf(1, "citserver: usage: "
843 "citserver [-tTraceFile] [-d] [-f]"
844 " [-xLogLevel] [-hHomeDir]\n");
850 /* Tell 'em who's in da house */
852 "\nMultithreaded message server for Citadel/UX\n"
853 "Copyright (C) 1987-2000 by the Citadel/UX development team.\n"
854 "Citadel/UX is free software, covered by the GNU General Public License, and\n"
855 "you are welcome to change it and/or distribute copies of it under certain\n"
856 "conditions. There is absolutely no warranty for this software. Please\n"
857 "read the 'COPYING.txt' file for details.\n\n");
861 openlog("citserver", LOG_PID, LOG_USER);
863 /* Load site-specific parameters */
864 lprintf(7, "Loading citadel.config\n");
868 * Do non system dependent startup functions.
873 * Bind the server to our favorite ports.
875 CtdlRegisterServiceHook(0, /* Unix */
877 citproto_begin_session,
879 CtdlRegisterServiceHook(config.c_port_number, /* TCP */
881 citproto_begin_session,
885 * Load any server-side modules (plugins) available here.
887 lprintf(7, "Initializing loadable modules\n");
888 if ((moddir = malloc(strlen(bbs_home_directory) + 9)) != NULL) {
889 sprintf(moddir, "%s/modules", bbs_home_directory);
890 DLoader_Init(moddir);
895 * The rescan pipe exists so that worker threads can be woken up and
896 * told to re-scan the context list for fd's to listen on. This is
897 * necessary, for example, when a context is about to go idle and needs
898 * to get back on that list.
901 lprintf(1, "Can't create rescan pipe!\n");
906 * Set up a fd_set containing all the master sockets to which we
907 * always listen. It's computationally less expensive to just copy
908 * this to a local fd_set when starting a new select() and then add
909 * the client sockets than it is to initialize a new one and then
910 * figure out what to put there.
914 FD_SET(rescan[0], &masterfds);
915 if (rescan[0] > masterhighest) masterhighest = rescan[0];
917 for (serviceptr = ServiceHookTable; serviceptr != NULL;
918 serviceptr = serviceptr->next ) {
919 lprintf(9, "Will listen on master socket %d\n",
921 FD_SET(serviceptr->msock, &masterfds);
922 if (serviceptr->msock > masterhighest) {
923 masterhighest = serviceptr->msock;
929 * Now that we've bound the sockets, change to the BBS user id and its
930 * corresponding group ids
932 if (drop_root_perms) {
933 if ((pw = getpwuid(BBSUID)) == NULL)
934 lprintf(1, "WARNING: getpwuid(%d): %s\n"
935 "Group IDs will be incorrect.\n", BBSUID,
938 initgroups(pw->pw_name, pw->pw_gid);
939 if (setgid(pw->pw_gid))
940 lprintf(3, "setgid(%d): %s\n", pw->pw_gid,
943 lprintf(7, "Changing uid to %d\n", BBSUID);
944 if (setuid(BBSUID) != 0) {
945 lprintf(3, "setuid() failed: %s\n", strerror(errno));
950 * Create the housekeeper thread
952 lprintf(7, "Starting housekeeper thread\n");
953 pthread_attr_init(&attr);
954 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
955 if (pthread_create(&HousekeepingThread, &attr,
956 (void* (*)(void*)) housekeeping_loop, NULL) != 0) {
957 lprintf(1, "Can't create housekeeping thead: %s\n",
963 * Now create a bunch of worker threads.
965 for (i=0; i<(config.c_min_workers-1); ++i) {
966 pthread_attr_init(&attr);
967 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
968 if (pthread_create(&HousekeepingThread, &attr,
969 (void* (*)(void*)) worker_thread, NULL) != 0) {
970 lprintf(1, "Can't create worker thead: %s\n",
975 /* Now this thread can become a worker as well. */
983 * Bind a thread to a context.
985 inline void become_session(struct CitContext *which_con) {
986 pthread_setspecific(MyConKey, (void *)which_con );
992 * This loop just keeps going and going and going...
994 void worker_thread(void) {
998 struct CitContext *ptr;
999 struct CitContext *bind_me = NULL;
1002 struct CitContext *con= NULL; /* Temporary context pointer */
1003 struct ServiceFunctionHook *serviceptr;
1004 struct sockaddr_in fsin; /* Data for master socket */
1005 int alen; /* Data for master socket */
1006 int ssock; /* Descriptor for client socket */
1011 while (!time_to_die) {
1014 * A naive implementation would have all idle threads
1015 * calling select() and then they'd all wake up at once. We
1016 * solve this problem by putting the select() in a critical
1017 * section, so only one thread has the opportunity to wake
1018 * up. If we wake up on the master socket, create a new
1019 * session context; otherwise, just bind the thread to the
1020 * context we want and go on our merry way.
1023 begin_critical_section(S_I_WANNA_SELECT);
1024 SETUP_FD: memcpy(&readfds, &masterfds, sizeof(fd_set) );
1025 highest = masterhighest;
1026 begin_critical_section(S_SESSION_TABLE);
1027 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1028 if (ptr->state == CON_IDLE) {
1029 FD_SET(ptr->client_socket, &readfds);
1030 if (ptr->client_socket > highest)
1031 highest = ptr->client_socket;
1034 end_critical_section(S_SESSION_TABLE);
1036 tv.tv_sec = 60; /* wake up every minute if no input */
1038 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
1040 /* Now figure out who made this select() unblock.
1041 * First, check for an error or exit condition.
1044 end_critical_section(S_I_WANNA_SELECT);
1045 lprintf(9, "Exiting (%s)\n", strerror(errno));
1049 /* Next, check to see if it's a new client connecting
1050 * on a master socket.
1052 else for (serviceptr = ServiceHookTable; serviceptr != NULL;
1053 serviceptr = serviceptr->next ) {
1055 if (FD_ISSET(serviceptr->msock, &readfds)) {
1057 ssock = accept(serviceptr->msock,
1058 (struct sockaddr *)&fsin, &alen);
1060 lprintf(2, "citserver: accept(): %s\n",
1064 lprintf(7, "citserver: "
1065 "New client socket %d\n",
1068 /* New context will be created already
1069 * set up in the CON_EXECUTING state.
1071 con = CreateNewContext();
1073 /* Assign new socket number to it. */
1074 con->client_socket = ssock;
1075 con->h_command_function =
1076 serviceptr->h_command_function;
1078 /* Determine whether local socket */
1079 if (serviceptr->sockpath != NULL)
1080 con->is_local_socket = 1;
1082 /* Set the SO_REUSEADDR socket option */
1084 setsockopt(ssock, SOL_SOCKET,
1088 become_session(con);
1090 serviceptr->h_greeting_function();
1091 become_session(NULL);
1092 con->state = CON_IDLE;
1098 /* If the rescan pipe went active, someone is telling this
1099 * thread that the &readfds needs to be refreshed with more
1105 if (FD_ISSET(rescan[0], &readfds)) {
1106 read(rescan[0], &junk, 1);
1110 /* It must be a client socket. Find a context that has data
1111 * waiting on its socket *and* is in the CON_IDLE state.
1115 begin_critical_section(S_SESSION_TABLE);
1116 for (ptr = ContextList;
1117 ( (ptr != NULL) && (bind_me == NULL) );
1119 if ( (FD_ISSET(ptr->client_socket, &readfds))
1120 && (ptr->state == CON_IDLE) ) {
1124 if (bind_me != NULL) {
1125 /* Found one. Stake a claim to it before
1126 * letting anyone else touch the context list.
1128 bind_me->state = CON_EXECUTING;
1131 end_critical_section(S_SESSION_TABLE);
1132 end_critical_section(S_I_WANNA_SELECT);
1134 /* We're bound to a session, now do *one* command */
1135 if (bind_me != NULL) {
1136 become_session(bind_me);
1137 CC->h_command_function();
1138 become_session(NULL);
1139 bind_me->state = CON_IDLE;
1140 if (bind_me->kill_me == 1) {
1141 RemoveContext(bind_me);
1143 write(rescan[1], &junk, 1);
1147 dead_session_purge();
1148 if ((time(NULL) - last_timer) > 60L) {
1149 last_timer = time(NULL);
1150 PerformSessionHooks(EVT_TIMER);
1154 /* If control reaches this point, the server is shutting down */