4 * Citadel/UX "system dependent" stuff.
5 * See copyright.txt 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.
26 #include <sys/types.h>
29 #include <sys/socket.h>
31 #if TIME_WITH_SYS_TIME
32 # include <sys/time.h>
36 # include <sys/time.h>
43 #include <netinet/in.h>
57 #include "dynloader.h"
58 #include "sysdep_decls.h"
59 #include "citserver.h"
63 #include "housekeeping.h"
65 #include "serv_crypto.h"
67 #ifdef HAVE_SYS_SELECT_H
68 #include <sys/select.h>
75 #ifdef DEBUG_MEMORY_LEAKS
76 struct TheHeap *heap = NULL;
79 pthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */
80 pthread_key_t MyConKey; /* TSD key for MyContext() */
82 int verbosity = DEFAULT_VERBOSITY; /* Logging level */
84 struct CitContext masterCC;
85 int rescan[2]; /* The Rescan Pipe */
86 time_t last_purge = 0; /* Last dead session purge */
87 static int num_threads = 0; /* Current number of threads */
88 int num_sessions = 0; /* Current number of sessions */
90 fd_set masterfds; /* Master sockets etc. */
93 pthread_t initial_thread; /* tid for main() thread */
97 * lprintf() ... Write logging information
99 * Note: the variable "buf" below needs to be large enough to handle any
100 * log data sent through this function. BE CAREFUL!
102 void lprintf(int loglevel, const char *format, ...) {
106 va_start(arg_ptr, format);
107 vsprintf(buf, format, arg_ptr);
110 if (loglevel <= verbosity) {
114 gettimeofday(&tv, NULL);
115 tim = localtime(&(tv.tv_sec));
117 * Log provides millisecond accuracy. If you need
118 * microsecond accuracy and your OS supports it, change
119 * %03ld to %06ld and remove " / 1000" after tv.tv_usec.
121 fprintf(stderr, "%04d/%02d/%02d %2d:%02d:%02d.%03ld %s",
122 tim->tm_year + 1900, tim->tm_mon + 1, tim->tm_mday,
123 tim->tm_hour, tim->tm_min, tim->tm_sec,
124 (long)tv.tv_usec / 1000, buf);
128 PerformLogHooks(loglevel, buf);
133 #ifdef DEBUG_MEMORY_LEAKS
134 void *tracked_malloc(size_t tsize, char *tfile, int tline) {
136 struct TheHeap *hptr;
140 lprintf(3, "DANGER! mallok(%d) at %s:%d failed!\n",
141 tsize, tfile, tline);
145 hptr = (struct TheHeap *) malloc(sizeof(struct TheHeap));
146 strcpy(hptr->h_file, tfile);
147 hptr->h_line = tline;
154 char *tracked_strdup(const char *orig, char *tfile, int tline) {
157 s = tracked_malloc( (strlen(orig)+1), tfile, tline);
158 if (s == NULL) return NULL;
164 void tracked_free(void *ptr) {
165 struct TheHeap *hptr, *freeme;
167 if (heap->h_ptr == ptr) {
173 for (hptr=heap; hptr->next!=NULL; hptr=hptr->next) {
174 if (hptr->next->h_ptr == ptr) {
176 hptr->next = hptr->next->next;
185 void *tracked_realloc(void *ptr, size_t size) {
187 struct TheHeap *hptr;
189 newptr = realloc(ptr, size);
191 for (hptr=heap; hptr!=NULL; hptr=hptr->next) {
192 if (hptr->h_ptr == ptr) hptr->h_ptr = newptr;
199 void dump_tracked() {
200 struct TheHeap *hptr;
202 cprintf("%d Here's what's allocated...\n", LISTING_FOLLOWS);
203 for (hptr=heap; hptr!=NULL; hptr=hptr->next) {
204 cprintf("%20s %5d\n",
205 hptr->h_file, hptr->h_line);
217 * We used to use master_cleanup() as a signal handler to shut down the server.
218 * however, master_cleanup() and the functions it calls do some things that
219 * aren't such a good idea to do from a signal handler: acquiring mutexes,
220 * playing with signal masks on BSDI systems, etc. so instead we install the
221 * following signal handler to set a global variable to inform the main loop
222 * that it's time to call master_cleanup() and exit.
225 volatile int time_to_die = 0;
227 static RETSIGTYPE signal_cleanup(int signum) {
233 * Some initialization stuff...
235 void init_sysdep(void) {
242 /* Set up a bunch of semaphores to be used for critical sections */
243 for (a=0; a<MAX_SEMAPHORES; ++a) {
244 pthread_mutex_init(&Critters[a], NULL);
248 * Set up a place to put thread-specific data.
249 * We only need a single pointer per thread - it points to the
250 * CitContext structure (in the ContextList linked list) of the
251 * session to which the calling thread is currently bound.
253 if (pthread_key_create(&MyConKey, NULL) != 0) {
254 lprintf(1, "Can't create TSD key!! %s\n", strerror(errno));
258 * The action for unexpected signals and exceptions should be to
259 * call signal_cleanup() to gracefully shut down the server.
261 signal(SIGINT, signal_cleanup);
262 signal(SIGQUIT, signal_cleanup);
263 signal(SIGHUP, signal_cleanup);
264 signal(SIGTERM, 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);
276 * Obtain a semaphore lock to begin a critical section.
278 void begin_critical_section(int which_one)
280 /* lprintf(9, "begin_critical_section(%d)\n", which_one); */
281 /* ensure nobody ever tries to do a critical section within a
282 transaction; this could lead to deadlock. */
284 pthread_mutex_lock(&Critters[which_one]);
288 * Release a semaphore lock to end a critical section.
290 void end_critical_section(int which_one)
292 /* lprintf(9, "end_critical_section(%d)\n", which_one); */
293 pthread_mutex_unlock(&Critters[which_one]);
299 * This is a generic function to set up a master socket for listening on
300 * a TCP port. The server shuts down if the bind fails.
303 int ig_tcp_server(int port_number, int queue_len)
305 struct sockaddr_in sin;
307 int actual_queue_len;
309 actual_queue_len = queue_len;
310 if (actual_queue_len < 5) actual_queue_len = 5;
312 memset(&sin, 0, sizeof(sin));
313 sin.sin_family = AF_INET;
314 sin.sin_addr.s_addr = INADDR_ANY;
315 sin.sin_port = htons((u_short)port_number);
317 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
320 lprintf(1, "citserver: Can't create a socket: %s\n",
326 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
328 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
329 lprintf(1, "citserver: Can't bind: %s\n",
335 if (listen(s, actual_queue_len) < 0) {
336 lprintf(1, "citserver: Can't listen: %s\n", strerror(errno));
347 * Create a Unix domain socket and listen on it
349 int ig_uds_server(char *sockpath, int queue_len)
351 struct sockaddr_un addr;
354 int actual_queue_len;
356 actual_queue_len = queue_len;
357 if (actual_queue_len < 5) actual_queue_len = 5;
359 i = unlink(sockpath);
360 if (i != 0) if (errno != ENOENT) {
361 lprintf(1, "citserver: can't unlink %s: %s\n",
362 sockpath, strerror(errno));
366 memset(&addr, 0, sizeof(addr));
367 addr.sun_family = AF_UNIX;
368 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
370 s = socket(AF_UNIX, SOCK_STREAM, 0);
372 lprintf(1, "citserver: Can't create a socket: %s\n",
377 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
378 lprintf(1, "citserver: Can't bind: %s\n",
383 if (listen(s, actual_queue_len) < 0) {
384 lprintf(1, "citserver: Can't listen: %s\n", strerror(errno));
388 chmod(sockpath, 0777);
395 * Return a pointer to the CitContext structure bound to the thread which
396 * called this function. If there's no such binding (for example, if it's
397 * called by the housekeeper thread) then a generic 'master' CC is returned.
399 struct CitContext *MyContext(void) {
400 struct CitContext *retCC;
401 retCC = (struct CitContext *) pthread_getspecific(MyConKey);
402 if (retCC == NULL) retCC = &masterCC;
408 * Initialize a new context and place it in the list. The session number
409 * used to be the PID (which is why it's called cs_pid), but that was when we
410 * had one process per session. Now we just assign them sequentially, starting
411 * at 1 (don't change it to 0 because masterCC uses 0) and re-using them when
412 * sessions terminate.
414 struct CitContext *CreateNewContext(void) {
415 struct CitContext *me, *ptr;
417 me = (struct CitContext *) mallok(sizeof(struct CitContext));
419 lprintf(1, "citserver: can't allocate memory!!\n");
422 memset(me, 0, sizeof(struct CitContext));
424 /* The new context will be created already in the CON_EXECUTING state
425 * in order to prevent another thread from grabbing it while it's
428 me->state = CON_EXECUTING;
432 * Generate a unique session number and insert this context into
435 begin_critical_section(S_SESSION_TABLE);
437 if (ContextList == NULL) {
443 else if (ContextList->cs_pid > 1) {
444 me->next = ContextList;
450 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
451 if (ptr->next == NULL) {
453 me->cs_pid = ptr->cs_pid + 1;
457 else if (ptr->next->cs_pid > (ptr->cs_pid+1)) {
458 me->next = ptr->next;
460 me->cs_pid = ptr->cs_pid + 1;
466 DONE: ++num_sessions;
467 end_critical_section(S_SESSION_TABLE);
473 * client_write() ... Send binary data to the client.
475 void client_write(char *buf, int nbytes)
477 int bytes_written = 0;
483 if (CC->redirect_ssl) {
484 client_write_ssl(buf, nbytes);
489 if (CC->redirect_fp != NULL) {
490 fwrite(buf, nbytes, 1, CC->redirect_fp);
494 if (CC->redirect_sock > 0) {
495 sock = CC->redirect_sock; /* and continue below... */
498 sock = CC->client_socket;
501 while (bytes_written < nbytes) {
502 retval = write(sock, &buf[bytes_written],
503 nbytes - bytes_written);
505 lprintf(2, "client_write() failed: %s\n",
507 if (sock == CC->client_socket) CC->kill_me = 1;
510 bytes_written = bytes_written + retval;
516 * cprintf() ... Send formatted printable data to the client. It is
517 * implemented in terms of client_write() but remains in
518 * sysdep.c in case we port to somewhere without va_args...
520 void cprintf(const char *format, ...) {
524 va_start(arg_ptr, format);
525 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
526 buf[sizeof buf - 2] = '\n';
527 client_write(buf, strlen(buf));
533 * Read data from the client socket.
535 * 1 Requested number of bytes has been read.
536 * 0 Request timed out.
537 * -1 The socket is broken.
538 * If the socket breaks, the session will be terminated.
540 int client_read_to(char *buf, int bytes, int timeout)
548 if (CC->redirect_ssl) {
549 return (client_read_ssl(buf, bytes, timeout));
555 FD_SET(CC->client_socket, &rfds);
559 retval = select( (CC->client_socket)+1,
560 &rfds, NULL, NULL, &tv);
562 if (FD_ISSET(CC->client_socket, &rfds) == 0) {
566 rlen = read(CC->client_socket, &buf[len], bytes-len);
568 lprintf(2, "client_read() failed: %s\n",
579 * Read data from the client socket with default timeout.
580 * (This is implemented in terms of client_read_to() and could be
581 * justifiably moved out of sysdep.c)
583 inline int client_read(char *buf, int bytes)
585 return(client_read_to(buf, bytes, config.c_sleeping));
590 * client_gets() ... Get a LF-terminated line of text from the client.
591 * (This is implemented in terms of client_read() and could be
592 * justifiably moved out of sysdep.c)
594 int client_gets(char *buf)
598 /* Read one character at a time.
601 retval = client_read(&buf[i], 1);
602 if (retval != 1 || buf[i] == '\n' || i == (SIZ-1))
606 /* If we got a long line, discard characters until the newline.
609 while (buf[i] != '\n' && retval == 1)
610 retval = client_read(&buf[i], 1);
612 /* Strip the trailing newline and any trailing nonprintables (cr's)
615 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
616 buf[strlen(buf)-1] = 0;
617 if (retval < 0) strcpy(buf, "000");
624 * The system-dependent part of master_cleanup() - close the master socket.
626 void sysdep_master_cleanup(void) {
627 struct ServiceFunctionHook *serviceptr;
630 * close all protocol master sockets
632 for (serviceptr = ServiceHookTable; serviceptr != NULL;
633 serviceptr = serviceptr->next ) {
635 if (serviceptr->tcp_port > 0)
636 lprintf(3, "Closing listener on port %d\n",
637 serviceptr->tcp_port);
639 if (serviceptr->sockpath != NULL)
640 lprintf(3, "Closing listener on '%s'\n",
641 serviceptr->sockpath);
643 close(serviceptr->msock);
645 /* If it's a Unix domain socket, remove the file. */
646 if (serviceptr->sockpath != NULL) {
647 unlink(serviceptr->sockpath);
654 * Terminate another session.
655 * (This could justifiably be moved out of sysdep.c because it
656 * no longer does anything that is system-dependent.)
658 void kill_session(int session_to_kill) {
659 struct CitContext *ptr;
661 begin_critical_section(S_SESSION_TABLE);
662 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
663 if (ptr->cs_pid == session_to_kill) {
667 end_critical_section(S_SESSION_TABLE);
674 * Start running as a daemon. Only close stdio if do_close_stdio is set.
676 void start_daemon(int do_close_stdio) {
677 if (do_close_stdio) {
682 signal(SIGHUP,SIG_IGN);
683 signal(SIGINT,SIG_IGN);
684 signal(SIGQUIT,SIG_IGN);
685 if (fork()!=0) exit(0);
691 * Generic routine to convert a login name to a full name (gecos)
692 * Returns nonzero if a conversion took place
694 int convert_login(char NameToConvert[]) {
698 pw = getpwnam(NameToConvert);
703 strcpy(NameToConvert, pw->pw_gecos);
704 for (a=0; a<strlen(NameToConvert); ++a) {
705 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
711 struct worker_node *worker_list = NULL;
715 * create a worker thread. this function must always be called from within
716 * an S_WORKER_LIST critical section!
718 void create_worker(void) {
720 struct worker_node *n = mallok(sizeof *n);
724 lprintf(1, "can't allocate worker_node, exiting\n");
729 if ((ret = pthread_attr_init(&attr))) {
730 lprintf(1, "pthread_attr_init: %s\n", strerror(ret));
735 /* we seem to need something bigger than
736 FreeBSD's default of 64K of stack. */
738 if ((ret = pthread_attr_setstacksize(&attr, 128 * 1024))) {
739 lprintf(1, "pthread_attr_setstacksize: %s\n", strerror(ret));
744 if ((ret = pthread_create(&n->tid, &attr, worker_thread, NULL) != 0))
747 lprintf(1, "Can't create worker thread: %s\n",
751 n->next = worker_list;
758 * Purge all sessions which have the 'kill_me' flag set.
759 * This function has code to prevent it from running more than once every
760 * few seconds, because running it after every single unbind would waste a lot
761 * of CPU time and keep the context list locked too much.
763 * After that's done, we raise or lower the size of the worker thread pool
764 * if such an action is appropriate.
766 void dead_session_purge(void) {
767 struct CitContext *ptr, *rem;
768 struct worker_node **node, *tmp;
771 if ( (time(NULL) - last_purge) < 5 ) return; /* Too soon, go away */
776 begin_critical_section(S_SESSION_TABLE);
777 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
778 if ( (ptr->state == CON_IDLE) && (ptr->kill_me) ) {
782 end_critical_section(S_SESSION_TABLE);
784 /* RemoveContext() enters its own S_SESSION_TABLE critical
785 * section, so we have to do it like this.
788 lprintf(9, "Purging session %d\n", rem->cs_pid);
792 } while (rem != NULL);
795 /* Raise or lower the size of the worker thread pool if such
796 * an action is appropriate.
799 self = pthread_self();
801 if ( (num_sessions > num_threads)
802 && (num_threads < config.c_max_workers) ) {
803 begin_critical_section(S_WORKER_LIST);
805 end_critical_section(S_WORKER_LIST);
808 /* don't let the initial thread die since it's responsible for
809 waiting for all the other threads to terminate. */
810 else if ( (num_sessions < num_threads)
811 && (num_threads > config.c_min_workers)
812 && (self != initial_thread) ) {
814 begin_critical_section(S_WORKER_LIST);
817 /* we're exiting before server shutdown... unlink ourself from
818 the worker list and detach our thread to avoid memory leaks
821 for (node = &worker_list; *node != NULL; node = &(*node)->next)
822 if ((*node)->tid == self) {
824 *node = (*node)->next;
829 pthread_detach(self);
830 end_critical_section(S_WORKER_LIST);
841 * Redirect a session's output to a file or socket.
842 * This function may be called with a file handle *or* a socket (but not
843 * both). Call with neither to return output to its normal client socket.
845 void CtdlRedirectOutput(FILE *fp, int sock) {
847 if (fp != NULL) CC->redirect_fp = fp;
848 else CC->redirect_fp = NULL;
850 if (sock > 0) CC->redirect_sock = sock;
851 else CC->redirect_sock = (-1);
857 * masterCC is the context we use when not attached to a session. This
858 * function initializes it.
860 void InitializeMasterCC(void) {
861 memset(&masterCC, 0, sizeof(struct CitContext));
862 masterCC.internal_pgm = 1;
869 * Set up a fd_set containing all the master sockets to which we
870 * always listen. It's computationally less expensive to just copy
871 * this to a local fd_set when starting a new select() and then add
872 * the client sockets than it is to initialize a new one and then
873 * figure out what to put there.
875 void init_master_fdset(void) {
876 struct ServiceFunctionHook *serviceptr;
879 lprintf(9, "Initializing master fdset\n");
884 lprintf(9, "Will listen on rescan pipe %d\n", rescan[0]);
885 FD_SET(rescan[0], &masterfds);
886 if (rescan[0] > masterhighest) masterhighest = rescan[0];
888 for (serviceptr = ServiceHookTable; serviceptr != NULL;
889 serviceptr = serviceptr->next ) {
890 m = serviceptr->msock;
891 lprintf(9, "Will listen on master socket %d\n", m);
892 FD_SET(m, &masterfds);
893 if (m > masterhighest) {
897 lprintf(9, "masterhighest = %d\n", masterhighest);
902 * Bind a thread to a context. (It's inline merely to speed things up.)
904 INLINE void become_session(struct CitContext *which_con) {
905 pthread_setspecific(MyConKey, (void *)which_con );
911 * This loop just keeps going and going and going...
913 void *worker_thread(void *arg) {
917 struct CitContext *ptr;
918 struct CitContext *bind_me = NULL;
921 struct CitContext *con= NULL; /* Temporary context pointer */
922 struct ServiceFunctionHook *serviceptr;
923 struct sockaddr_in fsin; /* Data for master socket */
924 int alen; /* Data for master socket */
925 int ssock; /* Descriptor for client socket */
932 while (!time_to_die) {
935 * A naive implementation would have all idle threads
936 * calling select() and then they'd all wake up at once. We
937 * solve this problem by putting the select() in a critical
938 * section, so only one thread has the opportunity to wake
939 * up. If we wake up on a master socket, create a new
940 * session context; otherwise, just bind the thread to the
941 * context we want and go on our merry way.
944 /* make doubly sure we're not holding any stale db handles
945 * which might cause a deadlock.
949 begin_critical_section(S_I_WANNA_SELECT);
950 SETUP_FD: memcpy(&readfds, &masterfds, sizeof masterfds);
951 highest = masterhighest;
952 begin_critical_section(S_SESSION_TABLE);
953 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
954 if (ptr->state == CON_IDLE) {
955 FD_SET(ptr->client_socket, &readfds);
956 if (ptr->client_socket > highest)
957 highest = ptr->client_socket;
960 end_critical_section(S_SESSION_TABLE);
962 tv.tv_sec = 1; /* wake up every second if no input */
967 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
969 end_critical_section(S_I_WANNA_SELECT);
973 /* Now figure out who made this select() unblock.
974 * First, check for an error or exit condition.
977 if (errno != EINTR) {
978 lprintf(9, "Exiting (%s)\n", strerror(errno));
980 } else if (!time_to_die)
984 /* Next, check to see if it's a new client connecting
985 * on a master socket.
987 else for (serviceptr = ServiceHookTable; serviceptr != NULL;
988 serviceptr = serviceptr->next ) {
990 if (FD_ISSET(serviceptr->msock, &readfds)) {
992 ssock = accept(serviceptr->msock,
993 (struct sockaddr *)&fsin, &alen);
995 lprintf(2, "citserver: accept(): %s\n",
999 lprintf(7, "citserver: "
1000 "New client socket %d\n",
1003 /* New context will be created already
1004 * set up in the CON_EXECUTING state.
1006 con = CreateNewContext();
1008 /* Assign new socket number to it. */
1009 con->client_socket = ssock;
1010 con->h_command_function =
1011 serviceptr->h_command_function;
1013 /* Determine whether local socket */
1014 if (serviceptr->sockpath != NULL)
1015 con->is_local_socket = 1;
1017 /* Set the SO_REUSEADDR socket option */
1019 setsockopt(ssock, SOL_SOCKET,
1023 become_session(con);
1025 serviceptr->h_greeting_function();
1026 become_session(NULL);
1027 con->state = CON_IDLE;
1033 /* If the rescan pipe went active, someone is telling this
1034 * thread that the &readfds needs to be refreshed with more
1038 end_critical_section(S_I_WANNA_SELECT);
1042 if (FD_ISSET(rescan[0], &readfds)) {
1043 read(rescan[0], &junk, 1);
1047 /* It must be a client socket. Find a context that has data
1048 * waiting on its socket *and* is in the CON_IDLE state.
1052 begin_critical_section(S_SESSION_TABLE);
1053 for (ptr = ContextList;
1054 ( (ptr != NULL) && (bind_me == NULL) );
1056 if ( (FD_ISSET(ptr->client_socket, &readfds))
1057 && (ptr->state == CON_IDLE) ) {
1061 if (bind_me != NULL) {
1062 /* Found one. Stake a claim to it before
1063 * letting anyone else touch the context list.
1065 bind_me->state = CON_EXECUTING;
1068 end_critical_section(S_SESSION_TABLE);
1069 end_critical_section(S_I_WANNA_SELECT);
1071 /* We're bound to a session, now do *one* command */
1072 if (bind_me != NULL) {
1073 become_session(bind_me);
1074 CC->h_command_function();
1075 become_session(NULL);
1076 bind_me->state = CON_IDLE;
1077 if (bind_me->kill_me == 1) {
1078 RemoveContext(bind_me);
1080 write(rescan[1], &junk, 1);
1084 dead_session_purge();
1086 check_sched_shutdown();
1089 /* If control reaches this point, the server is shutting down */