2 * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3 * Copyright (c) 2003 Daniel Eischen <deischen@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
29 * SPDX-License-Identifier: BSD-3-Clause
31 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
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.
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
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
63 #include <sys/syscall.h>
64 #include "namespace.h"
72 #include "un-namespace.h"
74 #include "libc_private.h"
75 #include "rtld_lock.h"
76 #include "thr_private.h"
78 __weak_reference(_thr_atfork, _pthread_atfork);
79 __weak_reference(_thr_atfork, pthread_atfork);
82 _thr_atfork(void (*prepare)(void), void (*parent)(void),
85 struct pthread *curthread;
86 struct pthread_atfork *af;
90 if ((af = malloc(sizeof(struct pthread_atfork))) == NULL)
93 curthread = _get_curthread();
94 af->prepare = prepare;
97 THR_CRITICAL_ENTER(curthread);
98 _thr_rwl_wrlock(&_thr_atfork_lock);
99 TAILQ_INSERT_TAIL(&_thr_atfork_list, af, qe);
100 _thr_rwl_unlock(&_thr_atfork_lock);
101 THR_CRITICAL_LEAVE(curthread);
106 __pthread_cxa_finalize(struct dl_phdr_info *phdr_info)
108 atfork_head temp_list = TAILQ_HEAD_INITIALIZER(temp_list);
109 struct pthread *curthread;
110 struct pthread_atfork *af, *af1;
114 curthread = _get_curthread();
115 THR_CRITICAL_ENTER(curthread);
116 _thr_rwl_wrlock(&_thr_atfork_lock);
117 TAILQ_FOREACH_SAFE(af, &_thr_atfork_list, qe, af1) {
118 if (__elf_phdr_match_addr(phdr_info, af->prepare) ||
119 __elf_phdr_match_addr(phdr_info, af->parent) ||
120 __elf_phdr_match_addr(phdr_info, af->child)) {
121 TAILQ_REMOVE(&_thr_atfork_list, af, qe);
122 TAILQ_INSERT_TAIL(&temp_list, af, qe);
125 _thr_rwl_unlock(&_thr_atfork_lock);
126 THR_CRITICAL_LEAVE(curthread);
127 while ((af = TAILQ_FIRST(&temp_list)) != NULL) {
128 TAILQ_REMOVE(&temp_list, af, qe);
131 _thr_tsd_unload(phdr_info);
132 _thr_sigact_unload(phdr_info);
135 __weak_reference(__thr_fork, _fork);
140 struct pthread *curthread;
141 struct pthread_atfork *af;
143 int errsave, cancelsave;
145 int rtld_locks[MAX_RTLD_LOCKS];
147 if (!_thr_is_inited())
148 return (__sys_fork());
150 curthread = _get_curthread();
151 cancelsave = curthread->no_cancel;
152 curthread->no_cancel = 1;
153 _thr_rwl_rdlock(&_thr_atfork_lock);
155 /* Run down atfork prepare handlers. */
156 TAILQ_FOREACH_REVERSE(af, &_thr_atfork_list, atfork_head, qe) {
157 if (af->prepare != NULL)
162 * Block all signals until we reach a safe point.
164 _thr_signal_block(curthread);
165 _thr_signal_prefork();
168 * All bets are off as to what should happen soon if the parent
169 * process was not so kindly as to set up pthread fork hooks to
170 * relinquish all running threads.
172 if (_thr_isthreaded() != 0) {
174 __thr_malloc_prefork(curthread);
176 __thr_pshared_atfork_pre();
177 _rtld_atfork_pre(rtld_locks);
183 * Fork a new process.
184 * There is no easy way to pre-resolve the __sys_fork symbol
185 * without performing the fork. Use the syscall(2)
186 * indirection, the syscall symbol is resolved in
187 * _thr_rtld_init() with side-effect free call.
189 ret = syscall(SYS_fork);
193 curthread->cancel_pending = 0;
194 curthread->flags &= ~(THR_FLAGS_NEED_SUSPEND|THR_FLAGS_DETACHED);
197 * Thread list will be reinitialized, and later we call
198 * _libpthread_init(), it will add us back to list.
200 curthread->tlflags &= ~TLFLAGS_IN_TDLIST;
202 /* before thr_self() */
204 __thr_malloc_postfork(curthread);
206 /* child is a new kernel thread. */
207 thr_self(&curthread->tid);
209 /* clear other threads locked us. */
210 _thr_umutex_init(&curthread->lock);
211 _mutex_fork(curthread);
213 _thr_signal_postfork_child();
216 _rtld_atfork_post(rtld_locks);
217 __thr_pshared_atfork_post();
221 /* reinitalize library. */
222 _libpthread_init(curthread);
224 /* atfork is reinitialized by _libpthread_init()! */
225 _thr_rwl_rdlock(&_thr_atfork_lock);
233 /* Ready to continue, unblock signals. */
234 _thr_signal_unblock(curthread);
236 /* Run down atfork child handlers. */
237 TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
238 if (af->child != NULL)
241 _thr_rwlock_unlock(&_thr_atfork_lock);
242 curthread->no_cancel = cancelsave;
247 _thr_signal_postfork();
250 __thr_malloc_postfork(curthread);
251 _rtld_atfork_post(rtld_locks);
252 __thr_pshared_atfork_post();
256 /* Ready to continue, unblock signals. */
257 _thr_signal_unblock(curthread);
259 /* Run down atfork parent handlers. */
260 TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
261 if (af->parent != NULL)
265 _thr_rwlock_unlock(&_thr_atfork_lock);
266 curthread->no_cancel = cancelsave;
267 /* test async cancel */
268 if (curthread->cancel_async)
269 _thr_testcancel(curthread);