]> code.citadel.org Git - citadel.git/blobdiff - webcit/webserver.c
* Moved all diagnostic output to stderr
[citadel.git] / webcit / webserver.c
index 900ff3020cb938bf51d17275e91f2a570aaee61c..eb1b3b074bbd139c8e510ac19f8f6c17ce31b0b5 100644 (file)
@@ -44,11 +44,18 @@ int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
 int msock;                     /* master listening socket */
 extern void *context_loop(int);
 extern void *housekeeping_loop(void);
-extern pthread_mutex_t MasterCritter;
+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
  * a TCP port.  The server shuts down if the bind fails.
@@ -63,14 +70,15 @@ 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);
 
        s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
        if (s < 0) {
-               printf("webcit: Can't create a socket: %s\n",
+               fprintf(stderr, "webcit: Can't create a socket: %s\n",
                       strerror(errno));
                exit(errno);
        }
@@ -79,11 +87,11 @@ int ig_tcp_server(int port_number, int queue_len)
        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));
+               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);
@@ -101,9 +109,8 @@ void client_write(int sock, char *buf, int nbytes)
                retval = write(sock, &buf[bytes_written],
                               nbytes - bytes_written);
                if (retval < 1) {
-                       printf("client_write() failed: %s\n",
+                       fprintf(stderr, "client_write() failed: %s\n",
                               strerror(errno));
-                       pthread_exit(NULL);
                }
                bytes_written = bytes_written + retval;
        }
@@ -154,9 +161,8 @@ int client_read_to(int sock, char *buf, int bytes, int timeout)
                }
                rlen = read(sock, &buf[len], bytes - len);
                if (rlen < 1) {
-                       printf("client_read() failed: %s\n",
+                       fprintf(stderr, "client_read() failed: %s\n",
                               strerror(errno));
-                       pthread_exit(NULL);
                }
                len = len + rlen;
        }
@@ -230,9 +236,6 @@ void start_daemon(int do_close_stdio)
  */
 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 */
@@ -264,25 +267,33 @@ int main(int argc, char **argv)
                        defaultport = argv[optind];
        }
        /* Tell 'em who's in da house */
-       printf("WebCit version 2.00\n");
-       printf("Copyright (C) 1996-1999.  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 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(&MasterCritter, NULL);
-
-
+       pthread_mutex_init(&SessionListMutex, NULL);
+       pthread_mutex_init(&AcceptQueue, NULL);
 
        /*
         * Start up the housekeeping thread
@@ -294,35 +305,56 @@ int main(int argc, char **argv)
 
 
 
-       /* 
-        * Endless loop.  Listen on the master socket.  When a connection
-        * comes in, create a socket, a context, and a thread.
-        */
-       while (1) {
+       /* FIX ... we need to auto-size the thread pool */
+       for (i=0; i<10; ++i) {
+
+               /* 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);
-               printf("New connection on socket %d\n", ssock);
+               pthread_mutex_unlock(&AcceptQueue);
+
                if (ssock < 0) {
-                       printf("webcit: accept() failed: %s\n",
-                              strerror(errno));
+                       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));
-
-                       /* 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);
 }