From 659b46df9a49e0677fd7ff85a9842adac4abec84 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sat, 12 Aug 2017 21:32:28 +0200 Subject: [PATCH] server: Deplete close on fork list on exit. 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 | 34 ++++++++++++++++++++++++++-------- close_on_fork.h | 1 + server.c | 1 + 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/close_on_fork.c b/close_on_fork.c index 7e0c8e64..28c5eabb 100644 --- a/close_on_fork.c +++ b/close_on_fork.c @@ -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; @@ -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); - close(cof->fd); + if (close_fds) + close(cof->fd); 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); +} diff --git a/close_on_fork.h b/close_on_fork.h index 1bd0cd15..0dcc2b6c 100644 --- a/close_on_fork.h +++ b/close_on_fork.h @@ -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 deplete_close_on_fork_list(void); diff --git a/server.c b/server.c index 690f7163..593d92c5 100644 --- 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); + deplete_close_on_fork_list(); if (ret < 0) PARA_EMERG_LOG("%s\n", para_strerror(-ret)); } else { -- 2.39.5