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 "serv_extensions.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) {
115 gettimeofday(&tv, NULL);
116 /* Promote to time_t; types differ on some OSes (like darwin) */
117 unixtime = tv.tv_sec;
118 tim = localtime(&unixtime);
120 * Log provides millisecond accuracy. If you need
121 * microsecond accuracy and your OS supports it, change
122 * %03ld to %06ld and remove " / 1000" after tv.tv_usec.
124 if (CC && CC->cs_pid) {
126 "%04d/%02d/%02d %2d:%02d:%02d.%03ld [%3d] %s",
127 tim->tm_year + 1900, tim->tm_mon + 1,
128 tim->tm_mday, tim->tm_hour, tim->tm_min,
129 tim->tm_sec, (long)tv.tv_usec / 1000,
133 "%04d/%02d/%02d %2d:%02d:%02d.%03ld %s",
134 tim->tm_year + 1900, tim->tm_mon + 1,
135 tim->tm_mday, tim->tm_hour, tim->tm_min,
136 tim->tm_sec, (long)tv.tv_usec / 1000, buf);
141 PerformLogHooks(loglevel, buf);
146 #ifdef DEBUG_MEMORY_LEAKS
147 void *tracked_malloc(size_t tsize, char *tfile, int tline) {
149 struct TheHeap *hptr;
153 lprintf(3, "DANGER! mallok(%d) at %s:%d failed!\n",
154 tsize, tfile, tline);
158 hptr = (struct TheHeap *) malloc(sizeof(struct TheHeap));
159 strcpy(hptr->h_file, tfile);
160 hptr->h_line = tline;
167 char *tracked_strdup(const char *orig, char *tfile, int tline) {
170 s = tracked_malloc( (strlen(orig)+1), tfile, tline);
171 if (s == NULL) return NULL;
177 void tracked_free(void *ptr) {
178 struct TheHeap *hptr, *freeme;
180 if (heap->h_ptr == ptr) {
186 for (hptr=heap; hptr->next!=NULL; hptr=hptr->next) {
187 if (hptr->next->h_ptr == ptr) {
189 hptr->next = hptr->next->next;
198 void *tracked_realloc(void *ptr, size_t size) {
200 struct TheHeap *hptr;
202 newptr = realloc(ptr, size);
204 for (hptr=heap; hptr!=NULL; hptr=hptr->next) {
205 if (hptr->h_ptr == ptr) hptr->h_ptr = newptr;
212 void dump_tracked() {
213 struct TheHeap *hptr;
215 cprintf("%d Here's what's allocated...\n", LISTING_FOLLOWS);
216 for (hptr=heap; hptr!=NULL; hptr=hptr->next) {
217 cprintf("%20s %5d\n",
218 hptr->h_file, hptr->h_line);
230 * We used to use master_cleanup() as a signal handler to shut down the server.
231 * however, master_cleanup() and the functions it calls do some things that
232 * aren't such a good idea to do from a signal handler: acquiring mutexes,
233 * playing with signal masks on BSDI systems, etc. so instead we install the
234 * following signal handler to set a global variable to inform the main loop
235 * that it's time to call master_cleanup() and exit.
238 volatile int time_to_die = 0;
240 static RETSIGTYPE signal_cleanup(int signum) {
246 * Some initialization stuff...
248 void init_sysdep(void) {
255 /* Set up a bunch of semaphores to be used for critical sections */
256 for (a=0; a<MAX_SEMAPHORES; ++a) {
257 pthread_mutex_init(&Critters[a], NULL);
261 * Set up a place to put thread-specific data.
262 * We only need a single pointer per thread - it points to the
263 * CitContext structure (in the ContextList linked list) of the
264 * session to which the calling thread is currently bound.
266 if (pthread_key_create(&MyConKey, NULL) != 0) {
267 lprintf(1, "Can't create TSD key!! %s\n", strerror(errno));
271 * The action for unexpected signals and exceptions should be to
272 * call signal_cleanup() to gracefully shut down the server.
274 signal(SIGINT, signal_cleanup);
275 signal(SIGQUIT, signal_cleanup);
276 signal(SIGHUP, signal_cleanup);
277 signal(SIGTERM, signal_cleanup);
280 * Do not shut down the server on broken pipe signals, otherwise the
281 * whole Citadel service would come down whenever a single client
284 signal(SIGPIPE, SIG_IGN);
289 * Obtain a semaphore lock to begin a critical section.
291 void begin_critical_section(int which_one)
293 /* lprintf(9, "begin_critical_section(%d)\n", which_one); */
294 /* ensure nobody ever tries to do a critical section within a
295 transaction; this could lead to deadlock. */
297 pthread_mutex_lock(&Critters[which_one]);
301 * Release a semaphore lock to end a critical section.
303 void end_critical_section(int which_one)
305 /* lprintf(9, "end_critical_section(%d)\n", which_one); */
306 pthread_mutex_unlock(&Critters[which_one]);
312 * This is a generic function to set up a master socket for listening on
313 * a TCP port. The server shuts down if the bind fails.
316 int ig_tcp_server(int port_number, int queue_len)
318 struct sockaddr_in sin;
320 int actual_queue_len;
322 actual_queue_len = queue_len;
323 if (actual_queue_len < 5) actual_queue_len = 5;
325 memset(&sin, 0, sizeof(sin));
326 sin.sin_family = AF_INET;
327 sin.sin_addr.s_addr = INADDR_ANY;
328 sin.sin_port = htons((u_short)port_number);
330 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
333 lprintf(1, "citserver: Can't create a socket: %s\n",
339 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
341 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
342 lprintf(1, "citserver: Can't bind: %s\n",
348 if (listen(s, actual_queue_len) < 0) {
349 lprintf(1, "citserver: Can't listen: %s\n", strerror(errno));
360 * Create a Unix domain socket and listen on it
362 int ig_uds_server(char *sockpath, int queue_len)
364 struct sockaddr_un addr;
367 int actual_queue_len;
369 actual_queue_len = queue_len;
370 if (actual_queue_len < 5) actual_queue_len = 5;
372 i = unlink(sockpath);
373 if (i != 0) if (errno != ENOENT) {
374 lprintf(1, "citserver: can't unlink %s: %s\n",
375 sockpath, strerror(errno));
379 memset(&addr, 0, sizeof(addr));
380 addr.sun_family = AF_UNIX;
381 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
383 s = socket(AF_UNIX, SOCK_STREAM, 0);
385 lprintf(1, "citserver: Can't create a socket: %s\n",
390 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
391 lprintf(1, "citserver: Can't bind: %s\n",
396 if (listen(s, actual_queue_len) < 0) {
397 lprintf(1, "citserver: Can't listen: %s\n", strerror(errno));
401 chmod(sockpath, 0777);
408 * Return a pointer to the CitContext structure bound to the thread which
409 * called this function. If there's no such binding (for example, if it's
410 * called by the housekeeper thread) then a generic 'master' CC is returned.
412 * It's inlined because it's used *VERY* frequently.
414 inline struct CitContext *MyContext(void) {
415 return ((pthread_getspecific(MyConKey) == NULL)
417 : (struct CitContext *) pthread_getspecific(MyConKey)
423 * Initialize a new context and place it in the list. The session number
424 * used to be the PID (which is why it's called cs_pid), but that was when we
425 * had one process per session. Now we just assign them sequentially, starting
426 * at 1 (don't change it to 0 because masterCC uses 0) and re-using them when
427 * sessions terminate.
429 struct CitContext *CreateNewContext(void) {
430 struct CitContext *me, *ptr;
432 me = (struct CitContext *) mallok(sizeof(struct CitContext));
434 lprintf(1, "citserver: can't allocate memory!!\n");
437 memset(me, 0, sizeof(struct CitContext));
439 /* The new context will be created already in the CON_EXECUTING state
440 * in order to prevent another thread from grabbing it while it's
443 me->state = CON_EXECUTING;
447 * Generate a unique session number and insert this context into
450 begin_critical_section(S_SESSION_TABLE);
452 if (ContextList == NULL) {
458 else if (ContextList->cs_pid > 1) {
459 me->next = ContextList;
465 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
466 if (ptr->next == NULL) {
468 me->cs_pid = ptr->cs_pid + 1;
472 else if (ptr->next->cs_pid > (ptr->cs_pid+1)) {
473 me->next = ptr->next;
475 me->cs_pid = ptr->cs_pid + 1;
481 DONE: ++num_sessions;
482 end_critical_section(S_SESSION_TABLE);
488 * client_write() ... Send binary data to the client.
490 void client_write(char *buf, int nbytes)
492 int bytes_written = 0;
496 if (CC->redirect_fp != NULL) {
497 fwrite(buf, nbytes, 1, CC->redirect_fp);
501 if (CC->redirect_sock > 0) {
502 sock = CC->redirect_sock; /* and continue below... */
505 sock = CC->client_socket;
509 if (CC->redirect_ssl) {
510 client_write_ssl(buf, nbytes);
515 while (bytes_written < nbytes) {
516 retval = write(sock, &buf[bytes_written],
517 nbytes - bytes_written);
519 lprintf(2, "client_write() failed: %s\n",
521 if (sock == CC->client_socket) CC->kill_me = 1;
524 bytes_written = bytes_written + retval;
530 * cprintf() ... Send formatted printable data to the client. It is
531 * implemented in terms of client_write() but remains in
532 * sysdep.c in case we port to somewhere without va_args...
534 void cprintf(const char *format, ...) {
538 va_start(arg_ptr, format);
539 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
540 buf[sizeof buf - 2] = '\n';
541 client_write(buf, strlen(buf));
547 * Read data from the client socket.
549 * 1 Requested number of bytes has been read.
550 * 0 Request timed out.
551 * -1 The socket is broken.
552 * If the socket breaks, the session will be terminated.
554 int client_read_to(char *buf, int bytes, int timeout)
562 if (CC->redirect_ssl) {
563 return (client_read_ssl(buf, bytes, timeout));
569 FD_SET(CC->client_socket, &rfds);
573 retval = select( (CC->client_socket)+1,
574 &rfds, NULL, NULL, &tv);
576 if (FD_ISSET(CC->client_socket, &rfds) == 0) {
580 rlen = read(CC->client_socket, &buf[len], bytes-len);
582 lprintf(2, "client_read() failed: %s\n",
593 * Read data from the client socket with default timeout.
594 * (This is implemented in terms of client_read_to() and could be
595 * justifiably moved out of sysdep.c)
597 inline int client_read(char *buf, int bytes)
599 return(client_read_to(buf, bytes, config.c_sleeping));
604 * client_gets() ... Get a LF-terminated line of text from the client.
605 * (This is implemented in terms of client_read() and could be
606 * justifiably moved out of sysdep.c)
608 int client_gets(char *buf)
612 /* Read one character at a time.
615 retval = client_read(&buf[i], 1);
616 if (retval != 1 || buf[i] == '\n' || i == (SIZ-1))
620 /* If we got a long line, discard characters until the newline.
623 while (buf[i] != '\n' && retval == 1)
624 retval = client_read(&buf[i], 1);
626 /* Strip the trailing newline and any trailing nonprintables (cr's)
629 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
630 buf[strlen(buf)-1] = 0;
631 if (retval < 0) strcpy(buf, "000");
638 * The system-dependent part of master_cleanup() - close the master socket.
640 void sysdep_master_cleanup(void) {
641 struct ServiceFunctionHook *serviceptr;
644 * close all protocol master sockets
646 for (serviceptr = ServiceHookTable; serviceptr != NULL;
647 serviceptr = serviceptr->next ) {
649 if (serviceptr->tcp_port > 0)
650 lprintf(3, "Closing listener on port %d\n",
651 serviceptr->tcp_port);
653 if (serviceptr->sockpath != NULL)
654 lprintf(3, "Closing listener on '%s'\n",
655 serviceptr->sockpath);
657 close(serviceptr->msock);
659 /* If it's a Unix domain socket, remove the file. */
660 if (serviceptr->sockpath != NULL) {
661 unlink(serviceptr->sockpath);
668 * Terminate another session.
669 * (This could justifiably be moved out of sysdep.c because it
670 * no longer does anything that is system-dependent.)
672 void kill_session(int session_to_kill) {
673 struct CitContext *ptr;
675 begin_critical_section(S_SESSION_TABLE);
676 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
677 if (ptr->cs_pid == session_to_kill) {
681 end_critical_section(S_SESSION_TABLE);
688 * Start running as a daemon. Only close stdio if do_close_stdio is set.
690 void start_daemon(int do_close_stdio) {
691 if (do_close_stdio) {
696 signal(SIGHUP,SIG_IGN);
697 signal(SIGINT,SIG_IGN);
698 signal(SIGQUIT,SIG_IGN);
699 if (fork()!=0) exit(0);
705 * Generic routine to convert a login name to a full name (gecos)
706 * Returns nonzero if a conversion took place
708 int convert_login(char NameToConvert[]) {
712 pw = getpwnam(NameToConvert);
717 strcpy(NameToConvert, pw->pw_gecos);
718 for (a=0; a<strlen(NameToConvert); ++a) {
719 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
725 struct worker_node *worker_list = NULL;
729 * create a worker thread. this function must always be called from within
730 * an S_WORKER_LIST critical section!
732 void create_worker(void) {
734 struct worker_node *n;
737 n = mallok(sizeof(struct worker_node));
739 lprintf(1, "can't allocate worker_node, exiting\n");
744 if ((ret = pthread_attr_init(&attr))) {
745 lprintf(1, "pthread_attr_init: %s\n", strerror(ret));
750 /* we seem to need something bigger than FreeBSD's default 64k stack */
752 if ((ret = pthread_attr_setstacksize(&attr, 128 * 1024))) {
753 lprintf(1, "pthread_attr_setstacksize: %s\n", strerror(ret));
758 if ((ret = pthread_create(&n->tid, &attr, worker_thread, NULL) != 0))
761 lprintf(1, "Can't create worker thread: %s\n",
765 n->next = worker_list;
772 * Purge all sessions which have the 'kill_me' flag set.
773 * This function has code to prevent it from running more than once every
774 * few seconds, because running it after every single unbind would waste a lot
775 * of CPU time and keep the context list locked too much.
777 * After that's done, we raise or lower the size of the worker thread pool
778 * if such an action is appropriate.
780 void dead_session_purge(void) {
781 struct CitContext *ptr, *rem;
782 struct worker_node **node, *tmp;
785 if ( (time(NULL) - last_purge) < 5 ) return; /* Too soon, go away */
790 begin_critical_section(S_SESSION_TABLE);
791 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
792 if ( (ptr->state == CON_IDLE) && (ptr->kill_me) ) {
796 end_critical_section(S_SESSION_TABLE);
798 /* RemoveContext() enters its own S_SESSION_TABLE critical
799 * section, so we have to do it like this.
802 lprintf(9, "Purging session %d\n", rem->cs_pid);
806 } while (rem != NULL);
809 /* Raise or lower the size of the worker thread pool if such
810 * an action is appropriate.
813 self = pthread_self();
815 if ( (num_sessions > num_threads)
816 && (num_threads < config.c_max_workers) ) {
817 begin_critical_section(S_WORKER_LIST);
819 end_critical_section(S_WORKER_LIST);
822 /* don't let the initial thread die since it's responsible for
823 waiting for all the other threads to terminate. */
824 else if ( (num_sessions < num_threads)
825 && (num_threads > config.c_min_workers)
826 && (self != initial_thread) ) {
828 begin_critical_section(S_WORKER_LIST);
831 /* we're exiting before server shutdown... unlink ourself from
832 the worker list and detach our thread to avoid memory leaks
835 for (node = &worker_list; *node != NULL; node = &(*node)->next)
836 if ((*node)->tid == self) {
838 *node = (*node)->next;
843 pthread_detach(self);
844 end_critical_section(S_WORKER_LIST);
855 * Redirect a session's output to a file or socket.
856 * This function may be called with a file handle *or* a socket (but not
857 * both). Call with neither to return output to its normal client socket.
859 void CtdlRedirectOutput(FILE *fp, int sock) {
861 if (fp != NULL) CC->redirect_fp = fp;
862 else CC->redirect_fp = NULL;
864 if (sock > 0) CC->redirect_sock = sock;
865 else CC->redirect_sock = (-1);
871 * masterCC is the context we use when not attached to a session. This
872 * function initializes it.
874 void InitializeMasterCC(void) {
875 memset(&masterCC, 0, sizeof(struct CitContext));
876 masterCC.internal_pgm = 1;
883 * Set up a fd_set containing all the master sockets to which we
884 * always listen. It's computationally less expensive to just copy
885 * this to a local fd_set when starting a new select() and then add
886 * the client sockets than it is to initialize a new one and then
887 * figure out what to put there.
889 void init_master_fdset(void) {
890 struct ServiceFunctionHook *serviceptr;
893 lprintf(9, "Initializing master fdset\n");
898 lprintf(9, "Will listen on rescan pipe %d\n", rescan[0]);
899 FD_SET(rescan[0], &masterfds);
900 if (rescan[0] > masterhighest) masterhighest = rescan[0];
902 for (serviceptr = ServiceHookTable; serviceptr != NULL;
903 serviceptr = serviceptr->next ) {
904 m = serviceptr->msock;
905 lprintf(9, "Will listen on master socket %d\n", m);
906 FD_SET(m, &masterfds);
907 if (m > masterhighest) {
911 lprintf(9, "masterhighest = %d\n", masterhighest);
916 * Bind a thread to a context. (It's inline merely to speed things up.)
918 INLINE void become_session(struct CitContext *which_con) {
919 pthread_setspecific(MyConKey, (void *)which_con );
925 * This loop just keeps going and going and going...
927 void *worker_thread(void *arg) {
931 struct CitContext *ptr;
932 struct CitContext *bind_me = NULL;
935 struct CitContext *con= NULL; /* Temporary context pointer */
936 struct ServiceFunctionHook *serviceptr;
937 int ssock; /* Descriptor for client socket */
944 while (!time_to_die) {
947 * A naive implementation would have all idle threads
948 * calling select() and then they'd all wake up at once. We
949 * solve this problem by putting the select() in a critical
950 * section, so only one thread has the opportunity to wake
951 * up. If we wake up on a master socket, create a new
952 * session context; otherwise, just bind the thread to the
953 * context we want and go on our merry way.
956 /* make doubly sure we're not holding any stale db handles
957 * which might cause a deadlock.
961 begin_critical_section(S_I_WANNA_SELECT);
962 SETUP_FD: memcpy(&readfds, &masterfds, sizeof masterfds);
963 highest = masterhighest;
964 begin_critical_section(S_SESSION_TABLE);
965 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
966 if (ptr->state == CON_IDLE) {
967 FD_SET(ptr->client_socket, &readfds);
968 if (ptr->client_socket > highest)
969 highest = ptr->client_socket;
972 end_critical_section(S_SESSION_TABLE);
974 tv.tv_sec = 1; /* wake up every second if no input */
979 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
981 end_critical_section(S_I_WANNA_SELECT);
985 /* Now figure out who made this select() unblock.
986 * First, check for an error or exit condition.
989 if (errno != EINTR) {
990 lprintf(9, "Exiting (%s)\n", strerror(errno));
992 } else if (!time_to_die)
996 /* Next, check to see if it's a new client connecting
997 * on a master socket.
999 else for (serviceptr = ServiceHookTable; serviceptr != NULL;
1000 serviceptr = serviceptr->next ) {
1002 if (FD_ISSET(serviceptr->msock, &readfds)) {
1003 ssock = accept(serviceptr->msock, NULL, 0);
1005 lprintf(2, "citserver: accept(): %s\n",
1009 lprintf(7, "citserver: "
1010 "New client socket %d\n",
1013 /* New context will be created already
1014 * set up in the CON_EXECUTING state.
1016 con = CreateNewContext();
1018 /* Assign new socket number to it. */
1019 con->client_socket = ssock;
1020 con->h_command_function =
1021 serviceptr->h_command_function;
1023 /* Determine whether local socket */
1024 if (serviceptr->sockpath != NULL)
1025 con->is_local_socket = 1;
1027 /* Set the SO_REUSEADDR socket option */
1029 setsockopt(ssock, SOL_SOCKET,
1033 become_session(con);
1035 serviceptr->h_greeting_function();
1036 become_session(NULL);
1037 con->state = CON_IDLE;
1043 /* If the rescan pipe went active, someone is telling this
1044 * thread that the &readfds needs to be refreshed with more
1048 end_critical_section(S_I_WANNA_SELECT);
1052 if (FD_ISSET(rescan[0], &readfds)) {
1053 read(rescan[0], &junk, 1);
1057 /* It must be a client socket. Find a context that has data
1058 * waiting on its socket *and* is in the CON_IDLE state.
1062 begin_critical_section(S_SESSION_TABLE);
1063 for (ptr = ContextList;
1064 ( (ptr != NULL) && (bind_me == NULL) );
1066 if ( (FD_ISSET(ptr->client_socket, &readfds))
1067 && (ptr->state == CON_IDLE) ) {
1071 if (bind_me != NULL) {
1072 /* Found one. Stake a claim to it before
1073 * letting anyone else touch the context list.
1075 bind_me->state = CON_EXECUTING;
1078 end_critical_section(S_SESSION_TABLE);
1079 end_critical_section(S_I_WANNA_SELECT);
1081 /* We're bound to a session, now do *one* command */
1082 if (bind_me != NULL) {
1083 become_session(bind_me);
1084 CC->h_command_function();
1085 become_session(NULL);
1086 bind_me->state = CON_IDLE;
1087 if (bind_me->kill_me == 1) {
1088 RemoveContext(bind_me);
1090 write(rescan[1], &junk, 1);
1094 dead_session_purge();
1096 check_sched_shutdown();
1099 /* If control reaches this point, the server is shutting down */