]> code.citadel.org Git - citadel.git/blobdiff - webcit/webserver.c
* Moved all diagnostic output to stderr
[citadel.git] / webcit / webserver.c
index 0a65572ef0f6b0a4e5683ebd4493fa0e84ef76f2..eb1b3b074bbd139c8e510ac19f8f6c17ce31b0b5 100644 (file)
 #include <errno.h>
 #include <stdarg.h>
 #include <pthread.h>
+#include <signal.h>
 #include "webcit.h"
 
 #ifndef HAVE_SNPRINTF
-int vsnprintf (char *buf, size_t max, const char *fmt, va_list argp);
+int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
 #endif
 
-int msock;                                     /* master listening socket */
+int msock;                     /* master listening socket */
 extern void *context_loop(int);
-extern pthread_mutex_t MasterCritter;
+extern void *housekeeping_loop(void);
+extern pthread_mutex_t SessionListMutex;
+extern pthread_key_t MyConKey;
+
+
+
+
+
+const char *defaulthost = DEFAULT_HOST;
+const char *defaultport = DEFAULT_PORT;
+
+pthread_mutex_t AcceptQueue;
 
 /*
  * This is a generic function to set up a master socket for listening on
@@ -58,35 +70,32 @@ int ig_tcp_server(int port_number, int queue_len)
        sin.sin_addr.s_addr = INADDR_ANY;
 
        if (port_number == 0) {
-               printf("webcit: Cannot start: no port number specified.\n");
+               fprintf(stderr,
+                       "webcit: Cannot start: no port number specified.\n");
                exit(1);
-               }
-       
-       sin.sin_port = htons((u_short)port_number);
+       }
+       sin.sin_port = htons((u_short) port_number);
 
        s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
        if (s < 0) {
-               printf("webcit: Can't create a socket: %s\n",
-                       strerror(errno));
+               fprintf(stderr, "webcit: Can't create a socket: %s\n",
+                      strerror(errno));
                exit(errno);
-               }
-
+       }
        /* Set some socket options that make sense. */
        i = 1;
        setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
 
-       if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
-               printf("webcit: Can't bind: %s\n", strerror(errno));
+       if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
+               fprintf(stderr, "webcit: Can't bind: %s\n", strerror(errno));
                exit(errno);
-               }
-
+       }
        if (listen(s, queue_len) < 0) {
-               printf("webcit: Can't listen: %s\n", strerror(errno));
+               fprintf(stderr, "webcit: Can't listen: %s\n", strerror(errno));
                exit(errno);
-               }
-
-       return(s);
        }
+       return (s);
+}
 
 
 /*
@@ -98,71 +107,67 @@ void client_write(int sock, char *buf, int nbytes)
        int retval;
        while (bytes_written < nbytes) {
                retval = write(sock, &buf[bytes_written],
-                       nbytes - bytes_written);
+                              nbytes - bytes_written);
                if (retval < 1) {
-                       printf("client_write() failed: %s\n",
-                               strerror(errno));
-                       pthread_exit(NULL);
-                       }
-               bytes_written = bytes_written + retval;
+                       fprintf(stderr, "client_write() failed: %s\n",
+                              strerror(errno));
                }
+               bytes_written = bytes_written + retval;
        }
+}
 
 
 /*
- * cprintf()  ...   Send formatted printable data to the client.   It is
- *                  implemented in terms of client_write() but remains in
- *                  sysdep.c in case we port to somewhere without va_args...
+ * cprintf()  ...   Send formatted printable data to the client.
  */
-void cprintf(int sock, const char *format, ...) {   
-        va_list arg_ptr;   
-        char buf[256];   
-   
-        va_start(arg_ptr, format);   
-        if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
+void cprintf(int sock, const char *format,...)
+{
+       va_list arg_ptr;
+       char buf[256];
+
+       va_start(arg_ptr, format);
+       if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
                buf[sizeof buf - 2] = '\n';
-       client_write(sock, buf, strlen(buf)); 
+       client_write(sock, buf, strlen(buf));
        va_end(arg_ptr);
-       }   
+}
 
 
 /*
  * Read data from the client socket.
  * Return values are:
- *     1       Requested number of bytes has been read.
- *     0       Request timed out.
+ *      1       Requested number of bytes has been read.
+ *      0       Request timed out.
  * If the socket breaks, the session is immediately terminated.
  */
 int client_read_to(int sock, char *buf, int bytes, int timeout)
 {
-       int len,rlen;
+       int len, rlen;
        fd_set rfds;
        struct timeval tv;
        int retval;
 
        len = 0;
-       while(len<bytes) {
+       while (len < bytes) {
                FD_ZERO(&rfds);
                FD_SET(sock, &rfds);
                tv.tv_sec = timeout;
                tv.tv_usec = 0;
 
-               retval = select( (sock)+1, 
-                                       &rfds, NULL, NULL, &tv);
+               retval = select((sock) + 1,
+                               &rfds, NULL, NULL, &tv);
                if (FD_ISSET(sock, &rfds) == 0) {
-                       return(0);
-                       }
-
-               rlen = read(sock, &buf[len], bytes-len);
-               if (rlen<1) {
-                       printf("client_read() failed: %s\n",
-                               strerror(errno));
-                       pthread_exit(NULL);
-                       }
-               len = len + rlen;
+                       return (0);
                }
-       return(1);
+               rlen = read(sock, &buf[len], bytes - len);
+               if (rlen < 1) {
+                       fprintf(stderr, "client_read() failed: %s\n",
+                              strerror(errno));
+               }
+               len = len + rlen;
        }
+       return (1);
+}
 
 /*
  * Read data from the client socket with default timeout.
@@ -171,8 +176,8 @@ int client_read_to(int sock, char *buf, int bytes, int timeout)
  */
 int client_read(int sock, char *buf, int bytes)
 {
-       return(client_read_to(sock, buf, bytes, SLEEPING));
-       }
+       return (client_read_to(sock, buf, bytes, SLEEPING));
+}
 
 
 /*
@@ -186,11 +191,11 @@ int client_gets(int sock, char *buf)
 
        /* Read one character at a time.
         */
-       for (i = 0;;i++) {
+       for (i = 0;; i++) {
                retval = client_read(sock, &buf[i], 1);
                if (retval != 1 || buf[i] == '\n' || i == 255)
                        break;
-               }
+       }
 
        /* If we got a long line, discard characters until the newline.
         */
@@ -202,121 +207,154 @@ int client_gets(int sock, char *buf)
         * Strip any trailing not-printable characters.
         */
        buf[i] = 0;
-       while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1]))) {
-               buf[strlen(buf)-1] = 0;
-               }
-       return(retval);
+       while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1]))) {
+               buf[strlen(buf) - 1] = 0;
        }
+       return (retval);
+}
 
 
 /*
  * Start running as a daemon.  Only close stdio if do_close_stdio is set.
  */
-void start_daemon(int do_close_stdio) {
+void start_daemon(int do_close_stdio)
+{
        if (do_close_stdio) {
                /* close(0); */
                close(1);
                close(2);
-               }
-       signal(SIGHUP,SIG_IGN);
-       signal(SIGINT,SIG_IGN);
-       signal(SIGQUIT,SIG_IGN);
-       if (fork()!=0) exit(0);
        }
-
-/*
- * Issue an HTTP Redirect header
- * url must be a complete url (with http://)
- */
-void redirect(char *url) {
-       printf("Location: %s\n\n", url);
+       signal(SIGHUP, SIG_IGN);
+       signal(SIGINT, SIG_IGN);
+       signal(SIGQUIT, SIG_IGN);
+       if (fork() != 0)
+               exit(0);
 }
 
-const char *defaulthost = DEFAULT_HOST;
-const char *defaultport = DEFAULT_PORT;
-
 /*
  * Here's where it all begins.
  */
 int main(int argc, char **argv)
 {
-       struct sockaddr_in fsin;        /* Data for master socket */
-       int alen;                       /* Data for master socket */
-       int ssock;                      /* Descriptor for master socket */
-       pthread_t SessThread;           /* Thread descriptor */
-        pthread_attr_t attr;           /* Thread attributes */
-       int a, i;                       /* General-purpose variables */
-       int port = PORT_NUM;            /* Port to listen on */
-       struct linger WebLinger = { 1, 9000 };
+       pthread_t SessThread;   /* Thread descriptor */
+       pthread_attr_t attr;    /* Thread attributes */
+       int a, i;               /* General-purpose variables */
+       int port = PORT_NUM;    /* Port to listen on */
+       char tracefile[PATH_MAX];
 
        /* Parse command line */
-       while ((a = getopt(argc, argv, "hp:")) != EOF)
+       while ((a = getopt(argc, argv, "hp:t:")) != EOF)
                switch (a) {
-                   case 'p':
+               case 'p':
                        port = atoi(optarg);
                        break;
-                   default:
+               case 't':
+                       strcpy(tracefile, optarg);
+                       freopen(tracefile, "w", stdout);
+                       freopen(tracefile, "w", stderr);
+                       freopen(tracefile, "r", stdin);
+                       break;
+               default:
                        fprintf(stderr, "usage: webserver [-p localport] "
+                               "[-t tracefile] "
                                "[remotehost [remoteport]]\n");
                        return 1;
-                       }
+               }
 
        if (optind < argc) {
                defaulthost = argv[optind];
                if (++optind < argc)
                        defaultport = argv[optind];
-               }
-        
+       }
        /* Tell 'em who's in da house */
-       printf("WebCit v2 experimental\n");
-       printf("Copyright (C) 1996-1998 by Art Cancro.  ");
-       printf("All rights reserved.\n\n");
+       fprintf(stderr, SERVER "\n"
+               "Copyright (C) 1996-1999.  All rights reserved.\n\n");
+
+       if (chdir(WEBCITDIR) != 0)
+               perror("chdir");
+
+        /*
+         * Set up a place to put thread-specific data.
+         * We only need a single pointer per thread - it points to the
+         * wcsession struct to which the thread is currently bound.
+         */
+        if (pthread_key_create(&MyConKey, NULL) != 0) {
+                fprintf(stderr, "Can't create TSD key: %s\n", strerror(errno));
+        }
 
        /*
-        * Bind the server to our favourite port.
+        * Bind the server to our favorite port.
         * There is no need to check for errors, because ig_tcp_server()
         * exits if it doesn't succeed.
         */
-       printf("Attempting to bind to port %d...\n", port);
+       fprintf(stderr, "Attempting to bind to port %d...\n", port);
        msock = ig_tcp_server(port, 5);
-       printf("Listening on socket %d\n", msock);
+       fprintf(stderr, "Listening on socket %d\n", msock);
+       signal(SIGPIPE, SIG_IGN);
+
+       pthread_mutex_init(&SessionListMutex, NULL);
+       pthread_mutex_init(&AcceptQueue, NULL);
+
+       /*
+        * Start up the housekeeping thread
+        */
+       pthread_attr_init(&attr);
+       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+       pthread_create(&SessThread, &attr,
+                      (void *(*)(void *)) housekeeping_loop, NULL);
+
+
+
+       /* FIX ... we need to auto-size the thread pool */
+       for (i=0; i<10; ++i) {
 
-       pthread_mutex_init(&MasterCritter, NULL);
+               /* set attributes for the new thread */
+               pthread_attr_init(&attr);
+               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+
+               /* now create the thread */
+               if (pthread_create(&SessThread, &attr,
+                               (void *(*)(void *)) worker_entry, NULL)
+                   != 0) {
+                       fprintf(stderr, "webcit: can't create thread: %s\n",
+                              strerror(errno));
+               }
+       }
+
+       /* now become a worker thread too */
+       worker_entry();
+       pthread_exit(NULL);
+}
+
+
+/*
+ * Entry point for worker threads
+ */
+void worker_entry(void) {
+       int ssock;
+       struct sockaddr_in fsin;
+       int alen;
+       int i = 0;
+       int time_to_die = 0;
+
+       do {
+               /* Only one thread can accept at a time */
+               pthread_mutex_lock(&AcceptQueue);
+               ssock = accept(msock, (struct sockaddr *) &fsin, &alen);
+               pthread_mutex_unlock(&AcceptQueue);
 
-       /* 
-        * Endless loop.  Listen on the master socket.  When a connection
-        * comes in, create a socket, a context, and a thread.
-        */     
-       while (1) {
-               ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
-               printf("New connection on socket %d\n", ssock);
                if (ssock < 0) {
-                       printf("webcit: accept() failed: %s\n",
-                               strerror(errno));
-                       }
-               else {
+                       fprintf(stderr, "webcit: accept() failed: %s\n",
+                       strerror(errno));
+               } else {
                        /* Set the SO_REUSEADDR socket option */
                        i = 1;
                        setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
-                               &i, sizeof(i));
-                       setsockopt(ssock, SOL_SOCKET, SO_LINGER,
-                               &WebLinger, sizeof(struct linger));
-
-                       /* set attributes for the new thread */
-                       pthread_attr_init(&attr);
-                       pthread_attr_setdetachstate(&attr,
-                               PTHREAD_CREATE_DETACHED);
-
-                       /* now create the thread */
-                       if (pthread_create(&SessThread, &attr,
-                                          (void* (*)(void*)) context_loop,
-                                          (void*) ssock)
-                           != 0) {
-                               printf("webcit: can't create thread: %s\n",
-                                       strerror(errno));
-                               }
-
-                       }
+                               &i, sizeof(i));
+                       context_loop(ssock);
                }
-       }
 
+       } while (!time_to_die);
+
+       pthread_exit(NULL);
+}