* tcp_sockets.c: removed some dead code
authorNathan Bryant <loanshark@uncensored.citadel.org>
Sat, 5 Dec 1998 20:15:37 +0000 (20:15 +0000)
committerNathan Bryant <loanshark@uncensored.citadel.org>
Sat, 5 Dec 1998 20:15:37 +0000 (20:15 +0000)
        * context_loop.c: oops, I made a memory leak. fix fix fix. also close
          unused ends of pipes, fix a file descriptor leak and a race condition

webcit/ChangeLog
webcit/context_loop.c
webcit/tcp_sockets.c

index d6189ddc477599ab297aa5f0ee07decd3b33be0e..ee8fa93ecf6ccb4fd5e6b6cf6496ffe33ada3074 100644 (file)
@@ -1,3 +1,8 @@
+1998-12-05 Nathan Bryant <bryant@cs.usm.maine.edu>
+       * tcp_sockets.c: removed some dead code
+       * context_loop.c: oops, I made a memory leak. fix fix fix. also close
+         unused ends of pipes, fix a file descriptor leak and a race condition
+
 1998-12-04 Nathan Bryant <bryant@cs.usm.maine.edu>
        * webserver.c, context_loop.c, webcit.c: add commandline args for host
          and port
index fbb3bd0cd8ad103bb1627e396510c89ece28b96e..97eeff6ebf54fb9bda966aebe8d1675c9eb02204 100644 (file)
@@ -84,6 +84,24 @@ void req_gets(int sock, char *buf, char *hold) {
                }
        }
 
+/*
+ * Grab a lock on the session, so other threads don't try to access
+ * the pipes at the same time.
+ */
+static void lock_session(struct wc_session *session) {
+        printf("Locking session %d...\n", session->session_id);
+        pthread_mutex_lock(&session->critter);
+        printf("   ...got lock\n");
+       }
+
+/*
+ * Let go of the lock.
+ */
+static void unlock_session(struct wc_session *session) {
+       printf("Unlocking.\n");
+       pthread_mutex_unlock(&session->critter);
+       }
+
 extern const char *defaulthost;
 extern const char *defaultport;
 
@@ -135,6 +153,7 @@ void *context_loop(int sock) {
                for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
                        if (sptr->session_id == desired_session) {
                                TheSession = sptr;
+                               lock_session(TheSession);
                                }
                        }
                pthread_mutex_unlock(&MasterCritter);
@@ -152,14 +171,19 @@ void *context_loop(int sock) {
                pipe(TheSession->inpipe);
                pipe(TheSession->outpipe);
                pthread_mutex_init(&TheSession->critter, NULL);
+               lock_session(TheSession);
                TheSession->next = SessionList;
                SessionList = TheSession;
+               pthread_mutex_unlock(&MasterCritter);
                sprintf(str_session, "%d", TheSession->session_id);
                f = fork();
                fflush(stdout); fflush(stdin);
                if (f==0) {
                        dup2(TheSession->inpipe[0], 0);
                        dup2(TheSession->outpipe[1], 1);
+                       /* Close the ends of the pipes that we're not using */
+                       close(TheSession->inpipe[1]);
+                       close(TheSession->outpipe[0]);
                        execlp("./webcit", "webcit", str_session, defaulthost,
                               defaultport, NULL);
                        printf("HTTP/1.0 404 WebCit Failure\n\n");
@@ -171,17 +195,11 @@ void *context_loop(int sock) {
                        printf("<BODY>execlp() failed</BODY></HTML>\n");
                        exit(0);
                        }
-               pthread_mutex_unlock(&MasterCritter);
+               /* Close the ends of the pipes that we're not using */
+               close(TheSession->inpipe[0]);
+               close(TheSession->outpipe[1]);
                }
 
-       /*
-        * Grab a lock on the session, so other threads don't try to access
-        * the pipes at the same time.
-        */
-       printf("Locking session %d...\n", TheSession->session_id);
-       pthread_mutex_lock(&TheSession->critter);
-       printf("   ...got lock\n");
-
        /* 
         * Send the request to the appropriate session...
         */
@@ -227,13 +245,7 @@ void *context_loop(int sock) {
        printf("   Closing socket\n");
        close(sock);
 
-       /*
-        * Let go of the lock
-        */
-       printf("Unlocking.\n");
-       pthread_mutex_unlock(&TheSession->critter);
-
-
+       unlock_session(TheSession);
 
        /*
         * If the last response included a "close session" directive,
@@ -243,6 +255,8 @@ void *context_loop(int sock) {
                printf("Removing session.\n");
                pthread_mutex_lock(&MasterCritter);
 
+               lock_session(TheSession);
+
                if (SessionList==TheSession) {
                        SessionList = SessionList->next;
                        }
@@ -253,13 +267,16 @@ void *context_loop(int sock) {
                                        }
                                }
                        }
-       
+
+               close(TheSession->inpipe[1]);
+               close(TheSession->outpipe[0]);
+               unlock_session(TheSession);
                free(TheSession);
        
                pthread_mutex_unlock(&MasterCritter);
                }
 
-
+       free(req);
 
        /*
         * The thread handling this HTTP connection is now finished.
index e22a99517e5c9137110a4c0444bf205841ec019a..c6c58f236d2d4edb8b17fc37798e523bac2d5971 100644 (file)
@@ -144,26 +144,6 @@ void serv_gets(char *strbuf)
 
 
 
-/*
- * Attach to a Citadel server
- */
-void attach_to_server(int argc, char **argv)
-{
-       if (argc==1) {
-               server_is_local = 1;
-               serv_sock = connectsock("localhost","citadel","tcp");
-               }
-       if (argc==2) {
-               serv_sock = connectsock(argv[1],"citadel","tcp");
-               if ( (!strcmp(argv[1],"localhost"))
-               || (!strcmp(argv[1],"127.0.0.1")) ) server_is_local = 1;
-               }
-       if (argc>=3) serv_sock = connectsock(argv[1],argv[2],"tcp");
-
-       if (serv_sock < 0) exit(errno);
-       }
-
-
 /*
  * send binary to server
  */