]> code.citadel.org Git - citadel.git/blobdiff - citadel/sysdep.c
* Changed the comments at the beginning of each file to a consistent format
[citadel.git] / citadel / sysdep.c
index 5c613031302115215174eecb1c8d4a761a31afa0..b4d46c2e22fc0a6d207752e9218a8eccc7ac41c4 100644 (file)
@@ -1,14 +1,15 @@
 /*
+ * $Id$
+ *
  * Citadel/UX "system dependent" stuff.
  * See copyright.txt for copyright information.
  *
- * $Id$
- *
- * Here's where we (hopefully) have all the parts of the Citadel server that
+ * Here's where we (hopefully) have most parts of the Citadel server that
  * would need to be altered to run the server in a non-POSIX environment.
  * 
  * Eventually we'll try porting to a different platform and either have
  * multiple variants of this file or simply load it up with #ifdefs.
+ *
  */
 
 
 #include <ctype.h>
 #include <signal.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <sys/wait.h>
 #include <sys/socket.h>
 #include <sys/time.h>
 #include <limits.h>
 #include <netinet/in.h>
 #include <netdb.h>
+#include <sys/un.h>
 #include <string.h>
 #include <pwd.h>
 #include <errno.h>
@@ -241,6 +244,7 @@ void init_sysdep(void) {
  */
 void begin_critical_section(int which_one)
 {
+       /* lprintf(9, "begin_critical_section(%d)\n", which_one); */
        pthread_mutex_lock(&Critters[which_one]);
 }
 
@@ -249,6 +253,7 @@ void begin_critical_section(int which_one)
  */
 void end_critical_section(int which_one)
 {
+       /* lprintf(9, "end_critical_section(%d)\n", which_one); */
        pthread_mutex_unlock(&Critters[which_one]);
 }
 
@@ -257,6 +262,7 @@ void end_critical_section(int which_one)
 /*
  * This is a generic function to set up a master socket for listening on
  * a TCP port.  The server shuts down if the bind fails.
+ *
  */
 int ig_tcp_server(int port_number, int queue_len)
 {
@@ -266,27 +272,23 @@ int ig_tcp_server(int port_number, int queue_len)
        memset(&sin, 0, sizeof(sin));
        sin.sin_family = AF_INET;
        sin.sin_addr.s_addr = INADDR_ANY;
-
-       if (port_number == 0) {
-               lprintf(1, "citserver: illegal port number specified\n");
-               return(-1);
-       }
-       
        sin.sin_port = htons((u_short)port_number);
 
-       s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
+       s = socket(PF_INET, SOCK_STREAM,
+               (getprotobyname("tcp")->p_proto));
+
        if (s < 0) {
                lprintf(1, "citserver: Can't create a socket: %s\n",
                        strerror(errno));
                return(-1);
        }
 
-       /* Set the SO_REUSEADDR socket option, because it makes sense. */
        i = 1;
        setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
 
        if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
-               lprintf(1, "citserver: Can't bind: %s\n", strerror(errno));
+               lprintf(1, "citserver: Can't bind: %s\n",
+                       strerror(errno));
                return(-1);
        }
 
@@ -300,6 +302,44 @@ int ig_tcp_server(int port_number, int queue_len)
 
 
 
+/*
+ * Create a Unix domain socket and listen on it
+ */
+int ig_uds_server(char *sockpath, int queue_len)
+{
+       struct sockaddr_un addr;
+       int s;
+
+       unlink(sockpath);
+
+       memset(&addr, 0, sizeof(addr));
+       addr.sun_family = AF_UNIX;
+       safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
+
+       s = socket(AF_UNIX, SOCK_STREAM, 0);
+       if (s < 0) {
+               lprintf(1, "citserver: Can't create a socket: %s\n",
+                       strerror(errno));
+               return(-1);
+       }
+
+       if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+               lprintf(1, "citserver: Can't bind: %s\n",
+                       strerror(errno));
+               return(-1);
+       }
+
+       if (listen(s, queue_len) < 0) {
+               lprintf(1, "citserver: Can't listen: %s\n", strerror(errno));
+               return(-1);
+       }
+
+       chmod(sockpath, 0777);
+       return(s);
+}
+
+
+
 /*
  * Return a pointer to the CitContext structure bound to the thread which
  * called this function.  If there's no such binding (for example, if it's
@@ -499,7 +539,22 @@ int client_gets(char *buf)
  * The system-dependent part of master_cleanup() - close the master socket.
  */
 void sysdep_master_cleanup(void) {
-       /* FIX close all protocol master sockets here */
+       struct ServiceFunctionHook *serviceptr;
+
+       /*
+        * close all protocol master sockets
+        */
+       for (serviceptr = ServiceHookTable; serviceptr != NULL;
+           serviceptr = serviceptr->next ) {
+               lprintf(3, "Closing listener on port %d\n",
+                       serviceptr->tcp_port);
+               close(serviceptr->msock);
+
+               /* If it's a Unix domain socket, remove the file. */
+               if (serviceptr->sockpath != NULL) {
+                       unlink(serviceptr->sockpath);
+               }
+       }
 }
 
 
@@ -795,7 +850,7 @@ int main(int argc, char **argv)
        /* Tell 'em who's in da house */
        lprintf(1,
 "\nMultithreaded message server for Citadel/UX\n"
-"Copyright (C) 1987-1999 by the Citadel/UX development team.\n"
+"Copyright (C) 1987-2000 by the Citadel/UX development team.\n"
 "Citadel/UX is free software, covered by the GNU General Public License, and\n"
 "you are welcome to change it and/or distribute copies of it under certain\n"
 "conditions.  There is absolutely no warranty for this software.  Please\n"
@@ -803,7 +858,7 @@ int main(int argc, char **argv)
 
        /* Initialize... */
        init_sysdep();
-       openlog("citserver",LOG_PID,LOG_USER);
+       openlog("citserver", LOG_PID, LOG_USER);
 
        /* Load site-specific parameters */
        lprintf(7, "Loading citadel.config\n");
@@ -817,7 +872,12 @@ int main(int argc, char **argv)
        /*
         * Bind the server to our favorite ports.
         */
-       CtdlRegisterServiceHook(config.c_port_number,
+       CtdlRegisterServiceHook(0,                              /* Unix */
+                               "citadel.socket",
+                               citproto_begin_session,
+                               do_command_loop);
+       CtdlRegisterServiceHook(config.c_port_number,           /* TCP */
+                               NULL,
                                citproto_begin_session,
                                do_command_loop);
 
@@ -856,19 +916,11 @@ int main(int argc, char **argv)
 
        for (serviceptr = ServiceHookTable; serviceptr != NULL;
            serviceptr = serviceptr->next ) {
-               serviceptr->msock = ig_tcp_server(
-                       serviceptr->tcp_port, config.c_maxsessions);
-               if (serviceptr->msock >= 0) {
-                       FD_SET(serviceptr->msock, &masterfds);
-                       if (serviceptr->msock > masterhighest)
-                               masterhighest = serviceptr->msock;
-                       lprintf(7, "Bound to port %-5d (socket %d)\n",
-                               serviceptr->tcp_port,
-                               serviceptr->msock);
-               }
-               else {
-                       lprintf(1, "Unable to bind to port %d\n",
-                               serviceptr->tcp_port);
+               lprintf(9, "Will listen on master socket %d\n",
+                       serviceptr->msock);
+               FD_SET(serviceptr->msock, &masterfds);
+               if (serviceptr->msock > masterhighest) {
+                       masterhighest = serviceptr->msock;
                }
        }
 
@@ -956,9 +1008,6 @@ void worker_thread(void) {
 
        ++num_threads;
 
-       tv.tv_sec = 60;         /* wake up every minute if no input */
-       tv.tv_usec = 0;
-
        while (!time_to_die) {
 
                /* 
@@ -984,6 +1033,8 @@ SETUP_FD:  memcpy(&readfds, &masterfds, sizeof(fd_set) );
                }
                end_critical_section(S_SESSION_TABLE);
 
+               tv.tv_sec = 60;         /* wake up every minute if no input */
+               tv.tv_usec = 0;
                retval = select(highest + 1, &readfds, NULL, NULL, &tv);
 
                /* Now figure out who made this select() unblock.
@@ -996,7 +1047,7 @@ SETUP_FD:  memcpy(&readfds, &masterfds, sizeof(fd_set) );
                }
 
                /* Next, check to see if it's a new client connecting
-                * on the master socket.
+                * on a master socket.
                 */
                else for (serviceptr = ServiceHookTable; serviceptr != NULL;
                     serviceptr = serviceptr->next ) {
@@ -1023,6 +1074,10 @@ SETUP_FD:        memcpy(&readfds, &masterfds, sizeof(fd_set) );
                                        con->client_socket = ssock;
                                        con->h_command_function =
                                                serviceptr->h_command_function;
+
+                                       /* Determine whether local socket */
+                                       if (serviceptr->sockpath != NULL)
+                                               con->is_local_socket = 1;
        
                                        /* Set the SO_REUSEADDR socket option */
                                        i = 1;
@@ -1044,7 +1099,10 @@ SETUP_FD:        memcpy(&readfds, &masterfds, sizeof(fd_set) );
                 * thread that the &readfds needs to be refreshed with more
                 * current data.
                 */
-               if (!time_to_die) if (FD_ISSET(rescan[0], &readfds)) {
+               if (time_to_die)
+                       break;
+
+               if (FD_ISSET(rescan[0], &readfds)) {
                        read(rescan[0], &junk, 1);
                        goto SETUP_FD;
                }
@@ -1084,9 +1142,6 @@ SETUP_FD: memcpy(&readfds, &masterfds, sizeof(fd_set) );
                                } 
                                write(rescan[1], &junk, 1);
                        }
-                       else {
-                               lprintf(9, "Thread found nothing to do!\n");
-                       }
 
                }
                dead_session_purge();