4 * Citadel "system dependent" stuff.
5 * See COPYING 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.
22 #include <sys/types.h>
25 #include <sys/socket.h>
27 #include <sys/syslog.h>
29 #if TIME_WITH_SYS_TIME
30 # include <sys/time.h>
34 # include <sys/time.h>
41 #include <sys/resource.h>
42 #include <netinet/in.h>
43 #include <netinet/tcp.h>
44 #include <arpa/inet.h>
52 #define SHOW_ME_VAPPEND_PRINTF
53 #include <libcitadel.h>
56 #include "sysdep_decls.h"
57 #include "citserver.h"
61 #include "housekeeping.h"
62 #include "modules/crypto/serv_crypto.h" /* Needed for init_ssl, client_write_ssl, client_read_ssl, destruct_ssl */
66 #ifdef HAVE_SYS_SELECT_H
67 #include <sys/select.h>
74 #include "ctdl_module.h"
80 #ifdef DEBUG_MEMORY_LEAKS
88 struct igheap *igheap = NULL;
92 int verbosity = DEFAULT_VERBOSITY; /* Logging level */
94 int syslog_facility = LOG_DAEMON;
95 int enable_syslog = 0;
99 * CtdlLogPrintf() ... Write logging information
101 void CtdlLogPrintf(enum LogLevel loglevel, const char *format, ...) {
103 va_start(arg_ptr, format);
104 vCtdlLogPrintf(loglevel, format, arg_ptr);
108 void vCtdlLogPrintf(enum LogLevel loglevel, const char *format, va_list arg_ptr)
112 vsyslog((syslog_facility | loglevel), format, arg_ptr);
115 /* stderr output code */
116 if (enable_syslog || running_as_daemon) return;
118 /* if we run in forground and syslog is disabled, log to terminal */
119 if (loglevel <= verbosity) {
124 CitContext *CCC = CC;
126 gettimeofday(&tv, NULL);
127 /* Promote to time_t; types differ on some OSes (like darwin) */
128 unixtime = tv.tv_sec;
129 localtime_r(&unixtime, &tim);
136 char buf[SIZ], buf2[SIZ];
138 if ((CCC != NULL) && (CCC->cs_pid != 0)) {
140 "%04d/%02d/%02d %2d:%02d:%02d.%06ld xx [%3d] ",
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,
147 "%04d/%02d/%02d %2d:%02d:%02d.%06ld xx ",
148 tim.tm_year + 1900, tim.tm_mon + 1,
149 tim.tm_mday, tim.tm_hour, tim.tm_min,
150 tim.tm_sec, (long)tv.tv_usec);
152 vsnprintf(buf2, SIZ, format, arg_ptr);
154 fprintf(stderr, "%s%s", buf, buf2);
158 "%04d/%02d/%02d %2d:%02d:%02d.%06ld ",
159 tim.tm_year + 1900, tim.tm_mon + 1,
160 tim.tm_mday, tim.tm_hour, tim.tm_min,
161 tim.tm_sec, (long)tv.tv_usec);
164 if (CCC->cs_pid != 0)
165 StrBufAppendPrintf(lBuf,
168 else if (CCC->user.usernum != 0)
169 StrBufAppendPrintf(lBuf,
173 StrBufVAppendPrintf(lBuf, format, arg_ptr);
174 fwrite(ChrPtr(lBuf), 1, StrLength(lBuf), stderr);
177 if (CCC == NULL) FreeStrBuf(&lBuf);
185 * Signal handler to shut down the server.
188 volatile int exit_signal = 0;
189 volatile int shutdown_and_halt = 0;
190 volatile int restart_server = 0;
191 volatile int running_as_daemon = 0;
193 static RETSIGTYPE signal_cleanup(int signum) {
199 CtdlLogPrintf(CTDL_DEBUG, "Caught signal %d; shutting down.\n", signum);
200 exit_signal = signum;
204 static RETSIGTYPE signal_exit(int signum) {
211 * Some initialization stuff...
213 void init_sysdep(void) {
216 /* Avoid vulnerabilities related to FD_SETSIZE if we can. */
220 getrlimit(RLIMIT_NOFILE, &rl);
221 rl.rlim_cur = FD_SETSIZE;
222 rl.rlim_max = FD_SETSIZE;
223 setrlimit(RLIMIT_NOFILE, &rl);
227 /* If we've got OpenSSL, we're going to use it. */
233 * Set up a place to put thread-specific data.
234 * We only need a single pointer per thread - it points to the
235 * CitContext structure (in the ContextList linked list) of the
236 * session to which the calling thread is currently bound.
238 if (citthread_key_create(&MyConKey, NULL) != 0) {
239 CtdlLogPrintf(CTDL_CRIT, "Can't create TSD key: %s\n",
244 * The action for unexpected signals and exceptions should be to
245 * call signal_cleanup() to gracefully shut down the server.
248 sigaddset(&set, SIGINT); // intr = shutdown
249 // sigaddset(&set, SIGQUIT); // quit = force quit
250 sigaddset(&set, SIGHUP);
251 sigaddset(&set, SIGTERM);
252 // sigaddset(&set, SIGSEGV); // we want core dumps
253 // sigaddset(&set, SIGILL); // we want core dumps
254 // sigaddset(&set, SIGBUS);
255 sigprocmask(SIG_UNBLOCK, &set, NULL);
257 signal(SIGINT, signal_cleanup); // intr = shutdown
258 // signal(SIGQUIT, signal_cleanup); // quit = force quit
259 signal(SIGHUP, signal_cleanup);
260 signal(SIGTERM, signal_cleanup);
261 signal(SIGUSR2, signal_exit);
262 // signal(SIGSEGV, signal_cleanup); // we want coredumps
263 // signal(SIGILL, signal_cleanup); // we want core dumps
264 // signal(SIGBUS, signal_cleanup);
267 * Do not shut down the server on broken pipe signals, otherwise the
268 * whole Citadel service would come down whenever a single client
271 signal(SIGPIPE, SIG_IGN);
278 * This is a generic function to set up a master socket for listening on
279 * a TCP port. The server shuts down if the bind fails.
282 int ig_tcp_server(char *ip_addr, int port_number, int queue_len, char **errormessage)
284 struct sockaddr_in sin;
286 int actual_queue_len;
288 actual_queue_len = queue_len;
289 if (actual_queue_len < 5) actual_queue_len = 5;
291 memset(&sin, 0, sizeof(sin));
292 sin.sin_family = AF_INET;
293 sin.sin_port = htons((u_short)port_number);
294 if (ip_addr == NULL) {
295 sin.sin_addr.s_addr = INADDR_ANY;
298 sin.sin_addr.s_addr = inet_addr(ip_addr);
301 if (sin.sin_addr.s_addr == !INADDR_ANY) {
302 sin.sin_addr.s_addr = INADDR_ANY;
305 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
308 *errormessage = (char*) malloc(SIZ + 1);
309 snprintf(*errormessage, SIZ,
310 "citserver: Can't create a socket: %s",
312 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
317 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
319 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
320 *errormessage = (char*) malloc(SIZ + 1);
321 snprintf(*errormessage, SIZ,
322 "citserver: Can't bind: %s",
324 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
329 /* set to nonblock - we need this for some obscure situations */
330 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
331 *errormessage = (char*) malloc(SIZ + 1);
332 snprintf(*errormessage, SIZ,
333 "citserver: Can't set socket to non-blocking: %s",
335 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
340 if (listen(s, actual_queue_len) < 0) {
341 *errormessage = (char*) malloc(SIZ + 1);
342 snprintf(*errormessage, SIZ,
343 "citserver: Can't listen: %s",
345 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
356 * Create a Unix domain socket and listen on it
358 int ig_uds_server(char *sockpath, int queue_len, char **errormessage)
360 struct sockaddr_un addr;
363 int actual_queue_len;
364 #ifdef HAVE_STRUCT_UCRED
368 actual_queue_len = queue_len;
369 if (actual_queue_len < 5) actual_queue_len = 5;
371 i = unlink(sockpath);
372 if ((i != 0) && (errno != ENOENT)) {
373 *errormessage = (char*) malloc(SIZ + 1);
374 snprintf(*errormessage, SIZ, "citserver: can't unlink %s: %s",
375 sockpath, strerror(errno));
376 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
380 memset(&addr, 0, sizeof(addr));
381 addr.sun_family = AF_UNIX;
382 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
384 s = socket(AF_UNIX, SOCK_STREAM, 0);
386 *errormessage = (char*) malloc(SIZ + 1);
387 snprintf(*errormessage, SIZ,
388 "citserver: Can't create a socket: %s",
390 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
394 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
395 *errormessage = (char*) malloc(SIZ + 1);
396 snprintf(*errormessage, SIZ,
397 "citserver: Can't bind: %s",
399 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
403 /* set to nonblock - we need this for some obscure situations */
404 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
405 *errormessage = (char*) malloc(SIZ + 1);
406 snprintf(*errormessage, SIZ,
407 "citserver: Can't set socket to non-blocking: %s",
409 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
414 if (listen(s, actual_queue_len) < 0) {
415 *errormessage = (char*) malloc(SIZ + 1);
416 snprintf(*errormessage, SIZ,
417 "citserver: Can't listen: %s",
419 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
423 #ifdef HAVE_STRUCT_UCRED
424 setsockopt(s, SOL_SOCKET, SO_PASSCRED, &passcred, sizeof(passcred));
427 chmod(sockpath, S_ISGID|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
434 * The following functions implement output buffering on operating systems which
435 * support it (such as Linux and various BSD flavors).
439 # define HAVE_TCP_BUFFERING
442 # define HAVE_TCP_BUFFERING
443 # define TCP_CORK TCP_NOPUSH
445 #endif /* TCP_CORK */
446 #endif /* HAVE_DARWIN */
448 static unsigned on = 1, off = 0;
450 void buffer_output(void) {
451 #ifdef HAVE_TCP_BUFFERING
453 if (!CC->redirect_ssl)
455 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
459 void unbuffer_output(void) {
460 #ifdef HAVE_TCP_BUFFERING
462 if (!CC->redirect_ssl)
464 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
468 void flush_output(void) {
469 #ifdef HAVE_TCP_BUFFERING
470 struct CitContext *CCC = CC;
471 setsockopt(CCC->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
472 setsockopt(CCC->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
477 static void flush_client_inbuf(void)
481 FlushStrBuf(CCC->ReadBuf);
488 * client_write() ... Send binary data to the client.
490 int client_write(const char *buf, int nbytes)
492 int bytes_written = 0;
494 #ifndef HAVE_TCP_BUFFERING
495 int old_buffer_len = 0;
501 if (nbytes < 1) return(0);
503 // flush_client_inbuf();
505 if (Ctx->redirect_buffer != NULL) {
506 StrBufAppendBufPlain(Ctx->redirect_buffer,
512 if (Ctx->redirect_ssl) {
513 client_write_ssl(buf, nbytes);
517 if (Ctx->client_socket == -1) return -1;
519 fdflags = fcntl(Ctx->client_socket, F_GETFL);
521 while ((bytes_written < nbytes) && (Ctx->client_socket != -1)){
522 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
524 FD_SET(Ctx->client_socket, &wset);
525 if (select(1, NULL, &wset, NULL, NULL) == -1) {
528 CtdlLogPrintf(CTDL_DEBUG, "client_write(%d bytes) select() interrupted.\n", nbytes-bytes_written);
529 if (CtdlThreadCheckStop()) {
533 /* can't trust fd's and stuff so we need to re-create them */
537 CtdlLogPrintf(CTDL_ERR,
538 "client_write(%d bytes) select failed: %s (%d)\n",
539 nbytes - bytes_written,
540 strerror(errno), errno);
548 retval = write(Ctx->client_socket, &buf[bytes_written],
549 nbytes - bytes_written);
551 CtdlLogPrintf(CTDL_ERR,
552 "client_write(%d bytes) failed: %s (%d)\n",
553 nbytes - bytes_written,
554 strerror(errno), errno);
556 // CtdlLogPrintf(CTDL_DEBUG, "Tried to send: %s", &buf[bytes_written]);
560 bytes_written = bytes_written + retval;
565 void cputbuf(const StrBuf *Buf) {
566 client_write(ChrPtr(Buf), StrLength(Buf));
571 * cprintf() Send formatted printable data to the client.
572 * Implemented in terms of client_write() so it's technically not sysdep...
574 void cprintf(const char *format, ...) {
578 va_start(arg_ptr, format);
579 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
580 buf[sizeof buf - 2] = '\n';
581 client_write(buf, strlen(buf));
587 * Read data from the client socket.
589 * sock socket fd to read from
590 * buf buffer to read into
591 * bytes number of bytes to read
592 * timeout Number of seconds to wait before timing out
594 * Possible return values:
595 * 1 Requested number of bytes has been read.
596 * 0 Request timed out.
597 * -1 Connection is broken, or other error.
599 int client_read_blob(StrBuf *Target, int bytes, int timeout)
606 if (CCC->redirect_ssl) {
607 retval = client_read_sslblob(Target, bytes, timeout);
609 CtdlLogPrintf(CTDL_CRIT,
617 retval = StrBufReadBLOBBuffered(Target,
626 CtdlLogPrintf(CTDL_CRIT,
639 snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
641 fd = fopen(fn, "a+");
642 fprintf(fd, "Read: BufSize: %d BufContent: [",
644 rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
658 * to make client_read_random_blob() more efficient, increase buffer size.
659 * just use in greeting function, else your buffer may be flushed
661 void client_set_inbound_buf(long N)
663 FlushStrBuf(CC->ReadBuf);
664 ReAdjustEmptyBuf(CC->ReadBuf, N * SIZ, N * SIZ);
667 int client_read_random_blob(StrBuf *Target, int timeout)
672 rc = client_read_blob(Target, 1, timeout);
678 len = StrLength(CCC->ReadBuf);
679 pch = ChrPtr(CCC->ReadBuf);
683 if (CCC->Pos != NULL) {
684 len -= CCC->Pos - pch;
687 StrBufAppendBufPlain(Target, pch, len, 0);
688 FlushStrBuf(CCC->ReadBuf);
696 snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
698 fd = fopen(fn, "a+");
699 fprintf(fd, "Read: BufSize: %d BufContent: [",
701 rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
709 return StrLength(Target);
717 int client_read_to(char *buf, int bytes, int timeout)
722 rc = client_read_blob(CCC->MigrateBuf, bytes, timeout);
731 ChrPtr(CCC->MigrateBuf),
732 StrLength(CCC->MigrateBuf) + 1);
733 FlushStrBuf(CCC->MigrateBuf);
739 int HaveMoreLinesWaiting(CitContext *CCC)
741 if ((CCC->kill_me == 1) || (
742 (CCC->Pos == NULL) &&
743 (StrLength(CCC->ReadBuf) == 0) &&
744 (CCC->client_socket != -1)) )
752 * Read data from the client socket with default timeout.
753 * (This is implemented in terms of client_read_to() and could be
754 * justifiably moved out of sysdep.c)
756 INLINE int client_read(char *buf, int bytes)
758 return(client_read_to(buf, bytes, config.c_sleeping));
761 int CtdlClientGetLine(StrBuf *Target)
769 if (CCC->redirect_ssl) {
779 snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
781 fd = fopen(fn, "a+");
782 pch = ChrPtr(CCC->ReadBuf);
783 len = StrLength(CCC->ReadBuf);
784 if (CCC->Pos != NULL)
785 rlen = CC->Pos - pch;
789 /* fprintf(fd, "\n\n\nBufSize: %d BufPos: %d \nBufContent: [%s]\n\n_____________________\n",
792 fprintf(fd, "\n\n\nSSL1: BufSize: %d BufPos: %d \n_____________________\n",
795 rc = client_readline_sslbuffer(Target,
800 pch = ChrPtr(CCC->ReadBuf);
801 nlen = StrLength(CCC->ReadBuf);
802 if (CCC->Pos != NULL)
803 nrlen = CC->Pos - pch;
807 fprintf(fd, "\n\n\nBufSize: was: %d is: %d BufPos: was: %d is: %d \nBufContent: [%s]\n\n_____________________\n",
808 len, nlen, rlen, nrlen, pch);
810 fprintf(fd, "\n\n\nSSL2: BufSize: was: %d is: %d BufPos: was: %d is: %d \n",
811 len, nlen, rlen, nrlen);
813 fprintf(fd, "SSL3: Read: BufSize: %d BufContent: [%s]\n\n*************\n",
814 StrLength(Target), ChrPtr(Target));
818 CtdlLogPrintf(CTDL_CRIT,
830 int len, rlen, nlen, nrlen;
833 snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
835 fd = fopen(fn, "a+");
836 pch = ChrPtr(CCC->ReadBuf);
837 len = StrLength(CCC->ReadBuf);
838 if (CCC->Pos != NULL)
839 rlen = CC->Pos - pch;
843 /* fprintf(fd, "\n\n\nBufSize: %d BufPos: %d \nBufContent: [%s]\n\n_____________________\n",
846 fprintf(fd, "\n\n\nBufSize: %d BufPos: %d \n_____________________\n",
849 rc = StrBufTCP_read_buffered_line_fast(Target,
858 pch = ChrPtr(CCC->ReadBuf);
859 nlen = StrLength(CCC->ReadBuf);
860 if (CCC->Pos != NULL)
861 nrlen = CC->Pos - pch;
865 fprintf(fd, "\n\n\nBufSize: was: %d is: %d BufPos: was: %d is: %d \nBufContent: [%s]\n\n_____________________\n",
866 len, nlen, rlen, nrlen, pch);
868 fprintf(fd, "\n\n\nBufSize: was: %d is: %d BufPos: was: %d is: %d \n",
869 len, nlen, rlen, nrlen);
871 fprintf(fd, "Read: BufSize: %d BufContent: [%s]\n\n*************\n",
872 StrLength(Target), ChrPtr(Target));
875 if ((rc < 0) && (Error != NULL))
876 CtdlLogPrintf(CTDL_CRIT,
887 * client_getln() ... Get a LF-terminated line of text from the client.
888 * (This is implemented in terms of client_read() and could be
889 * justifiably moved out of sysdep.c)
891 int client_getln(char *buf, int bufsize)
897 retval = CtdlClientGetLine(CCC->MigrateBuf);
902 i = StrLength(CCC->MigrateBuf);
903 pCh = ChrPtr(CCC->MigrateBuf);
904 /* Strip the trailing LF, and the trailing CR if present.
909 && ( (pCh[i - 1]==13)
910 || ( pCh[i - 1]==10)) ) {
916 FlushStrBuf(CCC->MigrateBuf);
918 safestrncpy(&buf[i], "000", bufsize - i);
925 * Cleanup any contexts that are left lying around
929 void close_masters (void)
931 struct ServiceFunctionHook *serviceptr;
934 * close all protocol master sockets
936 for (serviceptr = ServiceHookTable; serviceptr != NULL;
937 serviceptr = serviceptr->next ) {
939 if (serviceptr->tcp_port > 0)
941 CtdlLogPrintf(CTDL_INFO, "Closing listener on port %d\n",
942 serviceptr->tcp_port);
943 serviceptr->tcp_port = 0;
946 if (serviceptr->sockpath != NULL)
947 CtdlLogPrintf(CTDL_INFO, "Closing listener on '%s'\n",
948 serviceptr->sockpath);
950 close(serviceptr->msock);
951 /* If it's a Unix domain socket, remove the file. */
952 if (serviceptr->sockpath != NULL) {
953 unlink(serviceptr->sockpath);
954 serviceptr->sockpath = NULL;
961 * The system-dependent part of master_cleanup() - close the master socket.
963 void sysdep_master_cleanup(void) {
972 CtdlDestroyProtoHooks();
973 CtdlDestroyDeleteHooks();
974 CtdlDestroyXmsgHooks();
975 CtdlDestroyNetprocHooks();
976 CtdlDestroyUserHooks();
977 CtdlDestroyMessageHook();
978 CtdlDestroyCleanupHooks();
979 CtdlDestroyFixedOutputHooks();
980 CtdlDestroySessionHooks();
981 CtdlDestroyServiceHook();
982 CtdlDestroyRoomHooks();
983 #ifdef HAVE_BACKTRACE
991 void graceful_shutdown(int signum) {
992 kill(current_child, signum);
993 unlink(file_pid_file);
998 int nFireUpsNonRestart = 0;
1002 * Start running as a daemon.
1004 void start_daemon(int unused) {
1012 /* Close stdin/stdout/stderr and replace them with /dev/null.
1013 * We don't just call close() because we don't want these fd's
1014 * to be reused for other files.
1016 if (chdir(ctdl_run_dir) != 0)
1017 CtdlLogPrintf(CTDL_EMERG,
1018 "unable to change into directory [%s]: %s",
1019 ctdl_run_dir, strerror(errno));
1026 signal(SIGHUP, SIG_IGN);
1027 signal(SIGINT, SIG_IGN);
1028 signal(SIGQUIT, SIG_IGN);
1032 if ((freopen("/dev/null", "r", stdin) != stdin) ||
1033 (freopen("/dev/null", "w", stdout) != stdout) ||
1034 (freopen("/dev/null", "w", stderr) != stderr))
1035 CtdlLogPrintf(CTDL_EMERG,
1036 "unable to reopen stdin/out/err %s",
1041 current_child = fork();
1043 signal(SIGTERM, graceful_shutdown);
1045 if (current_child < 0) {
1050 else if (current_child == 0) {
1051 return; /* continue starting citadel. */
1055 fp = fopen(file_pid_file, "w");
1057 fprintf(fp, ""F_PID_T"\n", getpid());
1060 waitpid(current_child, &status, 0);
1063 nFireUpsNonRestart = nFireUps;
1065 /* Exit code 0 means the watcher should exit */
1066 if (WIFEXITED(status) && (WEXITSTATUS(status) == CTDLEXIT_SHUTDOWN)) {
1070 /* Exit code 101-109 means the watcher should exit */
1071 else if (WIFEXITED(status) && (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109)) {
1075 /* Any other exit code, or no exit code, means we should restart. */
1079 ForkedPid = current_child;
1082 } while (do_restart);
1084 unlink(file_pid_file);
1085 exit(WEXITSTATUS(status));
1090 void checkcrash(void)
1092 if (nFireUpsNonRestart != nFireUps)
1096 CrashMail = NewStrBuf();
1097 CtdlLogPrintf(CTDL_ALERT, "Posting crash message\n");
1098 StrBufPrintf(CrashMail,
1100 " The Citadel server process (citserver) terminated unexpectedly."
1102 " This could be the result of a bug in the server program, or some external "
1104 " You can obtain more information about this by enabling core dumps.\n \n"
1105 " For more information, please see:\n \n"
1106 " http://citadel.org/doku.php/faq:mastering_your_os:gdb#how.do.i.make.my.system.produce.core-files"
1109 " If you have already done this, the core dump is likely to be found at %score.%d\n"
1111 ctdl_run_dir, ForkedPid);
1112 CtdlAideMessage(ChrPtr(CrashMail), "Citadel server process terminated unexpectedly");
1113 FreeStrBuf(&CrashMail);
1119 * Generic routine to convert a login name to a full name (gecos)
1120 * Returns nonzero if a conversion took place
1122 int convert_login(char NameToConvert[]) {
1126 pw = getpwnam(NameToConvert);
1131 strcpy(NameToConvert, pw->pw_gecos);
1132 for (a=0; a<strlen(NameToConvert); ++a) {
1133 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
1142 * This loop just keeps going and going and going...
1146 * This current implimentation of worker_thread creates a bottle neck in several situations
1147 * The first thing to remember is that a single thread can handle more than one connection at a time.
1148 * More threads mean less memory for the system to run in.
1149 * So for efficiency we want every thread to be doing something useful or waiting in the main loop for
1150 * something to happen anywhere.
1151 * This current implimentation requires worker threads to wait in other locations, after it has
1152 * been committed to a single connection which is very wasteful.
1153 * As an extreme case consider this:
1154 * A slow client connects and this slow client sends only one character each second.
1155 * With this current implimentation a single worker thread is dispatched to handle that connection
1156 * until such times as the client timeout expires, an error occurs on the socket or the client
1157 * completes its transmission.
1158 * THIS IS VERY BAD since that thread could have handled a read from many more clients in each one
1159 * second interval between chars.
1161 * It is my intention to re-write this code and the associated client_getln, client_read functions
1162 * to allow any thread to read data on behalf of any connection (context).
1163 * To do this I intend to have this main loop read chars into a buffer stored in the context.
1164 * Once the correct criteria for a full buffer is met then we will dispatch a thread to
1166 * This worker thread loop also needs to be able to handle binary data.
1169 void *worker_thread(void *arg) {
1172 CitContext *bind_me = NULL;
1176 int force_purge = 0;
1179 while (!CtdlThreadCheckStop()) {
1181 /* make doubly sure we're not holding any stale db handles
1182 * which might cause a deadlock.
1184 cdb_check_handles();
1185 do_select: force_purge = 0;
1186 bind_me = NULL; /* Which session shall we handle? */
1188 /* Initialize the fdset. */
1192 begin_critical_section(S_SESSION_TABLE);
1193 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1195 client_socket = ptr->client_socket;
1196 /* Dont select on dead sessions only truly idle ones */
1197 if ((ptr->state == CON_IDLE) &&
1198 (CC->kill_me == 0) &&
1199 (client_socket != -1))
1201 FD_SET(client_socket, &readfds);
1202 if (client_socket > highest)
1203 highest = client_socket;
1205 if ((bind_me == NULL) && (ptr->state == CON_READY)) {
1207 ptr->state = CON_EXECUTING;
1210 if ((bind_me == NULL) && (ptr->state == CON_GREETING)) {
1212 ptr->state = CON_STARTING;
1216 end_critical_section(S_SESSION_TABLE);
1222 /* If we got this far, it means that there are no sessions
1223 * which a previous thread marked for attention, so we go
1224 * ahead and get ready to select().
1227 if (!CtdlThreadCheckStop()) {
1228 tv.tv_sec = 1; /* wake up every second if no input */
1230 retval = CtdlThreadSelect(highest + 1, &readfds, NULL, NULL, &tv);
1235 /* Now figure out who made this select() unblock.
1236 * First, check for an error or exit condition.
1239 if (errno == EBADF) {
1240 CtdlLogPrintf(CTDL_NOTICE, "select() failed: (%s)\n",
1244 if (errno != EINTR) {
1245 CtdlLogPrintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
1246 CtdlThreadStopAll();
1249 CtdlLogPrintf(CTDL_DEBUG, "Interrupted CtdlThreadSelect.\n");
1250 if (CtdlThreadCheckStop()) return(NULL);
1254 else if(retval == 0) {
1255 if (CtdlThreadCheckStop()) return(NULL);
1258 /* It must be a client socket. Find a context that has data
1259 * waiting on its socket *and* is in the CON_IDLE state. Any
1260 * active sockets other than our chosen one are marked as
1261 * CON_READY so the next thread that comes around can just bind
1262 * to one without having to select() again.
1264 begin_critical_section(S_SESSION_TABLE);
1265 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1266 int checkfd = ptr->client_socket;
1267 if ((checkfd != -1) && (ptr->state == CON_IDLE) ){
1268 if (FD_ISSET(checkfd, &readfds)) {
1269 ptr->input_waiting = 1;
1271 bind_me = ptr; /* I choose you! */
1272 bind_me->state = CON_EXECUTING;
1275 ptr->state = CON_READY;
1277 } else if ((ptr->is_async) && (ptr->async_waiting) && (ptr->h_async_function)) {
1279 bind_me = ptr; /* I choose you! */
1280 bind_me->state = CON_EXECUTING;
1283 ptr->state = CON_READY;
1288 end_critical_section(S_SESSION_TABLE);
1291 /* We're bound to a session */
1292 if (bind_me != NULL) {
1293 become_session(bind_me);
1295 if (bind_me->state == CON_STARTING) {
1296 bind_me->state = CON_EXECUTING;
1297 begin_session(bind_me);
1298 bind_me->h_greeting_function();
1300 /* If the client has sent a command, execute it. */
1301 if (CC->input_waiting) {
1302 CC->h_command_function();
1304 while (HaveMoreLinesWaiting(CC))
1305 CC->h_command_function();
1307 CC->input_waiting = 0;
1310 /* If there are asynchronous messages waiting and the
1311 * client supports it, do those now */
1312 if ((CC->is_async) && (CC->async_waiting)
1313 && (CC->h_async_function != NULL)) {
1314 CC->h_async_function();
1315 CC->async_waiting = 0;
1318 force_purge = CC->kill_me;
1319 become_session(NULL);
1320 bind_me->state = CON_IDLE;
1323 dead_session_purge(force_purge);
1326 /* If control reaches this point, the server is shutting down */
1334 * A function to handle selecting on master sockets.
1335 * In other words it handles new connections.
1338 void *select_on_master (void *arg)
1340 struct ServiceFunctionHook *serviceptr;
1344 int ssock; /* Descriptor for client socket */
1345 CitContext *con= NULL; /* Temporary context pointer */
1350 while (!CtdlThreadCheckStop()) {
1351 /* Initialize the fdset. */
1352 FD_ZERO(&master_fds);
1355 /* First, add the various master sockets to the fdset. */
1356 for (serviceptr = ServiceHookTable; serviceptr != NULL;
1357 serviceptr = serviceptr->next ) {
1358 m = serviceptr->msock;
1359 FD_SET(m, &master_fds);
1365 if (!CtdlThreadCheckStop()) {
1366 tv.tv_sec = 60; /* wake up every second if no input */
1368 retval = CtdlThreadSelect(highest + 1, &master_fds, NULL, NULL, &tv);
1373 /* Now figure out who made this select() unblock.
1374 * First, check for an error or exit condition.
1377 if (errno == EBADF) {
1378 CtdlLogPrintf(CTDL_NOTICE, "select() failed: (%s)\n",
1382 if (errno != EINTR) {
1383 CtdlLogPrintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
1384 CtdlThreadStopAll();
1386 CtdlLogPrintf(CTDL_DEBUG, "Interrupted CtdlThreadSelect.\n");
1387 if (CtdlThreadCheckStop()) return(NULL);
1391 else if(retval == 0) {
1392 if (CtdlThreadCheckStop()) return(NULL);
1395 /* Next, check to see if it's a new client connecting
1396 * on a master socket.
1398 else for (serviceptr = ServiceHookTable; serviceptr != NULL;
1399 serviceptr = serviceptr->next ) {
1401 if (FD_ISSET(serviceptr->msock, &master_fds)) {
1402 ssock = accept(serviceptr->msock, NULL, 0);
1404 CtdlLogPrintf(CTDL_DEBUG,
1405 "New client socket %d\n",
1408 /* The master socket is non-blocking but the client
1409 * sockets need to be blocking, otherwise certain
1410 * operations barf on FreeBSD. Not a fatal error.
1412 if (fcntl(ssock, F_SETFL, 0) < 0) {
1413 CtdlLogPrintf(CTDL_EMERG,
1414 "citserver: Can't set socket to blocking: %s\n",
1418 /* New context will be created already
1419 * set up in the CON_EXECUTING state.
1421 con = CreateNewContext();
1423 /* Assign our new socket number to it. */
1424 con->client_socket = ssock;
1425 con->h_command_function =
1426 serviceptr->h_command_function;
1427 con->h_async_function =
1428 serviceptr->h_async_function;
1429 con->h_greeting_function = serviceptr->h_greeting_function;
1431 serviceptr->ServiceName;
1433 /* Determine whether it's a local socket */
1434 if (serviceptr->sockpath != NULL)
1435 con->is_local_socket = 1;
1437 /* Set the SO_REUSEADDR socket option */
1439 setsockopt(ssock, SOL_SOCKET,
1443 con->state = CON_GREETING;
1459 * Translate text facility name to syslog.h defined value.
1461 int SyslogFacility(char *name)
1470 { LOG_KERN, "kern" },
1471 { LOG_USER, "user" },
1472 { LOG_MAIL, "mail" },
1473 { LOG_DAEMON, "daemon" },
1474 { LOG_AUTH, "auth" },
1475 { LOG_SYSLOG, "syslog" },
1477 { LOG_NEWS, "news" },
1478 { LOG_UUCP, "uucp" },
1479 { LOG_LOCAL0, "local0" },
1480 { LOG_LOCAL1, "local1" },
1481 { LOG_LOCAL2, "local2" },
1482 { LOG_LOCAL3, "local3" },
1483 { LOG_LOCAL4, "local4" },
1484 { LOG_LOCAL5, "local5" },
1485 { LOG_LOCAL6, "local6" },
1486 { LOG_LOCAL7, "local7" },
1489 for(i = 0; facTbl[i].name != NULL; i++) {
1490 if(!strcasecmp(name, facTbl[i].name))
1491 return facTbl[i].facility;
1498 /********** MEM CHEQQER ***********/
1500 #ifdef DEBUG_MEMORY_LEAKS
1507 void *tracked_malloc(size_t size, char *file, int line) {
1508 struct igheap *thisheap;
1511 block = malloc(size);
1512 if (block == NULL) return(block);
1514 thisheap = malloc(sizeof(struct igheap));
1515 if (thisheap == NULL) {
1520 thisheap->block = block;
1521 strcpy(thisheap->file, file);
1522 thisheap->line = line;
1524 begin_critical_section(S_DEBUGMEMLEAKS);
1525 thisheap->next = igheap;
1527 end_critical_section(S_DEBUGMEMLEAKS);
1533 void *tracked_realloc(void *ptr, size_t size, char *file, int line) {
1534 struct igheap *thisheap;
1537 block = realloc(ptr, size);
1538 if (block == NULL) return(block);
1540 thisheap = malloc(sizeof(struct igheap));
1541 if (thisheap == NULL) {
1546 thisheap->block = block;
1547 strcpy(thisheap->file, file);
1548 thisheap->line = line;
1550 begin_critical_section(S_DEBUGMEMLEAKS);
1551 thisheap->next = igheap;
1553 end_critical_section(S_DEBUGMEMLEAKS);
1560 void tracked_free(void *ptr) {
1561 struct igheap *thisheap;
1562 struct igheap *trash;
1566 if (igheap == NULL) return;
1567 begin_critical_section(S_DEBUGMEMLEAKS);
1568 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1569 if (thisheap->next != NULL) {
1570 if (thisheap->next->block == ptr) {
1571 trash = thisheap->next;
1572 thisheap->next = thisheap->next->next;
1577 if (igheap->block == ptr) {
1579 igheap = igheap->next;
1582 end_critical_section(S_DEBUGMEMLEAKS);
1585 char *tracked_strdup(const char *s, char *file, int line) {
1588 if (s == NULL) return(NULL);
1589 ptr = tracked_malloc(strlen(s) + 1, file, line);
1590 if (ptr == NULL) return(NULL);
1591 strncpy(ptr, s, strlen(s));
1595 void dump_heap(void) {
1596 struct igheap *thisheap;
1598 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1599 CtdlLogPrintf(CTDL_CRIT, "UNFREED: %30s : %d\n",
1600 thisheap->file, thisheap->line);
1604 #endif /* DEBUG_MEMORY_LEAKS */