pthread_atfork.c revision 1.8.8.2 1 1.8.8.2 martin /* $NetBSD: pthread_atfork.c,v 1.8.8.2 2008/04/28 20:23:00 martin Exp $ */
2 1.8.8.2 martin
3 1.8.8.2 martin /*-
4 1.8.8.2 martin * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.8.8.2 martin * All rights reserved.
6 1.8.8.2 martin *
7 1.8.8.2 martin * This code is derived from software contributed to The NetBSD Foundation
8 1.8.8.2 martin * by Nathan J. Williams.
9 1.8.8.2 martin *
10 1.8.8.2 martin * Redistribution and use in source and binary forms, with or without
11 1.8.8.2 martin * modification, are permitted provided that the following conditions
12 1.8.8.2 martin * are met:
13 1.8.8.2 martin * 1. Redistributions of source code must retain the above copyright
14 1.8.8.2 martin * notice, this list of conditions and the following disclaimer.
15 1.8.8.2 martin * 2. Redistributions in binary form must reproduce the above copyright
16 1.8.8.2 martin * notice, this list of conditions and the following disclaimer in the
17 1.8.8.2 martin * documentation and/or other materials provided with the distribution.
18 1.8.8.2 martin *
19 1.8.8.2 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.8.8.2 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.8.8.2 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.8.8.2 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.8.8.2 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.8.8.2 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.8.8.2 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.8.8.2 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.8.8.2 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.8.8.2 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.8.8.2 martin * POSSIBILITY OF SUCH DAMAGE.
30 1.8.8.2 martin */
31 1.8.8.2 martin
32 1.8.8.2 martin #include <sys/cdefs.h>
33 1.8.8.2 martin #if defined(LIBC_SCCS) && !defined(lint)
34 1.8.8.2 martin __RCSID("$NetBSD: pthread_atfork.c,v 1.8.8.2 2008/04/28 20:23:00 martin Exp $");
35 1.8.8.2 martin #endif /* LIBC_SCCS and not lint */
36 1.8.8.2 martin
37 1.8.8.2 martin #include "namespace.h"
38 1.8.8.2 martin
39 1.8.8.2 martin #include <errno.h>
40 1.8.8.2 martin #include <stdlib.h>
41 1.8.8.2 martin #include <unistd.h>
42 1.8.8.2 martin #include <sys/queue.h>
43 1.8.8.2 martin #include "reentrant.h"
44 1.8.8.2 martin
45 1.8.8.2 martin #ifdef __weak_alias
46 1.8.8.2 martin __weak_alias(pthread_atfork, _pthread_atfork)
47 1.8.8.2 martin __weak_alias(fork, _fork)
48 1.8.8.2 martin #endif /* __weak_alias */
49 1.8.8.2 martin
50 1.8.8.2 martin pid_t __fork __P((void)); /* XXX */
51 1.8.8.2 martin
52 1.8.8.2 martin struct atfork_callback {
53 1.8.8.2 martin SIMPLEQ_ENTRY(atfork_callback) next;
54 1.8.8.2 martin void (*fn)(void);
55 1.8.8.2 martin };
56 1.8.8.2 martin
57 1.8.8.2 martin /*
58 1.8.8.2 martin * Hypothetically, we could protect the queues with a rwlock which is
59 1.8.8.2 martin * write-locked by pthread_atfork() and read-locked by fork(), but
60 1.8.8.2 martin * since the intended use of the functions is obtaining locks to hold
61 1.8.8.2 martin * across the fork, forking is going to be serialized anyway.
62 1.8.8.2 martin */
63 1.8.8.2 martin static struct atfork_callback atfork_builtin;
64 1.8.8.2 martin static mutex_t atfork_lock = MUTEX_INITIALIZER;
65 1.8.8.2 martin SIMPLEQ_HEAD(atfork_callback_q, atfork_callback);
66 1.8.8.2 martin
67 1.8.8.2 martin static struct atfork_callback_q prepareq = SIMPLEQ_HEAD_INITIALIZER(prepareq);
68 1.8.8.2 martin static struct atfork_callback_q parentq = SIMPLEQ_HEAD_INITIALIZER(parentq);
69 1.8.8.2 martin static struct atfork_callback_q childq = SIMPLEQ_HEAD_INITIALIZER(childq);
70 1.8.8.2 martin
71 1.8.8.2 martin static struct atfork_callback *
72 1.8.8.2 martin af_alloc(void)
73 1.8.8.2 martin {
74 1.8.8.2 martin
75 1.8.8.2 martin if (atfork_builtin.fn == NULL)
76 1.8.8.2 martin return &atfork_builtin;
77 1.8.8.2 martin
78 1.8.8.2 martin return malloc(sizeof(atfork_builtin));
79 1.8.8.2 martin }
80 1.8.8.2 martin
81 1.8.8.2 martin static void
82 1.8.8.2 martin af_free(struct atfork_callback *af)
83 1.8.8.2 martin {
84 1.8.8.2 martin
85 1.8.8.2 martin if (af != &atfork_builtin)
86 1.8.8.2 martin free(af);
87 1.8.8.2 martin }
88 1.8.8.2 martin
89 1.8.8.2 martin int
90 1.8.8.2 martin pthread_atfork(void (*prepare)(void), void (*parent)(void),
91 1.8.8.2 martin void (*child)(void))
92 1.8.8.2 martin {
93 1.8.8.2 martin struct atfork_callback *newprepare, *newparent, *newchild;
94 1.8.8.2 martin
95 1.8.8.2 martin newprepare = newparent = newchild = NULL;
96 1.8.8.2 martin
97 1.8.8.2 martin mutex_lock(&atfork_lock);
98 1.8.8.2 martin if (prepare != NULL) {
99 1.8.8.2 martin newprepare = af_alloc();
100 1.8.8.2 martin if (newprepare == NULL) {
101 1.8.8.2 martin mutex_unlock(&atfork_lock);
102 1.8.8.2 martin return ENOMEM;
103 1.8.8.2 martin }
104 1.8.8.2 martin newprepare->fn = prepare;
105 1.8.8.2 martin }
106 1.8.8.2 martin
107 1.8.8.2 martin if (parent != NULL) {
108 1.8.8.2 martin newparent = af_alloc();
109 1.8.8.2 martin if (newparent == NULL) {
110 1.8.8.2 martin if (newprepare != NULL)
111 1.8.8.2 martin af_free(newprepare);
112 1.8.8.2 martin mutex_unlock(&atfork_lock);
113 1.8.8.2 martin return ENOMEM;
114 1.8.8.2 martin }
115 1.8.8.2 martin newparent->fn = parent;
116 1.8.8.2 martin }
117 1.8.8.2 martin
118 1.8.8.2 martin if (child != NULL) {
119 1.8.8.2 martin newchild = af_alloc();
120 1.8.8.2 martin if (newchild == NULL) {
121 1.8.8.2 martin if (newprepare != NULL)
122 1.8.8.2 martin af_free(newprepare);
123 1.8.8.2 martin if (newparent != NULL)
124 1.8.8.2 martin af_free(newparent);
125 1.8.8.2 martin mutex_unlock(&atfork_lock);
126 1.8.8.2 martin return ENOMEM;
127 1.8.8.2 martin }
128 1.8.8.2 martin newchild->fn = child;
129 1.8.8.2 martin }
130 1.8.8.2 martin
131 1.8.8.2 martin /*
132 1.8.8.2 martin * The order in which the functions are called is specified as
133 1.8.8.2 martin * LIFO for the prepare handler and FIFO for the others; insert
134 1.8.8.2 martin * at the head and tail as appropriate so that SIMPLEQ_FOREACH()
135 1.8.8.2 martin * produces the right order.
136 1.8.8.2 martin */
137 1.8.8.2 martin if (prepare)
138 1.8.8.2 martin SIMPLEQ_INSERT_HEAD(&prepareq, newprepare, next);
139 1.8.8.2 martin if (parent)
140 1.8.8.2 martin SIMPLEQ_INSERT_TAIL(&parentq, newparent, next);
141 1.8.8.2 martin if (child)
142 1.8.8.2 martin SIMPLEQ_INSERT_TAIL(&childq, newchild, next);
143 1.8.8.2 martin mutex_unlock(&atfork_lock);
144 1.8.8.2 martin
145 1.8.8.2 martin return 0;
146 1.8.8.2 martin }
147 1.8.8.2 martin
148 1.8.8.2 martin pid_t
149 1.8.8.2 martin fork(void)
150 1.8.8.2 martin {
151 1.8.8.2 martin struct atfork_callback *iter;
152 1.8.8.2 martin pid_t ret;
153 1.8.8.2 martin
154 1.8.8.2 martin mutex_lock(&atfork_lock);
155 1.8.8.2 martin SIMPLEQ_FOREACH(iter, &prepareq, next)
156 1.8.8.2 martin (*iter->fn)();
157 1.8.8.2 martin
158 1.8.8.2 martin ret = __fork();
159 1.8.8.2 martin
160 1.8.8.2 martin if (ret != 0) {
161 1.8.8.2 martin /*
162 1.8.8.2 martin * We are the parent. It doesn't matter here whether
163 1.8.8.2 martin * the fork call succeeded or failed.
164 1.8.8.2 martin */
165 1.8.8.2 martin SIMPLEQ_FOREACH(iter, &parentq, next)
166 1.8.8.2 martin (*iter->fn)();
167 1.8.8.2 martin mutex_unlock(&atfork_lock);
168 1.8.8.2 martin } else {
169 1.8.8.2 martin /* We are the child */
170 1.8.8.2 martin SIMPLEQ_FOREACH(iter, &childq, next)
171 1.8.8.2 martin (*iter->fn)();
172 1.8.8.2 martin /*
173 1.8.8.2 martin * Note: We are explicitly *not* unlocking
174 1.8.8.2 martin * atfork_lock. Unlocking atfork_lock is problematic,
175 1.8.8.2 martin * because if any threads in the parent blocked on it
176 1.8.8.2 martin * between the initial lock and the fork() syscall,
177 1.8.8.2 martin * unlocking in the child will try to schedule
178 1.8.8.2 martin * threads, and either the internal mutex interlock or
179 1.8.8.2 martin * the runqueue spinlock could have been held at the
180 1.8.8.2 martin * moment of fork(). Since the other threads do not
181 1.8.8.2 martin * exist in this process, the spinlock will never be
182 1.8.8.2 martin * unlocked, and we would wedge.
183 1.8.8.2 martin * Instead, we reinitialize atfork_lock, since we know
184 1.8.8.2 martin * that the state of the atfork lists is consistent here,
185 1.8.8.2 martin * and that there are no other threads to be affected by
186 1.8.8.2 martin * the forcible cleaning of the queue.
187 1.8.8.2 martin * This permits double-forking to work, although
188 1.8.8.2 martin * it requires knowing that it's "safe" to initialize
189 1.8.8.2 martin * a locked mutex in this context.
190 1.8.8.2 martin *
191 1.8.8.2 martin * The problem exists for users of this interface,
192 1.8.8.2 martin * too, since the intented use of pthread_atfork() is
193 1.8.8.2 martin * to acquire locks across the fork call to ensure
194 1.8.8.2 martin * that the child sees consistent state. There's not
195 1.8.8.2 martin * much that can usefully be done in a child handler,
196 1.8.8.2 martin * and conventional wisdom discourages using them, but
197 1.8.8.2 martin * they're part of the interface, so here we are...
198 1.8.8.2 martin */
199 1.8.8.2 martin mutex_init(&atfork_lock, NULL);
200 1.8.8.2 martin }
201 1.8.8.2 martin
202 1.8.8.2 martin return ret;
203 1.8.8.2 martin }
204