From ce3307e81f5e401b15392146e31da8cfb5bb4ffd Mon Sep 17 00:00:00 2001 From: short <> Date: Mon, 18 Nov 2002 22:14:39 +0000 Subject: [PATCH] Fixed resource leakage during connection accept(2) --- src/main.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main.c b/src/main.c index 6f8c99b..d9f8cb8 100644 --- a/src/main.c +++ b/src/main.c @@ -499,11 +499,9 @@ void mainloop() if (connection != NULL) { if (access_check(access_list, connection, NULL, NULL)) { x = process_new(connection); - if (x != 0) { + if (x != 0) putlog(MMLOG_ERROR, "failed to create thread for %s", connection->ip); - - net_close(connection); - } + net_close(connection); } else { putlog(MMLOG_NETWORK, "refused connect from %s on port %d", connection->ip, connection->port); @@ -526,6 +524,14 @@ int process_new(CONNECTION * connection) int perr, thread = -1, i; pthread_attr_t thread_attr; + /* fork() before enqueue to threads[] */ + signal(SIGCHLD,sigchld); + perr=fork(); + if (perr>0) + return 0; + if (perr<0) + return perr; + for (i = 0; i < MAXTHREADS && thread == -1; i++) { pthread_mutex_lock(&threads[i].lock); if (threads[i].flags & THREAD_UNUSED) { @@ -553,16 +559,11 @@ int process_new(CONNECTION * connection) if (getuid() == 0) pthread_attr_setschedpolicy(&thread_attr, SCHED_FIFO); - signal(SIGCHLD,sigchld); - if (0==(perr=fork())) { - process_entry(connection); - _exit(0); - } - perr=close(connection->client->fd) + process_entry(connection); pthread_attr_destroy(&thread_attr); - return perr; + _exit(0); } /* -- 1.8.3.1