1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file close_on_fork.c Manage a list of fds that should be closed on fork. */
10 #include "close_on_fork.h"
12 static struct list_head close_on_fork_list;
13 static int initialized;
16 * Describes an element of the close-on-fork list.
20 struct close_on_fork {
21 /** The file descriptor which should be closed after fork(). */
23 /** The position in the close-on-fork list. */
24 struct list_head node;
28 * Add one file descriptor to the close-on-fork list.
30 * \param fd The file descriptor to add.
32 void add_close_on_fork_list(int fd)
34 struct close_on_fork *cof = alloc(sizeof(struct close_on_fork));
37 init_list_head(&close_on_fork_list);
41 para_list_add(&cof->node, &close_on_fork_list);
45 * Delete one file descriptor from the close-on-fork list.
47 * \param fd The file descriptor to delete.
49 * Noop if \a fd does not belong to the close-on-fork list.
51 void del_close_on_fork_list(int fd)
53 struct close_on_fork *cof, *tmp;
57 list_for_each_entry_safe(cof, tmp, &close_on_fork_list, node) {
65 static void deplete_cof_list(bool close_fds)
67 struct close_on_fork *cof, *tmp;
71 list_for_each_entry_safe(cof, tmp, &close_on_fork_list, node) {
72 PARA_DEBUG_LOG("closing fd %d\n", cof->fd);
81 * Close all fds in the list and destroy all list entries.
83 * This function calls close(3) for each fd in the close-on-fork list
84 * and empties the list afterwards.
86 * \sa \ref deplete_close_on_fork_list().
88 void close_listed_fds(void)
90 deplete_cof_list(true);
94 * Remove all listed fds from the close on fork list.
96 * This is like \ref close_listed_fds() but does not close the fds.
98 void deplete_close_on_fork_list(void)
100 deplete_cof_list(false);