server: Deplete close on fork list on exit.
authorAndre Noll <maan@tuebingen.mpg.de>
Sat, 12 Aug 2017 19:32:28 +0000 (21:32 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Tue, 13 Mar 2018 02:28:10 +0000 (03:28 +0100)
We empty the list in the command handler process by calling
close_listed_fds(), but the server process leaks the memory
allocated for the entries of the close on fork list.

This commit introduces deplete_close_on_fork_list() to empty the
list without closing any file descriptors. It is called from main()
to avoid the leak.

With the patch applied, valgrind --show-reachable=no no longer
complains about possibly lost blocks for the server process.

close_on_fork.c
close_on_fork.h
server.c

index 7e0c8e6474b48a932ca0c107862391466415ec96..28c5eabb1ae9a4821b60b638be559e837dc54f36 100644 (file)
@@ -62,13 +62,7 @@ void del_close_on_fork_list(int fd)
        }
 }
 
        }
 }
 
-/**
- * Close all fds in the list and destroy all list entries.
- *
- * This function calls close(3) for each fd in the close-on-fork list
- * and empties the list afterwards.
- */
-void close_listed_fds(void)
+static void deplete_cof_list(bool close_fds)
 {
        struct close_on_fork *cof, *tmp;
 
 {
        struct close_on_fork *cof, *tmp;
 
@@ -76,8 +70,32 @@ void close_listed_fds(void)
                return;
        list_for_each_entry_safe(cof, tmp, &close_on_fork_list, node) {
                PARA_DEBUG_LOG("closing fd %d\n", cof->fd);
                return;
        list_for_each_entry_safe(cof, tmp, &close_on_fork_list, node) {
                PARA_DEBUG_LOG("closing fd %d\n", cof->fd);
-               close(cof->fd);
+               if (close_fds)
+                       close(cof->fd);
                list_del(&cof->node);
                free(cof);
        }
 }
                list_del(&cof->node);
                free(cof);
        }
 }
+
+/**
+ * Close all fds in the list and destroy all list entries.
+ *
+ * This function calls close(3) for each fd in the close-on-fork list
+ * and empties the list afterwards.
+ *
+ * \sa \ref deplete_close_on_fork_list().
+ */
+void close_listed_fds(void)
+{
+       deplete_cof_list(true);
+}
+
+/**
+ * Remove all listed fds from the close on fork list.
+ *
+ * This is like \ref close_listed_fds() but does not close the fds.
+ */
+void deplete_close_on_fork_list(void)
+{
+       deplete_cof_list(false);
+}
index 1bd0cd15f72ef129f83db2a1ab56400b2b1580df..0dcc2b6cd7c5337e053aff9abc4433fcd8039d07 100644 (file)
@@ -2,3 +2,4 @@
 void del_close_on_fork_list(int fd);
 void add_close_on_fork_list(int fd);
 void close_listed_fds(void);
 void del_close_on_fork_list(int fd);
 void add_close_on_fork_list(int fd);
 void close_listed_fds(void);
+void deplete_close_on_fork_list(void);
index 690f7163984c2f00cb0de1e4982da4668676e500..593d92c5d3717fd3965336ca1c57d9a218377521 100644 (file)
--- a/server.c
+++ b/server.c
@@ -674,6 +674,7 @@ int main(int argc, char *argv[])
                daemon_set_hooks(NULL, NULL); /* only one process remaining */
                mutex_destroy(log_mutex);
                shm_detach(mmd);
                daemon_set_hooks(NULL, NULL); /* only one process remaining */
                mutex_destroy(log_mutex);
                shm_detach(mmd);
+               deplete_close_on_fork_list();
                if (ret < 0)
                        PARA_EMERG_LOG("%s\n", para_strerror(-ret));
        } else {
                if (ret < 0)
                        PARA_EMERG_LOG("%s\n", para_strerror(-ret));
        } else {