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 = para_malloc(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) {
66 * Close all fds in the list and destroy all list entries.
68 * This function calls close(3) for each fd in the close-on-fork list
69 * and empties the list afterwards.
71 void close_listed_fds(void)
73 struct close_on_fork *cof, *tmp;
77 list_for_each_entry_safe(cof, tmp, &close_on_fork_list, node) {
78 PARA_DEBUG_LOG("closing fd %d\n", cof->fd);