1a091d823SDavid Xu /*
2a091d823SDavid Xu * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3a091d823SDavid Xu * Copyright (c) 2003 Daniel Eischen <deischen@freebsd.org>
4a091d823SDavid Xu * All rights reserved.
5a091d823SDavid Xu *
6a091d823SDavid Xu * Redistribution and use in source and binary forms, with or without
7a091d823SDavid Xu * modification, are permitted provided that the following conditions
8a091d823SDavid Xu * are met:
9a091d823SDavid Xu * 1. Redistributions of source code must retain the above copyright
10a091d823SDavid Xu * notice, this list of conditions and the following disclaimer.
11a091d823SDavid Xu * 2. Neither the name of the author nor the names of any co-contributors
12a091d823SDavid Xu * may be used to endorse or promote products derived from this software
13a091d823SDavid Xu * without specific prior written permission.
14a091d823SDavid Xu *
15a091d823SDavid Xu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16a091d823SDavid Xu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17a091d823SDavid Xu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18a091d823SDavid Xu * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19a091d823SDavid Xu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20a091d823SDavid Xu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21a091d823SDavid Xu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22a091d823SDavid Xu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23a091d823SDavid Xu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24a091d823SDavid Xu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25a091d823SDavid Xu * SUCH DAMAGE.
26a091d823SDavid Xu */
27a091d823SDavid Xu
288a16b7a1SPedro F. Giffuni /*-
298a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
308a16b7a1SPedro F. Giffuni *
31a091d823SDavid Xu * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
32a091d823SDavid Xu * All rights reserved.
33a091d823SDavid Xu *
34a091d823SDavid Xu * Redistribution and use in source and binary forms, with or without
35a091d823SDavid Xu * modification, are permitted provided that the following conditions
36a091d823SDavid Xu * are met:
37a091d823SDavid Xu * 1. Redistributions of source code must retain the above copyright
38a091d823SDavid Xu * notice, this list of conditions and the following disclaimer.
39a091d823SDavid Xu * 2. Redistributions in binary form must reproduce the above copyright
40a091d823SDavid Xu * notice, this list of conditions and the following disclaimer in the
41a091d823SDavid Xu * documentation and/or other materials provided with the distribution.
42fed32d75SWarner Losh * 3. Neither the name of the author nor the names of any co-contributors
43a091d823SDavid Xu * may be used to endorse or promote products derived from this software
44a091d823SDavid Xu * without specific prior written permission.
45a091d823SDavid Xu *
46a091d823SDavid Xu * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
47a091d823SDavid Xu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48a091d823SDavid Xu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49a091d823SDavid Xu * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50a091d823SDavid Xu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51a091d823SDavid Xu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52a091d823SDavid Xu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53a091d823SDavid Xu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54a091d823SDavid Xu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55a091d823SDavid Xu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56a091d823SDavid Xu * SUCH DAMAGE.
57a091d823SDavid Xu *
58a091d823SDavid Xu */
59a091d823SDavid Xu
601c70d007SKonstantin Belousov #include <sys/syscall.h>
6137a6356bSDavid Xu #include "namespace.h"
62a091d823SDavid Xu #include <errno.h>
63ea246b63SKonstantin Belousov #include <link.h>
64a091d823SDavid Xu #include <string.h>
65a091d823SDavid Xu #include <stdlib.h>
66a091d823SDavid Xu #include <unistd.h>
67a091d823SDavid Xu #include <pthread.h>
68a091d823SDavid Xu #include <spinlock.h>
6937a6356bSDavid Xu #include "un-namespace.h"
70a091d823SDavid Xu
71a091d823SDavid Xu #include "libc_private.h"
72cb5c4b10SKonstantin Belousov #include "rtld_lock.h"
73a091d823SDavid Xu #include "thr_private.h"
74a091d823SDavid Xu
750ab1bfc7SKonstantin Belousov __weak_reference(_thr_atfork, _pthread_atfork);
760ab1bfc7SKonstantin Belousov __weak_reference(_thr_atfork, pthread_atfork);
77a091d823SDavid Xu
786f49eafbSKonstantin Belousov bool _thr_after_fork = false;
796f49eafbSKonstantin Belousov
80a091d823SDavid Xu int
_thr_atfork(void (* prepare)(void),void (* parent)(void),void (* child)(void))810ab1bfc7SKonstantin Belousov _thr_atfork(void (*prepare)(void), void (*parent)(void),
82a091d823SDavid Xu void (*child)(void))
83a091d823SDavid Xu {
84a091d823SDavid Xu struct pthread *curthread;
85a091d823SDavid Xu struct pthread_atfork *af;
86a091d823SDavid Xu
87a091d823SDavid Xu if ((af = malloc(sizeof(struct pthread_atfork))) == NULL)
88a091d823SDavid Xu return (ENOMEM);
89a091d823SDavid Xu
90a091d823SDavid Xu af->prepare = prepare;
91a091d823SDavid Xu af->parent = parent;
92a091d823SDavid Xu af->child = child;
93*4b202f4fSKyle Evans
94*4b202f4fSKyle Evans if (_thr_initial != NULL) {
95*4b202f4fSKyle Evans curthread = _get_curthread();
96a523216bSDavid Xu THR_CRITICAL_ENTER(curthread);
97a523216bSDavid Xu _thr_rwl_wrlock(&_thr_atfork_lock);
98a091d823SDavid Xu TAILQ_INSERT_TAIL(&_thr_atfork_list, af, qe);
99ada33a6eSDavid Xu _thr_rwl_unlock(&_thr_atfork_lock);
100a523216bSDavid Xu THR_CRITICAL_LEAVE(curthread);
101*4b202f4fSKyle Evans } else {
102*4b202f4fSKyle Evans TAILQ_INSERT_TAIL(&_thr_atfork_list, af, qe);
103*4b202f4fSKyle Evans }
104*4b202f4fSKyle Evans
105a091d823SDavid Xu return (0);
106a091d823SDavid Xu }
107a091d823SDavid Xu
108ea246b63SKonstantin Belousov void
__pthread_cxa_finalize(struct dl_phdr_info * phdr_info)109ea246b63SKonstantin Belousov __pthread_cxa_finalize(struct dl_phdr_info *phdr_info)
110ea246b63SKonstantin Belousov {
11112c61c22SDavid Xu atfork_head temp_list = TAILQ_HEAD_INITIALIZER(temp_list);
112ea246b63SKonstantin Belousov struct pthread *curthread;
113ea246b63SKonstantin Belousov struct pthread_atfork *af, *af1;
114ea246b63SKonstantin Belousov
115ea246b63SKonstantin Belousov _thr_check_init();
116ea246b63SKonstantin Belousov
117ea246b63SKonstantin Belousov curthread = _get_curthread();
11812c61c22SDavid Xu THR_CRITICAL_ENTER(curthread);
119ada33a6eSDavid Xu _thr_rwl_wrlock(&_thr_atfork_lock);
120ea246b63SKonstantin Belousov TAILQ_FOREACH_SAFE(af, &_thr_atfork_list, qe, af1) {
121ea246b63SKonstantin Belousov if (__elf_phdr_match_addr(phdr_info, af->prepare) ||
122ea246b63SKonstantin Belousov __elf_phdr_match_addr(phdr_info, af->parent) ||
123ea246b63SKonstantin Belousov __elf_phdr_match_addr(phdr_info, af->child)) {
124ea246b63SKonstantin Belousov TAILQ_REMOVE(&_thr_atfork_list, af, qe);
12512c61c22SDavid Xu TAILQ_INSERT_TAIL(&temp_list, af, qe);
126ea246b63SKonstantin Belousov }
127ea246b63SKonstantin Belousov }
128ada33a6eSDavid Xu _thr_rwl_unlock(&_thr_atfork_lock);
12912c61c22SDavid Xu THR_CRITICAL_LEAVE(curthread);
13012c61c22SDavid Xu while ((af = TAILQ_FIRST(&temp_list)) != NULL) {
13112c61c22SDavid Xu TAILQ_REMOVE(&temp_list, af, qe);
13212c61c22SDavid Xu free(af);
13312c61c22SDavid Xu }
134ed0ee6afSDavid Xu _thr_tsd_unload(phdr_info);
13502c3c858SDavid Xu _thr_sigact_unload(phdr_info);
136ea246b63SKonstantin Belousov }
137ea246b63SKonstantin Belousov
13821f749daSKonstantin Belousov enum thr_fork_mode {
13921f749daSKonstantin Belousov MODE_FORK,
14021f749daSKonstantin Belousov MODE_PDFORK,
14121f749daSKonstantin Belousov };
14237a6356bSDavid Xu
14321f749daSKonstantin Belousov struct thr_fork_args {
14421f749daSKonstantin Belousov enum thr_fork_mode mode;
14521f749daSKonstantin Belousov void *fdp;
14621f749daSKonstantin Belousov int flags;
14721f749daSKonstantin Belousov };
14821f749daSKonstantin Belousov
14921f749daSKonstantin Belousov static pid_t
thr_fork_impl(const struct thr_fork_args * a)15021f749daSKonstantin Belousov thr_fork_impl(const struct thr_fork_args *a)
151a091d823SDavid Xu {
152a091d823SDavid Xu struct pthread *curthread;
153a091d823SDavid Xu struct pthread_atfork *af;
154a091d823SDavid Xu pid_t ret;
1554173ebefSDavid Xu int errsave, cancelsave;
15643af51a2SBrian Feldman int was_threaded;
15710b40346SKonstantin Belousov int rtld_locks[MAX_RTLD_LOCKS];
158a091d823SDavid Xu
15921f749daSKonstantin Belousov if (!_thr_is_inited()) {
16021f749daSKonstantin Belousov switch (a->mode) {
16121f749daSKonstantin Belousov case MODE_FORK:
162a091d823SDavid Xu return (__sys_fork());
16321f749daSKonstantin Belousov case MODE_PDFORK:
16421f749daSKonstantin Belousov return (__sys_pdfork(a->fdp, a->flags));
16521f749daSKonstantin Belousov default:
16621f749daSKonstantin Belousov errno = EDOOFUS;
16721f749daSKonstantin Belousov return (-1);
16821f749daSKonstantin Belousov }
16921f749daSKonstantin Belousov }
170a091d823SDavid Xu
171a091d823SDavid Xu curthread = _get_curthread();
1724173ebefSDavid Xu cancelsave = curthread->no_cancel;
1734173ebefSDavid Xu curthread->no_cancel = 1;
174ada33a6eSDavid Xu _thr_rwl_rdlock(&_thr_atfork_lock);
175a091d823SDavid Xu
176a091d823SDavid Xu /* Run down atfork prepare handlers. */
177a091d823SDavid Xu TAILQ_FOREACH_REVERSE(af, &_thr_atfork_list, atfork_head, qe) {
178a091d823SDavid Xu if (af->prepare != NULL)
179a091d823SDavid Xu af->prepare();
180a091d823SDavid Xu }
181a091d823SDavid Xu
182a091d823SDavid Xu /*
183ada33a6eSDavid Xu * Block all signals until we reach a safe point.
184ada33a6eSDavid Xu */
185ada33a6eSDavid Xu _thr_signal_block(curthread);
186ada33a6eSDavid Xu _thr_signal_prefork();
187ada33a6eSDavid Xu
188ada33a6eSDavid Xu /*
18943af51a2SBrian Feldman * All bets are off as to what should happen soon if the parent
19043af51a2SBrian Feldman * process was not so kindly as to set up pthread fork hooks to
19143af51a2SBrian Feldman * relinquish all running threads.
192a091d823SDavid Xu */
19352828c0eSJason Evans if (_thr_isthreaded() != 0) {
19443af51a2SBrian Feldman was_threaded = 1;
195381c2d2eSKonstantin Belousov __thr_malloc_prefork(curthread);
19652828c0eSJason Evans _malloc_prefork();
19753fd961fSKonstantin Belousov __thr_pshared_atfork_pre();
198cb5c4b10SKonstantin Belousov _rtld_atfork_pre(rtld_locks);
199a091d823SDavid Xu } else {
20043af51a2SBrian Feldman was_threaded = 0;
201a091d823SDavid Xu }
202a091d823SDavid Xu
2031c70d007SKonstantin Belousov /*
2041c70d007SKonstantin Belousov * Fork a new process.
2051c70d007SKonstantin Belousov * There is no easy way to pre-resolve the __sys_fork symbol
2061c70d007SKonstantin Belousov * without performing the fork. Use the syscall(2)
2071c70d007SKonstantin Belousov * indirection, the syscall symbol is resolved in
2081c70d007SKonstantin Belousov * _thr_rtld_init() with side-effect free call.
2091c70d007SKonstantin Belousov */
21021f749daSKonstantin Belousov switch (a->mode) {
21121f749daSKonstantin Belousov case MODE_FORK:
2121c70d007SKonstantin Belousov ret = syscall(SYS_fork);
21321f749daSKonstantin Belousov break;
21421f749daSKonstantin Belousov case MODE_PDFORK:
21521f749daSKonstantin Belousov ret = syscall(SYS_pdfork, a->fdp, a->flags);
21621f749daSKonstantin Belousov break;
21721f749daSKonstantin Belousov default:
21821f749daSKonstantin Belousov ret = -1;
21921f749daSKonstantin Belousov errno = EDOOFUS;
22021f749daSKonstantin Belousov break;
22121f749daSKonstantin Belousov }
22221f749daSKonstantin Belousov
2231c70d007SKonstantin Belousov if (ret == 0) {
224a091d823SDavid Xu /* Child process */
225a091d823SDavid Xu errsave = errno;
226f08e1bf6SDavid Xu curthread->cancel_pending = 0;
227a9b764e2SDavid Xu curthread->flags &= ~(THR_FLAGS_NEED_SUSPEND|THR_FLAGS_DETACHED);
2285656b5faSDavid Xu
229a091d823SDavid Xu /*
230a091d823SDavid Xu * Thread list will be reinitialized, and later we call
231a091d823SDavid Xu * _libpthread_init(), it will add us back to list.
232a091d823SDavid Xu */
233a9b764e2SDavid Xu curthread->tlflags &= ~TLFLAGS_IN_TDLIST;
234a091d823SDavid Xu
235381c2d2eSKonstantin Belousov /* before thr_self() */
236381c2d2eSKonstantin Belousov if (was_threaded)
237381c2d2eSKonstantin Belousov __thr_malloc_postfork(curthread);
238381c2d2eSKonstantin Belousov
239a091d823SDavid Xu /* child is a new kernel thread. */
240a091d823SDavid Xu thr_self(&curthread->tid);
241a091d823SDavid Xu
242a091d823SDavid Xu /* clear other threads locked us. */
243bddd24cdSDavid Xu _thr_umutex_init(&curthread->lock);
244ada33a6eSDavid Xu _mutex_fork(curthread);
24597df3834SAlexander Kabaev
24602c3c858SDavid Xu _thr_signal_postfork_child();
24702c3c858SDavid Xu
24853fd961fSKonstantin Belousov if (was_threaded) {
2496f49eafbSKonstantin Belousov _thr_after_fork = true;
25097df3834SAlexander Kabaev _rtld_atfork_post(rtld_locks);
2516f49eafbSKonstantin Belousov _thr_after_fork = false;
25253fd961fSKonstantin Belousov __thr_pshared_atfork_post();
25353fd961fSKonstantin Belousov }
254a091d823SDavid Xu _thr_setthreaded(0);
255a091d823SDavid Xu
256a091d823SDavid Xu /* reinitalize library. */
257a091d823SDavid Xu _libpthread_init(curthread);
258a091d823SDavid Xu
259d6717e1bSKonstantin Belousov /* atfork is reinitialized by _libpthread_init()! */
260ada33a6eSDavid Xu _thr_rwl_rdlock(&_thr_atfork_lock);
261a091d823SDavid Xu
26243af51a2SBrian Feldman if (was_threaded) {
263dbb1c64eSEric van Gyzen _thr_setthreaded(1);
264e711c6f0SKonstantin Belousov _malloc_postfork();
265dbb1c64eSEric van Gyzen _thr_setthreaded(0);
26629986e1bSKonstantin Belousov }
267cb5c4b10SKonstantin Belousov
268ada33a6eSDavid Xu /* Ready to continue, unblock signals. */
269ada33a6eSDavid Xu _thr_signal_unblock(curthread);
270ada33a6eSDavid Xu
271a091d823SDavid Xu /* Run down atfork child handlers. */
272a091d823SDavid Xu TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
273a091d823SDavid Xu if (af->child != NULL)
274a091d823SDavid Xu af->child();
275a091d823SDavid Xu }
276ada33a6eSDavid Xu _thr_rwlock_unlock(&_thr_atfork_lock);
2774173ebefSDavid Xu curthread->no_cancel = cancelsave;
278a091d823SDavid Xu } else {
279a091d823SDavid Xu /* Parent process */
280a091d823SDavid Xu errsave = errno;
281a091d823SDavid Xu
28202c3c858SDavid Xu _thr_signal_postfork();
28302c3c858SDavid Xu
28443af51a2SBrian Feldman if (was_threaded) {
285381c2d2eSKonstantin Belousov __thr_malloc_postfork(curthread);
286cb5c4b10SKonstantin Belousov _rtld_atfork_post(rtld_locks);
28753fd961fSKonstantin Belousov __thr_pshared_atfork_post();
28852828c0eSJason Evans _malloc_postfork();
289cb5c4b10SKonstantin Belousov }
29097986a2eSDavid Xu
291ada33a6eSDavid Xu /* Ready to continue, unblock signals. */
292ada33a6eSDavid Xu _thr_signal_unblock(curthread);
293ada33a6eSDavid Xu
294a091d823SDavid Xu /* Run down atfork parent handlers. */
295a091d823SDavid Xu TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
296a091d823SDavid Xu if (af->parent != NULL)
297a091d823SDavid Xu af->parent();
298a091d823SDavid Xu }
299a091d823SDavid Xu
300ada33a6eSDavid Xu _thr_rwlock_unlock(&_thr_atfork_lock);
3014173ebefSDavid Xu curthread->no_cancel = cancelsave;
3024173ebefSDavid Xu /* test async cancel */
30393ea4a71SDavid Xu if (curthread->cancel_async)
3044173ebefSDavid Xu _thr_testcancel(curthread);
305a091d823SDavid Xu }
306a091d823SDavid Xu errno = errsave;
307a091d823SDavid Xu
308a091d823SDavid Xu return (ret);
309a091d823SDavid Xu }
31021f749daSKonstantin Belousov
31121f749daSKonstantin Belousov __weak_reference(__thr_fork, _fork);
31221f749daSKonstantin Belousov
31321f749daSKonstantin Belousov pid_t
__thr_fork(void)31421f749daSKonstantin Belousov __thr_fork(void)
31521f749daSKonstantin Belousov {
31621f749daSKonstantin Belousov struct thr_fork_args a;
31721f749daSKonstantin Belousov
31821f749daSKonstantin Belousov a.mode = MODE_FORK;
31921f749daSKonstantin Belousov return (thr_fork_impl(&a));
32021f749daSKonstantin Belousov }
32121f749daSKonstantin Belousov
32221f749daSKonstantin Belousov pid_t
__thr_pdfork(int * fdp,int flags)32321f749daSKonstantin Belousov __thr_pdfork(int *fdp, int flags)
32421f749daSKonstantin Belousov {
32521f749daSKonstantin Belousov struct thr_fork_args a;
32621f749daSKonstantin Belousov
32721f749daSKonstantin Belousov a.mode = MODE_PDFORK;
32821f749daSKonstantin Belousov a.fdp = fdp;
32921f749daSKonstantin Belousov a.flags = flags;
33021f749daSKonstantin Belousov return (thr_fork_impl(&a));
33121f749daSKonstantin Belousov }
332