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