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-2021 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.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
24 #include <sys/syslog.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <netinet/tcp.h>
33 #include <arpa/inet.h>
34 #define SHOW_ME_VAPPEND_PRINTF
35 #include <libcitadel.h>
36 #include "citserver.h"
38 #include "ctdl_module.h"
39 #include "sysdep_decls.h"
40 #include "modules/crypto/serv_crypto.h" // Needed for init_ssl, client_write_ssl, client_read_ssl
41 #include "housekeeping.h"
44 // Signal handler to shut down the server.
45 volatile int exit_signal = 0;
46 volatile int shutdown_and_halt = 0;
47 volatile int restart_server = 0;
48 volatile int running_as_daemon = 0;
51 static RETSIGTYPE signal_cleanup(int signum) {
52 syslog(LOG_DEBUG, "sysdep: caught signal %d", signum);
54 server_shutting_down = 1;
58 // Some initialization stuff...
59 void init_sysdep(void) {
62 // Avoid vulnerabilities related to FD_SETSIZE if we can.
66 getrlimit(RLIMIT_NOFILE, &rl);
67 rl.rlim_cur = FD_SETSIZE;
68 rl.rlim_max = FD_SETSIZE;
69 setrlimit(RLIMIT_NOFILE, &rl);
73 // If we've got OpenSSL, we're going to use it.
78 if (pthread_key_create(&ThreadKey, NULL) != 0) { // TSD for threads
79 syslog(LOG_ERR, "pthread_key_create() : %m");
83 if (pthread_key_create(&MyConKey, NULL) != 0) { // TSD for sessions
84 syslog(LOG_CRIT, "sysdep: can't create TSD key: %m");
88 // Interript, hangup, and terminate signals should cause the server to shut down.
90 sigaddset(&set, SIGINT);
91 sigaddset(&set, SIGHUP);
92 sigaddset(&set, SIGTERM);
93 sigprocmask(SIG_UNBLOCK, &set, NULL);
95 signal(SIGINT, signal_cleanup);
96 signal(SIGHUP, signal_cleanup);
97 signal(SIGTERM, signal_cleanup);
99 // Do not shut down the server on broken pipe signals, otherwise the
100 // whole Citadel service would come down whenever a single client
102 signal(SIGPIPE, SIG_IGN);
106 // This is a generic function to set up a master socket for listening on
107 // a TCP port. The server shuts down if the bind fails. (IPv4/IPv6 version)
109 // ip_addr IP address to bind
110 // port_number port number to bind
111 // queue_len number of incoming connections to allow in the queue
112 int ctdl_tcp_server(char *ip_addr, int port_number, int queue_len) {
114 struct sockaddr_in6 sin6;
115 struct sockaddr_in sin4;
119 memset(&sin6, 0, sizeof(sin6));
120 memset(&sin4, 0, sizeof(sin4));
121 sin6.sin6_family = AF_INET6;
122 sin4.sin_family = AF_INET;
124 if ( (ip_addr == NULL) // any IPv6
125 || (IsEmptyStr(ip_addr))
126 || (!strcmp(ip_addr, "*"))
129 sin6.sin6_addr = in6addr_any;
131 else if (!strcmp(ip_addr, "0.0.0.0")) // any IPv4
134 sin4.sin_addr.s_addr = INADDR_ANY;
136 else if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':'))) // specific IPv4
139 if (inet_pton(AF_INET, ip_addr, &sin4.sin_addr) <= 0) {
140 syslog(LOG_ALERT, "tcpserver: inet_pton: %m");
144 else // specific IPv6
147 if (inet_pton(AF_INET6, ip_addr, &sin6.sin6_addr) <= 0) {
148 syslog(LOG_ALERT, "tcpserver: inet_pton: %m");
153 if (port_number == 0) {
154 syslog(LOG_ALERT, "tcpserver: no port number was specified");
157 sin6.sin6_port = htons((u_short) port_number);
158 sin4.sin_port = htons((u_short) port_number);
160 p = getprotobyname("tcp");
162 syslog(LOG_ALERT, "tcpserver: getprotobyname: %m");
166 s = socket( ((ip_version == 6) ? PF_INET6 : PF_INET), SOCK_STREAM, (p->p_proto));
168 syslog(LOG_ALERT, "tcpserver: socket: %m");
171 // Set some socket options that make sense.
173 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
175 if (ip_version == 6) {
176 b = bind(s, (struct sockaddr *) &sin6, sizeof(sin6));
179 b = bind(s, (struct sockaddr *) &sin4, sizeof(sin4));
183 syslog(LOG_ALERT, "tcpserver: bind: %m");
187 fcntl(s, F_SETFL, O_NONBLOCK);
189 if (listen(s, ((queue_len >= 5) ? queue_len : 5) ) < 0) {
190 syslog(LOG_ALERT, "tcpserver: listen: %m");
197 // Create a Unix domain socket and listen on it
198 int ctdl_uds_server(char *sockpath, int queue_len) {
199 struct sockaddr_un addr;
202 int actual_queue_len;
203 #ifdef HAVE_STRUCT_UCRED
207 actual_queue_len = queue_len;
208 if (actual_queue_len < 5) actual_queue_len = 5;
210 i = unlink(sockpath);
211 if ((i != 0) && (errno != ENOENT)) {
212 syslog(LOG_ERR, "udsserver: %m");
216 memset(&addr, 0, sizeof(addr));
217 addr.sun_family = AF_UNIX;
218 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
220 s = socket(AF_UNIX, SOCK_STREAM, 0);
222 syslog(LOG_ERR, "udsserver: socket: %m");
226 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
227 syslog(LOG_ERR, "udsserver: bind: %m");
231 // set to nonblock - we need this for some obscure situations
232 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
233 syslog(LOG_ERR, "udsserver: fcntl: %m");
238 if (listen(s, actual_queue_len) < 0) {
239 syslog(LOG_ERR, "udsserver: listen: %m");
243 #ifdef HAVE_STRUCT_UCRED
244 setsockopt(s, SOL_SOCKET, SO_PASSCRED, &passcred, sizeof(passcred));
247 chmod(sockpath, S_ISGID|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
252 // The following functions implement output buffering on operating systems which
253 // support it (such as Linux and various BSD flavors).
256 # define HAVE_TCP_BUFFERING
259 # define HAVE_TCP_BUFFERING
260 # define TCP_CORK TCP_NOPUSH
263 #endif // HAVE_DARWIN
265 static unsigned on = 1, off = 0;
267 void buffer_output(void) {
268 #ifdef HAVE_TCP_BUFFERING
270 if (!CC->redirect_ssl)
272 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
276 void unbuffer_output(void) {
277 #ifdef HAVE_TCP_BUFFERING
279 if (!CC->redirect_ssl)
281 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
285 void flush_output(void) {
286 #ifdef HAVE_TCP_BUFFERING
287 struct CitContext *CCC = CC;
288 setsockopt(CCC->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
289 setsockopt(CCC->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
294 // close the client socket
295 void client_close(void) {
296 CitContext *CCC = CC;
299 if (CCC->client_socket <= 0) return;
300 syslog(LOG_DEBUG, "sysdep: closing socket %d", CCC->client_socket);
301 close(CCC->client_socket);
302 CCC->client_socket = -1 ;
306 // Send binary data to the client.
307 int client_write(const char *buf, int nbytes)
309 int bytes_written = 0;
311 #ifndef HAVE_TCP_BUFFERING
312 int old_buffer_len = 0;
318 if (nbytes < 1) return(0);
322 if (Ctx->redirect_buffer != NULL) {
323 StrBufAppendBufPlain(Ctx->redirect_buffer,
329 if (Ctx->redirect_ssl) {
330 client_write_ssl(buf, nbytes);
334 if (Ctx->client_socket == -1) return -1;
336 fdflags = fcntl(Ctx->client_socket, F_GETFL);
338 while ((bytes_written < nbytes) && (Ctx->client_socket != -1)){
339 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
341 FD_SET(Ctx->client_socket, &wset);
342 if (select(1, NULL, &wset, NULL, NULL) == -1) {
345 syslog(LOG_DEBUG, "sysdep: client_write(%d bytes) select() interrupted.", nbytes-bytes_written);
346 if (server_shutting_down) {
347 CC->kill_me = KILLME_SELECT_INTERRUPTED;
350 // can't trust fd's and stuff so we need to re-create them
354 syslog(LOG_ERR, "sysdep: client_write(%d bytes) select failed: %m", nbytes - bytes_written);
356 Ctx->kill_me = KILLME_SELECT_FAILED;
362 retval = write(Ctx->client_socket, &buf[bytes_written], nbytes - bytes_written);
364 syslog(LOG_ERR, "sysdep: client_write(%d bytes) failed: %m", nbytes - bytes_written);
366 Ctx->kill_me = KILLME_WRITE_FAILED;
369 bytes_written = bytes_written + retval;
374 void cputbuf(const StrBuf *Buf) {
375 client_write(ChrPtr(Buf), StrLength(Buf));
379 // Send formatted printable data to the client.
380 // Implemented in terms of client_write() so it's technically not sysdep...
381 void cprintf(const char *format, ...) {
385 va_start(arg_ptr, format);
386 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
387 buf[sizeof buf - 2] = '\n';
388 client_write(buf, strlen(buf));
393 // Read data from the client socket.
395 // sock socket fd to read from
396 // buf buffer to read into
397 // bytes number of bytes to read
398 // timeout Number of seconds to wait before timing out
400 // Possible return values:
401 // 1 Requested number of bytes has been read.
402 // 0 Request timed out.
403 // -1 Connection is broken, or other error.
404 int client_read_blob(StrBuf *Target, int bytes, int timeout) {
410 if (CCC->redirect_ssl) {
411 retval = client_read_sslblob(Target, bytes, timeout);
413 syslog(LOG_ERR, "sysdep: client_read_blob() failed");
419 retval = StrBufReadBLOBBuffered(Target,
421 &CCC->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)
443 FlushStrBuf(CCC->RecvBuf.Buf);
444 ReAdjustEmptyBuf(CCC->RecvBuf.Buf, N * SIZ, N * SIZ);
447 int client_read_random_blob(StrBuf *Target, int timeout)
452 rc = client_read_blob(Target, 1, timeout);
458 len = StrLength(CCC->RecvBuf.Buf);
459 pch = ChrPtr(CCC->RecvBuf.Buf);
463 if (CCC->RecvBuf.ReadWritePointer != NULL) {
464 len -= CCC->RecvBuf.ReadWritePointer - pch;
465 pch = CCC->RecvBuf.ReadWritePointer;
467 StrBufAppendBufPlain(Target, pch, len, 0);
468 FlushStrBuf(CCC->RecvBuf.Buf);
469 CCC->RecvBuf.ReadWritePointer = NULL;
470 return StrLength(Target);
478 int client_read_to(char *buf, int bytes, int timeout)
483 rc = client_read_blob(CCC->MigrateBuf, bytes, timeout);
492 ChrPtr(CCC->MigrateBuf),
493 StrLength(CCC->MigrateBuf) + 1);
494 FlushStrBuf(CCC->MigrateBuf);
500 int HaveMoreLinesWaiting(CitContext *CCC) {
501 if ((CCC->kill_me != 0) ||
502 ( (CCC->RecvBuf.ReadWritePointer == NULL) &&
503 (StrLength(CCC->RecvBuf.Buf) == 0) &&
504 (CCC->client_socket != -1)) )
511 // Read data from the client socket with default timeout.
512 // (This is implemented in terms of client_read_to() and could be
513 // justifiably moved out of sysdep.c)
514 INLINE int client_read(char *buf, int bytes) {
515 return(client_read_to(buf, bytes, CtdlGetConfigInt("c_sleeping")));
518 int CtdlClientGetLine(StrBuf *Target) {
525 if (CCC->redirect_ssl) {
526 rc = client_readline_sslbuffer(Target, CCC->RecvBuf.Buf, &CCC->RecvBuf.ReadWritePointer, 1);
532 rc = StrBufTCP_read_buffered_line_fast(Target,
534 &CCC->RecvBuf.ReadWritePointer,
545 // Get a LF-terminated line of text from the client.
546 // (This is implemented in terms of client_read() and could be
547 // justifiably moved out of sysdep.c)
548 int client_getln(char *buf, int bufsize) {
553 retval = CtdlClientGetLine(CCC->MigrateBuf);
558 i = StrLength(CCC->MigrateBuf);
559 pCh = ChrPtr(CCC->MigrateBuf);
560 // Strip the trailing LF, and the trailing CR if present.
564 && ( (pCh[i - 1]==13)
565 || ( pCh[i - 1]==10)) ) {
571 FlushStrBuf(CCC->MigrateBuf);
573 safestrncpy(&buf[i], "000", bufsize - i);
579 // Cleanup any contexts that are left lying around
580 void close_masters(void) {
581 struct ServiceFunctionHook *serviceptr;
584 // close all protocol master sockets
585 for (serviceptr = ServiceHookTable; serviceptr != NULL;
586 serviceptr = serviceptr->next ) {
588 if (serviceptr->tcp_port > 0) {
589 if (serviceptr->msock == -1) {
590 Text = "not closing again";
595 syslog(LOG_INFO, "sysdep: %s %d listener on port %d",
600 serviceptr->tcp_port = 0;
603 if (serviceptr->sockpath != NULL) {
604 if (serviceptr->msock == -1) {
605 Text = "not closing again";
610 syslog(LOG_INFO, "sysdep: %s %d listener on '%s'",
617 if (serviceptr->msock != -1) {
618 close(serviceptr->msock);
619 serviceptr->msock = -1;
622 // If it's a Unix domain socket, remove the file.
623 if (serviceptr->sockpath != NULL) {
624 unlink(serviceptr->sockpath);
625 serviceptr->sockpath = NULL;
631 // The system-dependent part of master_cleanup() - close the master socket.
632 void sysdep_master_cleanup(void) {
640 void graceful_shutdown(int signum) {
641 kill(current_child, signum);
642 unlink(file_pid_file);
647 int nFireUpsNonRestart = 0;
650 // Start running as a daemon.
651 void start_daemon(int unused) {
658 // Close stdin/stdout/stderr and replace them with /dev/null.
659 // We don't just call close() because we don't want these fd's
660 // to be reused for other files.
666 signal(SIGHUP, SIG_IGN);
667 signal(SIGINT, SIG_IGN);
668 signal(SIGQUIT, SIG_IGN);
672 if ( (freopen("/dev/null", "r", stdin) != stdin) ||
673 (freopen("/dev/null", "w", stdout) != stdout) ||
674 (freopen("/dev/null", "w", stderr) != stderr)
676 syslog(LOG_ERR, "sysdep: unable to reopen stdio: %m");
680 current_child = fork();
681 signal(SIGTERM, graceful_shutdown);
682 if (current_child < 0) {
686 else if (current_child == 0) {
687 return; // continue starting citadel.
690 fp = fopen(file_pid_file, "w");
692 fprintf(fp, ""F_PID_T"\n", getpid());
695 waitpid(current_child, &status, 0);
697 nFireUpsNonRestart = nFireUps;
699 // Exit code 0 means the watcher should exit
700 if (WIFEXITED(status) && (WEXITSTATUS(status) == CTDLEXIT_SHUTDOWN)) {
704 // Exit code 101-109 means the watcher should exit
705 else if (WIFEXITED(status) && (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109)) {
709 // Any other exit code, or no exit code, means we should restart.
713 ForkedPid = current_child;
716 } while (do_restart);
718 unlink(file_pid_file);
719 exit(WEXITSTATUS(status));
723 void checkcrash(void) {
724 if (nFireUpsNonRestart != nFireUps) {
726 CrashMail = NewStrBuf();
727 syslog(LOG_ALERT, "sysdep: posting crash message");
728 StrBufPrintf(CrashMail,
730 " The Citadel server process (citserver) terminated unexpectedly."
732 " This could be the result of a bug in the server program, or some external "
734 " You can obtain more information about this by enabling core dumps.\n \n"
735 " For more information, please see:\n \n"
736 " http://citadel.org/doku.php?id=faq:mastering_your_os:gdb#how.do.i.make.my.system.produce.core-files"
739 " If you have already done this, the core dump is likely to be found at %score.%d\n"
741 ctdl_run_dir, ForkedPid);
742 CtdlAideMessage(ChrPtr(CrashMail), "Citadel server process terminated unexpectedly");
743 FreeStrBuf(&CrashMail);
748 // Generic routine to convert a login name to a full name (gecos)
749 // Returns nonzero if a conversion took place
750 int convert_login(char NameToConvert[]) {
754 pw = getpwnam(NameToConvert);
759 strcpy(NameToConvert, pw->pw_gecos);
760 for (a=0; a<strlen(NameToConvert); ++a) {
761 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
768 void HuntBadSession(void) {
773 struct ServiceFunctionHook *serviceptr;
775 // Next, add all of the client sockets
776 begin_critical_section(S_SESSION_TABLE);
777 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
778 if ((ptr->state == CON_SYS) && (ptr->client_socket == 0))
780 // Initialize the fdset.
783 tv.tv_sec = 0; // wake up every second if no input
786 // Don't select on dead sessions, only truly idle ones
787 if ( (ptr->state == CON_IDLE)
788 && (ptr->kill_me == 0)
789 && (ptr->client_socket > 0)
791 FD_SET(ptr->client_socket, &readfds);
792 if (ptr->client_socket > highest)
793 highest = ptr->client_socket;
795 if ((select(highest + 1, &readfds, NULL, NULL, &tv) < 0) && (errno == EBADF))
799 "sysdep: killing session CC[%d] bad FD: [%d] User[%s] Host[%s:%s]",
807 ptr->client_socket = -1;
812 end_critical_section(S_SESSION_TABLE);
814 // First, add the various master sockets to the fdset.
815 for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
817 // Initialize the fdset.
819 tv.tv_sec = 0; // wake up every second if no input
822 FD_SET(serviceptr->msock, &readfds);
823 if (serviceptr->msock > highest) {
824 highest = serviceptr->msock;
826 if ((select(highest + 1, &readfds, NULL, NULL, &tv) < 0) &&
829 // Gotcha! server socket dead? commit suicide!
830 syslog(LOG_ERR, "sysdep: found bad FD: %d and its a server socket! Shutting Down!", serviceptr->msock);
831 server_shutting_down = 1;
838 // This loop just keeps going and going and going...
839 void *worker_thread(void *blah) {
842 CitContext *bind_me = NULL;
847 struct ServiceFunctionHook *serviceptr;
848 int ssock; // Descriptor for client socket
849 CitContext *con = NULL; // Temporary context pointer
852 pthread_mutex_lock(&ThreadCountMutex);
854 pthread_mutex_unlock(&ThreadCountMutex);
856 while (!server_shutting_down) {
858 // make doubly sure we're not holding any stale db handles which might cause a deadlock
860 do_select: force_purge = 0;
861 bind_me = NULL; // Which session shall we handle?
863 // Initialize the fdset
867 // First, add the various master sockets to the fdset.
868 for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
869 FD_SET(serviceptr->msock, &readfds);
870 if (serviceptr->msock > highest) {
871 highest = serviceptr->msock;
875 // Next, add all of the client sockets.
876 begin_critical_section(S_SESSION_TABLE);
877 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
878 if ((ptr->state == CON_SYS) && (ptr->client_socket == 0))
881 // Don't select on dead sessions, only truly idle ones
882 if ( (ptr->state == CON_IDLE)
883 && (ptr->kill_me == 0)
884 && (ptr->client_socket > 0)
886 FD_SET(ptr->client_socket, &readfds);
887 if (ptr->client_socket > highest)
888 highest = ptr->client_socket;
890 if ((bind_me == NULL) && (ptr->state == CON_READY)) {
892 ptr->state = CON_EXECUTING;
895 if ((bind_me == NULL) && (ptr->state == CON_GREETING)) {
897 ptr->state = CON_STARTING;
901 end_critical_section(S_SESSION_TABLE);
907 // If we got this far, it means that there are no sessions
908 // which a previous thread marked for attention, so we go
909 // ahead and get ready to select().
911 if (!server_shutting_down) {
912 tv.tv_sec = 1; // wake up every second if no input
914 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
921 // Now figure out who made this select() unblock.
922 // First, check for an error or exit condition.
924 if (errno == EBADF) {
925 syslog(LOG_ERR, "sysdep: select() failed: %m");
929 if (errno != EINTR) {
930 syslog(LOG_ERR, "sysdep: exiting: %m");
931 server_shutting_down = 1;
934 if (server_shutting_down) {
941 else if (retval == 0) {
942 if (server_shutting_down) {
948 // Next, check to see if it's a new client connecting on a master socket.
950 else if ((retval > 0) && (!server_shutting_down)) for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next) {
952 if (FD_ISSET(serviceptr->msock, &readfds)) {
953 ssock = accept(serviceptr->msock, NULL, 0);
955 syslog(LOG_DEBUG, "sysdep: new client socket %d", ssock);
957 // The master socket is non-blocking but the client
958 // sockets need to be blocking, otherwise certain
959 // operations barf on FreeBSD. Not a fatal error.
960 if (fcntl(ssock, F_SETFL, 0) < 0) {
961 syslog(LOG_ERR, "sysdep: Can't set socket to blocking: %m");
964 // New context will be created already
965 // set up in the CON_EXECUTING state.
966 con = CreateNewContext();
968 // Assign our new socket number to it.
969 con->tcp_port = serviceptr->tcp_port;
970 con->client_socket = ssock;
971 con->h_command_function = serviceptr->h_command_function;
972 con->h_async_function = serviceptr->h_async_function;
973 con->h_greeting_function = serviceptr->h_greeting_function;
974 con->ServiceName = serviceptr->ServiceName;
976 // Connections on a local client are always from the same host
977 if (serviceptr->sockpath != NULL) {
978 con->is_local_client = 1;
981 // Set the SO_REUSEADDR socket option
983 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
984 con->state = CON_GREETING;
992 // It must be a client socket. Find a context that has data
993 // waiting on its socket *and* is in the CON_IDLE state. Any
994 // active sockets other than our chosen one are marked as
995 // CON_READY so the next thread that comes around can just bind
996 // to one without having to select() again.
997 begin_critical_section(S_SESSION_TABLE);
998 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
999 int checkfd = ptr->client_socket;
1000 if ((checkfd != -1) && (ptr->state == CON_IDLE) ){
1001 if (FD_ISSET(checkfd, &readfds)) {
1002 ptr->input_waiting = 1;
1004 bind_me = ptr; // I choose you!
1005 bind_me->state = CON_EXECUTING;
1008 ptr->state = CON_READY;
1010 } else if ((ptr->is_async) && (ptr->async_waiting) && (ptr->h_async_function)) {
1012 bind_me = ptr; // I choose you!
1013 bind_me->state = CON_EXECUTING;
1016 ptr->state = CON_READY;
1021 end_critical_section(S_SESSION_TABLE);
1024 // We're bound to a session
1025 pthread_mutex_lock(&ThreadCountMutex);
1027 pthread_mutex_unlock(&ThreadCountMutex);
1029 if (bind_me != NULL) {
1030 become_session(bind_me);
1032 if (bind_me->state == CON_STARTING) {
1033 bind_me->state = CON_EXECUTING;
1034 begin_session(bind_me);
1035 bind_me->h_greeting_function();
1037 // If the client has sent a command, execute it.
1038 if (CC->input_waiting) {
1039 CC->h_command_function();
1041 while (HaveMoreLinesWaiting(CC))
1042 CC->h_command_function();
1044 CC->input_waiting = 0;
1047 // If there are asynchronous messages waiting and the client supports it, do those now
1048 if ((CC->is_async) && (CC->async_waiting) && (CC->h_async_function != NULL)) {
1049 CC->h_async_function();
1050 CC->async_waiting = 0;
1053 force_purge = CC->kill_me;
1054 become_session(NULL);
1055 bind_me->state = CON_IDLE;
1058 dead_session_purge(force_purge);
1061 pthread_mutex_lock(&ThreadCountMutex);
1063 if ((active_workers + CtdlGetConfigInt("c_min_workers") < num_workers) &&
1064 (num_workers > CtdlGetConfigInt("c_min_workers")))
1067 pthread_mutex_unlock(&ThreadCountMutex);
1070 pthread_mutex_unlock(&ThreadCountMutex);
1073 // If control reaches this point, the server is shutting down
1074 pthread_mutex_lock(&ThreadCountMutex);
1076 pthread_mutex_unlock(&ThreadCountMutex);
1082 // Translate text facility name to syslog.h defined value.
1083 int SyslogFacility(char *name)
1092 { LOG_KERN, "kern" },
1093 { LOG_USER, "user" },
1094 { LOG_MAIL, "mail" },
1095 { LOG_DAEMON, "daemon" },
1096 { LOG_AUTH, "auth" },
1097 { LOG_SYSLOG, "syslog" },
1099 { LOG_NEWS, "news" },
1100 { LOG_UUCP, "uucp" },
1101 { LOG_LOCAL0, "local0" },
1102 { LOG_LOCAL1, "local1" },
1103 { LOG_LOCAL2, "local2" },
1104 { LOG_LOCAL3, "local3" },
1105 { LOG_LOCAL4, "local4" },
1106 { LOG_LOCAL5, "local5" },
1107 { LOG_LOCAL6, "local6" },
1108 { LOG_LOCAL7, "local7" },
1111 for(i = 0; facTbl[i].name != NULL; i++) {
1112 if(!strcasecmp(name, facTbl[i].name))
1113 return facTbl[i].facility;