2 * Copyright (C) 2005-2014 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file close_on_fork.c Manage a list of fds that should be closed on fork. */
14 #include "close_on_fork.h"
16 static struct list_head close_on_fork_list;
17 static int initialized;
20 * Describes an element of the close-on-fork list.
24 struct close_on_fork {
25 /** The file descriptor which should be closed after fork(). */
27 /** The position in the close-on-fork list. */
28 struct list_head node;
32 * Add one file descriptor to the close-on-fork list.
34 * \param fd The file descriptor to add.
36 void add_close_on_fork_list(int fd)
38 struct close_on_fork *cof = para_malloc(sizeof(struct close_on_fork));
41 INIT_LIST_HEAD(&close_on_fork_list);
45 para_list_add(&cof->node, &close_on_fork_list);
49 * Delete one file descriptor from the close-on-fork list.
51 * \param fd The file descriptor to delete.
53 * Noop if \a fd does not belong to the close-on-fork list.
55 void del_close_on_fork_list(int fd)
57 struct close_on_fork *cof, *tmp;
61 list_for_each_entry_safe(cof, tmp, &close_on_fork_list, node) {
70 * Close all fds in the list and destroy all list entries.
72 * This function calls close(3) for each fd in the close-on-fork list
73 * and empties the list afterwards.
75 void close_listed_fds(void)
77 struct close_on_fork *cof, *tmp;
81 list_for_each_entry_safe(cof, tmp, &close_on_fork_list, node) {
82 PARA_DEBUG_LOG("closing fd %d\n", cof->fd);