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;
96 int print_to_logfile = 1;
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 || !print_to_logfile) return;
118 /* if we run in forground and syslog is disabled, log to terminal */
119 if (loglevel <= verbosity) {
123 CitContext *CCC = MyContext();
124 ThreadTSD *cTSD = CTP;
125 CtdlThreadNode *node = NULL;
132 if ((node != NULL) && (node->reltid != 0)) {
133 lwpid = node->reltid;
136 gettimeofday(&tv, NULL);
138 /* Promote to time_t; types differ on some OSes (like darwin) */
139 unixtime = tv.tv_sec;
140 localtime_r(&unixtime, &tim);
143 "%04d/%02d/%02d %2d:%02d:%02d.%06ld ",
144 tim.tm_year + 1900, tim.tm_mon + 1,
145 tim.tm_mday, tim.tm_hour, tim.tm_min,
146 tim.tm_sec, (long)tv.tv_usec
150 fprintf(stderr, "[LWP:%ld] ", lwpid);
154 if (CCC->cs_pid != 0) {
155 fprintf(stderr, "[%3d] ", CCC->cs_pid);
157 else if (CCC->user.usernum != 0) {
158 fprintf(stderr, "[:%ld] ", CCC->user.usernum);
162 vfprintf(stderr, format, arg_ptr);
170 * Signal handler to shut down the server.
173 volatile int exit_signal = 0;
174 volatile int shutdown_and_halt = 0;
175 volatile int restart_server = 0;
176 volatile int running_as_daemon = 0;
178 static RETSIGTYPE signal_cleanup(int signum) {
184 CtdlLogPrintf(CTDL_DEBUG, "Caught signal %d; shutting down.\n", signum);
185 exit_signal = signum;
189 static RETSIGTYPE signal_exit(int signum) {
196 * Some initialization stuff...
198 void init_sysdep(void) {
201 /* Avoid vulnerabilities related to FD_SETSIZE if we can. */
205 getrlimit(RLIMIT_NOFILE, &rl);
206 rl.rlim_cur = FD_SETSIZE;
207 rl.rlim_max = FD_SETSIZE;
208 setrlimit(RLIMIT_NOFILE, &rl);
212 /* If we've got OpenSSL, we're going to use it. */
218 * Set up a place to put thread-specific data.
219 * We only need a single pointer per thread - it points to the
220 * CitContext structure (in the ContextList linked list) of the
221 * session to which the calling thread is currently bound.
223 if (citthread_key_create(&MyConKey, NULL) != 0) {
224 CtdlLogPrintf(CTDL_CRIT, "Can't create TSD key: %s\n",
229 * The action for unexpected signals and exceptions should be to
230 * call signal_cleanup() to gracefully shut down the server.
233 sigaddset(&set, SIGINT); // intr = shutdown
234 // sigaddset(&set, SIGQUIT); // quit = force quit
235 sigaddset(&set, SIGHUP);
236 sigaddset(&set, SIGTERM);
237 // sigaddset(&set, SIGSEGV); // we want core dumps
238 // sigaddset(&set, SIGILL); // we want core dumps
239 // sigaddset(&set, SIGBUS);
240 sigprocmask(SIG_UNBLOCK, &set, NULL);
242 signal(SIGINT, signal_cleanup); // intr = shutdown
243 // signal(SIGQUIT, signal_cleanup); // quit = force quit
244 signal(SIGHUP, signal_cleanup);
245 signal(SIGTERM, signal_cleanup);
246 signal(SIGUSR2, signal_exit);
247 // signal(SIGSEGV, signal_cleanup); // we want coredumps
248 // signal(SIGILL, signal_cleanup); // we want core dumps
249 // signal(SIGBUS, signal_cleanup);
252 * Do not shut down the server on broken pipe signals, otherwise the
253 * whole Citadel service would come down whenever a single client
256 signal(SIGPIPE, SIG_IGN);
263 * This is a generic function to set up a master socket for listening on
264 * a TCP port. The server shuts down if the bind fails.
267 int ig_tcp_server(char *ip_addr, int port_number, int queue_len, char **errormessage)
269 struct sockaddr_in sin;
271 int actual_queue_len;
273 actual_queue_len = queue_len;
274 if (actual_queue_len < 5) actual_queue_len = 5;
276 memset(&sin, 0, sizeof(sin));
277 sin.sin_family = AF_INET;
278 sin.sin_port = htons((u_short)port_number);
279 if (ip_addr == NULL) {
280 sin.sin_addr.s_addr = INADDR_ANY;
283 sin.sin_addr.s_addr = inet_addr(ip_addr);
286 if (sin.sin_addr.s_addr == !INADDR_ANY) {
287 sin.sin_addr.s_addr = INADDR_ANY;
290 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
293 *errormessage = (char*) malloc(SIZ + 1);
294 snprintf(*errormessage, SIZ,
295 "citserver: Can't create a socket: %s",
297 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
302 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
304 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
305 *errormessage = (char*) malloc(SIZ + 1);
306 snprintf(*errormessage, SIZ,
307 "citserver: Can't bind: %s",
309 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
314 /* set to nonblock - we need this for some obscure situations */
315 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
316 *errormessage = (char*) malloc(SIZ + 1);
317 snprintf(*errormessage, SIZ,
318 "citserver: Can't set socket to non-blocking: %s",
320 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
325 if (listen(s, actual_queue_len) < 0) {
326 *errormessage = (char*) malloc(SIZ + 1);
327 snprintf(*errormessage, SIZ,
328 "citserver: Can't listen: %s",
330 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
341 * Create a Unix domain socket and listen on it
343 int ig_uds_server(char *sockpath, int queue_len, char **errormessage)
345 struct sockaddr_un addr;
348 int actual_queue_len;
349 #ifdef HAVE_STRUCT_UCRED
353 actual_queue_len = queue_len;
354 if (actual_queue_len < 5) actual_queue_len = 5;
356 i = unlink(sockpath);
357 if ((i != 0) && (errno != ENOENT)) {
358 *errormessage = (char*) malloc(SIZ + 1);
359 snprintf(*errormessage, SIZ, "citserver: can't unlink %s: %s",
360 sockpath, strerror(errno));
361 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
365 memset(&addr, 0, sizeof(addr));
366 addr.sun_family = AF_UNIX;
367 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
369 s = socket(AF_UNIX, SOCK_STREAM, 0);
371 *errormessage = (char*) malloc(SIZ + 1);
372 snprintf(*errormessage, SIZ,
373 "citserver: Can't create a socket: %s",
375 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
379 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
380 *errormessage = (char*) malloc(SIZ + 1);
381 snprintf(*errormessage, SIZ,
382 "citserver: Can't bind: %s",
384 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
388 /* set to nonblock - we need this for some obscure situations */
389 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
390 *errormessage = (char*) malloc(SIZ + 1);
391 snprintf(*errormessage, SIZ,
392 "citserver: Can't set socket to non-blocking: %s",
394 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
399 if (listen(s, actual_queue_len) < 0) {
400 *errormessage = (char*) malloc(SIZ + 1);
401 snprintf(*errormessage, SIZ,
402 "citserver: Can't listen: %s",
404 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
408 #ifdef HAVE_STRUCT_UCRED
409 setsockopt(s, SOL_SOCKET, SO_PASSCRED, &passcred, sizeof(passcred));
412 chmod(sockpath, S_ISGID|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
419 * The following functions implement output buffering on operating systems which
420 * support it (such as Linux and various BSD flavors).
424 # define HAVE_TCP_BUFFERING
427 # define HAVE_TCP_BUFFERING
428 # define TCP_CORK TCP_NOPUSH
430 #endif /* TCP_CORK */
431 #endif /* HAVE_DARWIN */
433 static unsigned on = 1, off = 0;
435 void buffer_output(void) {
436 #ifdef HAVE_TCP_BUFFERING
438 if (!CC->redirect_ssl)
440 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
444 void unbuffer_output(void) {
445 #ifdef HAVE_TCP_BUFFERING
447 if (!CC->redirect_ssl)
449 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
453 void flush_output(void) {
454 #ifdef HAVE_TCP_BUFFERING
455 struct CitContext *CCC = CC;
456 setsockopt(CCC->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
457 setsockopt(CCC->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
462 static void flush_client_inbuf(void)
466 FlushStrBuf(CCC->ReadBuf);
473 * client_write() ... Send binary data to the client.
475 int client_write(const char *buf, int nbytes)
477 int bytes_written = 0;
479 #ifndef HAVE_TCP_BUFFERING
480 int old_buffer_len = 0;
486 if (nbytes < 1) return(0);
488 // flush_client_inbuf();
490 if (Ctx->redirect_buffer != NULL) {
491 StrBufAppendBufPlain(Ctx->redirect_buffer,
497 if (Ctx->redirect_ssl) {
498 client_write_ssl(buf, nbytes);
502 if (Ctx->client_socket == -1) return -1;
504 fdflags = fcntl(Ctx->client_socket, F_GETFL);
506 while ((bytes_written < nbytes) && (Ctx->client_socket != -1)){
507 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
509 FD_SET(Ctx->client_socket, &wset);
510 if (select(1, NULL, &wset, NULL, NULL) == -1) {
513 CtdlLogPrintf(CTDL_DEBUG, "client_write(%d bytes) select() interrupted.\n", nbytes-bytes_written);
514 if (CtdlThreadCheckStop()) {
518 /* can't trust fd's and stuff so we need to re-create them */
522 CtdlLogPrintf(CTDL_ERR,
523 "client_write(%d bytes) select failed: %s (%d)\n",
524 nbytes - bytes_written,
525 strerror(errno), errno);
533 retval = write(Ctx->client_socket, &buf[bytes_written],
534 nbytes - bytes_written);
536 CtdlLogPrintf(CTDL_ERR,
537 "client_write(%d bytes) failed: %s (%d)\n",
538 nbytes - bytes_written,
539 strerror(errno), errno);
541 // CtdlLogPrintf(CTDL_DEBUG, "Tried to send: %s", &buf[bytes_written]);
545 bytes_written = bytes_written + retval;
550 void cputbuf(const StrBuf *Buf) {
551 client_write(ChrPtr(Buf), StrLength(Buf));
556 * cprintf() Send formatted printable data to the client.
557 * Implemented in terms of client_write() so it's technically not sysdep...
559 void cprintf(const char *format, ...) {
563 va_start(arg_ptr, format);
564 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
565 buf[sizeof buf - 2] = '\n';
566 client_write(buf, strlen(buf));
572 * Read data from the client socket.
574 * sock socket fd to read from
575 * buf buffer to read into
576 * bytes number of bytes to read
577 * timeout Number of seconds to wait before timing out
579 * Possible return values:
580 * 1 Requested number of bytes has been read.
581 * 0 Request timed out.
582 * -1 Connection is broken, or other error.
584 int client_read_blob(StrBuf *Target, int bytes, int timeout)
591 if (CCC->redirect_ssl) {
592 retval = client_read_sslblob(Target, bytes, timeout);
594 CtdlLogPrintf(CTDL_CRIT,
602 retval = StrBufReadBLOBBuffered(Target,
611 CtdlLogPrintf(CTDL_CRIT,
624 snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
626 fd = fopen(fn, "a+");
627 fprintf(fd, "Read: BufSize: %d BufContent: [",
629 rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
643 * to make client_read_random_blob() more efficient, increase buffer size.
644 * just use in greeting function, else your buffer may be flushed
646 void client_set_inbound_buf(long N)
648 FlushStrBuf(CC->ReadBuf);
649 ReAdjustEmptyBuf(CC->ReadBuf, N * SIZ, N * SIZ);
652 int client_read_random_blob(StrBuf *Target, int timeout)
657 rc = client_read_blob(Target, 1, timeout);
663 len = StrLength(CCC->ReadBuf);
664 pch = ChrPtr(CCC->ReadBuf);
668 if (CCC->Pos != NULL) {
669 len -= CCC->Pos - pch;
672 StrBufAppendBufPlain(Target, pch, len, 0);
673 FlushStrBuf(CCC->ReadBuf);
681 snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
683 fd = fopen(fn, "a+");
684 fprintf(fd, "Read: BufSize: %d BufContent: [",
686 rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
694 return StrLength(Target);
702 int client_read_to(char *buf, int bytes, int timeout)
707 rc = client_read_blob(CCC->MigrateBuf, bytes, timeout);
716 ChrPtr(CCC->MigrateBuf),
717 StrLength(CCC->MigrateBuf) + 1);
718 FlushStrBuf(CCC->MigrateBuf);
724 int HaveMoreLinesWaiting(CitContext *CCC)
726 if ((CCC->kill_me == 1) || (
727 (CCC->Pos == NULL) &&
728 (StrLength(CCC->ReadBuf) == 0) &&
729 (CCC->client_socket != -1)) )
737 * Read data from the client socket with default timeout.
738 * (This is implemented in terms of client_read_to() and could be
739 * justifiably moved out of sysdep.c)
741 INLINE int client_read(char *buf, int bytes)
743 return(client_read_to(buf, bytes, config.c_sleeping));
746 int CtdlClientGetLine(StrBuf *Target)
754 if (CCC->redirect_ssl) {
764 snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
766 fd = fopen(fn, "a+");
767 pch = ChrPtr(CCC->ReadBuf);
768 len = StrLength(CCC->ReadBuf);
769 if (CCC->Pos != NULL)
770 rlen = CC->Pos - pch;
774 /* fprintf(fd, "\n\n\nBufSize: %d BufPos: %d \nBufContent: [%s]\n\n_____________________\n",
777 fprintf(fd, "\n\n\nSSL1: BufSize: %d BufPos: %d \n_____________________\n",
780 rc = client_readline_sslbuffer(Target,
785 pch = ChrPtr(CCC->ReadBuf);
786 nlen = StrLength(CCC->ReadBuf);
787 if (CCC->Pos != NULL)
788 nrlen = CC->Pos - pch;
792 fprintf(fd, "\n\n\nBufSize: was: %d is: %d BufPos: was: %d is: %d \nBufContent: [%s]\n\n_____________________\n",
793 len, nlen, rlen, nrlen, pch);
795 fprintf(fd, "\n\n\nSSL2: BufSize: was: %d is: %d BufPos: was: %d is: %d \n",
796 len, nlen, rlen, nrlen);
798 fprintf(fd, "SSL3: Read: BufSize: %d BufContent: [%s]\n\n*************\n",
799 StrLength(Target), ChrPtr(Target));
803 CtdlLogPrintf(CTDL_CRIT,
815 int len, rlen, nlen, nrlen;
818 snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
820 fd = fopen(fn, "a+");
821 pch = ChrPtr(CCC->ReadBuf);
822 len = StrLength(CCC->ReadBuf);
823 if (CCC->Pos != NULL)
824 rlen = CC->Pos - pch;
828 /* fprintf(fd, "\n\n\nBufSize: %d BufPos: %d \nBufContent: [%s]\n\n_____________________\n",
831 fprintf(fd, "\n\n\nBufSize: %d BufPos: %d \n_____________________\n",
834 rc = StrBufTCP_read_buffered_line_fast(Target,
843 pch = ChrPtr(CCC->ReadBuf);
844 nlen = StrLength(CCC->ReadBuf);
845 if (CCC->Pos != NULL)
846 nrlen = CC->Pos - pch;
850 fprintf(fd, "\n\n\nBufSize: was: %d is: %d BufPos: was: %d is: %d \nBufContent: [%s]\n\n_____________________\n",
851 len, nlen, rlen, nrlen, pch);
853 fprintf(fd, "\n\n\nBufSize: was: %d is: %d BufPos: was: %d is: %d \n",
854 len, nlen, rlen, nrlen);
856 fprintf(fd, "Read: BufSize: %d BufContent: [%s]\n\n*************\n",
857 StrLength(Target), ChrPtr(Target));
860 if ((rc < 0) && (Error != NULL))
861 CtdlLogPrintf(CTDL_CRIT,
872 * client_getln() ... Get a LF-terminated line of text from the client.
873 * (This is implemented in terms of client_read() and could be
874 * justifiably moved out of sysdep.c)
876 int client_getln(char *buf, int bufsize)
882 retval = CtdlClientGetLine(CCC->MigrateBuf);
887 i = StrLength(CCC->MigrateBuf);
888 pCh = ChrPtr(CCC->MigrateBuf);
889 /* Strip the trailing LF, and the trailing CR if present.
894 && ( (pCh[i - 1]==13)
895 || ( pCh[i - 1]==10)) ) {
901 FlushStrBuf(CCC->MigrateBuf);
903 safestrncpy(&buf[i], "000", bufsize - i);
910 * Cleanup any contexts that are left lying around
914 void close_masters (void)
916 struct ServiceFunctionHook *serviceptr;
919 * close all protocol master sockets
921 for (serviceptr = ServiceHookTable; serviceptr != NULL;
922 serviceptr = serviceptr->next ) {
924 if (serviceptr->tcp_port > 0)
926 CtdlLogPrintf(CTDL_INFO, "Closing listener on port %d\n",
927 serviceptr->tcp_port);
928 serviceptr->tcp_port = 0;
931 if (serviceptr->sockpath != NULL)
932 CtdlLogPrintf(CTDL_INFO, "Closing listener on '%s'\n",
933 serviceptr->sockpath);
935 close(serviceptr->msock);
936 /* If it's a Unix domain socket, remove the file. */
937 if (serviceptr->sockpath != NULL) {
938 unlink(serviceptr->sockpath);
939 serviceptr->sockpath = NULL;
946 * The system-dependent part of master_cleanup() - close the master socket.
948 void sysdep_master_cleanup(void) {
957 CtdlDestroyProtoHooks();
958 CtdlDestroyDeleteHooks();
959 CtdlDestroyXmsgHooks();
960 CtdlDestroyNetprocHooks();
961 CtdlDestroyUserHooks();
962 CtdlDestroyMessageHook();
963 CtdlDestroyCleanupHooks();
964 CtdlDestroyFixedOutputHooks();
965 CtdlDestroySessionHooks();
966 CtdlDestroyServiceHook();
967 CtdlDestroyRoomHooks();
968 #ifdef HAVE_BACKTRACE
976 void graceful_shutdown(int signum) {
977 kill(current_child, signum);
978 unlink(file_pid_file);
983 int nFireUpsNonRestart = 0;
987 * Start running as a daemon.
989 void start_daemon(int unused) {
997 /* Close stdin/stdout/stderr and replace them with /dev/null.
998 * We don't just call close() because we don't want these fd's
999 * to be reused for other files.
1001 if (chdir(ctdl_run_dir) != 0)
1002 CtdlLogPrintf(CTDL_EMERG,
1003 "unable to change into directory [%s]: %s",
1004 ctdl_run_dir, strerror(errno));
1011 signal(SIGHUP, SIG_IGN);
1012 signal(SIGINT, SIG_IGN);
1013 signal(SIGQUIT, SIG_IGN);
1017 if ((freopen("/dev/null", "r", stdin) != stdin) ||
1018 (freopen("/dev/null", "w", stdout) != stdout) ||
1019 (freopen("/dev/null", "w", stderr) != stderr))
1020 CtdlLogPrintf(CTDL_EMERG,
1021 "unable to reopen stdin/out/err %s",
1026 current_child = fork();
1028 signal(SIGTERM, graceful_shutdown);
1030 if (current_child < 0) {
1035 else if (current_child == 0) {
1036 return; /* continue starting citadel. */
1040 fp = fopen(file_pid_file, "w");
1042 fprintf(fp, ""F_PID_T"\n", getpid());
1045 waitpid(current_child, &status, 0);
1048 nFireUpsNonRestart = nFireUps;
1050 /* Exit code 0 means the watcher should exit */
1051 if (WIFEXITED(status) && (WEXITSTATUS(status) == CTDLEXIT_SHUTDOWN)) {
1055 /* Exit code 101-109 means the watcher should exit */
1056 else if (WIFEXITED(status) && (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109)) {
1060 /* Any other exit code, or no exit code, means we should restart. */
1064 ForkedPid = current_child;
1067 } while (do_restart);
1069 unlink(file_pid_file);
1070 exit(WEXITSTATUS(status));
1075 void checkcrash(void)
1077 if (nFireUpsNonRestart != nFireUps)
1081 CrashMail = NewStrBuf();
1082 CtdlLogPrintf(CTDL_ALERT, "Posting crash message\n");
1083 StrBufPrintf(CrashMail,
1085 " The Citadel server process (citserver) terminated unexpectedly."
1087 " This could be the result of a bug in the server program, or some external "
1089 " You can obtain more information about this by enabling core dumps.\n \n"
1090 " For more information, please see:\n \n"
1091 " http://citadel.org/doku.php/faq:mastering_your_os:gdb#how.do.i.make.my.system.produce.core-files"
1094 " If you have already done this, the core dump is likely to be found at %score.%d\n"
1096 ctdl_run_dir, ForkedPid);
1097 CtdlAideMessage(ChrPtr(CrashMail), "Citadel server process terminated unexpectedly");
1098 FreeStrBuf(&CrashMail);
1104 * Generic routine to convert a login name to a full name (gecos)
1105 * Returns nonzero if a conversion took place
1107 int convert_login(char NameToConvert[]) {
1111 pw = getpwnam(NameToConvert);
1116 strcpy(NameToConvert, pw->pw_gecos);
1117 for (a=0; a<strlen(NameToConvert); ++a) {
1118 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
1127 * This loop just keeps going and going and going...
1131 * This current implimentation of worker_thread creates a bottle neck in several situations
1132 * The first thing to remember is that a single thread can handle more than one connection at a time.
1133 * More threads mean less memory for the system to run in.
1134 * So for efficiency we want every thread to be doing something useful or waiting in the main loop for
1135 * something to happen anywhere.
1136 * This current implimentation requires worker threads to wait in other locations, after it has
1137 * been committed to a single connection which is very wasteful.
1138 * As an extreme case consider this:
1139 * A slow client connects and this slow client sends only one character each second.
1140 * With this current implimentation a single worker thread is dispatched to handle that connection
1141 * until such times as the client timeout expires, an error occurs on the socket or the client
1142 * completes its transmission.
1143 * THIS IS VERY BAD since that thread could have handled a read from many more clients in each one
1144 * second interval between chars.
1146 * It is my intention to re-write this code and the associated client_getln, client_read functions
1147 * to allow any thread to read data on behalf of any connection (context).
1148 * To do this I intend to have this main loop read chars into a buffer stored in the context.
1149 * Once the correct criteria for a full buffer is met then we will dispatch a thread to
1151 * This worker thread loop also needs to be able to handle binary data.
1154 void *worker_thread(void *arg) {
1157 CitContext *bind_me = NULL;
1161 int force_purge = 0;
1164 while (!CtdlThreadCheckStop()) {
1166 /* make doubly sure we're not holding any stale db handles
1167 * which might cause a deadlock.
1169 cdb_check_handles();
1170 do_select: force_purge = 0;
1171 bind_me = NULL; /* Which session shall we handle? */
1173 /* Initialize the fdset. */
1177 begin_critical_section(S_SESSION_TABLE);
1178 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1180 client_socket = ptr->client_socket;
1181 /* Dont select on dead sessions only truly idle ones */
1182 if ((ptr->state == CON_IDLE) &&
1183 (CC->kill_me == 0) &&
1184 (client_socket != -1))
1186 FD_SET(client_socket, &readfds);
1187 if (client_socket > highest)
1188 highest = client_socket;
1190 if ((bind_me == NULL) && (ptr->state == CON_READY)) {
1192 ptr->state = CON_EXECUTING;
1195 if ((bind_me == NULL) && (ptr->state == CON_GREETING)) {
1197 ptr->state = CON_STARTING;
1201 end_critical_section(S_SESSION_TABLE);
1207 /* If we got this far, it means that there are no sessions
1208 * which a previous thread marked for attention, so we go
1209 * ahead and get ready to select().
1212 if (!CtdlThreadCheckStop()) {
1213 tv.tv_sec = 1; /* wake up every second if no input */
1215 retval = CtdlThreadSelect(highest + 1, &readfds, NULL, NULL, &tv);
1220 /* Now figure out who made this select() unblock.
1221 * First, check for an error or exit condition.
1224 if (errno == EBADF) {
1225 CtdlLogPrintf(CTDL_NOTICE, "select() failed: (%s)\n",
1229 if (errno != EINTR) {
1230 CtdlLogPrintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
1231 CtdlThreadStopAll();
1234 CtdlLogPrintf(CTDL_DEBUG, "Interrupted CtdlThreadSelect.\n");
1235 if (CtdlThreadCheckStop()) return(NULL);
1239 else if(retval == 0) {
1240 if (CtdlThreadCheckStop()) return(NULL);
1243 /* It must be a client socket. Find a context that has data
1244 * waiting on its socket *and* is in the CON_IDLE state. Any
1245 * active sockets other than our chosen one are marked as
1246 * CON_READY so the next thread that comes around can just bind
1247 * to one without having to select() again.
1249 begin_critical_section(S_SESSION_TABLE);
1250 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1251 int checkfd = ptr->client_socket;
1252 if ((checkfd != -1) && (ptr->state == CON_IDLE) ){
1253 if (FD_ISSET(checkfd, &readfds)) {
1254 ptr->input_waiting = 1;
1256 bind_me = ptr; /* I choose you! */
1257 bind_me->state = CON_EXECUTING;
1260 ptr->state = CON_READY;
1262 } else if ((ptr->is_async) && (ptr->async_waiting) && (ptr->h_async_function)) {
1264 bind_me = ptr; /* I choose you! */
1265 bind_me->state = CON_EXECUTING;
1268 ptr->state = CON_READY;
1273 end_critical_section(S_SESSION_TABLE);
1276 /* We're bound to a session */
1277 if (bind_me != NULL) {
1278 become_session(bind_me);
1280 if (bind_me->state == CON_STARTING) {
1281 bind_me->state = CON_EXECUTING;
1282 begin_session(bind_me);
1283 bind_me->h_greeting_function();
1285 /* If the client has sent a command, execute it. */
1286 if (CC->input_waiting) {
1287 CC->h_command_function();
1289 while (HaveMoreLinesWaiting(CC))
1290 CC->h_command_function();
1292 CC->input_waiting = 0;
1295 /* If there are asynchronous messages waiting and the
1296 * client supports it, do those now */
1297 if ((CC->is_async) && (CC->async_waiting)
1298 && (CC->h_async_function != NULL)) {
1299 CC->h_async_function();
1300 CC->async_waiting = 0;
1303 force_purge = CC->kill_me;
1304 become_session(NULL);
1305 bind_me->state = CON_IDLE;
1308 dead_session_purge(force_purge);
1311 /* If control reaches this point, the server is shutting down */
1319 * A function to handle selecting on master sockets.
1320 * In other words it handles new connections.
1323 void *select_on_master (void *arg)
1325 struct ServiceFunctionHook *serviceptr;
1329 int ssock; /* Descriptor for client socket */
1330 CitContext *con= NULL; /* Temporary context pointer */
1334 struct CitContext select_on_master_CC;
1336 CtdlFillSystemContext(&select_on_master_CC, "select_on_master");
1337 citthread_setspecific(MyConKey, (void *)&select_on_master_CC);
1339 while (!CtdlThreadCheckStop()) {
1340 /* Initialize the fdset. */
1341 FD_ZERO(&master_fds);
1344 /* First, add the various master sockets to the fdset. */
1345 for (serviceptr = ServiceHookTable; serviceptr != NULL;
1346 serviceptr = serviceptr->next ) {
1347 m = serviceptr->msock;
1348 FD_SET(m, &master_fds);
1354 if (!CtdlThreadCheckStop()) {
1355 tv.tv_sec = 60; /* wake up every second if no input */
1357 retval = CtdlThreadSelect(highest + 1, &master_fds, NULL, NULL, &tv);
1362 /* Now figure out who made this select() unblock.
1363 * First, check for an error or exit condition.
1366 if (errno == EBADF) {
1367 CtdlLogPrintf(CTDL_NOTICE, "select() failed: (%s)\n",
1371 if (errno != EINTR) {
1372 CtdlLogPrintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
1373 CtdlThreadStopAll();
1375 CtdlLogPrintf(CTDL_DEBUG, "Interrupted CtdlThreadSelect.\n");
1376 if (CtdlThreadCheckStop()) return(NULL);
1380 else if(retval == 0) {
1381 if (CtdlThreadCheckStop()) return(NULL);
1384 /* Next, check to see if it's a new client connecting
1385 * on a master socket.
1387 else for (serviceptr = ServiceHookTable; serviceptr != NULL;
1388 serviceptr = serviceptr->next ) {
1390 if (FD_ISSET(serviceptr->msock, &master_fds)) {
1391 ssock = accept(serviceptr->msock, NULL, 0);
1393 CtdlLogPrintf(CTDL_DEBUG,
1394 "New client socket %d\n",
1397 /* The master socket is non-blocking but the client
1398 * sockets need to be blocking, otherwise certain
1399 * operations barf on FreeBSD. Not a fatal error.
1401 if (fcntl(ssock, F_SETFL, 0) < 0) {
1402 CtdlLogPrintf(CTDL_EMERG,
1403 "citserver: Can't set socket to blocking: %s\n",
1407 /* New context will be created already
1408 * set up in the CON_EXECUTING state.
1410 con = CreateNewContext();
1412 /* Assign our new socket number to it. */
1413 con->client_socket = ssock;
1414 con->h_command_function =
1415 serviceptr->h_command_function;
1416 con->h_async_function =
1417 serviceptr->h_async_function;
1418 con->h_greeting_function = serviceptr->h_greeting_function;
1420 serviceptr->ServiceName;
1422 /* Determine whether it's a local socket */
1423 if (serviceptr->sockpath != NULL)
1424 con->is_local_socket = 1;
1426 /* Set the SO_REUSEADDR socket option */
1428 setsockopt(ssock, SOL_SOCKET,
1432 con->state = CON_GREETING;
1441 CtdlClearSystemContext();
1450 * Translate text facility name to syslog.h defined value.
1452 int SyslogFacility(char *name)
1461 { LOG_KERN, "kern" },
1462 { LOG_USER, "user" },
1463 { LOG_MAIL, "mail" },
1464 { LOG_DAEMON, "daemon" },
1465 { LOG_AUTH, "auth" },
1466 { LOG_SYSLOG, "syslog" },
1468 { LOG_NEWS, "news" },
1469 { LOG_UUCP, "uucp" },
1470 { LOG_LOCAL0, "local0" },
1471 { LOG_LOCAL1, "local1" },
1472 { LOG_LOCAL2, "local2" },
1473 { LOG_LOCAL3, "local3" },
1474 { LOG_LOCAL4, "local4" },
1475 { LOG_LOCAL5, "local5" },
1476 { LOG_LOCAL6, "local6" },
1477 { LOG_LOCAL7, "local7" },
1480 for(i = 0; facTbl[i].name != NULL; i++) {
1481 if(!strcasecmp(name, facTbl[i].name))
1482 return facTbl[i].facility;
1489 /********** MEM CHEQQER ***********/
1491 #ifdef DEBUG_MEMORY_LEAKS
1498 void *tracked_malloc(size_t size, char *file, int line) {
1499 struct igheap *thisheap;
1502 block = malloc(size);
1503 if (block == NULL) return(block);
1505 thisheap = malloc(sizeof(struct igheap));
1506 if (thisheap == NULL) {
1511 thisheap->block = block;
1512 strcpy(thisheap->file, file);
1513 thisheap->line = line;
1515 begin_critical_section(S_DEBUGMEMLEAKS);
1516 thisheap->next = igheap;
1518 end_critical_section(S_DEBUGMEMLEAKS);
1524 void *tracked_realloc(void *ptr, size_t size, char *file, int line) {
1525 struct igheap *thisheap;
1528 block = realloc(ptr, size);
1529 if (block == NULL) return(block);
1531 thisheap = malloc(sizeof(struct igheap));
1532 if (thisheap == NULL) {
1537 thisheap->block = block;
1538 strcpy(thisheap->file, file);
1539 thisheap->line = line;
1541 begin_critical_section(S_DEBUGMEMLEAKS);
1542 thisheap->next = igheap;
1544 end_critical_section(S_DEBUGMEMLEAKS);
1551 void tracked_free(void *ptr) {
1552 struct igheap *thisheap;
1553 struct igheap *trash;
1557 if (igheap == NULL) return;
1558 begin_critical_section(S_DEBUGMEMLEAKS);
1559 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1560 if (thisheap->next != NULL) {
1561 if (thisheap->next->block == ptr) {
1562 trash = thisheap->next;
1563 thisheap->next = thisheap->next->next;
1568 if (igheap->block == ptr) {
1570 igheap = igheap->next;
1573 end_critical_section(S_DEBUGMEMLEAKS);
1576 char *tracked_strdup(const char *s, char *file, int line) {
1579 if (s == NULL) return(NULL);
1580 ptr = tracked_malloc(strlen(s) + 1, file, line);
1581 if (ptr == NULL) return(NULL);
1582 strncpy(ptr, s, strlen(s));
1586 void dump_heap(void) {
1587 struct igheap *thisheap;
1589 for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1590 CtdlLogPrintf(CTDL_CRIT, "UNFREED: %30s : %d\n",
1591 thisheap->file, thisheap->line);
1595 #endif /* DEBUG_MEMORY_LEAKS */