xref: /freebsd/lib/libthr/thread/thr_fork.c (revision e0c4386e)
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 #include <sys/syscall.h>
62 #include "namespace.h"
63 #include <errno.h>
64 #include <link.h>
65 #include <string.h>
66 #include <stdlib.h>
67 #include <unistd.h>
68 #include <pthread.h>
69 #include <spinlock.h>
70 #include "un-namespace.h"
71 
72 #include "libc_private.h"
73 #include "rtld_lock.h"
74 #include "thr_private.h"
75 
76 __weak_reference(_thr_atfork, _pthread_atfork);
77 __weak_reference(_thr_atfork, pthread_atfork);
78 
79 bool _thr_after_fork = false;
80 
81 int
82 _thr_atfork(void (*prepare)(void), void (*parent)(void),
83     void (*child)(void))
84 {
85 	struct pthread *curthread;
86 	struct pthread_atfork *af;
87 
88 	_thr_check_init();
89 
90 	if ((af = malloc(sizeof(struct pthread_atfork))) == NULL)
91 		return (ENOMEM);
92 
93 	curthread = _get_curthread();
94 	af->prepare = prepare;
95 	af->parent = parent;
96 	af->child = child;
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);
102 	return (0);
103 }
104 
105 void
106 __pthread_cxa_finalize(struct dl_phdr_info *phdr_info)
107 {
108 	atfork_head    temp_list = TAILQ_HEAD_INITIALIZER(temp_list);
109 	struct pthread *curthread;
110 	struct pthread_atfork *af, *af1;
111 
112 	_thr_check_init();
113 
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);
123 		}
124 	}
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);
129 		free(af);
130 	}
131 	_thr_tsd_unload(phdr_info);
132 	_thr_sigact_unload(phdr_info);
133 }
134 
135 enum thr_fork_mode {
136 	MODE_FORK,
137 	MODE_PDFORK,
138 };
139 
140 struct thr_fork_args {
141 	enum thr_fork_mode mode;
142 	void *fdp;
143 	int flags;
144 };
145 
146 static pid_t
147 thr_fork_impl(const struct thr_fork_args *a)
148 {
149 	struct pthread *curthread;
150 	struct pthread_atfork *af;
151 	pid_t ret;
152 	int errsave, cancelsave;
153 	int was_threaded;
154 	int rtld_locks[MAX_RTLD_LOCKS];
155 
156 	if (!_thr_is_inited()) {
157 		switch (a->mode) {
158 		case MODE_FORK:
159 			return (__sys_fork());
160 		case MODE_PDFORK:
161 			return (__sys_pdfork(a->fdp, a->flags));
162 		default:
163 			errno = EDOOFUS;
164 			return (-1);
165 		}
166 	}
167 
168 	curthread = _get_curthread();
169 	cancelsave = curthread->no_cancel;
170 	curthread->no_cancel = 1;
171 	_thr_rwl_rdlock(&_thr_atfork_lock);
172 
173 	/* Run down atfork prepare handlers. */
174 	TAILQ_FOREACH_REVERSE(af, &_thr_atfork_list, atfork_head, qe) {
175 		if (af->prepare != NULL)
176 			af->prepare();
177 	}
178 
179 	/*
180 	 * Block all signals until we reach a safe point.
181 	 */
182 	_thr_signal_block(curthread);
183 	_thr_signal_prefork();
184 
185 	/*
186 	 * All bets are off as to what should happen soon if the parent
187 	 * process was not so kindly as to set up pthread fork hooks to
188 	 * relinquish all running threads.
189 	 */
190 	if (_thr_isthreaded() != 0) {
191 		was_threaded = 1;
192 		__thr_malloc_prefork(curthread);
193 		_malloc_prefork();
194 		__thr_pshared_atfork_pre();
195 		_rtld_atfork_pre(rtld_locks);
196 	} else {
197 		was_threaded = 0;
198 	}
199 
200 	/*
201 	 * Fork a new process.
202 	 * There is no easy way to pre-resolve the __sys_fork symbol
203 	 * without performing the fork.  Use the syscall(2)
204 	 * indirection, the syscall symbol is resolved in
205 	 * _thr_rtld_init() with side-effect free call.
206 	 */
207 	switch (a->mode) {
208 	case MODE_FORK:
209 		ret = syscall(SYS_fork);
210 		break;
211 	case MODE_PDFORK:
212 		ret = syscall(SYS_pdfork, a->fdp, a->flags);
213 		break;
214 	default:
215 		ret = -1;
216 		errno = EDOOFUS;
217 		break;
218 	}
219 
220 	if (ret == 0) {
221 		/* Child process */
222 		errsave = errno;
223 		curthread->cancel_pending = 0;
224 		curthread->flags &= ~(THR_FLAGS_NEED_SUSPEND|THR_FLAGS_DETACHED);
225 
226 		/*
227 		 * Thread list will be reinitialized, and later we call
228 		 * _libpthread_init(), it will add us back to list.
229 		 */
230 		curthread->tlflags &= ~TLFLAGS_IN_TDLIST;
231 
232 		/* before thr_self() */
233 		if (was_threaded)
234 			__thr_malloc_postfork(curthread);
235 
236 		/* child is a new kernel thread. */
237 		thr_self(&curthread->tid);
238 
239 		/* clear other threads locked us. */
240 		_thr_umutex_init(&curthread->lock);
241 		_mutex_fork(curthread);
242 
243 		_thr_signal_postfork_child();
244 
245 		if (was_threaded) {
246 			_thr_after_fork = true;
247 			_rtld_atfork_post(rtld_locks);
248 			_thr_after_fork = false;
249 			__thr_pshared_atfork_post();
250 		}
251 		_thr_setthreaded(0);
252 
253 		/* reinitalize library. */
254 		_libpthread_init(curthread);
255 
256 		/* atfork is reinitialized by _libpthread_init()! */
257 		_thr_rwl_rdlock(&_thr_atfork_lock);
258 
259 		if (was_threaded) {
260 			_thr_setthreaded(1);
261 			_malloc_postfork();
262 			_thr_setthreaded(0);
263 		}
264 
265 		/* Ready to continue, unblock signals. */
266 		_thr_signal_unblock(curthread);
267 
268 		/* Run down atfork child handlers. */
269 		TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
270 			if (af->child != NULL)
271 				af->child();
272 		}
273 		_thr_rwlock_unlock(&_thr_atfork_lock);
274 		curthread->no_cancel = cancelsave;
275 	} else {
276 		/* Parent process */
277 		errsave = errno;
278 
279 		_thr_signal_postfork();
280 
281 		if (was_threaded) {
282 			__thr_malloc_postfork(curthread);
283 			_rtld_atfork_post(rtld_locks);
284 			__thr_pshared_atfork_post();
285 			_malloc_postfork();
286 		}
287 
288 		/* Ready to continue, unblock signals. */
289 		_thr_signal_unblock(curthread);
290 
291 		/* Run down atfork parent handlers. */
292 		TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
293 			if (af->parent != NULL)
294 				af->parent();
295 		}
296 
297 		_thr_rwlock_unlock(&_thr_atfork_lock);
298 		curthread->no_cancel = cancelsave;
299 		/* test async cancel */
300 		if (curthread->cancel_async)
301 			_thr_testcancel(curthread);
302 	}
303 	errno = errsave;
304 
305 	return (ret);
306 }
307 
308 __weak_reference(__thr_fork, _fork);
309 
310 pid_t
311 __thr_fork(void)
312 {
313 	struct thr_fork_args a;
314 
315 	a.mode = MODE_FORK;
316 	return (thr_fork_impl(&a));
317 }
318 
319 pid_t
320 __thr_pdfork(int *fdp, int flags)
321 {
322 	struct thr_fork_args a;
323 
324 	a.mode = MODE_PDFORK;
325 	a.fdp = fdp;
326 	a.flags = flags;
327 	return (thr_fork_impl(&a));
328 }
329