Fixed resource leakage during connection accept(2) lace
authorshort <>
Mon, 18 Nov 2002 22:14:39 +0000 (22:14 +0000)
committershort <>
Mon, 18 Nov 2002 22:14:39 +0000 (22:14 +0000)
src/main.c

index 6f8c99b..d9f8cb8 100644 (file)
@@ -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);
 }
 
 /*