1 // Citadel "system dependent" stuff.
3 // Here's where we (hopefully) have most parts of the Citadel server that
4 // might need tweaking when run on different operating system variants.
6 // Copyright (c) 1987-2023 by the citadel.org team
8 // This program is open source software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License, version 3.
19 #include <sys/syslog.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <netinet/tcp.h>
28 #include <arpa/inet.h>
29 #define SHOW_ME_VAPPEND_PRINTF
30 #include <libcitadel.h>
31 #include "citserver.h"
33 #include "ctdl_module.h"
34 #include "sysdep_decls.h"
35 #include "modules/crypto/serv_crypto.h" // Needed for init_ssl, client_write_ssl, client_read_ssl
36 #include "housekeeping.h"
39 // Signal handler to shut down the server.
40 volatile int exit_signal = 0;
41 volatile int shutdown_and_halt = 0;
42 volatile int restart_server = 0;
43 volatile int running_as_daemon = 0;
46 static RETSIGTYPE signal_cleanup(int signum) {
47 syslog(LOG_DEBUG, "sysdep: caught signal %d", signum);
49 server_shutting_down = 1;
53 // Some initialization stuff...
54 void init_sysdep(void) {
57 // Avoid vulnerabilities related to FD_SETSIZE if we can.
61 getrlimit(RLIMIT_NOFILE, &rl);
62 rl.rlim_cur = FD_SETSIZE;
63 rl.rlim_max = FD_SETSIZE;
64 setrlimit(RLIMIT_NOFILE, &rl);
68 // If we've got OpenSSL, we're going to use it.
73 if (pthread_key_create(&ThreadKey, NULL) != 0) { // TSD for threads
74 syslog(LOG_ERR, "pthread_key_create() : %m");
75 exit(CTDLEXIT_THREAD);
78 if (pthread_key_create(&MyConKey, NULL) != 0) { // TSD for sessions
79 syslog(LOG_CRIT, "sysdep: can't create TSD key: %m");
80 exit(CTDLEXIT_THREAD);
83 // Interript, hangup, and terminate signals should cause the server to shut down.
85 sigaddset(&set, SIGINT);
86 sigaddset(&set, SIGHUP);
87 sigaddset(&set, SIGTERM);
88 sigprocmask(SIG_UNBLOCK, &set, NULL);
90 signal(SIGINT, signal_cleanup);
91 signal(SIGHUP, signal_cleanup);
92 signal(SIGTERM, signal_cleanup);
94 // Do not shut down the server on broken pipe signals, otherwise the
95 // whole Citadel service would come down whenever a single client
97 signal(SIGPIPE, SIG_IGN);
101 // This is a generic function to set up a master socket for listening on
102 // a TCP port. The server shuts down if the bind fails. (IPv4/IPv6 version)
104 // ip_addr IP address to bind
105 // port_number port number to bind
106 // queue_len number of incoming connections to allow in the queue
107 int ctdl_tcp_server(char *ip_addr, int port_number, int queue_len) {
109 struct sockaddr_in6 sin6;
110 struct sockaddr_in sin4;
114 memset(&sin6, 0, sizeof(sin6));
115 memset(&sin4, 0, sizeof(sin4));
116 sin6.sin6_family = AF_INET6;
117 sin4.sin_family = AF_INET;
119 if ( (ip_addr == NULL) // any IPv6
120 || (IsEmptyStr(ip_addr))
121 || (!strcmp(ip_addr, "*"))
124 sin6.sin6_addr = in6addr_any;
126 else if (!strcmp(ip_addr, "0.0.0.0")) { // any IPv4
128 sin4.sin_addr.s_addr = INADDR_ANY;
130 else if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':'))) { // specific IPv4
132 if (inet_pton(AF_INET, ip_addr, &sin4.sin_addr) <= 0) {
133 syslog(LOG_ALERT, "tcpserver: inet_pton: %m");
137 else { // specific IPv6
139 if (inet_pton(AF_INET6, ip_addr, &sin6.sin6_addr) <= 0) {
140 syslog(LOG_ALERT, "tcpserver: inet_pton: %m");
145 if (port_number == 0) {
146 syslog(LOG_ALERT, "tcpserver: no port number was specified");
149 sin6.sin6_port = htons((u_short) port_number);
150 sin4.sin_port = htons((u_short) port_number);
152 p = getprotobyname("tcp");
154 syslog(LOG_ALERT, "tcpserver: getprotobyname: %m");
158 s = socket( ((ip_version == 6) ? PF_INET6 : PF_INET), SOCK_STREAM, (p->p_proto));
160 syslog(LOG_ALERT, "tcpserver: socket: %m");
163 // Set some socket options that make sense.
165 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
167 if (ip_version == 6) {
168 b = bind(s, (struct sockaddr *) &sin6, sizeof(sin6));
171 b = bind(s, (struct sockaddr *) &sin4, sizeof(sin4));
175 syslog(LOG_ALERT, "tcpserver: bind: %m");
179 fcntl(s, F_SETFL, O_NONBLOCK);
181 if (listen(s, ((queue_len >= 5) ? queue_len : 5) ) < 0) {
182 syslog(LOG_ALERT, "tcpserver: listen: %m");
189 // Create a Unix domain socket and listen on it
190 int ctdl_uds_server(char *sockpath, int queue_len) {
191 struct sockaddr_un addr;
194 int actual_queue_len;
195 #ifdef HAVE_STRUCT_UCRED
199 actual_queue_len = queue_len;
200 if (actual_queue_len < 5) actual_queue_len = 5;
202 i = unlink(sockpath);
203 if ((i != 0) && (errno != ENOENT)) {
204 syslog(LOG_ERR, "udsserver: %m");
208 memset(&addr, 0, sizeof(addr));
209 addr.sun_family = AF_UNIX;
210 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
212 s = socket(AF_UNIX, SOCK_STREAM, 0);
214 syslog(LOG_ERR, "udsserver: socket: %m");
218 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
219 syslog(LOG_ERR, "udsserver: bind: %m");
223 // set to nonblock - we need this for some obscure situations
224 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
225 syslog(LOG_ERR, "udsserver: fcntl: %m");
230 if (listen(s, actual_queue_len) < 0) {
231 syslog(LOG_ERR, "udsserver: listen: %m");
235 #ifdef HAVE_STRUCT_UCRED
236 setsockopt(s, SOL_SOCKET, SO_PASSCRED, &passcred, sizeof(passcred));
239 chmod(sockpath, S_ISGID|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
244 // The following functions implement output buffering on operating systems which
245 // support it (such as Linux and various BSD flavors).
248 # define HAVE_TCP_BUFFERING
251 # define HAVE_TCP_BUFFERING
252 # define TCP_CORK TCP_NOPUSH
255 #endif // HAVE_DARWIN
257 static unsigned on = 1, off = 0;
259 void buffer_output(void) {
260 #ifdef HAVE_TCP_BUFFERING
262 if (!CC->redirect_ssl)
264 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
268 void unbuffer_output(void) {
269 #ifdef HAVE_TCP_BUFFERING
271 if (!CC->redirect_ssl)
273 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
277 void flush_output(void) {
278 #ifdef HAVE_TCP_BUFFERING
279 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
280 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
285 // close the client socket
286 void client_close(void) {
288 if (CC->client_socket <= 0) return;
289 syslog(LOG_DEBUG, "sysdep: closing socket %d", CC->client_socket);
290 close(CC->client_socket);
291 CC->client_socket = -1 ;
295 // Send binary data to the client.
296 int client_write(const char *buf, int nbytes) {
297 int bytes_written = 0;
299 #ifndef HAVE_TCP_BUFFERING
300 int old_buffer_len = 0;
306 if (nbytes < 1) return(0);
309 if (Ctx->redirect_buffer != NULL) {
310 StrBufAppendBufPlain(Ctx->redirect_buffer, buf, nbytes, 0);
315 if (Ctx->redirect_ssl) {
316 client_write_ssl(buf, nbytes);
320 if (Ctx->client_socket == -1) return -1;
322 fdflags = fcntl(Ctx->client_socket, F_GETFL);
324 while ((bytes_written < nbytes) && (Ctx->client_socket != -1)){
325 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
327 FD_SET(Ctx->client_socket, &wset);
328 if (select(1, NULL, &wset, NULL, NULL) == -1) {
329 if (errno == EINTR) {
330 syslog(LOG_DEBUG, "sysdep: client_write(%d bytes) select() interrupted.", nbytes-bytes_written);
331 if (server_shutting_down) {
332 CC->kill_me = KILLME_SELECT_INTERRUPTED;
336 // can't trust fd's and stuff so we need to re-create them
341 syslog(LOG_ERR, "sysdep: client_write(%d bytes) select failed: %m", nbytes - bytes_written);
343 Ctx->kill_me = KILLME_SELECT_FAILED;
349 retval = write(Ctx->client_socket, &buf[bytes_written], nbytes - bytes_written);
351 syslog(LOG_ERR, "sysdep: client_write(%d bytes) failed: %m", nbytes - bytes_written);
353 Ctx->kill_me = KILLME_WRITE_FAILED;
356 bytes_written = bytes_written + retval;
362 void cputbuf(const StrBuf *Buf) {
363 client_write(ChrPtr(Buf), StrLength(Buf));
367 // Send formatted printable data to the client.
368 // Implemented in terms of client_write() so it's technically not sysdep...
369 void cprintf(const char *format, ...) {
373 va_start(arg_ptr, format);
374 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
375 buf[sizeof buf - 2] = '\n';
376 client_write(buf, strlen(buf));
381 // Read data from the client socket.
383 // sock socket fd to read from
384 // buf buffer to read into
385 // bytes number of bytes to read
386 // timeout Number of seconds to wait before timing out
388 // Possible return values:
389 // 1 Requested number of bytes has been read.
390 // 0 Request timed out.
391 // -1 Connection is broken, or other error.
392 int client_read_blob(StrBuf *Target, int bytes, int timeout) {
397 if (CC->redirect_ssl) {
398 retval = client_read_sslblob(Target, bytes, timeout);
400 syslog(LOG_ERR, "sysdep: client_read_blob() failed");
406 retval = StrBufReadBLOBBuffered(Target,
408 &CC->RecvBuf.ReadWritePointer,
416 syslog(LOG_ERR, "sysdep: client_read_blob() failed: %s", Error);
425 // to make client_read_random_blob() more efficient, increase buffer size.
426 // just use in greeting function, else your buffer may be flushed
427 void client_set_inbound_buf(long N) {
428 FlushStrBuf(CC->RecvBuf.Buf);
429 ReAdjustEmptyBuf(CC->RecvBuf.Buf, N * SIZ, N * SIZ);
433 int client_read_random_blob(StrBuf *Target, int timeout) {
436 rc = client_read_blob(Target, 1, timeout);
441 len = StrLength(CC->RecvBuf.Buf);
442 pch = ChrPtr(CC->RecvBuf.Buf);
445 if (CC->RecvBuf.ReadWritePointer != NULL) {
446 len -= CC->RecvBuf.ReadWritePointer - pch;
447 pch = CC->RecvBuf.ReadWritePointer;
449 StrBufAppendBufPlain(Target, pch, len, 0);
450 FlushStrBuf(CC->RecvBuf.Buf);
451 CC->RecvBuf.ReadWritePointer = NULL;
452 return StrLength(Target);
461 int client_read_to(char *buf, int bytes, int timeout) {
464 rc = client_read_blob(CC->MigrateBuf, bytes, timeout);
471 ChrPtr(CC->MigrateBuf),
472 StrLength(CC->MigrateBuf) + 1);
473 FlushStrBuf(CC->MigrateBuf);
479 int HaveMoreLinesWaiting(CitContext *ctx) {
480 if ( (ctx->kill_me != 0)
481 || ( (ctx->RecvBuf.ReadWritePointer == NULL)
482 && (StrLength(ctx->RecvBuf.Buf) == 0)
483 && (ctx->client_socket != -1))
491 // Read data from the client socket with default timeout.
492 // (This is implemented in terms of client_read_to() and could be
493 // justifiably moved out of sysdep.c)
494 INLINE int client_read(char *buf, int bytes) {
495 return(client_read_to(buf, bytes, CtdlGetConfigInt("c_sleeping")));
499 int CtdlClientGetLine(StrBuf *Target) {
505 if (CC->redirect_ssl) {
506 rc = client_readline_sslbuffer(Target, CC->RecvBuf.Buf, &CC->RecvBuf.ReadWritePointer, 1);
512 rc = StrBufTCP_read_buffered_line_fast(Target,
514 &CC->RecvBuf.ReadWritePointer,
525 // Get a LF-terminated line of text from the client.
526 // (This is implemented in terms of client_read() and could be
527 // justifiably moved out of sysdep.c)
528 int client_getln(char *buf, int bufsize) {
532 retval = CtdlClientGetLine(CC->MigrateBuf);
537 i = StrLength(CC->MigrateBuf);
538 pCh = ChrPtr(CC->MigrateBuf);
539 // Strip the trailing LF, and the trailing CR if present.
543 && ( (pCh[i - 1]==13)
544 || ( pCh[i - 1]==10)) ) {
550 FlushStrBuf(CC->MigrateBuf);
552 safestrncpy(&buf[i], "000", bufsize - i);
558 // The system-dependent part of master_cleanup() - close the master socket.
559 void sysdep_master_cleanup(void) {
566 void graceful_shutdown(int signum) {
567 kill(current_child, signum);
568 unlink(file_pid_file);
573 int nFireUpsNonRestart = 0;
576 // Start running as a daemon.
577 void start_daemon(int unused) {
584 // Close stdin/stdout/stderr and replace them with /dev/null.
585 // We don't just call close() because we don't want these fd's
586 // to be reused for other files.
592 signal(SIGHUP, SIG_IGN);
593 signal(SIGINT, SIG_IGN);
594 signal(SIGQUIT, SIG_IGN);
598 if ( (freopen("/dev/null", "r", stdin) != stdin) ||
599 (freopen("/dev/null", "w", stdout) != stdout) ||
600 (freopen("/dev/null", "w", stderr) != stderr)
602 syslog(LOG_ERR, "sysdep: unable to reopen stdio: %m");
606 current_child = fork();
607 signal(SIGTERM, graceful_shutdown);
608 if (current_child < 0) {
612 else if (current_child == 0) {
613 return; // continue starting citadel.
616 fp = fopen(file_pid_file, "w");
618 fprintf(fp, ""F_PID_T"\n", getpid());
621 waitpid(current_child, &status, 0);
623 nFireUpsNonRestart = nFireUps;
625 // Exit code 0 means the watcher should exit
626 if (WIFEXITED(status) && (WEXITSTATUS(status) == CTDLEXIT_SHUTDOWN)) {
630 // Exit code 101-109 means the watcher should exit
631 else if (WIFEXITED(status) && (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109)) {
635 // Any other exit code, or no exit code, means we should restart.
639 ForkedPid = current_child;
642 } while (do_restart);
644 unlink(file_pid_file);
645 exit(WEXITSTATUS(status));
649 void checkcrash(void) {
650 if (nFireUpsNonRestart != nFireUps) {
652 CrashMail = NewStrBuf();
653 syslog(LOG_ALERT, "sysdep: posting crash message");
654 StrBufPrintf(CrashMail,
656 " The Citadel server process (citserver) terminated unexpectedly."
658 " This could be the result of a bug in the server program, or some external "
660 " You can obtain more information about this by enabling core dumps.\n \n"
661 " For more information, please see:\n \n"
662 " http://citadel.org/doku.php?id=faq:mastering_your_os:gdb#how.do.i.make.my.system.produce.core-files"
665 " If you have already done this, the core dump is likely to be found at %score.%d\n"
667 ctdl_run_dir, ForkedPid);
668 CtdlAideMessage(ChrPtr(CrashMail), "Citadel server process terminated unexpectedly");
669 FreeStrBuf(&CrashMail);
674 // Generic routine to convert a login name to a full name (gecos)
675 // Returns nonzero if a conversion took place
676 int convert_login(char NameToConvert[]) {
680 pw = getpwnam(NameToConvert);
685 strcpy(NameToConvert, pw->pw_gecos);
686 for (a=0; a<strlen(NameToConvert); ++a) {
687 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
694 void HuntBadSession(void) {
699 struct ServiceFunctionHook *serviceptr;
701 // Next, add all of the client sockets
702 begin_critical_section(S_SESSION_TABLE);
703 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
704 if ((ptr->state == CON_SYS) && (ptr->client_socket == 0))
706 // Initialize the fdset.
709 tv.tv_sec = 0; // wake up every second if no input
712 // Don't select on dead sessions, only truly idle ones
713 if ( (ptr->state == CON_IDLE)
714 && (ptr->kill_me == 0)
715 && (ptr->client_socket > 0)
717 FD_SET(ptr->client_socket, &readfds);
718 if (ptr->client_socket > highest)
719 highest = ptr->client_socket;
721 if ((select(highest + 1, &readfds, NULL, NULL, &tv) < 0) && (errno == EBADF)) {
724 "sysdep: killing session CC[%d] bad FD: [%d] User[%s] Host[%s:%s]",
732 ptr->client_socket = -1;
737 end_critical_section(S_SESSION_TABLE);
739 // First, add the various master sockets to the fdset.
740 for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
742 // Initialize the fdset.
744 tv.tv_sec = 0; // wake up every second if no input
747 FD_SET(serviceptr->msock, &readfds);
748 if (serviceptr->msock > highest) {
749 highest = serviceptr->msock;
751 if ((select(highest + 1, &readfds, NULL, NULL, &tv) < 0) && (errno == EBADF)) {
752 // Gotcha! server socket dead? commit suicide!
753 syslog(LOG_ERR, "sysdep: found bad FD: %d and its a server socket! Shutting Down!", serviceptr->msock);
754 server_shutting_down = 1;
762 // select() can wake up on any of the following conditions:
763 // 1. A new client connection on a master socket
764 // 2. Received data on a client socket
766 // That's why we are blocking on select() and not accept().
767 void *worker_thread(void *blah) {
770 CitContext *bind_me = NULL;
775 struct ServiceFunctionHook *serviceptr;
776 int ssock; // Descriptor for client socket
777 CitContext *con = NULL; // Temporary context pointer
780 pthread_mutex_lock(&ThreadCountMutex);
782 pthread_mutex_unlock(&ThreadCountMutex);
784 while (!server_shutting_down) {
786 // make doubly sure we're not holding any stale db handles which might cause a deadlock
788 do_select: force_purge = 0;
789 bind_me = NULL; // Which session shall we handle?
791 // Initialize the fdset
795 // First, add the various master sockets to the fdset.
796 for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
797 FD_SET(serviceptr->msock, &readfds);
798 if (serviceptr->msock > highest) {
799 highest = serviceptr->msock;
803 // Next, add all of the client sockets.
804 begin_critical_section(S_SESSION_TABLE);
805 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
806 if ((ptr->state == CON_SYS) && (ptr->client_socket == 0))
809 // Don't select on dead sessions, only truly idle ones
810 if ( (ptr->state == CON_IDLE)
811 && (ptr->kill_me == 0)
812 && (ptr->client_socket > 0)
814 FD_SET(ptr->client_socket, &readfds);
815 if (ptr->client_socket > highest)
816 highest = ptr->client_socket;
818 if ((bind_me == NULL) && (ptr->state == CON_READY)) {
820 ptr->state = CON_EXECUTING;
823 if ((bind_me == NULL) && (ptr->state == CON_GREETING)) {
825 ptr->state = CON_STARTING;
829 end_critical_section(S_SESSION_TABLE);
835 // If we got this far, it means that there are no sessions
836 // which a previous thread marked for attention, so we go
837 // ahead and get ready to select().
839 if (!server_shutting_down) {
840 tv.tv_sec = 1; // wake up every second if no input
842 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
849 // Now figure out who made this select() unblock.
850 // First, check for an error or exit condition.
852 if (errno == EBADF) {
853 syslog(LOG_ERR, "sysdep: select() failed: %m");
857 if (errno != EINTR) {
858 syslog(LOG_ERR, "sysdep: exiting: %m");
859 server_shutting_down = 1;
862 if (server_shutting_down) {
869 else if (retval == 0) {
870 if (server_shutting_down) {
876 // Next, check to see if it's a new client connecting on a master socket.
878 else if ((retval > 0) && (!server_shutting_down)) for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next) {
880 if (FD_ISSET(serviceptr->msock, &readfds)) {
881 ssock = accept(serviceptr->msock, NULL, 0);
883 syslog(LOG_DEBUG, "sysdep: new client socket %d", ssock);
885 // The master socket is non-blocking but the client
886 // sockets need to be blocking, otherwise certain
887 // operations barf on FreeBSD. Not a fatal error.
888 if (fcntl(ssock, F_SETFL, 0) < 0) {
889 syslog(LOG_ERR, "sysdep: Can't set socket to blocking: %m");
892 // New context will be created already
893 // set up in the CON_EXECUTING state.
894 con = CreateNewContext();
896 // Assign our new socket number to it.
897 con->tcp_port = serviceptr->tcp_port;
898 con->client_socket = ssock;
899 con->h_command_function = serviceptr->h_command_function;
900 con->h_async_function = serviceptr->h_async_function;
901 con->h_greeting_function = serviceptr->h_greeting_function;
902 con->ServiceName = serviceptr->ServiceName;
904 // Connections on a local client are always from the same host
905 if (serviceptr->sockpath != NULL) {
906 con->is_local_client = 1;
909 // Set the SO_REUSEADDR socket option
911 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
912 con->state = CON_GREETING;
920 // It must be a client socket. Find a context that has data
921 // waiting on its socket *and* is in the CON_IDLE state. Any
922 // active sockets other than our chosen one are marked as
923 // CON_READY so the next thread that comes around can just bind
924 // to one without having to select() again.
925 begin_critical_section(S_SESSION_TABLE);
926 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
927 int checkfd = ptr->client_socket;
928 if ((checkfd != -1) && (ptr->state == CON_IDLE) ){
929 if (FD_ISSET(checkfd, &readfds)) {
930 ptr->input_waiting = 1;
932 bind_me = ptr; // I choose you!
933 bind_me->state = CON_EXECUTING;
936 ptr->state = CON_READY;
938 } else if ((ptr->is_async) && (ptr->async_waiting) && (ptr->h_async_function)) {
940 bind_me = ptr; // I choose you!
941 bind_me->state = CON_EXECUTING;
944 ptr->state = CON_READY;
949 end_critical_section(S_SESSION_TABLE);
952 // We're bound to a session
953 pthread_mutex_lock(&ThreadCountMutex);
955 pthread_mutex_unlock(&ThreadCountMutex);
957 if (bind_me != NULL) {
958 become_session(bind_me);
960 if (bind_me->state == CON_STARTING) {
961 bind_me->state = CON_EXECUTING;
962 begin_session(bind_me);
963 bind_me->h_greeting_function();
965 // If the client has sent a command, execute it.
966 if (CC->input_waiting) {
967 CC->h_command_function();
969 while (HaveMoreLinesWaiting(CC))
970 CC->h_command_function();
972 CC->input_waiting = 0;
975 // If there are asynchronous messages waiting and the client supports it, do those now
976 if ((CC->is_async) && (CC->async_waiting) && (CC->h_async_function != NULL)) {
977 CC->h_async_function();
978 CC->async_waiting = 0;
981 force_purge = CC->kill_me;
982 become_session(NULL);
983 bind_me->state = CON_IDLE;
986 dead_session_purge(force_purge);
989 pthread_mutex_lock(&ThreadCountMutex);
991 if ((active_workers + CtdlGetConfigInt("c_min_workers") < num_workers) &&
992 (num_workers > CtdlGetConfigInt("c_min_workers")))
995 pthread_mutex_unlock(&ThreadCountMutex);
998 pthread_mutex_unlock(&ThreadCountMutex);
1001 // If control reaches this point, the server is shutting down
1002 pthread_mutex_lock(&ThreadCountMutex);
1004 pthread_mutex_unlock(&ThreadCountMutex);
1010 // Translate text facility name to syslog.h defined value.
1011 int SyslogFacility(char *name)
1020 { LOG_KERN, "kern" },
1021 { LOG_USER, "user" },
1022 { LOG_MAIL, "mail" },
1023 { LOG_DAEMON, "daemon" },
1024 { LOG_AUTH, "auth" },
1025 { LOG_SYSLOG, "syslog" },
1027 { LOG_NEWS, "news" },
1028 { LOG_UUCP, "uucp" },
1029 { LOG_LOCAL0, "local0" },
1030 { LOG_LOCAL1, "local1" },
1031 { LOG_LOCAL2, "local2" },
1032 { LOG_LOCAL3, "local3" },
1033 { LOG_LOCAL4, "local4" },
1034 { LOG_LOCAL5, "local5" },
1035 { LOG_LOCAL6, "local6" },
1036 { LOG_LOCAL7, "local7" },
1039 for(i = 0; facTbl[i].name != NULL; i++) {
1040 if(!strcasecmp(name, facTbl[i].name))
1041 return facTbl[i].facility;