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 * If we ever port to a different platform and either have multiple
11 * variants of this file or simply load it up with #ifdefs.
26 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/syslog.h>
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
37 # include <sys/time.h>
44 #include <netinet/in.h>
57 #include "serv_extensions.h"
58 #include "sysdep_decls.h"
59 #include "citserver.h"
63 #include "housekeeping.h"
65 #include "serv_crypto.h"
67 #ifdef HAVE_SYS_SELECT_H
68 #include <sys/select.h>
75 #ifdef DEBUG_MEMORY_LEAKS
76 struct TheHeap *heap = NULL;
79 pthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */
80 pthread_key_t MyConKey; /* TSD key for MyContext() */
82 int verbosity = DEFAULT_VERBOSITY; /* Logging level */
84 struct CitContext masterCC;
85 int rescan[2]; /* The Rescan Pipe */
86 time_t last_purge = 0; /* Last dead session purge */
87 static int num_threads = 0; /* Current number of threads */
88 int num_sessions = 0; /* Current number of sessions */
90 fd_set masterfds; /* Master sockets etc. */
93 pthread_t initial_thread; /* tid for main() thread */
95 int syslog_facility = (-1);
99 * lprintf() ... Write logging information
101 * Note: the variable "buf" below needs to be large enough to handle any
102 * log data sent through this function. BE CAREFUL!
104 void lprintf(int loglevel, const char *format, ...) {
108 va_start(arg_ptr, format);
109 vsnprintf(buf, sizeof(buf), format, arg_ptr);
112 if (syslog_facility >= 0) {
113 if (loglevel <= verbosity) {
114 syslog(LOG_NOTICE, buf);
117 else if (loglevel <= verbosity) {
122 gettimeofday(&tv, NULL);
123 /* Promote to time_t; types differ on some OSes (like darwin) */
124 unixtime = tv.tv_sec;
125 tim = localtime(&unixtime);
127 * Log provides millisecond accuracy. If you need
128 * microsecond accuracy and your OS supports it, change
129 * %03ld to %06ld and remove " / 1000" after tv.tv_usec.
131 if (CC && CC->cs_pid) {
133 "%04d/%02d/%02d %2d:%02d:%02d.%03ld [%3d] %s",
134 tim->tm_year + 1900, tim->tm_mon + 1,
135 tim->tm_mday, tim->tm_hour, tim->tm_min,
136 tim->tm_sec, (long)tv.tv_usec / 1000,
140 "%04d/%02d/%02d %2d:%02d:%02d.%03ld %s",
141 tim->tm_year + 1900, tim->tm_mon + 1,
142 tim->tm_mday, tim->tm_hour, tim->tm_min,
143 tim->tm_sec, (long)tv.tv_usec / 1000, buf);
148 PerformLogHooks(loglevel, buf);
153 #ifdef DEBUG_MEMORY_LEAKS
154 void *tracked_malloc(size_t tsize, char *tfile, int tline) {
156 struct TheHeap *hptr;
160 lprintf(3, "DANGER! mallok(%d) at %s:%d failed!\n",
161 tsize, tfile, tline);
165 hptr = (struct TheHeap *) malloc(sizeof(struct TheHeap));
166 strcpy(hptr->h_file, tfile);
167 hptr->h_line = tline;
174 char *tracked_strdup(const char *orig, char *tfile, int tline) {
177 s = tracked_malloc( (strlen(orig)+1), tfile, tline);
178 if (s == NULL) return NULL;
184 void tracked_free(void *ptr) {
185 struct TheHeap *hptr, *freeme;
187 if (heap->h_ptr == ptr) {
193 for (hptr=heap; hptr->next!=NULL; hptr=hptr->next) {
194 if (hptr->next->h_ptr == ptr) {
196 hptr->next = hptr->next->next;
205 void *tracked_realloc(void *ptr, size_t size) {
207 struct TheHeap *hptr;
209 newptr = realloc(ptr, size);
211 for (hptr=heap; hptr!=NULL; hptr=hptr->next) {
212 if (hptr->h_ptr == ptr) hptr->h_ptr = newptr;
219 void dump_tracked() {
220 struct TheHeap *hptr;
222 cprintf("%d Here's what's allocated...\n", LISTING_FOLLOWS);
223 for (hptr=heap; hptr!=NULL; hptr=hptr->next) {
224 cprintf("%20s %5d\n",
225 hptr->h_file, hptr->h_line);
237 * We used to use master_cleanup() as a signal handler to shut down the server.
238 * however, master_cleanup() and the functions it calls do some things that
239 * aren't such a good idea to do from a signal handler: acquiring mutexes,
240 * playing with signal masks on BSDI systems, etc. so instead we install the
241 * following signal handler to set a global variable to inform the main loop
242 * that it's time to call master_cleanup() and exit.
245 volatile int time_to_die = 0;
247 static RETSIGTYPE signal_cleanup(int signum) {
253 * Some initialization stuff...
255 void init_sysdep(void) {
262 /* Set up a bunch of semaphores to be used for critical sections */
263 for (a=0; a<MAX_SEMAPHORES; ++a) {
264 pthread_mutex_init(&Critters[a], NULL);
268 * Set up a place to put thread-specific data.
269 * We only need a single pointer per thread - it points to the
270 * CitContext structure (in the ContextList linked list) of the
271 * session to which the calling thread is currently bound.
273 if (pthread_key_create(&MyConKey, NULL) != 0) {
274 lprintf(1, "Can't create TSD key!! %s\n", strerror(errno));
278 * The action for unexpected signals and exceptions should be to
279 * call signal_cleanup() to gracefully shut down the server.
281 signal(SIGINT, signal_cleanup);
282 signal(SIGQUIT, signal_cleanup);
283 signal(SIGHUP, signal_cleanup);
284 signal(SIGTERM, signal_cleanup);
287 * Do not shut down the server on broken pipe signals, otherwise the
288 * whole Citadel service would come down whenever a single client
291 signal(SIGPIPE, SIG_IGN);
296 * Obtain a semaphore lock to begin a critical section.
298 void begin_critical_section(int which_one)
300 /* lprintf(9, "begin_critical_section(%d)\n", which_one); */
301 /* ensure nobody ever tries to do a critical section within a
302 transaction; this could lead to deadlock. */
304 pthread_mutex_lock(&Critters[which_one]);
308 * Release a semaphore lock to end a critical section.
310 void end_critical_section(int which_one)
312 /* lprintf(9, "end_critical_section(%d)\n", which_one); */
313 pthread_mutex_unlock(&Critters[which_one]);
319 * This is a generic function to set up a master socket for listening on
320 * a TCP port. The server shuts down if the bind fails.
323 int ig_tcp_server(int port_number, int queue_len)
325 struct sockaddr_in sin;
327 int actual_queue_len;
329 actual_queue_len = queue_len;
330 if (actual_queue_len < 5) actual_queue_len = 5;
332 memset(&sin, 0, sizeof(sin));
333 sin.sin_family = AF_INET;
334 sin.sin_addr.s_addr = INADDR_ANY;
335 sin.sin_port = htons((u_short)port_number);
337 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
340 lprintf(1, "citserver: Can't create a socket: %s\n",
346 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
348 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
349 lprintf(1, "citserver: Can't bind: %s\n",
355 if (listen(s, actual_queue_len) < 0) {
356 lprintf(1, "citserver: Can't listen: %s\n", strerror(errno));
367 * Create a Unix domain socket and listen on it
369 int ig_uds_server(char *sockpath, int queue_len)
371 struct sockaddr_un addr;
374 int actual_queue_len;
376 actual_queue_len = queue_len;
377 if (actual_queue_len < 5) actual_queue_len = 5;
379 i = unlink(sockpath);
380 if (i != 0) if (errno != ENOENT) {
381 lprintf(1, "citserver: can't unlink %s: %s\n",
382 sockpath, strerror(errno));
386 memset(&addr, 0, sizeof(addr));
387 addr.sun_family = AF_UNIX;
388 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
390 s = socket(AF_UNIX, SOCK_STREAM, 0);
392 lprintf(1, "citserver: Can't create a socket: %s\n",
397 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
398 lprintf(1, "citserver: Can't bind: %s\n",
403 if (listen(s, actual_queue_len) < 0) {
404 lprintf(1, "citserver: Can't listen: %s\n", strerror(errno));
408 chmod(sockpath, 0777);
415 * Return a pointer to the CitContext structure bound to the thread which
416 * called this function. If there's no such binding (for example, if it's
417 * called by the housekeeper thread) then a generic 'master' CC is returned.
419 * It's inlined because it's used *VERY* frequently.
421 INLINE struct CitContext *MyContext(void) {
422 return ((pthread_getspecific(MyConKey) == NULL)
424 : (struct CitContext *) pthread_getspecific(MyConKey)
430 * Initialize a new context and place it in the list. The session number
431 * used to be the PID (which is why it's called cs_pid), but that was when we
432 * had one process per session. Now we just assign them sequentially, starting
433 * at 1 (don't change it to 0 because masterCC uses 0) and re-using them when
434 * sessions terminate.
436 struct CitContext *CreateNewContext(void) {
437 struct CitContext *me, *ptr;
439 me = (struct CitContext *) mallok(sizeof(struct CitContext));
441 lprintf(1, "citserver: can't allocate memory!!\n");
444 memset(me, 0, sizeof(struct CitContext));
446 /* The new context will be created already in the CON_EXECUTING state
447 * in order to prevent another thread from grabbing it while it's
450 me->state = CON_EXECUTING;
454 * Generate a unique session number and insert this context into
457 begin_critical_section(S_SESSION_TABLE);
459 if (ContextList == NULL) {
465 else if (ContextList->cs_pid > 1) {
466 me->next = ContextList;
472 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
473 if (ptr->next == NULL) {
475 me->cs_pid = ptr->cs_pid + 1;
479 else if (ptr->next->cs_pid > (ptr->cs_pid+1)) {
480 me->next = ptr->next;
482 me->cs_pid = ptr->cs_pid + 1;
488 DONE: ++num_sessions;
489 end_critical_section(S_SESSION_TABLE);
495 * buffer_output() ... tell client_write to buffer all output until
496 * instructed to dump it all out later
498 void buffer_output(void) {
499 if (CC->buffering == 0) {
502 CC->output_buffer = mallok(SIZ);
507 * unbuffer_output() ... dump out all that output we've been buffering.
509 void unbuffer_output(void) {
510 if (CC->buffering == 1) {
512 client_write(CC->output_buffer, CC->buffer_len);
513 phree(CC->output_buffer);
514 CC->output_buffer = NULL;
522 * client_write() ... Send binary data to the client.
524 void client_write(char *buf, int nbytes)
526 int bytes_written = 0;
529 int old_buffer_len = 0;
531 if (CC->redirect_fp != NULL) {
532 fwrite(buf, nbytes, 1, CC->redirect_fp);
536 if (CC->redirect_sock > 0) {
537 sock = CC->redirect_sock; /* and continue below... */
540 sock = CC->client_socket;
543 /* If we're buffering for later, do that now. */
545 old_buffer_len = CC->buffer_len;
546 CC->buffer_len += nbytes;
547 CC->output_buffer = reallok(CC->output_buffer, CC->buffer_len);
548 memcpy(&CC->output_buffer[old_buffer_len], buf, nbytes);
552 /* Ok, at this point we're not buffering. Go ahead and write. */
555 if (CC->redirect_ssl) {
556 client_write_ssl(buf, nbytes);
561 while (bytes_written < nbytes) {
562 retval = write(sock, &buf[bytes_written],
563 nbytes - bytes_written);
565 lprintf(2, "client_write() failed: %s\n",
567 if (sock == CC->client_socket) CC->kill_me = 1;
570 bytes_written = bytes_written + retval;
576 * cprintf() ... Send formatted printable data to the client. It is
577 * implemented in terms of client_write() but remains in
578 * sysdep.c in case we port to somewhere without va_args...
580 void cprintf(const char *format, ...) {
584 va_start(arg_ptr, format);
585 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
586 buf[sizeof buf - 2] = '\n';
587 client_write(buf, strlen(buf));
593 * Read data from the client socket.
595 * 1 Requested number of bytes has been read.
596 * 0 Request timed out.
597 * -1 The socket is broken.
598 * If the socket breaks, the session will be terminated.
600 int client_read_to(char *buf, int bytes, int timeout)
608 if (CC->redirect_ssl) {
609 return (client_read_ssl(buf, bytes, timeout));
615 FD_SET(CC->client_socket, &rfds);
619 retval = select( (CC->client_socket)+1,
620 &rfds, NULL, NULL, &tv);
622 if (FD_ISSET(CC->client_socket, &rfds) == 0) {
626 rlen = read(CC->client_socket, &buf[len], bytes-len);
628 lprintf(2, "client_read() failed: %s\n",
639 * Read data from the client socket with default timeout.
640 * (This is implemented in terms of client_read_to() and could be
641 * justifiably moved out of sysdep.c)
643 INLINE int client_read(char *buf, int bytes)
645 return(client_read_to(buf, bytes, config.c_sleeping));
650 * client_gets() ... Get a LF-terminated line of text from the client.
651 * (This is implemented in terms of client_read() and could be
652 * justifiably moved out of sysdep.c)
654 int client_gets(char *buf)
658 /* Read one character at a time.
661 retval = client_read(&buf[i], 1);
662 if (retval != 1 || buf[i] == '\n' || i == (SIZ-1))
666 /* If we got a long line, discard characters until the newline.
669 while (buf[i] != '\n' && retval == 1)
670 retval = client_read(&buf[i], 1);
672 /* Strip the trailing newline and any trailing nonprintables (cr's)
675 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
676 buf[strlen(buf)-1] = 0;
677 if (retval < 0) strcpy(buf, "000");
684 * The system-dependent part of master_cleanup() - close the master socket.
686 void sysdep_master_cleanup(void) {
687 struct ServiceFunctionHook *serviceptr;
690 * close all protocol master sockets
692 for (serviceptr = ServiceHookTable; serviceptr != NULL;
693 serviceptr = serviceptr->next ) {
695 if (serviceptr->tcp_port > 0)
696 lprintf(3, "Closing listener on port %d\n",
697 serviceptr->tcp_port);
699 if (serviceptr->sockpath != NULL)
700 lprintf(3, "Closing listener on '%s'\n",
701 serviceptr->sockpath);
703 close(serviceptr->msock);
705 /* If it's a Unix domain socket, remove the file. */
706 if (serviceptr->sockpath != NULL) {
707 unlink(serviceptr->sockpath);
714 * Terminate another session.
715 * (This could justifiably be moved out of sysdep.c because it
716 * no longer does anything that is system-dependent.)
718 void kill_session(int session_to_kill) {
719 struct CitContext *ptr;
721 begin_critical_section(S_SESSION_TABLE);
722 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
723 if (ptr->cs_pid == session_to_kill) {
727 end_critical_section(S_SESSION_TABLE);
734 * Start running as a daemon. Only close stdio if do_close_stdio is set.
736 void start_daemon(int do_close_stdio) {
737 if (do_close_stdio) {
742 signal(SIGHUP,SIG_IGN);
743 signal(SIGINT,SIG_IGN);
744 signal(SIGQUIT,SIG_IGN);
745 if (fork()!=0) exit(0);
751 * Generic routine to convert a login name to a full name (gecos)
752 * Returns nonzero if a conversion took place
754 int convert_login(char NameToConvert[]) {
758 pw = getpwnam(NameToConvert);
763 strcpy(NameToConvert, pw->pw_gecos);
764 for (a=0; a<strlen(NameToConvert); ++a) {
765 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
771 struct worker_node *worker_list = NULL;
775 * create a worker thread. this function must always be called from within
776 * an S_WORKER_LIST critical section!
778 void create_worker(void) {
780 struct worker_node *n;
783 n = mallok(sizeof(struct worker_node));
785 lprintf(1, "can't allocate worker_node, exiting\n");
790 if ((ret = pthread_attr_init(&attr))) {
791 lprintf(1, "pthread_attr_init: %s\n", strerror(ret));
796 /* we seem to need something bigger than FreeBSD's default 64k stack */
798 if ((ret = pthread_attr_setstacksize(&attr, 128 * 1024))) {
799 lprintf(1, "pthread_attr_setstacksize: %s\n", strerror(ret));
804 if ((ret = pthread_create(&n->tid, &attr, worker_thread, NULL) != 0))
807 lprintf(1, "Can't create worker thread: %s\n",
811 n->next = worker_list;
818 * Purge all sessions which have the 'kill_me' flag set.
819 * This function has code to prevent it from running more than once every
820 * few seconds, because running it after every single unbind would waste a lot
821 * of CPU time and keep the context list locked too much.
823 * After that's done, we raise or lower the size of the worker thread pool
824 * if such an action is appropriate.
826 void dead_session_purge(void) {
827 struct CitContext *ptr, *rem;
828 struct worker_node **node, *tmp;
831 if ( (time(NULL) - last_purge) < 5 ) return; /* Too soon, go away */
836 begin_critical_section(S_SESSION_TABLE);
837 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
838 if ( (ptr->state == CON_IDLE) && (ptr->kill_me) ) {
842 end_critical_section(S_SESSION_TABLE);
844 /* RemoveContext() enters its own S_SESSION_TABLE critical
845 * section, so we have to do it like this.
848 lprintf(9, "Purging session %d\n", rem->cs_pid);
852 } while (rem != NULL);
855 /* Raise or lower the size of the worker thread pool if such
856 * an action is appropriate.
859 self = pthread_self();
861 if ( (num_sessions > num_threads)
862 && (num_threads < config.c_max_workers) ) {
863 begin_critical_section(S_WORKER_LIST);
865 end_critical_section(S_WORKER_LIST);
868 /* don't let the initial thread die since it's responsible for
869 waiting for all the other threads to terminate. */
870 else if ( (num_sessions < num_threads)
871 && (num_threads > config.c_min_workers)
872 && (self != initial_thread) ) {
874 begin_critical_section(S_WORKER_LIST);
877 /* we're exiting before server shutdown... unlink ourself from
878 the worker list and detach our thread to avoid memory leaks
881 for (node = &worker_list; *node != NULL; node = &(*node)->next)
882 if ((*node)->tid == self) {
884 *node = (*node)->next;
889 pthread_detach(self);
890 end_critical_section(S_WORKER_LIST);
901 * Redirect a session's output to a file or socket.
902 * This function may be called with a file handle *or* a socket (but not
903 * both). Call with neither to return output to its normal client socket.
905 void CtdlRedirectOutput(FILE *fp, int sock) {
907 if (fp != NULL) CC->redirect_fp = fp;
908 else CC->redirect_fp = NULL;
910 if (sock > 0) CC->redirect_sock = sock;
911 else CC->redirect_sock = (-1);
917 * masterCC is the context we use when not attached to a session. This
918 * function initializes it.
920 void InitializeMasterCC(void) {
921 memset(&masterCC, 0, sizeof(struct CitContext));
922 masterCC.internal_pgm = 1;
929 * Set up a fd_set containing all the master sockets to which we
930 * always listen. It's computationally less expensive to just copy
931 * this to a local fd_set when starting a new select() and then add
932 * the client sockets than it is to initialize a new one and then
933 * figure out what to put there.
935 void init_master_fdset(void) {
936 struct ServiceFunctionHook *serviceptr;
939 lprintf(9, "Initializing master fdset\n");
944 lprintf(9, "Will listen on rescan pipe %d\n", rescan[0]);
945 FD_SET(rescan[0], &masterfds);
946 if (rescan[0] > masterhighest) masterhighest = rescan[0];
948 for (serviceptr = ServiceHookTable; serviceptr != NULL;
949 serviceptr = serviceptr->next ) {
950 m = serviceptr->msock;
951 lprintf(9, "Will listen on master socket %d\n", m);
952 FD_SET(m, &masterfds);
953 if (m > masterhighest) {
957 lprintf(9, "masterhighest = %d\n", masterhighest);
962 * Bind a thread to a context. (It's inline merely to speed things up.)
964 INLINE void become_session(struct CitContext *which_con) {
965 pthread_setspecific(MyConKey, (void *)which_con );
971 * This loop just keeps going and going and going...
973 void *worker_thread(void *arg) {
977 struct CitContext *ptr;
978 struct CitContext *bind_me = NULL;
981 struct CitContext *con= NULL; /* Temporary context pointer */
982 struct ServiceFunctionHook *serviceptr;
983 int ssock; /* Descriptor for client socket */
990 while (!time_to_die) {
993 * A naive implementation would have all idle threads
994 * calling select() and then they'd all wake up at once
995 * (known in computer science as the "thundering herd"
996 * problem). We solve this problem by putting the select()
997 * in a critical section, so only one thread has the
998 * opportunity to wake up. If we wake up on a master
999 * socket, create a new session context; otherwise, just
1000 * bind the thread to the context we want and go on our
1004 /* make doubly sure we're not holding any stale db handles
1005 * which might cause a deadlock.
1007 cdb_check_handles();
1009 begin_critical_section(S_I_WANNA_SELECT);
1010 SETUP_FD: memcpy(&readfds, &masterfds, sizeof masterfds);
1011 highest = masterhighest;
1012 begin_critical_section(S_SESSION_TABLE);
1013 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1014 if (ptr->state == CON_IDLE) {
1015 FD_SET(ptr->client_socket, &readfds);
1016 if (ptr->client_socket > highest)
1017 highest = ptr->client_socket;
1020 end_critical_section(S_SESSION_TABLE);
1022 tv.tv_sec = 1; /* wake up every second if no input */
1027 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
1029 end_critical_section(S_I_WANNA_SELECT);
1033 /* Now figure out who made this select() unblock.
1034 * First, check for an error or exit condition.
1037 if (errno != EINTR) {
1038 lprintf(9, "Exiting (%s)\n", strerror(errno));
1040 } else if (!time_to_die)
1044 /* Next, check to see if it's a new client connecting
1045 * on a master socket.
1047 else for (serviceptr = ServiceHookTable; serviceptr != NULL;
1048 serviceptr = serviceptr->next ) {
1050 if (FD_ISSET(serviceptr->msock, &readfds)) {
1051 ssock = accept(serviceptr->msock, NULL, 0);
1053 lprintf(2, "citserver: accept(): %s\n",
1057 lprintf(7, "citserver: "
1058 "New client socket %d\n",
1061 /* New context will be created already
1062 * set up in the CON_EXECUTING state.
1064 con = CreateNewContext();
1066 /* Assign new socket number to it. */
1067 con->client_socket = ssock;
1068 con->h_command_function =
1069 serviceptr->h_command_function;
1071 /* Determine whether local socket */
1072 if (serviceptr->sockpath != NULL)
1073 con->is_local_socket = 1;
1075 /* Set the SO_REUSEADDR socket option */
1077 setsockopt(ssock, SOL_SOCKET,
1081 become_session(con);
1083 serviceptr->h_greeting_function();
1084 become_session(NULL);
1085 con->state = CON_IDLE;
1091 /* If the rescan pipe went active, someone is telling this
1092 * thread that the &readfds needs to be refreshed with more
1096 end_critical_section(S_I_WANNA_SELECT);
1100 if (FD_ISSET(rescan[0], &readfds)) {
1101 read(rescan[0], &junk, 1);
1105 /* It must be a client socket. Find a context that has data
1106 * waiting on its socket *and* is in the CON_IDLE state.
1110 begin_critical_section(S_SESSION_TABLE);
1111 for (ptr = ContextList;
1112 ( (ptr != NULL) && (bind_me == NULL) );
1114 if ( (FD_ISSET(ptr->client_socket, &readfds))
1115 && (ptr->state == CON_IDLE) ) {
1119 if (bind_me != NULL) {
1120 /* Found one. Stake a claim to it before
1121 * letting anyone else touch the context list.
1123 bind_me->state = CON_EXECUTING;
1126 end_critical_section(S_SESSION_TABLE);
1127 end_critical_section(S_I_WANNA_SELECT);
1129 /* We're bound to a session, now do *one* command */
1130 if (bind_me != NULL) {
1131 become_session(bind_me);
1132 CC->h_command_function();
1133 become_session(NULL);
1134 bind_me->state = CON_IDLE;
1135 if (bind_me->kill_me == 1) {
1136 RemoveContext(bind_me);
1138 write(rescan[1], &junk, 1);
1142 dead_session_purge();
1144 check_sched_shutdown();
1147 /* If control reaches this point, the server is shutting down */
1157 * Translate text facility name to syslog.h defined value.
1159 int SyslogFacility(char *name)
1168 { LOG_KERN, "kern" },
1169 { LOG_USER, "user" },
1170 { LOG_MAIL, "mail" },
1171 { LOG_DAEMON, "daemon" },
1172 { LOG_AUTH, "auth" },
1173 { LOG_SYSLOG, "syslog" },
1175 { LOG_NEWS, "news" },
1176 { LOG_UUCP, "uucp" },
1177 { LOG_LOCAL0, "local0" },
1178 { LOG_LOCAL1, "local1" },
1179 { LOG_LOCAL2, "local2" },
1180 { LOG_LOCAL3, "local3" },
1181 { LOG_LOCAL4, "local4" },
1182 { LOG_LOCAL5, "local5" },
1183 { LOG_LOCAL6, "local6" },
1184 { LOG_LOCAL7, "local7" },
1187 for(i = 0; facTbl[i].name != NULL; i++) {
1188 if(!strcasecmp(name, facTbl[i].name))
1189 return facTbl[i].facility;