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);
48 signal(signum, SIG_DFL);
50 server_shutting_down = 1;
54 // Some initialization stuff...
55 void init_sysdep(void) {
58 // Avoid vulnerabilities related to FD_SETSIZE if we can.
62 getrlimit(RLIMIT_NOFILE, &rl);
63 rl.rlim_cur = FD_SETSIZE;
64 rl.rlim_max = FD_SETSIZE;
65 setrlimit(RLIMIT_NOFILE, &rl);
69 // If we've got OpenSSL, we're going to use it.
74 if (pthread_key_create(&ThreadKey, NULL) != 0) { // TSD for threads
75 syslog(LOG_ERR, "pthread_key_create() : %m");
76 exit(CTDLEXIT_THREAD);
79 if (pthread_key_create(&MyConKey, NULL) != 0) { // TSD for sessions
80 syslog(LOG_CRIT, "sysdep: can't create TSD key: %m");
81 exit(CTDLEXIT_THREAD);
84 // Interript, hangup, and terminate signals should cause the server to shut down.
86 sigaddset(&set, SIGINT);
87 sigaddset(&set, SIGHUP);
88 sigaddset(&set, SIGTERM);
89 sigaddset(&set, SIGSEGV);
90 sigprocmask(SIG_UNBLOCK, &set, NULL);
92 signal(SIGINT, signal_cleanup);
93 signal(SIGHUP, signal_cleanup);
94 signal(SIGTERM, signal_cleanup);
95 signal(SIGSEGV, signal_cleanup);
97 // Do not shut down the server on broken pipe signals, otherwise the
98 // whole Citadel service would come down whenever a single client
100 signal(SIGPIPE, SIG_IGN);
104 // This is a generic function to set up a master socket for listening on
105 // a TCP port. The server shuts down if the bind fails. (IPv4/IPv6 version)
107 // ip_addr IP address to bind
108 // port_number port number to bind
109 // queue_len number of incoming connections to allow in the queue
110 int ctdl_tcp_server(char *ip_addr, int port_number, int queue_len) {
112 struct sockaddr_in6 sin6;
113 struct sockaddr_in sin4;
117 memset(&sin6, 0, sizeof(sin6));
118 memset(&sin4, 0, sizeof(sin4));
119 sin6.sin6_family = AF_INET6;
120 sin4.sin_family = AF_INET;
122 if ( (ip_addr == NULL) // any IPv6
123 || (IsEmptyStr(ip_addr))
124 || (!strcmp(ip_addr, "*"))
127 sin6.sin6_addr = in6addr_any;
129 else if (!strcmp(ip_addr, "0.0.0.0")) { // any IPv4
131 sin4.sin_addr.s_addr = INADDR_ANY;
133 else if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':'))) { // specific IPv4
135 if (inet_pton(AF_INET, ip_addr, &sin4.sin_addr) <= 0) {
136 syslog(LOG_ALERT, "tcpserver: inet_pton: %m");
140 else { // specific IPv6
142 if (inet_pton(AF_INET6, ip_addr, &sin6.sin6_addr) <= 0) {
143 syslog(LOG_ALERT, "tcpserver: inet_pton: %m");
148 if (port_number == 0) {
149 syslog(LOG_ALERT, "tcpserver: no port number was specified");
152 sin6.sin6_port = htons((u_short) port_number);
153 sin4.sin_port = htons((u_short) port_number);
155 p = getprotobyname("tcp");
157 syslog(LOG_ALERT, "tcpserver: getprotobyname: %m");
161 s = socket( ((ip_version == 6) ? PF_INET6 : PF_INET), SOCK_STREAM, (p->p_proto));
163 syslog(LOG_ALERT, "tcpserver: socket: %m");
166 // Set some socket options that make sense.
168 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
170 if (ip_version == 6) {
171 b = bind(s, (struct sockaddr *) &sin6, sizeof(sin6));
174 b = bind(s, (struct sockaddr *) &sin4, sizeof(sin4));
178 syslog(LOG_ALERT, "tcpserver: bind: %m");
182 fcntl(s, F_SETFL, O_NONBLOCK);
184 if (listen(s, ((queue_len >= 5) ? queue_len : 5) ) < 0) {
185 syslog(LOG_ALERT, "tcpserver: listen: %m");
192 // Create a Unix domain socket and listen on it
193 int ctdl_uds_server(char *sockpath, int queue_len) {
194 struct sockaddr_un addr;
197 int actual_queue_len;
198 #ifdef HAVE_STRUCT_UCRED
202 actual_queue_len = queue_len;
203 if (actual_queue_len < 5) actual_queue_len = 5;
205 i = unlink(sockpath);
206 if ((i != 0) && (errno != ENOENT)) {
207 syslog(LOG_ERR, "udsserver: %m");
211 memset(&addr, 0, sizeof(addr));
212 addr.sun_family = AF_UNIX;
213 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
215 s = socket(AF_UNIX, SOCK_STREAM, 0);
217 syslog(LOG_ERR, "udsserver: socket: %m");
221 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
222 syslog(LOG_ERR, "udsserver: bind: %m");
226 // set to nonblock - we need this for some obscure situations
227 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
228 syslog(LOG_ERR, "udsserver: fcntl: %m");
233 if (listen(s, actual_queue_len) < 0) {
234 syslog(LOG_ERR, "udsserver: listen: %m");
238 #ifdef HAVE_STRUCT_UCRED
239 setsockopt(s, SOL_SOCKET, SO_PASSCRED, &passcred, sizeof(passcred));
242 chmod(sockpath, S_ISGID|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
247 // The following functions implement output buffering on operating systems which
248 // support it (such as Linux and various BSD flavors).
251 # define HAVE_TCP_BUFFERING
254 # define HAVE_TCP_BUFFERING
255 # define TCP_CORK TCP_NOPUSH
258 #endif // HAVE_DARWIN
260 static unsigned on = 1, off = 0;
262 void buffer_output(void) {
263 #ifdef HAVE_TCP_BUFFERING
265 if (!CC->redirect_ssl)
267 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
271 void unbuffer_output(void) {
272 #ifdef HAVE_TCP_BUFFERING
274 if (!CC->redirect_ssl)
276 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
280 void flush_output(void) {
281 #ifdef HAVE_TCP_BUFFERING
282 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
283 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
288 // close the client socket
289 void client_close(void) {
291 if (CC->client_socket <= 0) return;
292 syslog(LOG_DEBUG, "sysdep: closing socket %d", CC->client_socket);
293 close(CC->client_socket);
294 CC->client_socket = -1 ;
298 // Send binary data to the client.
299 int client_write(const char *buf, int nbytes) {
300 int bytes_written = 0;
302 #ifndef HAVE_TCP_BUFFERING
303 int old_buffer_len = 0;
309 if (nbytes < 1) return(0);
312 if (Ctx->redirect_buffer != NULL) {
313 StrBufAppendBufPlain(Ctx->redirect_buffer, buf, nbytes, 0);
318 if (Ctx->redirect_ssl) {
319 client_write_ssl(buf, nbytes);
323 if (Ctx->client_socket == -1) return -1;
325 fdflags = fcntl(Ctx->client_socket, F_GETFL);
327 while ((bytes_written < nbytes) && (Ctx->client_socket != -1)){
328 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
330 FD_SET(Ctx->client_socket, &wset);
331 if (select(1, NULL, &wset, NULL, NULL) == -1) {
332 if (errno == EINTR) {
333 syslog(LOG_DEBUG, "sysdep: client_write(%d bytes) select() interrupted.", nbytes-bytes_written);
334 if (server_shutting_down) {
335 CC->kill_me = KILLME_SELECT_INTERRUPTED;
339 // can't trust fd's and stuff so we need to re-create them
344 syslog(LOG_ERR, "sysdep: client_write(%d bytes) select failed: %m", nbytes - bytes_written);
346 Ctx->kill_me = KILLME_SELECT_FAILED;
352 retval = write(Ctx->client_socket, &buf[bytes_written], nbytes - bytes_written);
354 syslog(LOG_ERR, "sysdep: client_write(%d bytes) failed: %m", nbytes - bytes_written);
356 Ctx->kill_me = KILLME_WRITE_FAILED;
359 bytes_written = bytes_written + retval;
365 void cputbuf(const StrBuf *Buf) {
366 client_write(ChrPtr(Buf), StrLength(Buf));
370 // Send formatted printable data to the client.
371 // Implemented in terms of client_write() so it's technically not sysdep...
372 void cprintf(const char *format, ...) {
377 va_start(arg_ptr, format);
378 rc = vsnprintf(buf, sizeof buf, format, arg_ptr);
380 syslog(LOG_ERR,"Console output error occured. Format: %s",format);
382 if (rc >= sizeof buf) {
383 // Output was truncated.
384 // Chop at the end of the buffer
385 buf[sizeof buf - 2] = '\n';
386 buf[sizeof buf - 1] = 0;
388 client_write(buf, strlen(buf));
394 // Read data from the client socket.
396 // sock socket fd to read from
397 // buf buffer to read into
398 // bytes number of bytes to read
399 // timeout Number of seconds to wait before timing out
401 // Possible return values:
402 // 1 Requested number of bytes has been read.
403 // 0 Request timed out.
404 // -1 Connection is broken, or other error.
405 int client_read_blob(StrBuf *Target, int bytes, int timeout) {
410 if (CC->redirect_ssl) {
411 retval = client_read_sslblob(Target, bytes, timeout);
413 syslog(LOG_ERR, "sysdep: client_read_blob() failed");
419 retval = StrBufReadBLOBBuffered(Target,
421 &CC->RecvBuf.ReadWritePointer,
429 syslog(LOG_ERR, "sysdep: client_read_blob() failed: %s", Error);
438 // to make client_read_random_blob() more efficient, increase buffer size.
439 // just use in greeting function, else your buffer may be flushed
440 void client_set_inbound_buf(long N) {
441 FlushStrBuf(CC->RecvBuf.Buf);
442 ReAdjustEmptyBuf(CC->RecvBuf.Buf, N * SIZ, N * SIZ);
446 int client_read_random_blob(StrBuf *Target, int timeout) {
449 rc = client_read_blob(Target, 1, timeout);
454 len = StrLength(CC->RecvBuf.Buf);
455 pch = ChrPtr(CC->RecvBuf.Buf);
458 if (CC->RecvBuf.ReadWritePointer != NULL) {
459 len -= CC->RecvBuf.ReadWritePointer - pch;
460 pch = CC->RecvBuf.ReadWritePointer;
462 StrBufAppendBufPlain(Target, pch, len, 0);
463 FlushStrBuf(CC->RecvBuf.Buf);
464 CC->RecvBuf.ReadWritePointer = NULL;
465 return StrLength(Target);
474 int client_read_to(char *buf, int bytes, int timeout) {
477 rc = client_read_blob(CC->MigrateBuf, bytes, timeout);
484 ChrPtr(CC->MigrateBuf),
485 StrLength(CC->MigrateBuf) + 1);
486 FlushStrBuf(CC->MigrateBuf);
492 int HaveMoreLinesWaiting(CitContext *ctx) {
493 if ( (ctx->kill_me != 0)
494 || ( (ctx->RecvBuf.ReadWritePointer == NULL)
495 && (StrLength(ctx->RecvBuf.Buf) == 0)
496 && (ctx->client_socket != -1))
504 // Read data from the client socket with default timeout.
505 // (This is implemented in terms of client_read_to() and could be
506 // justifiably moved out of sysdep.c)
507 INLINE int client_read(char *buf, int bytes) {
508 return(client_read_to(buf, bytes, CtdlGetConfigInt("c_sleeping")));
512 int CtdlClientGetLine(StrBuf *Target) {
518 if (CC->redirect_ssl) {
519 rc = client_readline_sslbuffer(Target, CC->RecvBuf.Buf, &CC->RecvBuf.ReadWritePointer, 1);
525 rc = StrBufTCP_read_buffered_line_fast(Target,
527 &CC->RecvBuf.ReadWritePointer,
538 // Get a LF-terminated line of text from the client.
539 // (This is implemented in terms of client_read() and could be
540 // justifiably moved out of sysdep.c)
541 int client_getln(char *buf, int bufsize) {
545 retval = CtdlClientGetLine(CC->MigrateBuf);
550 i = StrLength(CC->MigrateBuf);
551 pCh = ChrPtr(CC->MigrateBuf);
552 // Strip the trailing LF, and the trailing CR if present.
556 && ( (pCh[i - 1]==13)
557 || ( pCh[i - 1]==10)) ) {
563 FlushStrBuf(CC->MigrateBuf);
565 safestrncpy(&buf[i], "000", bufsize - i);
571 // The system-dependent part of master_cleanup() - close the master socket.
572 void sysdep_master_cleanup(void) {
579 void graceful_shutdown(int signum) {
580 kill(current_child, signum);
581 unlink(file_pid_file);
586 int nFireUpsNonRestart = 0;
589 // Start running as a daemon.
590 void start_daemon(int unused) {
597 // Close stdin/stdout/stderr and replace them with /dev/null.
598 // We don't just call close() because we don't want these fd's
599 // to be reused for other files.
605 signal(SIGHUP, SIG_IGN);
606 signal(SIGINT, SIG_IGN);
607 signal(SIGQUIT, SIG_IGN);
611 if ( (freopen("/dev/null", "r", stdin) != stdin) ||
612 (freopen("/dev/null", "w", stdout) != stdout) ||
613 (freopen("/dev/null", "w", stderr) != stderr)
615 syslog(LOG_ERR, "sysdep: unable to reopen stdio: %m");
619 current_child = fork();
620 signal(SIGTERM, graceful_shutdown);
621 if (current_child < 0) {
625 else if (current_child == 0) {
626 return; // continue starting citadel.
629 fp = fopen(file_pid_file, "w");
631 fprintf(fp, ""F_PID_T"\n", getpid());
634 waitpid(current_child, &status, 0);
636 nFireUpsNonRestart = nFireUps;
638 // Exit code 0 means the watcher should exit
639 if (WIFEXITED(status) && (WEXITSTATUS(status) == CTDLEXIT_SHUTDOWN)) {
643 // Exit code 101-109 means the watcher should exit
644 else if (WIFEXITED(status) && (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109)) {
648 // Any other exit code, or no exit code, means we should restart.
652 ForkedPid = current_child;
655 } while (do_restart);
657 unlink(file_pid_file);
658 exit(WEXITSTATUS(status));
662 void checkcrash(void) {
663 if (nFireUpsNonRestart != nFireUps) {
665 CrashMail = NewStrBuf();
666 syslog(LOG_ALERT, "sysdep: posting crash message");
667 StrBufPrintf(CrashMail,
669 " The Citadel server process (citserver) terminated unexpectedly."
671 " This could be the result of a bug in the server program, or some external "
673 " You can obtain more information about this by enabling core dumps.\n \n"
674 " For more information, please see:\n \n"
675 " http://citadel.org/doku.php?id=faq:mastering_your_os:gdb#how.do.i.make.my.system.produce.core-files"
678 " If you have already done this, the core dump is likely to be found at %score.%d\n"
680 ctdl_run_dir, ForkedPid);
681 CtdlAideMessage(ChrPtr(CrashMail), "Citadel server process terminated unexpectedly");
682 FreeStrBuf(&CrashMail);
687 // Generic routine to convert a login name to a full name (gecos)
688 // Returns nonzero if a conversion took place
689 int convert_login(char NameToConvert[]) {
693 pw = getpwnam(NameToConvert);
698 strcpy(NameToConvert, pw->pw_gecos);
699 for (a=0; a<strlen(NameToConvert); ++a) {
700 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
707 void HuntBadSession(void) {
712 struct ServiceFunctionHook *serviceptr;
714 // Next, add all of the client sockets
715 begin_critical_section(S_SESSION_TABLE);
716 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
717 if ((ptr->state == CON_SYS) && (ptr->client_socket == 0))
719 // Initialize the fdset.
722 tv.tv_sec = 0; // wake up every second if no input
725 // Don't select on dead sessions, only truly idle ones
726 if ( (ptr->state == CON_IDLE)
727 && (ptr->kill_me == 0)
728 && (ptr->client_socket > 0)
730 FD_SET(ptr->client_socket, &readfds);
731 if (ptr->client_socket > highest)
732 highest = ptr->client_socket;
734 if ((select(highest + 1, &readfds, NULL, NULL, &tv) < 0) && (errno == EBADF)) {
737 "sysdep: killing session CC[%d] bad FD: [%d] User[%s] Host[%s:%s]",
745 ptr->client_socket = -1;
750 end_critical_section(S_SESSION_TABLE);
752 // First, add the various master sockets to the fdset.
753 for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
755 // Initialize the fdset.
757 tv.tv_sec = 0; // wake up every second if no input
760 FD_SET(serviceptr->msock, &readfds);
761 if (serviceptr->msock > highest) {
762 highest = serviceptr->msock;
764 if ((select(highest + 1, &readfds, NULL, NULL, &tv) < 0) && (errno == EBADF)) {
765 // Gotcha! server socket dead? commit suicide!
766 syslog(LOG_ERR, "sysdep: found bad FD: %d and its a server socket! Shutting Down!", serviceptr->msock);
767 server_shutting_down = 1;
775 // select() can wake up on any of the following conditions:
776 // 1. A new client connection on a master socket
777 // 2. Received data on a client socket
779 // That's why we are blocking on select() and not accept().
780 void *worker_thread(void *blah) {
783 CitContext *bind_me = NULL;
788 struct ServiceFunctionHook *serviceptr;
789 int ssock; // Descriptor for client socket
790 CitContext *con = NULL; // Temporary context pointer
793 pthread_mutex_lock(&ThreadCountMutex);
795 pthread_mutex_unlock(&ThreadCountMutex);
797 while (!server_shutting_down) {
799 // make doubly sure we're not holding any stale db handles which might cause a deadlock
801 do_select: force_purge = 0;
802 bind_me = NULL; // Which session shall we handle?
804 // Initialize the fdset
808 // First, add the various master sockets to the fdset.
809 for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
810 FD_SET(serviceptr->msock, &readfds);
811 if (serviceptr->msock > highest) {
812 highest = serviceptr->msock;
816 // Next, add all of the client sockets.
817 begin_critical_section(S_SESSION_TABLE);
818 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
819 if ((ptr->state == CON_SYS) && (ptr->client_socket == 0))
822 // Don't select on dead sessions, only truly idle ones
823 if ( (ptr->state == CON_IDLE)
824 && (ptr->kill_me == 0)
825 && (ptr->client_socket > 0)
827 FD_SET(ptr->client_socket, &readfds);
828 if (ptr->client_socket > highest)
829 highest = ptr->client_socket;
831 if ((bind_me == NULL) && (ptr->state == CON_READY)) {
833 ptr->state = CON_EXECUTING;
836 if ((bind_me == NULL) && (ptr->state == CON_GREETING)) {
838 ptr->state = CON_STARTING;
842 end_critical_section(S_SESSION_TABLE);
848 // If we got this far, it means that there are no sessions
849 // which a previous thread marked for attention, so we go
850 // ahead and get ready to select().
852 if (!server_shutting_down) {
853 tv.tv_sec = 1; // wake up every second if no input
855 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
862 // Now figure out who made this select() unblock.
863 // First, check for an error or exit condition.
865 if (errno == EBADF) {
866 syslog(LOG_ERR, "sysdep: select() failed: %m");
870 if (errno != EINTR) {
871 syslog(LOG_ERR, "sysdep: exiting: %m");
872 server_shutting_down = 1;
875 if (server_shutting_down) {
882 else if (retval == 0) {
883 if (server_shutting_down) {
889 // Next, check to see if it's a new client connecting on a master socket.
891 else if ((retval > 0) && (!server_shutting_down)) for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next) {
893 if (FD_ISSET(serviceptr->msock, &readfds)) {
894 ssock = accept(serviceptr->msock, NULL, 0);
896 syslog(LOG_DEBUG, "sysdep: new client socket %d", ssock);
898 // The master socket is non-blocking but the client
899 // sockets need to be blocking, otherwise certain
900 // operations barf on FreeBSD. Not a fatal error.
901 if (fcntl(ssock, F_SETFL, 0) < 0) {
902 syslog(LOG_ERR, "sysdep: Can't set socket to blocking: %m");
905 // New context will be created already
906 // set up in the CON_EXECUTING state.
907 con = CreateNewContext();
909 // Assign our new socket number to it.
910 con->tcp_port = serviceptr->tcp_port;
911 con->client_socket = ssock;
912 con->h_command_function = serviceptr->h_command_function;
913 con->h_async_function = serviceptr->h_async_function;
914 con->h_greeting_function = serviceptr->h_greeting_function;
915 con->ServiceName = serviceptr->ServiceName;
917 // Connections on a local client are always from the same host
918 if (serviceptr->sockpath != NULL) {
919 con->is_local_client = 1;
922 // Set the SO_REUSEADDR socket option
924 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
925 con->state = CON_GREETING;
933 // It must be a client socket. Find a context that has data
934 // waiting on its socket *and* is in the CON_IDLE state. Any
935 // active sockets other than our chosen one are marked as
936 // CON_READY so the next thread that comes around can just bind
937 // to one without having to select() again.
938 begin_critical_section(S_SESSION_TABLE);
939 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
940 int checkfd = ptr->client_socket;
941 if ((checkfd != -1) && (ptr->state == CON_IDLE) ){
942 if (FD_ISSET(checkfd, &readfds)) {
943 ptr->input_waiting = 1;
945 bind_me = ptr; // I choose you!
946 bind_me->state = CON_EXECUTING;
949 ptr->state = CON_READY;
951 } else if ((ptr->is_async) && (ptr->async_waiting) && (ptr->h_async_function)) {
953 bind_me = ptr; // I choose you!
954 bind_me->state = CON_EXECUTING;
957 ptr->state = CON_READY;
962 end_critical_section(S_SESSION_TABLE);
965 // We're bound to a session
966 pthread_mutex_lock(&ThreadCountMutex);
968 pthread_mutex_unlock(&ThreadCountMutex);
970 if (bind_me != NULL) {
971 become_session(bind_me);
973 if (bind_me->state == CON_STARTING) {
974 bind_me->state = CON_EXECUTING;
975 begin_session(bind_me);
976 bind_me->h_greeting_function();
978 // If the client has sent a command, execute it.
979 if (CC->input_waiting) {
980 CC->h_command_function();
982 while (HaveMoreLinesWaiting(CC))
983 CC->h_command_function();
985 CC->input_waiting = 0;
988 // If there are asynchronous messages waiting and the client supports it, do those now
989 if ((CC->is_async) && (CC->async_waiting) && (CC->h_async_function != NULL)) {
990 CC->h_async_function();
991 CC->async_waiting = 0;
994 force_purge = CC->kill_me;
995 become_session(NULL);
996 bind_me->state = CON_IDLE;
999 dead_session_purge(force_purge);
1002 pthread_mutex_lock(&ThreadCountMutex);
1004 if ((active_workers + CtdlGetConfigInt("c_min_workers") < num_workers) &&
1005 (num_workers > CtdlGetConfigInt("c_min_workers")))
1008 pthread_mutex_unlock(&ThreadCountMutex);
1011 pthread_mutex_unlock(&ThreadCountMutex);
1014 // If control reaches this point, the server is shutting down
1015 pthread_mutex_lock(&ThreadCountMutex);
1017 pthread_mutex_unlock(&ThreadCountMutex);
1023 // Translate text facility name to syslog.h defined value.
1024 int SyslogFacility(char *name)
1033 { LOG_KERN, "kern" },
1034 { LOG_USER, "user" },
1035 { LOG_MAIL, "mail" },
1036 { LOG_DAEMON, "daemon" },
1037 { LOG_AUTH, "auth" },
1038 { LOG_SYSLOG, "syslog" },
1040 { LOG_NEWS, "news" },
1041 { LOG_UUCP, "uucp" },
1042 { LOG_LOCAL0, "local0" },
1043 { LOG_LOCAL1, "local1" },
1044 { LOG_LOCAL2, "local2" },
1045 { LOG_LOCAL3, "local3" },
1046 { LOG_LOCAL4, "local4" },
1047 { LOG_LOCAL5, "local5" },
1048 { LOG_LOCAL6, "local6" },
1049 { LOG_LOCAL7, "local7" },
1052 for(i = 0; facTbl[i].name != NULL; i++) {
1053 if(!strcasecmp(name, facTbl[i].name))
1054 return facTbl[i].facility;