2 * Citadel/UX "system dependent" stuff.
3 * See copyright.txt for copyright information.
7 * Here's where we (hopefully) have all the parts of the Citadel server that
8 * would need to be altered to run the server in a non-POSIX environment.
9 * Wherever possible, we use function wrappers and type definitions to create
10 * abstractions that are platform-independent from the calling side.
12 * Eventually we'll try porting to a different platform and either have
13 * multiple variants of this file or simply load it up with #ifdefs.
24 #include <sys/types.h>
26 #include <sys/socket.h>
29 #include <netinet/in.h>
41 #include "sysdep_decls.h"
42 #include "citserver.h"
46 #include "housekeeping.h"
47 #include "dynloader.h"
50 #ifdef HAVE_SYS_SELECT_H
51 #include <sys/select.h>
58 #ifdef DEBUG_MEMORY_LEAKS
59 struct TheHeap *heap = NULL;
62 pthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */
63 pthread_key_t MyConKey; /* TSD key for MyContext() */
65 int msock; /* master listening socket */
66 int verbosity = 3; /* Logging level */
68 struct CitContext masterCC;
72 * lprintf() ... Write logging information
74 void lprintf(int loglevel, const char *format, ...) {
78 va_start(arg_ptr, format);
79 vsprintf(buf, format, arg_ptr);
82 if (loglevel <= verbosity) {
83 fprintf(stderr, "%s", buf);
87 PerformLogHooks(loglevel, buf);
92 #ifdef DEBUG_MEMORY_LEAKS
93 void *tracked_malloc(size_t tsize, char *tfile, int tline) {
98 if (ptr == NULL) return(NULL);
100 hptr = (struct TheHeap *) malloc(sizeof(struct TheHeap));
101 strcpy(hptr->h_file, tfile);
102 hptr->h_line = tline;
110 void tracked_free(void *ptr) {
111 struct TheHeap *hptr, *freeme;
113 if (heap->h_ptr == ptr) {
119 for (hptr=heap; hptr->next!=NULL; hptr=hptr->next) {
120 if (hptr->next->h_ptr == ptr) {
122 hptr->next = hptr->next->next;
131 void *tracked_realloc(void *ptr, size_t size) {
133 struct TheHeap *hptr;
135 newptr = realloc(ptr, size);
137 for (hptr=heap; hptr!=NULL; hptr=hptr->next) {
138 if (hptr->h_ptr == ptr) hptr->h_ptr = newptr;
145 void dump_tracked() {
146 struct TheHeap *hptr;
148 cprintf("%d Here's what's allocated...\n", LISTING_FOLLOWS);
149 for (hptr=heap; hptr!=NULL; hptr=hptr->next) {
150 cprintf("%20s %5d\n",
151 hptr->h_file, hptr->h_line);
157 #ifndef HAVE_PTHREAD_CANCEL
159 * signal handler to fake thread cancellation; only required on BSDI as far
163 static pthread_t main_thread_id;
165 static RETSIGTYPE cancel_thread(int signum) {
171 * we used to use master_cleanup() as a signal handler to shut down the server.
172 * however, master_cleanup() and the functions it calls do some things that
173 * aren't such a good idea to do from a signal handler: acquiring mutexes,
174 * playing with signal masks on BSDI systems, etc. so instead we install the
175 * following signal handler to set a global variable to inform the main loop
176 * that it's time to call master_cleanup() and exit.
179 static volatile int time_to_die = 0;
181 static RETSIGTYPE signal_cleanup(int signum) {
187 * Some initialization stuff...
189 void init_sysdep(void) {
192 /* Set up a bunch of semaphores to be used for critical sections */
193 for (a=0; a<MAX_SEMAPHORES; ++a) {
194 pthread_mutex_init(&Critters[a], NULL);
198 * Set up a place to put thread-specific data.
199 * We only need a single pointer per thread - it points to the
200 * thread's CitContext structure in the ContextList linked list.
202 if (pthread_key_create(&MyConKey, NULL) != 0) {
203 lprintf(1, "Can't create TSD key!! %s\n", strerror(errno));
207 * The action for unexpected signals and exceptions should be to
208 * call signal_cleanup() to gracefully shut down the server.
210 signal(SIGINT, signal_cleanup);
211 signal(SIGQUIT, signal_cleanup);
212 signal(SIGHUP, signal_cleanup);
213 signal(SIGTERM, signal_cleanup);
214 signal(SIGPIPE, SIG_IGN);
215 #ifndef HAVE_PTHREAD_CANCEL /* fake it - only BSDI afaik */
216 main_thread_id = pthread_self();
217 signal(SIGUSR1, cancel_thread);
223 * Obtain a semaphore lock to begin a critical section.
225 void begin_critical_section(int which_one)
227 #ifdef HAVE_PTHREAD_CANCEL
233 /* lprintf(8, "begin_critical_section(%d)\n", which_one); */
235 #ifdef HAVE_PTHREAD_CANCEL
236 /* Don't get interrupted during the critical section */
237 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldval);
239 /* We're faking cancellation with signals. Block SIGUSR1 while we're in
240 * the critical section. */
241 if (!pthread_equal(pthread_self(), main_thread_id)) {
243 sigaddset(&set, SIGUSR1);
244 pthread_sigmask(SIG_BLOCK, &set, NULL);
248 /* Obtain a semaphore */
249 pthread_mutex_lock(&Critters[which_one]);
254 * Release a semaphore lock to end a critical section.
256 void end_critical_section(int which_one)
258 #ifdef HAVE_PTHREAD_CANCEL
264 /* lprintf(8, " end_critical_section(%d)\n", which_one); */
266 /* Let go of the semaphore */
267 pthread_mutex_unlock(&Critters[which_one]);
269 #ifdef HAVE_PTHREAD_CANCEL
270 /* If a cancel was sent during the critical section, do it now.
271 * Then re-enable thread cancellation.
273 pthread_testcancel();
274 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldval);
275 pthread_testcancel();
277 /* We're faking it. Unblock SIGUSR1; signals sent during the critical
278 * section should now be able to kill us. */
279 if (!pthread_equal(pthread_self(), main_thread_id)) {
281 sigaddset(&set, SIGUSR1);
282 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
291 * This is a generic function to set up a master socket for listening on
292 * a TCP port. The server shuts down if the bind fails.
294 int ig_tcp_server(int port_number, int queue_len)
296 struct sockaddr_in sin;
299 memset(&sin, 0, sizeof(sin));
300 sin.sin_family = AF_INET;
301 sin.sin_addr.s_addr = INADDR_ANY;
303 if (port_number == 0) {
305 "citserver: No port number specified. Run setup.\n");
309 sin.sin_port = htons((u_short)port_number);
311 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
313 lprintf(1, "citserver: Can't create a socket: %s\n",
318 /* Set the SO_REUSEADDR socket option, because it makes sense. */
320 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
322 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
323 lprintf(1, "citserver: Can't bind: %s\n", strerror(errno));
327 if (listen(s, queue_len) < 0) {
328 lprintf(1, "citserver: Can't listen: %s\n", strerror(errno));
337 * Return a pointer to a thread's own CitContext structure (old)
338 * NOTE: this version of MyContext() is commented out because it is no longer
339 * in use. It was written before I discovered TSD keys. This
340 * version pounds through the context list until it finds the one matching
341 * the currently running thread. It remains here, commented out, in case it
342 * is needed for future ports to threading libraries which have the equivalent
343 * of pthread_self() but not pthread_key_create() and its ilk.
345 * struct CitContext *MyContext() {
346 * struct CitContext *ptr;
349 * me = pthread_self();
350 * for (ptr=ContextList; ptr!=NULL; ptr=ptr->next) {
351 * if (ptr->mythread == me) return(ptr);
358 * Return a pointer to a thread's own CitContext structure (new)
360 struct CitContext *MyContext(void) {
361 struct CitContext *retCC;
362 retCC = (struct CitContext *) pthread_getspecific(MyConKey);
363 if (retCC == NULL) retCC = &masterCC;
369 * Wedge our way into the context list.
371 struct CitContext *CreateNewContext(void) {
372 struct CitContext *me;
374 lprintf(9, "CreateNewContext: calling malloc()\n");
375 me = (struct CitContext *) mallok(sizeof(struct CitContext));
377 lprintf(1, "citserver: can't allocate memory!!\n");
380 memset(me, 0, sizeof(struct CitContext));
382 begin_critical_section(S_SESSION_TABLE);
383 me->next = ContextList;
385 end_critical_section(S_SESSION_TABLE);
390 * Add a thread's thread ID to the context
392 void InitMyContext(struct CitContext *con)
394 #ifdef HAVE_PTHREAD_CANCEL
398 con->mythread = pthread_self();
399 #ifdef HAVE_PTHREAD_CANCEL
400 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldval);
401 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldval);
403 if (pthread_setspecific(MyConKey, (void *)con) != 0) {
404 lprintf(1, "ERROR! pthread_setspecific() failed: %s\n",
410 * Remove a context from the context list.
412 void RemoveContext(struct CitContext *con)
414 struct CitContext *ptr;
416 lprintf(7, "Starting RemoveContext()\n");
417 lprintf(9, "Session count before RemoveContext is %d\n",
420 lprintf(7, "WARNING: RemoveContext() called with null!\n");
425 * session_count() starts its own S_SESSION_TABLE critical section;
426 * so do not call it from within this loop.
428 begin_critical_section(S_SESSION_TABLE);
429 lprintf(7, "Closing socket %d\n", con->client_socket);
430 close(con->client_socket);
432 lprintf(9, "Dereferencing session context\n");
433 if (ContextList==con) {
434 ContextList = ContextList->next;
437 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
438 if (ptr->next == con) {
439 ptr->next = con->next;
444 lprintf(9, "Freeing session context...\n");
446 lprintf(9, "...done.\n");
447 end_critical_section(S_SESSION_TABLE);
449 lprintf(9, "Session count after RemoveContext is %d\n",
452 lprintf(7, "Done with RemoveContext\n");
457 * Return the number of sessions currently running.
458 * (This should probably be moved out of sysdep.c)
460 int session_count(void) {
461 struct CitContext *ptr;
464 lprintf(9, "session_count() starting\n");
465 begin_critical_section(S_SESSION_TABLE);
466 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
468 lprintf(9, "Counted session %3d (%d)\n", ptr->cs_pid, TheCount);
470 end_critical_section(S_SESSION_TABLE);
472 lprintf(9, "session_count() finishing\n");
478 * client_write() ... Send binary data to the client.
480 void client_write(char *buf, int nbytes)
482 int bytes_written = 0;
484 while (bytes_written < nbytes) {
485 retval = write(CC->client_socket, &buf[bytes_written],
486 nbytes - bytes_written);
488 lprintf(2, "client_write() failed: %s\n",
492 bytes_written = bytes_written + retval;
498 * cprintf() ... Send formatted printable data to the client. It is
499 * implemented in terms of client_write() but remains in
500 * sysdep.c in case we port to somewhere without va_args...
502 void cprintf(const char *format, ...) {
506 va_start(arg_ptr, format);
507 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
508 buf[sizeof buf - 2] = '\n';
509 client_write(buf, strlen(buf));
515 * Read data from the client socket.
517 * 1 Requested number of bytes has been read.
518 * 0 Request timed out.
519 * If the socket breaks, the session is immediately terminated.
521 int client_read_to(char *buf, int bytes, int timeout)
531 FD_SET(CC->client_socket, &rfds);
535 retval = select( (CC->client_socket)+1,
536 &rfds, NULL, NULL, &tv);
537 if (FD_ISSET(CC->client_socket, &rfds) == 0) {
541 rlen = read(CC->client_socket, &buf[len], bytes-len);
543 lprintf(2, "client_read() failed: %s\n",
553 * Read data from the client socket with default timeout.
554 * (This is implemented in terms of client_read_to() and could be
555 * justifiably moved out of sysdep.c)
557 int client_read(char *buf, int bytes)
559 return(client_read_to(buf, bytes, config.c_sleeping));
564 * client_gets() ... Get a LF-terminated line of text from the client.
565 * (This is implemented in terms of client_read() and could be
566 * justifiably moved out of sysdep.c)
568 int client_gets(char *buf)
572 /* Read one character at a time.
575 retval = client_read(&buf[i], 1);
576 if (retval != 1 || buf[i] == '\n' || i == 255)
580 /* If we got a long line, discard characters until the newline.
583 while (buf[i] != '\n' && retval == 1)
584 retval = client_read(&buf[i], 1);
586 /* Strip the trailing newline and any trailing nonprintables (cr's)
589 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
590 buf[strlen(buf)-1] = 0;
597 * The system-dependent part of master_cleanup() - close the master socket.
599 void sysdep_master_cleanup(void) {
600 lprintf(7, "Closing master socket %d\n", msock);
605 * Cleanup routine to be called when one thread is shutting down.
607 void cleanup(int exit_code)
609 /* Terminate the thread.
610 * Its cleanup handler will call cleanup_stuff()
612 lprintf(7, "Calling pthread_exit()\n");
617 * Terminate another session.
619 void kill_session(int session_to_kill) {
620 struct CitContext *ptr;
623 lprintf(9, "kill_session() scanning for thread to cancel...\n");
624 begin_critical_section(S_SESSION_TABLE);
625 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
626 if (ptr->cs_pid == session_to_kill) {
627 killme = ptr->mythread;
630 end_critical_section(S_SESSION_TABLE);
631 lprintf(9, "kill_session() finished scanning.\n");
634 #ifdef HAVE_PTHREAD_CANCEL
635 lprintf(9, "calling pthread_cancel()\n");
636 pthread_cancel(killme);
638 pthread_kill(killme, SIGUSR1);
645 * The system-dependent wrapper around the main context loop.
647 void *sd_context_loop(struct CitContext *con) {
648 pthread_cleanup_push(*cleanup_stuff, NULL);
650 pthread_cleanup_pop(0);
656 * Start running as a daemon. Only close stdio if do_close_stdio is set.
658 void start_daemon(int do_close_stdio) {
659 if (do_close_stdio) {
664 signal(SIGHUP,SIG_IGN);
665 signal(SIGINT,SIG_IGN);
666 signal(SIGQUIT,SIG_IGN);
667 if (fork()!=0) exit(0);
673 * Tie in to the 'netsetup' program.
675 * (We're going to hope that netsetup never feeds more than 4096 bytes back.)
677 void cmd_nset(char *cmdbuf)
684 char netsetup_args[3][256];
686 if (CC->usersupp.axlevel < 6) {
687 cprintf("%d Higher access required.\n",
688 ERROR + HIGHER_ACCESS_REQUIRED);
692 for (a=1; a<=3; ++a) {
693 if (num_parms(cmdbuf) >= a) {
694 extract(netsetup_args[a-1], cmdbuf, a-1);
695 for (b=0; b<strlen(netsetup_args[a-1]); ++b) {
696 if (netsetup_args[a-1][b] == 34) {
697 netsetup_args[a-1][b] = '_';
702 netsetup_args[a-1][0] = 0;
706 sprintf(fbuf, "./netsetup \"%s\" \"%s\" \"%s\" </dev/null 2>&1",
707 netsetup_args[0], netsetup_args[1], netsetup_args[2]);
708 netsetup = popen(fbuf, "r");
709 if (netsetup == NULL) {
710 cprintf("%d %s\n", ERROR, strerror(errno));
715 while (ch = getc(netsetup), (ch > 0)) {
716 fbuf[strlen(fbuf)+1] = 0;
717 fbuf[strlen(fbuf)] = ch;
720 retcode = pclose(netsetup);
723 for (a=0; a<strlen(fbuf); ++a) {
724 if (fbuf[a] < 32) fbuf[a] = 32;
727 cprintf("%d %s\n", ERROR, fbuf);
731 cprintf("%d Command succeeded. Output follows:\n", LISTING_FOLLOWS);
733 if (fbuf[strlen(fbuf)-1] != 10) cprintf("\n");
740 * Generic routine to convert a login name to a full name (gecos)
741 * Returns nonzero if a conversion took place
743 int convert_login(char NameToConvert[]) {
747 pw = getpwnam(NameToConvert);
752 strcpy(NameToConvert, pw->pw_gecos);
753 for (a=0; a<strlen(NameToConvert); ++a) {
754 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
766 * Here's where it all begins.
768 int main(int argc, char **argv)
770 struct sockaddr_in fsin; /* Data for master socket */
771 int alen; /* Data for master socket */
772 int ssock; /* Descriptor for master socket */
773 THREAD SessThread; /* Thread descriptor */
774 pthread_attr_t attr; /* Thread attributes */
775 struct CitContext *con; /* Temporary context pointer */
776 char tracefile[128]; /* Name of file to log traces to */
777 int a, i; /* General-purpose variables */
782 int drop_root_perms = 1;
784 /* specify default port name and trace file */
785 strcpy(tracefile, "");
787 /* parse command-line arguments */
788 for (a=1; a<argc; ++a) {
790 /* -t specifies where to log trace messages to */
791 if (!strncmp(argv[a], "-t", 2)) {
792 strcpy(tracefile, argv[a]);
793 strcpy(tracefile, &tracefile[2]);
794 freopen(tracefile, "r", stdin);
795 freopen(tracefile, "w", stdout);
796 freopen(tracefile, "w", stderr);
799 /* run in the background if -d was specified */
800 else if (!strcmp(argv[a], "-d")) {
801 start_daemon( (strlen(tracefile) > 0) ? 0 : 1 ) ;
804 /* -x specifies the desired logging level */
805 else if (!strncmp(argv[a], "-x", 2)) {
806 strcpy(convbuf, argv[a]);
807 verbosity = atoi(&convbuf[2]);
810 else if (!strncmp(argv[a], "-h", 2)) {
811 strcpy(convbuf, argv[a]);
812 strcpy(bbs_home_directory, &convbuf[2]);
816 /* -r tells the server not to drop root permissions. don't use
817 * this unless you know what you're doing. this should be
818 * removed in the next release if it proves unnecessary. */
819 else if (!strcmp(argv[a], "-r"))
822 /* any other parameter makes it crash and burn */
824 lprintf(1, "citserver: usage: ");
825 lprintf(1, "citserver [-tTraceFile]");
826 lprintf(1, " [-d] [-xLogLevel] [-hHomeDir]\n");
832 /* Tell 'em who's in da house */
833 lprintf(1, "Multithreaded message server for %s\n", CITADEL);
834 lprintf(1, "Copyright (C) 1987-1999 by Art Cancro. ");
835 lprintf(1, "All rights reserved.\n\n");
839 openlog("citserver",LOG_PID,LOG_USER);
840 /* Load site-specific parameters */
841 lprintf(7, "Loading citadel.config\n");
845 * Bind the server to our favourite port.
846 * There is no need to check for errors, because ig_tcp_server()
847 * exits if it doesn't succeed.
849 lprintf(7, "Attempting to bind to port %d...\n", config.c_port_number);
850 msock = ig_tcp_server(config.c_port_number, 5);
851 lprintf(7, "Listening on socket %d\n", msock);
854 * Now that we've bound the socket, change to the BBS user id and its
855 * corresponding group id
857 if (drop_root_perms) {
858 if ((pw = getpwuid(BBSUID)) == NULL)
859 lprintf(1, "getpwuid(%d): %s\n", BBSUID,
861 else if (setgid(pw->pw_gid))
862 lprintf(3, "setgid(%d): %s\n", pw->pw_gid,
864 lprintf(7, "Changing uid to %d\n", BBSUID);
865 if (setuid(BBSUID) != 0) {
866 lprintf(3, "setuid() failed: %s\n", strerror(errno));
870 lprintf(7, "Initializing loadable modules\n");
871 DLoader_Init("./modules");
872 lprintf(9, "Modules done initializing.\n");
875 * Do non system dependent startup functions.
880 * Endless loop. Listen on the master socket. When a connection
881 * comes in, create a socket, a context, and a thread.
883 while (!time_to_die) {
884 /* we need to check if a signal has been delivered. because
885 * syscalls may be restartable across signals, we call
886 * select with a timeout of 1 second and repeatedly check for
889 FD_SET(msock, &readfds);
892 if (select(msock + 1, &readfds, NULL, NULL, &tv) <= 0)
895 ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
897 lprintf(2, "citserver: accept() failed: %s\n",
901 lprintf(7, "citserver: Client socket %d\n", ssock);
902 lprintf(9, "creating context\n");
903 con = CreateNewContext();
904 con->client_socket = ssock;
906 /* Set the SO_REUSEADDR socket option */
907 lprintf(9, "setting socket options\n");
909 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
912 /* set attributes for the new thread */
913 lprintf(9, "setting thread attributes\n");
914 pthread_attr_init(&attr);
915 pthread_attr_setdetachstate(&attr,
916 PTHREAD_CREATE_DETACHED);
918 /* now create the thread */
919 lprintf(9, "creating thread\n");
920 if (pthread_create(&SessThread, &attr,
921 (void* (*)(void*)) sd_context_loop,
925 "citserver: can't create thread: %s\n",
930 * (defunct -- now done at thread creation time)
931 * if (pthread_detach(&SessThread) != 0) {
933 * "citserver: can't detach thread: %s\n",
937 lprintf(9, "done!\n");