configure.ac: do not check for malloc.h
[paraslash.git] / close_on_fork.c
1 /*
2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file close_on_fork.c manage a list of fds that should be closed on fork */
20 #include "para.h"
21 #include "list.h"
22 #include "string.h"
23
24 static struct list_head close_on_fork_list;
25 static int initialized;
26
27 /**
28 * describes an element of the close-on-fork list
29 *
30 * \sa list.h
31 */
32 struct close_on_fork {
33 /** the file descriptor which should be closed after fork() */
34 int fd;
35 /** the position in the close-on-fork list */
36 struct list_head node;
37 };
38
39 /**
40 * add one file descriptor to the close-on-fork list
41 *
42 * \param fd the file descriptor to add
43 */
44 void add_close_on_fork_list(int fd)
45 {
46 struct close_on_fork *cof = para_malloc(sizeof(struct close_on_fork));
47
48 if (!initialized) {
49 INIT_LIST_HEAD(&close_on_fork_list);
50 initialized = 1;
51 }
52 cof->fd = fd;
53 list_add(&cof->node, &close_on_fork_list);
54 }
55
56
57 /**
58 * delete one file descriptor from the close-on-fork list
59 *
60 * \param fd the file descriptor to delete
61 *
62 * Noop if \a fd does not belong to the close-on-fork list.
63 */
64 void del_close_on_fork_list(int fd)
65 {
66 struct close_on_fork *cof, *tmp;
67
68 if (!initialized)
69 return;
70 list_for_each_entry_safe(cof, tmp, &close_on_fork_list, node) {
71 if (fd != cof->fd)
72 continue;
73 list_del(&cof->node);
74 free(cof);
75 }
76 }
77
78 /**
79 * call close(3) for each fd in the close-on-fork list
80 */
81 void close_listed_fds(void)
82 {
83 struct close_on_fork *cof;
84
85 if (!initialized)
86 return;
87 list_for_each_entry(cof, &close_on_fork_list, node) {
88 PARA_DEBUG_LOG("closing fd %d\n", cof->fd);
89 close(cof->fd);
90 }
91 }