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