X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;f=close_on_fork.c;h=28c5eabb1ae9a4821b60b638be559e837dc54f36;hb=53fe3c3cca7caef5565750181daf70aa0598c2a9;hp=839802b8fd45d5420510eab20f926528c21dbad2;hpb=471684761a2039bbc89aa1e3c33c62de6bef86cf;p=paraslash.git diff --git a/close_on_fork.c b/close_on_fork.c index 839802b8..28c5eabb 100644 --- a/close_on_fork.c +++ b/close_on_fork.c @@ -1,33 +1,33 @@ -/* - * Copyright (C) 2005-2006 Andre Noll - * - * Licensed under the GPL v2. For licencing details see COPYING. - */ +/* Copyright (C) 2005 Andre Noll , see file COPYING. */ + +/** \file close_on_fork.c Manage a list of fds that should be closed on fork. */ + +#include -/** \file close_on_fork.c manage a list of fds that should be closed on fork */ #include "para.h" #include "list.h" #include "string.h" +#include "close_on_fork.h" static struct list_head close_on_fork_list; static int initialized; /** - * describes an element of the close-on-fork list + * Describes an element of the close-on-fork list. * - * \sa list.h + * \sa \ref list.h. */ struct close_on_fork { - /** the file descriptor which should be closed after fork() */ + /** The file descriptor which should be closed after fork(). */ int fd; - /** the position in the close-on-fork list */ + /** The position in the close-on-fork list. */ struct list_head node; }; /** - * add one file descriptor to the close-on-fork list + * Add one file descriptor to the close-on-fork list. * - * \param fd the file descriptor to add + * \param fd The file descriptor to add. */ void add_close_on_fork_list(int fd) { @@ -41,11 +41,10 @@ void add_close_on_fork_list(int fd) para_list_add(&cof->node, &close_on_fork_list); } - /** - * delete one file descriptor from the close-on-fork list + * Delete one file descriptor from the close-on-fork list. * - * \param fd the file descriptor to delete + * \param fd The file descriptor to delete. * * Noop if \a fd does not belong to the close-on-fork list. */ @@ -63,17 +62,40 @@ void del_close_on_fork_list(int fd) } } -/** - * call close(3) for each fd in the close-on-fork list - */ -void close_listed_fds(void) +static void deplete_cof_list(bool close_fds) { - struct close_on_fork *cof; + struct close_on_fork *cof, *tmp; if (!initialized) return; - list_for_each_entry(cof, &close_on_fork_list, node) { + 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); +}