From 0d7a27484c152b493751ce798b135ca15a03fa0b Mon Sep 17 00:00:00 2001 From: Nathan Bryant Date: Sat, 5 Dec 1998 20:15:37 +0000 Subject: [PATCH] * 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 --- webcit/ChangeLog | 5 ++++ webcit/context_loop.c | 53 ++++++++++++++++++++++++++++--------------- webcit/tcp_sockets.c | 20 ---------------- 3 files changed, 40 insertions(+), 38 deletions(-) diff --git a/webcit/ChangeLog b/webcit/ChangeLog index d6189ddc4..ee8fa93ec 100644 --- a/webcit/ChangeLog +++ b/webcit/ChangeLog @@ -1,3 +1,8 @@ +1998-12-05 Nathan Bryant + * 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 * webserver.c, context_loop.c, webcit.c: add commandline args for host and port diff --git a/webcit/context_loop.c b/webcit/context_loop.c index fbb3bd0cd..97eeff6eb 100644 --- a/webcit/context_loop.c +++ b/webcit/context_loop.c @@ -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("execlp() failed\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. diff --git a/webcit/tcp_sockets.c b/webcit/tcp_sockets.c index e22a99517..c6c58f236 100644 --- a/webcit/tcp_sockets.c +++ b/webcit/tcp_sockets.c @@ -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 */ -- 2.30.2