2 * Copyright (C) 2005-2009 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. */
15 static struct list_head close_on_fork_list
;
16 static int initialized
;
19 * Describes an element of the close-on-fork list.
23 struct close_on_fork
{
24 /** The file descriptor which should be closed after fork(). */
26 /** The position in the close-on-fork list. */
27 struct list_head node
;
31 * Add one file descriptor to the close-on-fork list.
33 * \param fd The file descriptor to add.
35 void add_close_on_fork_list(int fd
)
37 struct close_on_fork
*cof
= para_malloc(sizeof(struct close_on_fork
));
40 INIT_LIST_HEAD(&close_on_fork_list
);
44 para_list_add(&cof
->node
, &close_on_fork_list
);
48 * Delete one file descriptor from the close-on-fork list.
50 * \param fd The file descriptor to delete.
52 * Noop if \a fd does not belong to the close-on-fork list.
54 void del_close_on_fork_list(int fd
)
56 struct close_on_fork
*cof
, *tmp
;
60 list_for_each_entry_safe(cof
, tmp
, &close_on_fork_list
, node
) {
69 * Close all fds in the list and destroy all list entries.
71 * This function calls close(3) for each fd in the close-on-fork list
72 * and empties the list afterwards.
74 void close_listed_fds(void)
76 struct close_on_fork
*cof
, *tmp
;
80 list_for_each_entry_safe(cof
, tmp
, &close_on_fork_list
, node
) {
81 PARA_DEBUG_LOG("closing fd %d\n", cof
->fd
);