xref: /freebsd/lib/libthr/thread/thr_fork.c (revision 4928135658a9d0eaee37003df6137ab363fcb0b4)
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/cdefs.h>
61 __FBSDID("$FreeBSD$");
62 
63 #include <sys/syscall.h>
64 #include "namespace.h"
65 #include <errno.h>
66 #include <link.h>
67 #include <string.h>
68 #include <stdlib.h>
69 #include <unistd.h>
70 #include <pthread.h>
71 #include <spinlock.h>
72 #include "un-namespace.h"
73 
74 #include "libc_private.h"
75 #include "rtld_lock.h"
76 #include "thr_private.h"
77 
78 __weak_reference(_pthread_atfork, pthread_atfork);
79 
80 int
81 _pthread_atfork(void (*prepare)(void), void (*parent)(void),
82     void (*child)(void))
83 {
84 	struct pthread *curthread;
85 	struct pthread_atfork *af;
86 
87 	_thr_check_init();
88 
89 	if ((af = malloc(sizeof(struct pthread_atfork))) == NULL)
90 		return (ENOMEM);
91 
92 	curthread = _get_curthread();
93 	af->prepare = prepare;
94 	af->parent = parent;
95 	af->child = child;
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 	return (0);
102 }
103 
104 void
105 __pthread_cxa_finalize(struct dl_phdr_info *phdr_info)
106 {
107 	atfork_head    temp_list = TAILQ_HEAD_INITIALIZER(temp_list);
108 	struct pthread *curthread;
109 	struct pthread_atfork *af, *af1;
110 
111 	_thr_check_init();
112 
113 	curthread = _get_curthread();
114 	THR_CRITICAL_ENTER(curthread);
115 	_thr_rwl_wrlock(&_thr_atfork_lock);
116 	TAILQ_FOREACH_SAFE(af, &_thr_atfork_list, qe, af1) {
117 		if (__elf_phdr_match_addr(phdr_info, af->prepare) ||
118 		    __elf_phdr_match_addr(phdr_info, af->parent) ||
119 		    __elf_phdr_match_addr(phdr_info, af->child)) {
120 			TAILQ_REMOVE(&_thr_atfork_list, af, qe);
121 			TAILQ_INSERT_TAIL(&temp_list, af, qe);
122 		}
123 	}
124 	_thr_rwl_unlock(&_thr_atfork_lock);
125 	THR_CRITICAL_LEAVE(curthread);
126 	while ((af = TAILQ_FIRST(&temp_list)) != NULL) {
127 		TAILQ_REMOVE(&temp_list, af, qe);
128 		free(af);
129 	}
130 	_thr_tsd_unload(phdr_info);
131 	_thr_sigact_unload(phdr_info);
132 }
133 
134 __weak_reference(__thr_fork, _fork);
135 
136 pid_t
137 __thr_fork(void)
138 {
139 	struct pthread *curthread;
140 	struct pthread_atfork *af;
141 	pid_t ret;
142 	int errsave, cancelsave;
143 	int was_threaded;
144 	int rtld_locks[MAX_RTLD_LOCKS];
145 
146 	if (!_thr_is_inited())
147 		return (__sys_fork());
148 
149 	curthread = _get_curthread();
150 	cancelsave = curthread->no_cancel;
151 	curthread->no_cancel = 1;
152 	_thr_rwl_rdlock(&_thr_atfork_lock);
153 
154 	/* Run down atfork prepare handlers. */
155 	TAILQ_FOREACH_REVERSE(af, &_thr_atfork_list, atfork_head, qe) {
156 		if (af->prepare != NULL)
157 			af->prepare();
158 	}
159 
160 	/*
161 	 * Block all signals until we reach a safe point.
162 	 */
163 	_thr_signal_block(curthread);
164 	_thr_signal_prefork();
165 
166 	/*
167 	 * All bets are off as to what should happen soon if the parent
168 	 * process was not so kindly as to set up pthread fork hooks to
169 	 * relinquish all running threads.
170 	 */
171 	if (_thr_isthreaded() != 0) {
172 		was_threaded = 1;
173 		_malloc_prefork();
174 		__thr_pshared_atfork_pre();
175 		_rtld_atfork_pre(rtld_locks);
176 	} else {
177 		was_threaded = 0;
178 	}
179 
180 	/*
181 	 * Fork a new process.
182 	 * There is no easy way to pre-resolve the __sys_fork symbol
183 	 * without performing the fork.  Use the syscall(2)
184 	 * indirection, the syscall symbol is resolved in
185 	 * _thr_rtld_init() with side-effect free call.
186 	 */
187 	ret = syscall(SYS_fork);
188 	if (ret == 0) {
189 		/* Child process */
190 		errsave = errno;
191 		curthread->cancel_pending = 0;
192 		curthread->flags &= ~(THR_FLAGS_NEED_SUSPEND|THR_FLAGS_DETACHED);
193 
194 		/*
195 		 * Thread list will be reinitialized, and later we call
196 		 * _libpthread_init(), it will add us back to list.
197 		 */
198 		curthread->tlflags &= ~TLFLAGS_IN_TDLIST;
199 
200 		/* child is a new kernel thread. */
201 		thr_self(&curthread->tid);
202 
203 		/* clear other threads locked us. */
204 		_thr_umutex_init(&curthread->lock);
205 		_mutex_fork(curthread);
206 
207 		_thr_signal_postfork_child();
208 
209 		if (was_threaded) {
210 			_rtld_atfork_post(rtld_locks);
211 			__thr_pshared_atfork_post();
212 		}
213 		_thr_setthreaded(0);
214 
215 		/* reinitalize library. */
216 		_libpthread_init(curthread);
217 
218 		/* atfork is reinitialized by _libpthread_init()! */
219 		_thr_rwl_rdlock(&_thr_atfork_lock);
220 
221 		if (was_threaded) {
222 			__isthreaded = 1;
223 			_malloc_postfork();
224 			__isthreaded = 0;
225 		}
226 
227 		/* Ready to continue, unblock signals. */
228 		_thr_signal_unblock(curthread);
229 
230 		/* Run down atfork child handlers. */
231 		TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
232 			if (af->child != NULL)
233 				af->child();
234 		}
235 		_thr_rwlock_unlock(&_thr_atfork_lock);
236 		curthread->no_cancel = cancelsave;
237 	} else {
238 		/* Parent process */
239 		errsave = errno;
240 
241 		_thr_signal_postfork();
242 
243 		if (was_threaded) {
244 			_rtld_atfork_post(rtld_locks);
245 			__thr_pshared_atfork_post();
246 			_malloc_postfork();
247 		}
248 
249 		/* Ready to continue, unblock signals. */
250 		_thr_signal_unblock(curthread);
251 
252 		/* Run down atfork parent handlers. */
253 		TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
254 			if (af->parent != NULL)
255 				af->parent();
256 		}
257 
258 		_thr_rwlock_unlock(&_thr_atfork_lock);
259 		curthread->no_cancel = cancelsave;
260 		/* test async cancel */
261 		if (curthread->cancel_async)
262 			_thr_testcancel(curthread);
263 	}
264 	errno = errsave;
265 
266 	return (ret);
267 }
268