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, destruct_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 - backtrace follows:", signum);
58 FILE *backtrace_fp = NULL;
60 if (backtrace_filename != NULL) {
61 backtrace_fp = fopen(backtrace_filename, "w");
64 bt_size = backtrace(bt, 1024);
65 bt_syms = backtrace_symbols(bt, bt_size);
66 for (i = 1; i < bt_size; i++) {
67 syslog(LOG_DEBUG, "%s", bt_syms[i]);
69 fprintf(backtrace_fp, "%s\n", bt_syms[i]);
79 server_shutting_down = 1;
83 // Some initialization stuff...
84 void init_sysdep(void) {
87 // Avoid vulnerabilities related to FD_SETSIZE if we can.
91 getrlimit(RLIMIT_NOFILE, &rl);
92 rl.rlim_cur = FD_SETSIZE;
93 rl.rlim_max = FD_SETSIZE;
94 setrlimit(RLIMIT_NOFILE, &rl);
98 // If we've got OpenSSL, we're going to use it.
103 if (pthread_key_create(&ThreadKey, NULL) != 0) { // TSD for threads
104 syslog(LOG_ERR, "pthread_key_create() : %m");
108 if (pthread_key_create(&MyConKey, NULL) != 0) { // TSD for sessions
109 syslog(LOG_CRIT, "sysdep: can't create TSD key: %m");
113 // Interript, hangup, and terminate signals should cause the server to shut down.
115 sigaddset(&set, SIGINT);
116 sigaddset(&set, SIGHUP);
117 sigaddset(&set, SIGTERM);
118 sigaddset(&set, SIGSEGV);
119 sigprocmask(SIG_UNBLOCK, &set, NULL);
121 signal(SIGINT, signal_cleanup);
122 signal(SIGHUP, signal_cleanup);
123 signal(SIGTERM, signal_cleanup);
124 signal(SIGSEGV, signal_cleanup);
126 // Do not shut down the server on broken pipe signals, otherwise the
127 // whole Citadel service would come down whenever a single client
129 signal(SIGPIPE, SIG_IGN);
133 // This is a generic function to set up a master socket for listening on
134 // a TCP port. The server shuts down if the bind fails. (IPv4/IPv6 version)
136 // ip_addr IP address to bind
137 // port_number port number to bind
138 // queue_len number of incoming connections to allow in the queue
139 int ctdl_tcp_server(char *ip_addr, int port_number, int queue_len) {
141 struct sockaddr_in6 sin6;
142 struct sockaddr_in sin4;
146 memset(&sin6, 0, sizeof(sin6));
147 memset(&sin4, 0, sizeof(sin4));
148 sin6.sin6_family = AF_INET6;
149 sin4.sin_family = AF_INET;
151 if ( (ip_addr == NULL) // any IPv6
152 || (IsEmptyStr(ip_addr))
153 || (!strcmp(ip_addr, "*"))
156 sin6.sin6_addr = in6addr_any;
158 else if (!strcmp(ip_addr, "0.0.0.0")) // any IPv4
161 sin4.sin_addr.s_addr = INADDR_ANY;
163 else if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':'))) // specific IPv4
166 if (inet_pton(AF_INET, ip_addr, &sin4.sin_addr) <= 0) {
167 syslog(LOG_ALERT, "tcpserver: inet_pton: %m");
171 else // specific IPv6
174 if (inet_pton(AF_INET6, ip_addr, &sin6.sin6_addr) <= 0) {
175 syslog(LOG_ALERT, "tcpserver: inet_pton: %m");
180 if (port_number == 0) {
181 syslog(LOG_ALERT, "tcpserver: no port number was specified");
184 sin6.sin6_port = htons((u_short) port_number);
185 sin4.sin_port = htons((u_short) port_number);
187 p = getprotobyname("tcp");
189 syslog(LOG_ALERT, "tcpserver: getprotobyname: %m");
193 s = socket( ((ip_version == 6) ? PF_INET6 : PF_INET), SOCK_STREAM, (p->p_proto));
195 syslog(LOG_ALERT, "tcpserver: socket: %m");
198 // Set some socket options that make sense.
200 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
202 if (ip_version == 6) {
203 b = bind(s, (struct sockaddr *) &sin6, sizeof(sin6));
206 b = bind(s, (struct sockaddr *) &sin4, sizeof(sin4));
210 syslog(LOG_ALERT, "tcpserver: bind: %m");
214 fcntl(s, F_SETFL, O_NONBLOCK);
216 if (listen(s, ((queue_len >= 5) ? queue_len : 5) ) < 0) {
217 syslog(LOG_ALERT, "tcpserver: listen: %m");
224 // Create a Unix domain socket and listen on it
225 int ctdl_uds_server(char *sockpath, int queue_len) {
226 struct sockaddr_un addr;
229 int actual_queue_len;
230 #ifdef HAVE_STRUCT_UCRED
234 actual_queue_len = queue_len;
235 if (actual_queue_len < 5) actual_queue_len = 5;
237 i = unlink(sockpath);
238 if ((i != 0) && (errno != ENOENT)) {
239 syslog(LOG_ERR, "udsserver: %m");
243 memset(&addr, 0, sizeof(addr));
244 addr.sun_family = AF_UNIX;
245 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
247 s = socket(AF_UNIX, SOCK_STREAM, 0);
249 syslog(LOG_ERR, "udsserver: socket: %m");
253 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
254 syslog(LOG_ERR, "udsserver: bind: %m");
258 // set to nonblock - we need this for some obscure situations
259 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
260 syslog(LOG_ERR, "udsserver: fcntl: %m");
265 if (listen(s, actual_queue_len) < 0) {
266 syslog(LOG_ERR, "udsserver: listen: %m");
270 #ifdef HAVE_STRUCT_UCRED
271 setsockopt(s, SOL_SOCKET, SO_PASSCRED, &passcred, sizeof(passcred));
274 chmod(sockpath, S_ISGID|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
279 // The following functions implement output buffering on operating systems which
280 // support it (such as Linux and various BSD flavors).
283 # define HAVE_TCP_BUFFERING
286 # define HAVE_TCP_BUFFERING
287 # define TCP_CORK TCP_NOPUSH
290 #endif // HAVE_DARWIN
292 static unsigned on = 1, off = 0;
294 void buffer_output(void) {
295 #ifdef HAVE_TCP_BUFFERING
297 if (!CC->redirect_ssl)
299 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
303 void unbuffer_output(void) {
304 #ifdef HAVE_TCP_BUFFERING
306 if (!CC->redirect_ssl)
308 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
312 void flush_output(void) {
313 #ifdef HAVE_TCP_BUFFERING
314 struct CitContext *CCC = CC;
315 setsockopt(CCC->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
316 setsockopt(CCC->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
321 // close the client socket
322 void client_close(void) {
323 CitContext *CCC = CC;
326 if (CCC->client_socket <= 0) return;
327 syslog(LOG_DEBUG, "sysdep: closing socket %d", CCC->client_socket);
328 close(CCC->client_socket);
329 CCC->client_socket = -1 ;
333 // Send binary data to the client.
334 int client_write(const char *buf, int nbytes)
336 int bytes_written = 0;
338 #ifndef HAVE_TCP_BUFFERING
339 int old_buffer_len = 0;
345 if (nbytes < 1) return(0);
349 if (Ctx->redirect_buffer != NULL) {
350 StrBufAppendBufPlain(Ctx->redirect_buffer,
356 if (Ctx->redirect_ssl) {
357 client_write_ssl(buf, nbytes);
361 if (Ctx->client_socket == -1) return -1;
363 fdflags = fcntl(Ctx->client_socket, F_GETFL);
365 while ((bytes_written < nbytes) && (Ctx->client_socket != -1)){
366 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
368 FD_SET(Ctx->client_socket, &wset);
369 if (select(1, NULL, &wset, NULL, NULL) == -1) {
372 syslog(LOG_DEBUG, "sysdep: client_write(%d bytes) select() interrupted.", nbytes-bytes_written);
373 if (server_shutting_down) {
374 CC->kill_me = KILLME_SELECT_INTERRUPTED;
377 // can't trust fd's and stuff so we need to re-create them
381 syslog(LOG_ERR, "sysdep: client_write(%d bytes) select failed: %m", nbytes - bytes_written);
383 Ctx->kill_me = KILLME_SELECT_FAILED;
389 retval = write(Ctx->client_socket, &buf[bytes_written], nbytes - bytes_written);
391 syslog(LOG_ERR, "sysdep: client_write(%d bytes) failed: %m", nbytes - bytes_written);
393 Ctx->kill_me = KILLME_WRITE_FAILED;
396 bytes_written = bytes_written + retval;
401 void cputbuf(const StrBuf *Buf) {
402 client_write(ChrPtr(Buf), StrLength(Buf));
406 // Send formatted printable data to the client.
407 // Implemented in terms of client_write() so it's technically not sysdep...
408 void cprintf(const char *format, ...) {
412 va_start(arg_ptr, format);
413 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
414 buf[sizeof buf - 2] = '\n';
415 client_write(buf, strlen(buf));
420 // Read data from the client socket.
422 // sock socket fd to read from
423 // buf buffer to read into
424 // bytes number of bytes to read
425 // timeout Number of seconds to wait before timing out
427 // Possible return values:
428 // 1 Requested number of bytes has been read.
429 // 0 Request timed out.
430 // -1 Connection is broken, or other error.
431 int client_read_blob(StrBuf *Target, int bytes, int timeout) {
437 if (CCC->redirect_ssl) {
438 retval = client_read_sslblob(Target, bytes, timeout);
440 syslog(LOG_ERR, "sysdep: client_read_blob() failed");
446 retval = StrBufReadBLOBBuffered(Target,
448 &CCC->RecvBuf.ReadWritePointer,
456 syslog(LOG_ERR, "sysdep: client_read_blob() failed: %s", Error);
465 // to make client_read_random_blob() more efficient, increase buffer size.
466 // just use in greeting function, else your buffer may be flushed
467 void client_set_inbound_buf(long N)
470 FlushStrBuf(CCC->RecvBuf.Buf);
471 ReAdjustEmptyBuf(CCC->RecvBuf.Buf, N * SIZ, N * SIZ);
474 int client_read_random_blob(StrBuf *Target, int timeout)
479 rc = client_read_blob(Target, 1, timeout);
485 len = StrLength(CCC->RecvBuf.Buf);
486 pch = ChrPtr(CCC->RecvBuf.Buf);
490 if (CCC->RecvBuf.ReadWritePointer != NULL) {
491 len -= CCC->RecvBuf.ReadWritePointer - pch;
492 pch = CCC->RecvBuf.ReadWritePointer;
494 StrBufAppendBufPlain(Target, pch, len, 0);
495 FlushStrBuf(CCC->RecvBuf.Buf);
496 CCC->RecvBuf.ReadWritePointer = NULL;
497 return StrLength(Target);
505 int client_read_to(char *buf, int bytes, int timeout)
510 rc = client_read_blob(CCC->MigrateBuf, bytes, timeout);
519 ChrPtr(CCC->MigrateBuf),
520 StrLength(CCC->MigrateBuf) + 1);
521 FlushStrBuf(CCC->MigrateBuf);
527 int HaveMoreLinesWaiting(CitContext *CCC) {
528 if ((CCC->kill_me != 0) ||
529 ( (CCC->RecvBuf.ReadWritePointer == NULL) &&
530 (StrLength(CCC->RecvBuf.Buf) == 0) &&
531 (CCC->client_socket != -1)) )
538 // Read data from the client socket with default timeout.
539 // (This is implemented in terms of client_read_to() and could be
540 // justifiably moved out of sysdep.c)
541 INLINE int client_read(char *buf, int bytes) {
542 return(client_read_to(buf, bytes, CtdlGetConfigInt("c_sleeping")));
545 int CtdlClientGetLine(StrBuf *Target) {
552 if (CCC->redirect_ssl) {
553 rc = client_readline_sslbuffer(Target, CCC->RecvBuf.Buf, &CCC->RecvBuf.ReadWritePointer, 1);
559 rc = StrBufTCP_read_buffered_line_fast(Target,
561 &CCC->RecvBuf.ReadWritePointer,
572 // Get a LF-terminated line of text from the client.
573 // (This is implemented in terms of client_read() and could be
574 // justifiably moved out of sysdep.c)
575 int client_getln(char *buf, int bufsize) {
580 retval = CtdlClientGetLine(CCC->MigrateBuf);
585 i = StrLength(CCC->MigrateBuf);
586 pCh = ChrPtr(CCC->MigrateBuf);
587 // Strip the trailing LF, and the trailing CR if present.
591 && ( (pCh[i - 1]==13)
592 || ( pCh[i - 1]==10)) ) {
598 FlushStrBuf(CCC->MigrateBuf);
600 safestrncpy(&buf[i], "000", bufsize - i);
606 // Cleanup any contexts that are left lying around
607 void close_masters(void) {
608 struct ServiceFunctionHook *serviceptr;
611 // close all protocol master sockets
612 for (serviceptr = ServiceHookTable; serviceptr != NULL;
613 serviceptr = serviceptr->next ) {
615 if (serviceptr->tcp_port > 0) {
616 if (serviceptr->msock == -1) {
617 Text = "not closing again";
622 syslog(LOG_INFO, "sysdep: %s %d listener on port %d",
627 serviceptr->tcp_port = 0;
630 if (serviceptr->sockpath != NULL) {
631 if (serviceptr->msock == -1) {
632 Text = "not closing again";
637 syslog(LOG_INFO, "sysdep: %s %d listener on '%s'",
644 if (serviceptr->msock != -1) {
645 close(serviceptr->msock);
646 serviceptr->msock = -1;
649 // If it's a Unix domain socket, remove the file.
650 if (serviceptr->sockpath != NULL) {
651 unlink(serviceptr->sockpath);
652 serviceptr->sockpath = NULL;
658 // The system-dependent part of master_cleanup() - close the master socket.
659 void sysdep_master_cleanup(void) {
670 void graceful_shutdown(int signum) {
671 kill(current_child, signum);
672 unlink(file_pid_file);
677 int nFireUpsNonRestart = 0;
680 // Start running as a daemon.
681 void start_daemon(int unused) {
688 // Close stdin/stdout/stderr and replace them with /dev/null.
689 // We don't just call close() because we don't want these fd's
690 // to be reused for other files.
696 signal(SIGHUP, SIG_IGN);
697 signal(SIGINT, SIG_IGN);
698 signal(SIGQUIT, SIG_IGN);
702 if ( (freopen("/dev/null", "r", stdin) != stdin) ||
703 (freopen("/dev/null", "w", stdout) != stdout) ||
704 (freopen("/dev/null", "w", stderr) != stderr)
706 syslog(LOG_ERR, "sysdep: unable to reopen stdio: %m");
710 current_child = fork();
711 signal(SIGTERM, graceful_shutdown);
712 if (current_child < 0) {
716 else if (current_child == 0) {
717 return; // continue starting citadel.
720 fp = fopen(file_pid_file, "w");
722 fprintf(fp, ""F_PID_T"\n", getpid());
725 waitpid(current_child, &status, 0);
727 nFireUpsNonRestart = nFireUps;
729 // Exit code 0 means the watcher should exit
730 if (WIFEXITED(status) && (WEXITSTATUS(status) == CTDLEXIT_SHUTDOWN)) {
734 // Exit code 101-109 means the watcher should exit
735 else if (WIFEXITED(status) && (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109)) {
739 // Any other exit code, or no exit code, means we should restart.
743 ForkedPid = current_child;
746 } while (do_restart);
748 unlink(file_pid_file);
749 exit(WEXITSTATUS(status));
753 void checkcrash(void) {
754 if (nFireUpsNonRestart != nFireUps) {
756 CrashMail = NewStrBuf();
757 syslog(LOG_ALERT, "sysdep: posting crash message");
758 StrBufPrintf(CrashMail,
760 " The Citadel server process (citserver) terminated unexpectedly."
762 " This could be the result of a bug in the server program, or some external "
764 " You can obtain more information about this by enabling core dumps.\n \n"
765 " For more information, please see:\n \n"
766 " http://citadel.org/doku.php?id=faq:mastering_your_os:gdb#how.do.i.make.my.system.produce.core-files"
769 " If you have already done this, the core dump is likely to be found at %score.%d\n"
771 ctdl_run_dir, ForkedPid);
772 CtdlAideMessage(ChrPtr(CrashMail), "Citadel server process terminated unexpectedly");
773 FreeStrBuf(&CrashMail);
778 // Generic routine to convert a login name to a full name (gecos)
779 // Returns nonzero if a conversion took place
780 int convert_login(char NameToConvert[]) {
784 pw = getpwnam(NameToConvert);
789 strcpy(NameToConvert, pw->pw_gecos);
790 for (a=0; a<strlen(NameToConvert); ++a) {
791 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
798 void HuntBadSession(void) {
803 struct ServiceFunctionHook *serviceptr;
805 // Next, add all of the client sockets
806 begin_critical_section(S_SESSION_TABLE);
807 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
808 if ((ptr->state == CON_SYS) && (ptr->client_socket == 0))
810 // Initialize the fdset.
813 tv.tv_sec = 0; // wake up every second if no input
816 // Don't select on dead sessions, only truly idle ones
817 if ( (ptr->state == CON_IDLE)
818 && (ptr->kill_me == 0)
819 && (ptr->client_socket > 0)
821 FD_SET(ptr->client_socket, &readfds);
822 if (ptr->client_socket > highest)
823 highest = ptr->client_socket;
825 if ((select(highest + 1, &readfds, NULL, NULL, &tv) < 0) && (errno == EBADF))
829 "sysdep: killing session CC[%d] bad FD: [%d] User[%s] Host[%s:%s]",
837 ptr->client_socket = -1;
842 end_critical_section(S_SESSION_TABLE);
844 // First, add the various master sockets to the fdset.
845 for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
847 // Initialize the fdset.
849 tv.tv_sec = 0; // wake up every second if no input
852 FD_SET(serviceptr->msock, &readfds);
853 if (serviceptr->msock > highest) {
854 highest = serviceptr->msock;
856 if ((select(highest + 1, &readfds, NULL, NULL, &tv) < 0) &&
859 // Gotcha! server socket dead? commit suicide!
860 syslog(LOG_ERR, "sysdep: found bad FD: %d and its a server socket! Shutting Down!", serviceptr->msock);
861 server_shutting_down = 1;
868 // This loop just keeps going and going and going...
869 void *worker_thread(void *blah) {
872 CitContext *bind_me = NULL;
877 struct ServiceFunctionHook *serviceptr;
878 int ssock; // Descriptor for client socket
879 CitContext *con = NULL; // Temporary context pointer
882 pthread_mutex_lock(&ThreadCountMutex);
884 pthread_mutex_unlock(&ThreadCountMutex);
886 while (!server_shutting_down) {
888 // make doubly sure we're not holding any stale db handles which might cause a deadlock
890 do_select: force_purge = 0;
891 bind_me = NULL; // Which session shall we handle?
893 // Initialize the fdset
897 // First, add the various master sockets to the fdset.
898 for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
899 FD_SET(serviceptr->msock, &readfds);
900 if (serviceptr->msock > highest) {
901 highest = serviceptr->msock;
905 // Next, add all of the client sockets.
906 begin_critical_section(S_SESSION_TABLE);
907 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
908 if ((ptr->state == CON_SYS) && (ptr->client_socket == 0))
911 // Don't select on dead sessions, only truly idle ones
912 if ( (ptr->state == CON_IDLE)
913 && (ptr->kill_me == 0)
914 && (ptr->client_socket > 0)
916 FD_SET(ptr->client_socket, &readfds);
917 if (ptr->client_socket > highest)
918 highest = ptr->client_socket;
920 if ((bind_me == NULL) && (ptr->state == CON_READY)) {
922 ptr->state = CON_EXECUTING;
925 if ((bind_me == NULL) && (ptr->state == CON_GREETING)) {
927 ptr->state = CON_STARTING;
931 end_critical_section(S_SESSION_TABLE);
937 // If we got this far, it means that there are no sessions
938 // which a previous thread marked for attention, so we go
939 // ahead and get ready to select().
941 if (!server_shutting_down) {
942 tv.tv_sec = 1; // wake up every second if no input
944 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
951 // Now figure out who made this select() unblock.
952 // First, check for an error or exit condition.
954 if (errno == EBADF) {
955 syslog(LOG_ERR, "sysdep: select() failed: %m");
959 if (errno != EINTR) {
960 syslog(LOG_ERR, "sysdep: exiting: %m");
961 server_shutting_down = 1;
964 if (server_shutting_down) {
971 else if (retval == 0) {
972 if (server_shutting_down) {
978 // Next, check to see if it's a new client connecting on a master socket.
980 else if ((retval > 0) && (!server_shutting_down)) for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next) {
982 if (FD_ISSET(serviceptr->msock, &readfds)) {
983 ssock = accept(serviceptr->msock, NULL, 0);
985 syslog(LOG_DEBUG, "sysdep: new client socket %d", ssock);
987 // The master socket is non-blocking but the client
988 // sockets need to be blocking, otherwise certain
989 // operations barf on FreeBSD. Not a fatal error.
990 if (fcntl(ssock, F_SETFL, 0) < 0) {
991 syslog(LOG_ERR, "sysdep: Can't set socket to blocking: %m");
994 // New context will be created already
995 // set up in the CON_EXECUTING state.
996 con = CreateNewContext();
998 // Assign our new socket number to it.
999 con->tcp_port = serviceptr->tcp_port;
1000 con->client_socket = ssock;
1001 con->h_command_function = serviceptr->h_command_function;
1002 con->h_async_function = serviceptr->h_async_function;
1003 con->h_greeting_function = serviceptr->h_greeting_function;
1004 con->ServiceName = serviceptr->ServiceName;
1006 // Connections on a local client are always from the same host
1007 if (serviceptr->sockpath != NULL) {
1008 con->is_local_client = 1;
1011 // Set the SO_REUSEADDR socket option
1013 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
1014 con->state = CON_GREETING;
1022 // It must be a client socket. Find a context that has data
1023 // waiting on its socket *and* is in the CON_IDLE state. Any
1024 // active sockets other than our chosen one are marked as
1025 // CON_READY so the next thread that comes around can just bind
1026 // to one without having to select() again.
1027 begin_critical_section(S_SESSION_TABLE);
1028 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1029 int checkfd = ptr->client_socket;
1030 if ((checkfd != -1) && (ptr->state == CON_IDLE) ){
1031 if (FD_ISSET(checkfd, &readfds)) {
1032 ptr->input_waiting = 1;
1034 bind_me = ptr; // I choose you!
1035 bind_me->state = CON_EXECUTING;
1038 ptr->state = CON_READY;
1040 } else if ((ptr->is_async) && (ptr->async_waiting) && (ptr->h_async_function)) {
1042 bind_me = ptr; // I choose you!
1043 bind_me->state = CON_EXECUTING;
1046 ptr->state = CON_READY;
1051 end_critical_section(S_SESSION_TABLE);
1054 // We're bound to a session
1055 pthread_mutex_lock(&ThreadCountMutex);
1057 pthread_mutex_unlock(&ThreadCountMutex);
1059 if (bind_me != NULL) {
1060 become_session(bind_me);
1062 if (bind_me->state == CON_STARTING) {
1063 bind_me->state = CON_EXECUTING;
1064 begin_session(bind_me);
1065 bind_me->h_greeting_function();
1067 // If the client has sent a command, execute it.
1068 if (CC->input_waiting) {
1069 CC->h_command_function();
1071 while (HaveMoreLinesWaiting(CC))
1072 CC->h_command_function();
1074 CC->input_waiting = 0;
1077 // If there are asynchronous messages waiting and the client supports it, do those now
1078 if ((CC->is_async) && (CC->async_waiting) && (CC->h_async_function != NULL)) {
1079 CC->h_async_function();
1080 CC->async_waiting = 0;
1083 force_purge = CC->kill_me;
1084 become_session(NULL);
1085 bind_me->state = CON_IDLE;
1088 dead_session_purge(force_purge);
1091 pthread_mutex_lock(&ThreadCountMutex);
1093 if ((active_workers + CtdlGetConfigInt("c_min_workers") < num_workers) &&
1094 (num_workers > CtdlGetConfigInt("c_min_workers")))
1097 pthread_mutex_unlock(&ThreadCountMutex);
1100 pthread_mutex_unlock(&ThreadCountMutex);
1103 // If control reaches this point, the server is shutting down
1104 pthread_mutex_lock(&ThreadCountMutex);
1106 pthread_mutex_unlock(&ThreadCountMutex);
1112 // Translate text facility name to syslog.h defined value.
1113 int SyslogFacility(char *name)
1122 { LOG_KERN, "kern" },
1123 { LOG_USER, "user" },
1124 { LOG_MAIL, "mail" },
1125 { LOG_DAEMON, "daemon" },
1126 { LOG_AUTH, "auth" },
1127 { LOG_SYSLOG, "syslog" },
1129 { LOG_NEWS, "news" },
1130 { LOG_UUCP, "uucp" },
1131 { LOG_LOCAL0, "local0" },
1132 { LOG_LOCAL1, "local1" },
1133 { LOG_LOCAL2, "local2" },
1134 { LOG_LOCAL3, "local3" },
1135 { LOG_LOCAL4, "local4" },
1136 { LOG_LOCAL5, "local5" },
1137 { LOG_LOCAL6, "local6" },
1138 { LOG_LOCAL7, "local7" },
1141 for(i = 0; facTbl[i].name != NULL; i++) {
1142 if(!strcasecmp(name, facTbl[i].name))
1143 return facTbl[i].facility;