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 vsnprintf(buf, sizeof(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 if (CC && CC->cs_pid) {
123 "%04d/%02d/%02d %2d:%02d:%02d.%03ld [%3d] %s",
124 tim->tm_year + 1900, tim->tm_mon + 1,
125 tim->tm_mday, tim->tm_hour, tim->tm_min,
126 tim->tm_sec, (long)tv.tv_usec / 1000,
130 "%04d/%02d/%02d %2d:%02d:%02d.%03ld %s",
131 tim->tm_year + 1900, tim->tm_mon + 1,
132 tim->tm_mday, tim->tm_hour, tim->tm_min,
133 tim->tm_sec, (long)tv.tv_usec / 1000, buf);
138 PerformLogHooks(loglevel, buf);
143 #ifdef DEBUG_MEMORY_LEAKS
144 void *tracked_malloc(size_t tsize, char *tfile, int tline) {
146 struct TheHeap *hptr;
150 lprintf(3, "DANGER! mallok(%d) at %s:%d failed!\n",
151 tsize, tfile, tline);
155 hptr = (struct TheHeap *) malloc(sizeof(struct TheHeap));
156 strcpy(hptr->h_file, tfile);
157 hptr->h_line = tline;
164 char *tracked_strdup(const char *orig, char *tfile, int tline) {
167 s = tracked_malloc( (strlen(orig)+1), tfile, tline);
168 if (s == NULL) return NULL;
174 void tracked_free(void *ptr) {
175 struct TheHeap *hptr, *freeme;
177 if (heap->h_ptr == ptr) {
183 for (hptr=heap; hptr->next!=NULL; hptr=hptr->next) {
184 if (hptr->next->h_ptr == ptr) {
186 hptr->next = hptr->next->next;
195 void *tracked_realloc(void *ptr, size_t size) {
197 struct TheHeap *hptr;
199 newptr = realloc(ptr, size);
201 for (hptr=heap; hptr!=NULL; hptr=hptr->next) {
202 if (hptr->h_ptr == ptr) hptr->h_ptr = newptr;
209 void dump_tracked() {
210 struct TheHeap *hptr;
212 cprintf("%d Here's what's allocated...\n", LISTING_FOLLOWS);
213 for (hptr=heap; hptr!=NULL; hptr=hptr->next) {
214 cprintf("%20s %5d\n",
215 hptr->h_file, hptr->h_line);
227 * We used to use master_cleanup() as a signal handler to shut down the server.
228 * however, master_cleanup() and the functions it calls do some things that
229 * aren't such a good idea to do from a signal handler: acquiring mutexes,
230 * playing with signal masks on BSDI systems, etc. so instead we install the
231 * following signal handler to set a global variable to inform the main loop
232 * that it's time to call master_cleanup() and exit.
235 volatile int time_to_die = 0;
237 static RETSIGTYPE signal_cleanup(int signum) {
243 * Some initialization stuff...
245 void init_sysdep(void) {
252 /* Set up a bunch of semaphores to be used for critical sections */
253 for (a=0; a<MAX_SEMAPHORES; ++a) {
254 pthread_mutex_init(&Critters[a], NULL);
258 * Set up a place to put thread-specific data.
259 * We only need a single pointer per thread - it points to the
260 * CitContext structure (in the ContextList linked list) of the
261 * session to which the calling thread is currently bound.
263 if (pthread_key_create(&MyConKey, NULL) != 0) {
264 lprintf(1, "Can't create TSD key!! %s\n", strerror(errno));
268 * The action for unexpected signals and exceptions should be to
269 * call signal_cleanup() to gracefully shut down the server.
271 signal(SIGINT, signal_cleanup);
272 signal(SIGQUIT, signal_cleanup);
273 signal(SIGHUP, signal_cleanup);
274 signal(SIGTERM, signal_cleanup);
277 * Do not shut down the server on broken pipe signals, otherwise the
278 * whole Citadel service would come down whenever a single client
281 signal(SIGPIPE, SIG_IGN);
286 * Obtain a semaphore lock to begin a critical section.
288 void begin_critical_section(int which_one)
290 /* lprintf(9, "begin_critical_section(%d)\n", which_one); */
291 /* ensure nobody ever tries to do a critical section within a
292 transaction; this could lead to deadlock. */
294 pthread_mutex_lock(&Critters[which_one]);
298 * Release a semaphore lock to end a critical section.
300 void end_critical_section(int which_one)
302 /* lprintf(9, "end_critical_section(%d)\n", which_one); */
303 pthread_mutex_unlock(&Critters[which_one]);
309 * This is a generic function to set up a master socket for listening on
310 * a TCP port. The server shuts down if the bind fails.
313 int ig_tcp_server(int port_number, int queue_len)
315 struct sockaddr_in sin;
317 int actual_queue_len;
319 actual_queue_len = queue_len;
320 if (actual_queue_len < 5) actual_queue_len = 5;
322 memset(&sin, 0, sizeof(sin));
323 sin.sin_family = AF_INET;
324 sin.sin_addr.s_addr = INADDR_ANY;
325 sin.sin_port = htons((u_short)port_number);
327 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
330 lprintf(1, "citserver: Can't create a socket: %s\n",
336 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
338 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
339 lprintf(1, "citserver: Can't bind: %s\n",
345 if (listen(s, actual_queue_len) < 0) {
346 lprintf(1, "citserver: Can't listen: %s\n", strerror(errno));
357 * Create a Unix domain socket and listen on it
359 int ig_uds_server(char *sockpath, int queue_len)
361 struct sockaddr_un addr;
364 int actual_queue_len;
366 actual_queue_len = queue_len;
367 if (actual_queue_len < 5) actual_queue_len = 5;
369 i = unlink(sockpath);
370 if (i != 0) if (errno != ENOENT) {
371 lprintf(1, "citserver: can't unlink %s: %s\n",
372 sockpath, strerror(errno));
376 memset(&addr, 0, sizeof(addr));
377 addr.sun_family = AF_UNIX;
378 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
380 s = socket(AF_UNIX, SOCK_STREAM, 0);
382 lprintf(1, "citserver: Can't create a socket: %s\n",
387 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
388 lprintf(1, "citserver: Can't bind: %s\n",
393 if (listen(s, actual_queue_len) < 0) {
394 lprintf(1, "citserver: Can't listen: %s\n", strerror(errno));
398 chmod(sockpath, 0777);
405 * Return a pointer to the CitContext structure bound to the thread which
406 * called this function. If there's no such binding (for example, if it's
407 * called by the housekeeper thread) then a generic 'master' CC is returned.
409 struct CitContext *MyContext(void) {
410 struct CitContext *retCC;
411 retCC = (struct CitContext *) pthread_getspecific(MyConKey);
412 if (retCC == NULL) retCC = &masterCC;
418 * Initialize a new context and place it in the list. The session number
419 * used to be the PID (which is why it's called cs_pid), but that was when we
420 * had one process per session. Now we just assign them sequentially, starting
421 * at 1 (don't change it to 0 because masterCC uses 0) and re-using them when
422 * sessions terminate.
424 struct CitContext *CreateNewContext(void) {
425 struct CitContext *me, *ptr;
427 me = (struct CitContext *) mallok(sizeof(struct CitContext));
429 lprintf(1, "citserver: can't allocate memory!!\n");
432 memset(me, 0, sizeof(struct CitContext));
434 /* The new context will be created already in the CON_EXECUTING state
435 * in order to prevent another thread from grabbing it while it's
438 me->state = CON_EXECUTING;
442 * Generate a unique session number and insert this context into
445 begin_critical_section(S_SESSION_TABLE);
447 if (ContextList == NULL) {
453 else if (ContextList->cs_pid > 1) {
454 me->next = ContextList;
460 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
461 if (ptr->next == NULL) {
463 me->cs_pid = ptr->cs_pid + 1;
467 else if (ptr->next->cs_pid > (ptr->cs_pid+1)) {
468 me->next = ptr->next;
470 me->cs_pid = ptr->cs_pid + 1;
476 DONE: ++num_sessions;
477 end_critical_section(S_SESSION_TABLE);
483 * client_write() ... Send binary data to the client.
485 void client_write(char *buf, int nbytes)
487 int bytes_written = 0;
491 if (CC->redirect_fp != NULL) {
492 fwrite(buf, nbytes, 1, CC->redirect_fp);
496 if (CC->redirect_sock > 0) {
497 sock = CC->redirect_sock; /* and continue below... */
500 sock = CC->client_socket;
504 if (CC->redirect_ssl) {
505 client_write_ssl(buf, nbytes);
510 while (bytes_written < nbytes) {
511 retval = write(sock, &buf[bytes_written],
512 nbytes - bytes_written);
514 lprintf(2, "client_write() failed: %s\n",
516 if (sock == CC->client_socket) CC->kill_me = 1;
519 bytes_written = bytes_written + retval;
525 * cprintf() ... Send formatted printable data to the client. It is
526 * implemented in terms of client_write() but remains in
527 * sysdep.c in case we port to somewhere without va_args...
529 void cprintf(const char *format, ...) {
533 va_start(arg_ptr, format);
534 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
535 buf[sizeof buf - 2] = '\n';
536 client_write(buf, strlen(buf));
542 * Read data from the client socket.
544 * 1 Requested number of bytes has been read.
545 * 0 Request timed out.
546 * -1 The socket is broken.
547 * If the socket breaks, the session will be terminated.
549 int client_read_to(char *buf, int bytes, int timeout)
557 if (CC->redirect_ssl) {
558 return (client_read_ssl(buf, bytes, timeout));
564 FD_SET(CC->client_socket, &rfds);
568 retval = select( (CC->client_socket)+1,
569 &rfds, NULL, NULL, &tv);
571 if (FD_ISSET(CC->client_socket, &rfds) == 0) {
575 rlen = read(CC->client_socket, &buf[len], bytes-len);
577 lprintf(2, "client_read() failed: %s\n",
588 * Read data from the client socket with default timeout.
589 * (This is implemented in terms of client_read_to() and could be
590 * justifiably moved out of sysdep.c)
592 inline int client_read(char *buf, int bytes)
594 return(client_read_to(buf, bytes, config.c_sleeping));
599 * client_gets() ... Get a LF-terminated line of text from the client.
600 * (This is implemented in terms of client_read() and could be
601 * justifiably moved out of sysdep.c)
603 int client_gets(char *buf)
607 /* Read one character at a time.
610 retval = client_read(&buf[i], 1);
611 if (retval != 1 || buf[i] == '\n' || i == (SIZ-1))
615 /* If we got a long line, discard characters until the newline.
618 while (buf[i] != '\n' && retval == 1)
619 retval = client_read(&buf[i], 1);
621 /* Strip the trailing newline and any trailing nonprintables (cr's)
624 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
625 buf[strlen(buf)-1] = 0;
626 if (retval < 0) strcpy(buf, "000");
633 * The system-dependent part of master_cleanup() - close the master socket.
635 void sysdep_master_cleanup(void) {
636 struct ServiceFunctionHook *serviceptr;
639 * close all protocol master sockets
641 for (serviceptr = ServiceHookTable; serviceptr != NULL;
642 serviceptr = serviceptr->next ) {
644 if (serviceptr->tcp_port > 0)
645 lprintf(3, "Closing listener on port %d\n",
646 serviceptr->tcp_port);
648 if (serviceptr->sockpath != NULL)
649 lprintf(3, "Closing listener on '%s'\n",
650 serviceptr->sockpath);
652 close(serviceptr->msock);
654 /* If it's a Unix domain socket, remove the file. */
655 if (serviceptr->sockpath != NULL) {
656 unlink(serviceptr->sockpath);
663 * Terminate another session.
664 * (This could justifiably be moved out of sysdep.c because it
665 * no longer does anything that is system-dependent.)
667 void kill_session(int session_to_kill) {
668 struct CitContext *ptr;
670 begin_critical_section(S_SESSION_TABLE);
671 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
672 if (ptr->cs_pid == session_to_kill) {
676 end_critical_section(S_SESSION_TABLE);
683 * Start running as a daemon. Only close stdio if do_close_stdio is set.
685 void start_daemon(int do_close_stdio) {
686 if (do_close_stdio) {
691 signal(SIGHUP,SIG_IGN);
692 signal(SIGINT,SIG_IGN);
693 signal(SIGQUIT,SIG_IGN);
694 if (fork()!=0) exit(0);
700 * Generic routine to convert a login name to a full name (gecos)
701 * Returns nonzero if a conversion took place
703 int convert_login(char NameToConvert[]) {
707 pw = getpwnam(NameToConvert);
712 strcpy(NameToConvert, pw->pw_gecos);
713 for (a=0; a<strlen(NameToConvert); ++a) {
714 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
720 struct worker_node *worker_list = NULL;
724 * create a worker thread. this function must always be called from within
725 * an S_WORKER_LIST critical section!
727 void create_worker(void) {
729 struct worker_node *n = mallok(sizeof *n);
733 lprintf(1, "can't allocate worker_node, exiting\n");
738 if ((ret = pthread_attr_init(&attr))) {
739 lprintf(1, "pthread_attr_init: %s\n", strerror(ret));
744 /* we seem to need something bigger than
745 FreeBSD's default of 64K of stack. */
747 if ((ret = pthread_attr_setstacksize(&attr, 128 * 1024))) {
748 lprintf(1, "pthread_attr_setstacksize: %s\n", strerror(ret));
753 if ((ret = pthread_create(&n->tid, &attr, worker_thread, NULL) != 0))
756 lprintf(1, "Can't create worker thread: %s\n",
760 n->next = worker_list;
767 * Purge all sessions which have the 'kill_me' flag set.
768 * This function has code to prevent it from running more than once every
769 * few seconds, because running it after every single unbind would waste a lot
770 * of CPU time and keep the context list locked too much.
772 * After that's done, we raise or lower the size of the worker thread pool
773 * if such an action is appropriate.
775 void dead_session_purge(void) {
776 struct CitContext *ptr, *rem;
777 struct worker_node **node, *tmp;
780 if ( (time(NULL) - last_purge) < 5 ) return; /* Too soon, go away */
785 begin_critical_section(S_SESSION_TABLE);
786 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
787 if ( (ptr->state == CON_IDLE) && (ptr->kill_me) ) {
791 end_critical_section(S_SESSION_TABLE);
793 /* RemoveContext() enters its own S_SESSION_TABLE critical
794 * section, so we have to do it like this.
797 lprintf(9, "Purging session %d\n", rem->cs_pid);
801 } while (rem != NULL);
804 /* Raise or lower the size of the worker thread pool if such
805 * an action is appropriate.
808 self = pthread_self();
810 if ( (num_sessions > num_threads)
811 && (num_threads < config.c_max_workers) ) {
812 begin_critical_section(S_WORKER_LIST);
814 end_critical_section(S_WORKER_LIST);
817 /* don't let the initial thread die since it's responsible for
818 waiting for all the other threads to terminate. */
819 else if ( (num_sessions < num_threads)
820 && (num_threads > config.c_min_workers)
821 && (self != initial_thread) ) {
823 begin_critical_section(S_WORKER_LIST);
826 /* we're exiting before server shutdown... unlink ourself from
827 the worker list and detach our thread to avoid memory leaks
830 for (node = &worker_list; *node != NULL; node = &(*node)->next)
831 if ((*node)->tid == self) {
833 *node = (*node)->next;
838 pthread_detach(self);
839 end_critical_section(S_WORKER_LIST);
850 * Redirect a session's output to a file or socket.
851 * This function may be called with a file handle *or* a socket (but not
852 * both). Call with neither to return output to its normal client socket.
854 void CtdlRedirectOutput(FILE *fp, int sock) {
856 if (fp != NULL) CC->redirect_fp = fp;
857 else CC->redirect_fp = NULL;
859 if (sock > 0) CC->redirect_sock = sock;
860 else CC->redirect_sock = (-1);
866 * masterCC is the context we use when not attached to a session. This
867 * function initializes it.
869 void InitializeMasterCC(void) {
870 memset(&masterCC, 0, sizeof(struct CitContext));
871 masterCC.internal_pgm = 1;
878 * Set up a fd_set containing all the master sockets to which we
879 * always listen. It's computationally less expensive to just copy
880 * this to a local fd_set when starting a new select() and then add
881 * the client sockets than it is to initialize a new one and then
882 * figure out what to put there.
884 void init_master_fdset(void) {
885 struct ServiceFunctionHook *serviceptr;
888 lprintf(9, "Initializing master fdset\n");
893 lprintf(9, "Will listen on rescan pipe %d\n", rescan[0]);
894 FD_SET(rescan[0], &masterfds);
895 if (rescan[0] > masterhighest) masterhighest = rescan[0];
897 for (serviceptr = ServiceHookTable; serviceptr != NULL;
898 serviceptr = serviceptr->next ) {
899 m = serviceptr->msock;
900 lprintf(9, "Will listen on master socket %d\n", m);
901 FD_SET(m, &masterfds);
902 if (m > masterhighest) {
906 lprintf(9, "masterhighest = %d\n", masterhighest);
911 * Bind a thread to a context. (It's inline merely to speed things up.)
913 INLINE void become_session(struct CitContext *which_con) {
914 pthread_setspecific(MyConKey, (void *)which_con );
920 * This loop just keeps going and going and going...
922 void *worker_thread(void *arg) {
926 struct CitContext *ptr;
927 struct CitContext *bind_me = NULL;
930 struct CitContext *con= NULL; /* Temporary context pointer */
931 struct ServiceFunctionHook *serviceptr;
932 struct sockaddr_in fsin; /* Data for master socket */
933 int alen; /* Data for master socket */
934 int ssock; /* Descriptor for client socket */
941 while (!time_to_die) {
944 * A naive implementation would have all idle threads
945 * calling select() and then they'd all wake up at once. We
946 * solve this problem by putting the select() in a critical
947 * section, so only one thread has the opportunity to wake
948 * up. If we wake up on a master socket, create a new
949 * session context; otherwise, just bind the thread to the
950 * context we want and go on our merry way.
953 /* make doubly sure we're not holding any stale db handles
954 * which might cause a deadlock.
958 begin_critical_section(S_I_WANNA_SELECT);
959 SETUP_FD: memcpy(&readfds, &masterfds, sizeof masterfds);
960 highest = masterhighest;
961 begin_critical_section(S_SESSION_TABLE);
962 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
963 if (ptr->state == CON_IDLE) {
964 FD_SET(ptr->client_socket, &readfds);
965 if (ptr->client_socket > highest)
966 highest = ptr->client_socket;
969 end_critical_section(S_SESSION_TABLE);
971 tv.tv_sec = 1; /* wake up every second if no input */
976 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
978 end_critical_section(S_I_WANNA_SELECT);
982 /* Now figure out who made this select() unblock.
983 * First, check for an error or exit condition.
986 if (errno != EINTR) {
987 lprintf(9, "Exiting (%s)\n", strerror(errno));
989 } else if (!time_to_die)
993 /* Next, check to see if it's a new client connecting
994 * on a master socket.
996 else for (serviceptr = ServiceHookTable; serviceptr != NULL;
997 serviceptr = serviceptr->next ) {
999 if (FD_ISSET(serviceptr->msock, &readfds)) {
1001 ssock = accept(serviceptr->msock,
1002 (struct sockaddr *)&fsin, &alen);
1004 lprintf(2, "citserver: accept(): %s\n",
1008 lprintf(7, "citserver: "
1009 "New client socket %d\n",
1012 /* New context will be created already
1013 * set up in the CON_EXECUTING state.
1015 con = CreateNewContext();
1017 /* Assign new socket number to it. */
1018 con->client_socket = ssock;
1019 con->h_command_function =
1020 serviceptr->h_command_function;
1022 /* Determine whether local socket */
1023 if (serviceptr->sockpath != NULL)
1024 con->is_local_socket = 1;
1026 /* Set the SO_REUSEADDR socket option */
1028 setsockopt(ssock, SOL_SOCKET,
1032 become_session(con);
1034 serviceptr->h_greeting_function();
1035 become_session(NULL);
1036 con->state = CON_IDLE;
1042 /* If the rescan pipe went active, someone is telling this
1043 * thread that the &readfds needs to be refreshed with more
1047 end_critical_section(S_I_WANNA_SELECT);
1051 if (FD_ISSET(rescan[0], &readfds)) {
1052 read(rescan[0], &junk, 1);
1056 /* It must be a client socket. Find a context that has data
1057 * waiting on its socket *and* is in the CON_IDLE state.
1061 begin_critical_section(S_SESSION_TABLE);
1062 for (ptr = ContextList;
1063 ( (ptr != NULL) && (bind_me == NULL) );
1065 if ( (FD_ISSET(ptr->client_socket, &readfds))
1066 && (ptr->state == CON_IDLE) ) {
1070 if (bind_me != NULL) {
1071 /* Found one. Stake a claim to it before
1072 * letting anyone else touch the context list.
1074 bind_me->state = CON_EXECUTING;
1077 end_critical_section(S_SESSION_TABLE);
1078 end_critical_section(S_I_WANNA_SELECT);
1080 /* We're bound to a session, now do *one* command */
1081 if (bind_me != NULL) {
1082 become_session(bind_me);
1083 CC->h_command_function();
1084 become_session(NULL);
1085 bind_me->state = CON_IDLE;
1086 if (bind_me->kill_me == 1) {
1087 RemoveContext(bind_me);
1089 write(rescan[1], &junk, 1);
1093 dead_session_purge();
1095 check_sched_shutdown();
1098 /* If control reaches this point, the server is shutting down */