xref: /freebsd/lib/libthr/thread/thr_fork.c (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
1 /*
2  * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3  * Copyright (c) 2003 Daniel Eischen <deischen@freebsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Neither the name of the author nor the names of any co-contributors
12  *    may be used to endorse or promote products derived from this software
13  *    without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*-
29  * SPDX-License-Identifier: BSD-3-Clause
30  *
31  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
32  * All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  * 1. Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in the
41  *    documentation and/or other materials provided with the distribution.
42  * 3. Neither the name of the author nor the names of any co-contributors
43  *    may be used to endorse or promote products derived from this software
44  *    without specific prior written permission.
45  *
46  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56  * SUCH DAMAGE.
57  *
58  */
59 
60 #include <sys/syscall.h>
61 #include "namespace.h"
62 #include <errno.h>
63 #include <link.h>
64 #include <string.h>
65 #include <stdlib.h>
66 #include <unistd.h>
67 #include <pthread.h>
68 #include <spinlock.h>
69 #include "un-namespace.h"
70 
71 #include "libc_private.h"
72 #include "rtld_lock.h"
73 #include "thr_private.h"
74 
75 __weak_reference(_thr_atfork, _pthread_atfork);
76 __weak_reference(_thr_atfork, pthread_atfork);
77 
78 bool _thr_after_fork = false;
79 
80 int
81 _thr_atfork(void (*prepare)(void), void (*parent)(void),
82     void (*child)(void))
83 {
84 	struct pthread *curthread;
85 	struct pthread_atfork *af;
86 
87 	if ((af = malloc(sizeof(struct pthread_atfork))) == NULL)
88 		return (ENOMEM);
89 
90 	af->prepare = prepare;
91 	af->parent = parent;
92 	af->child = child;
93 
94 	if (_thr_initial != NULL) {
95 		curthread = _get_curthread();
96 		THR_CRITICAL_ENTER(curthread);
97 		_thr_rwl_wrlock(&_thr_atfork_lock);
98 		TAILQ_INSERT_TAIL(&_thr_atfork_list, af, qe);
99 		_thr_rwl_unlock(&_thr_atfork_lock);
100 		THR_CRITICAL_LEAVE(curthread);
101 	} else {
102 		TAILQ_INSERT_TAIL(&_thr_atfork_list, af, qe);
103 	}
104 
105 	return (0);
106 }
107 
108 void
109 __pthread_cxa_finalize(struct dl_phdr_info *phdr_info)
110 {
111 	atfork_head    temp_list = TAILQ_HEAD_INITIALIZER(temp_list);
112 	struct pthread *curthread;
113 	struct pthread_atfork *af, *af1;
114 
115 	_thr_check_init();
116 
117 	curthread = _get_curthread();
118 	THR_CRITICAL_ENTER(curthread);
119 	_thr_rwl_wrlock(&_thr_atfork_lock);
120 	TAILQ_FOREACH_SAFE(af, &_thr_atfork_list, qe, af1) {
121 		if (__elf_phdr_match_addr(phdr_info, af->prepare) ||
122 		    __elf_phdr_match_addr(phdr_info, af->parent) ||
123 		    __elf_phdr_match_addr(phdr_info, af->child)) {
124 			TAILQ_REMOVE(&_thr_atfork_list, af, qe);
125 			TAILQ_INSERT_TAIL(&temp_list, af, qe);
126 		}
127 	}
128 	_thr_rwl_unlock(&_thr_atfork_lock);
129 	THR_CRITICAL_LEAVE(curthread);
130 	while ((af = TAILQ_FIRST(&temp_list)) != NULL) {
131 		TAILQ_REMOVE(&temp_list, af, qe);
132 		free(af);
133 	}
134 	_thr_tsd_unload(phdr_info);
135 	_thr_sigact_unload(phdr_info);
136 }
137 
138 enum thr_fork_mode {
139 	MODE_FORK,
140 	MODE_PDFORK,
141 };
142 
143 struct thr_fork_args {
144 	enum thr_fork_mode mode;
145 	void *fdp;
146 	int flags;
147 };
148 
149 static pid_t
150 thr_fork_impl(const struct thr_fork_args *a)
151 {
152 	struct pthread *curthread;
153 	struct pthread_atfork *af;
154 	pid_t ret;
155 	int errsave, cancelsave;
156 	int was_threaded;
157 	int rtld_locks[MAX_RTLD_LOCKS];
158 
159 	if (!_thr_is_inited()) {
160 		switch (a->mode) {
161 		case MODE_FORK:
162 			return (__sys_fork());
163 		case MODE_PDFORK:
164 			return (__sys_pdfork(a->fdp, a->flags));
165 		default:
166 			errno = EDOOFUS;
167 			return (-1);
168 		}
169 	}
170 
171 	curthread = _get_curthread();
172 	cancelsave = curthread->no_cancel;
173 	curthread->no_cancel = 1;
174 	_thr_rwl_rdlock(&_thr_atfork_lock);
175 
176 	/* Run down atfork prepare handlers. */
177 	TAILQ_FOREACH_REVERSE(af, &_thr_atfork_list, atfork_head, qe) {
178 		if (af->prepare != NULL)
179 			af->prepare();
180 	}
181 
182 	/*
183 	 * Block all signals until we reach a safe point.
184 	 */
185 	_thr_signal_block(curthread);
186 	_thr_signal_prefork();
187 
188 	/*
189 	 * All bets are off as to what should happen soon if the parent
190 	 * process was not so kindly as to set up pthread fork hooks to
191 	 * relinquish all running threads.
192 	 */
193 	if (_thr_isthreaded() != 0) {
194 		was_threaded = 1;
195 		__thr_malloc_prefork(curthread);
196 		_malloc_prefork();
197 		__thr_pshared_atfork_pre();
198 		_rtld_atfork_pre(rtld_locks);
199 	} else {
200 		was_threaded = 0;
201 	}
202 
203 	/*
204 	 * Fork a new process.
205 	 * There is no easy way to pre-resolve the __sys_fork symbol
206 	 * without performing the fork.  Use the syscall(2)
207 	 * indirection, the syscall symbol is resolved in
208 	 * _thr_rtld_init() with side-effect free call.
209 	 */
210 	switch (a->mode) {
211 	case MODE_FORK:
212 		ret = syscall(SYS_fork);
213 		break;
214 	case MODE_PDFORK:
215 		ret = syscall(SYS_pdfork, a->fdp, a->flags);
216 		break;
217 	default:
218 		ret = -1;
219 		errno = EDOOFUS;
220 		break;
221 	}
222 
223 	if (ret == 0) {
224 		/* Child process */
225 		errsave = errno;
226 		curthread->cancel_pending = 0;
227 		curthread->flags &= ~(THR_FLAGS_NEED_SUSPEND|THR_FLAGS_DETACHED);
228 
229 		/*
230 		 * Thread list will be reinitialized, and later we call
231 		 * _libpthread_init(), it will add us back to list.
232 		 */
233 		curthread->tlflags &= ~TLFLAGS_IN_TDLIST;
234 
235 		/* before thr_self() */
236 		if (was_threaded)
237 			__thr_malloc_postfork(curthread);
238 
239 		/* child is a new kernel thread. */
240 		thr_self(&curthread->tid);
241 
242 		/* clear other threads locked us. */
243 		_thr_umutex_init(&curthread->lock);
244 		_mutex_fork(curthread);
245 
246 		_thr_signal_postfork_child();
247 
248 		if (was_threaded) {
249 			_thr_after_fork = true;
250 			_rtld_atfork_post(rtld_locks);
251 			_thr_after_fork = false;
252 			__thr_pshared_atfork_post();
253 		}
254 		_thr_setthreaded(0);
255 
256 		/* reinitalize library. */
257 		_libpthread_init(curthread);
258 
259 		/* atfork is reinitialized by _libpthread_init()! */
260 		_thr_rwl_rdlock(&_thr_atfork_lock);
261 
262 		if (was_threaded) {
263 			_thr_setthreaded(1);
264 			_malloc_postfork();
265 			_thr_setthreaded(0);
266 		}
267 
268 		/* Ready to continue, unblock signals. */
269 		_thr_signal_unblock(curthread);
270 
271 		/* Run down atfork child handlers. */
272 		TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
273 			if (af->child != NULL)
274 				af->child();
275 		}
276 		_thr_rwlock_unlock(&_thr_atfork_lock);
277 		curthread->no_cancel = cancelsave;
278 	} else {
279 		/* Parent process */
280 		errsave = errno;
281 
282 		_thr_signal_postfork();
283 
284 		if (was_threaded) {
285 			__thr_malloc_postfork(curthread);
286 			_rtld_atfork_post(rtld_locks);
287 			__thr_pshared_atfork_post();
288 			_malloc_postfork();
289 		}
290 
291 		/* Ready to continue, unblock signals. */
292 		_thr_signal_unblock(curthread);
293 
294 		/* Run down atfork parent handlers. */
295 		TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
296 			if (af->parent != NULL)
297 				af->parent();
298 		}
299 
300 		_thr_rwlock_unlock(&_thr_atfork_lock);
301 		curthread->no_cancel = cancelsave;
302 		/* test async cancel */
303 		if (curthread->cancel_async)
304 			_thr_testcancel(curthread);
305 	}
306 	errno = errsave;
307 
308 	return (ret);
309 }
310 
311 __weak_reference(__thr_fork, _fork);
312 
313 pid_t
314 __thr_fork(void)
315 {
316 	struct thr_fork_args a;
317 
318 	a.mode = MODE_FORK;
319 	return (thr_fork_impl(&a));
320 }
321 
322 pid_t
323 __thr_pdfork(int *fdp, int flags)
324 {
325 	struct thr_fork_args a;
326 
327 	a.mode = MODE_PDFORK;
328 	a.fdp = fdp;
329 	a.flags = flags;
330 	return (thr_fork_impl(&a));
331 }
332