xref: /dflybsd-src/lib/libthread_xu/thread/thr_fork.c (revision f53c59abfc0df55e8eea01535cbd9791481f9b7c)
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  * $FreeBSD: head/lib/libthr/thread/thr_fork.c 213096 2010-08-23 $
28  */
29 
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 <machine/tls.h>
63 #include <errno.h>
64 #include <link.h>
65 #include <string.h>
66 #include <stdlib.h>
67 #include <unistd.h>
68 #include <fcntl.h>
69 #include <pthread.h>
70 #include <spinlock.h>
71 #include "un-namespace.h"
72 
73 #include "libc_private.h"
74 #include "thr_private.h"
75 
76 struct atfork_head	_thr_atfork_list;
77 struct atfork_head	_thr_atfork_kern_list;
78 umtx_t	_thr_atfork_lock;
79 
80 /*
81  * Execute a function in parent before and after the fork, and
82  * in the child.
83  */
84 int
85 _pthread_atfork(void (*prepare)(void), void (*parent)(void),
86 		void (*child)(void))
87 {
88 	struct pthread *curthread;
89 	struct pthread_atfork *af;
90 
91 	if ((af = malloc(sizeof(struct pthread_atfork))) == NULL)
92 		return (ENOMEM);
93 
94 	curthread = tls_get_curthread();
95 	af->prepare = prepare;
96 	af->parent = parent;
97 	af->child = child;
98 	THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
99 	TAILQ_INSERT_TAIL(&_thr_atfork_list, af, qe);
100 	THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
101 	return (0);
102 }
103 
104 /*
105  * Private at-fork used by the rtld and sem code, guaranteed to order
106  * after user fork handlers in prepare, and before user fork handlers
107  * in the post-fork parent and in the child.
108  *
109  * This is used to ensure no interference between internal and user
110  * fork handlers, in particular we do not want to lock-out rtld or
111  * semaphores before user fork handlers run, and we want to recover
112  * before any user post-fork handlers run.
113  */
114 void
115 _thr_atfork_kern(void (*prepare)(void), void (*parent)(void),
116 		 void (*child)(void))
117 {
118 	struct pthread *curthread;
119 	struct pthread_atfork *af;
120 
121 	af = malloc(sizeof(struct pthread_atfork));
122 
123 	curthread = tls_get_curthread();
124 	af->prepare = prepare;
125 	af->parent = parent;
126 	af->child = child;
127 	THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
128 	TAILQ_INSERT_TAIL(&_thr_atfork_kern_list, af, qe);
129 	THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
130 }
131 
132 void
133 __pthread_cxa_finalize(struct dl_phdr_info *phdr_info)
134 {
135 	struct pthread *curthread;
136 	struct pthread_atfork *af, *af1;
137 
138 	curthread = tls_get_curthread();
139 	THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
140 	TAILQ_FOREACH_MUTABLE(af, &_thr_atfork_list, qe, af1) {
141 		if (__elf_phdr_match_addr(phdr_info, af->prepare) ||
142 		    __elf_phdr_match_addr(phdr_info, af->parent) ||
143 		    __elf_phdr_match_addr(phdr_info, af->child)) {
144 			TAILQ_REMOVE(&_thr_atfork_list, af, qe);
145 			free(af);
146 		}
147 	}
148 	THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
149 }
150 
151 /*
152  * For a while, allow libpthread to work with a libc that doesn't
153  * export the malloc lock.
154  */
155 #pragma weak __malloc_lock
156 
157 pid_t _fork(void);
158 
159 pid_t
160 _fork(void)
161 {
162 	static umtx_t inprogress;
163 	static int waiters;
164 	umtx_t tmp;
165 
166 	struct pthread *curthread;
167 	struct pthread_atfork *af;
168 	pid_t ret;
169 	int errsave;
170 #ifndef __DragonFly__
171 	int unlock_malloc;
172 #endif
173 
174 	if (!_thr_is_inited())
175 		return (__syscall(SYS_fork));
176 
177 	curthread = tls_get_curthread();
178 
179 	THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
180 	tmp = inprogress;
181 	while (tmp) {
182 		waiters++;
183 		THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
184 		_thr_umtx_wait(&inprogress, tmp, NULL, 0);
185 		THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
186 		waiters--;
187 		tmp = inprogress;
188 	}
189 	inprogress = 1;
190 	THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
191 
192 	/* Run down atfork prepare handlers. */
193 	TAILQ_FOREACH_REVERSE(af, &_thr_atfork_list, atfork_head, qe) {
194 		if (af->prepare != NULL)
195 			af->prepare();
196 	}
197 	TAILQ_FOREACH_REVERSE(af, &_thr_atfork_kern_list, atfork_head, qe) {
198 		if (af->prepare != NULL)
199 			af->prepare();
200 	}
201 
202 #ifndef __DragonFly__
203 	/*
204 	 * Try our best to protect memory from being corrupted in
205 	 * child process because another thread in malloc code will
206 	 * simply be kill by fork().
207 	 */
208 	if ((_thr_isthreaded() != 0) && (__malloc_lock != NULL)) {
209 		unlock_malloc = 1;
210 		_spinlock(__malloc_lock);
211 	} else {
212 		unlock_malloc = 0;
213 	}
214 #endif
215 
216 	/*
217 	 * Block all signals until we reach a safe point.
218 	 */
219 	_thr_signal_block(curthread);
220 #ifdef _PTHREADS_DEBUGGING
221 	_thr_log("fork-parent\n", 12);
222 #endif
223 
224 	/* Fork a new process: */
225 	if ((ret = __syscall(SYS_fork)) == 0) {
226 		/* Child process */
227 		errsave = errno;
228 		inprogress = 0;
229 		curthread->cancelflags &= ~THR_CANCEL_NEEDED;
230 		/*
231 		 * Thread list will be reinitialized, and later we call
232 		 * _libpthread_init(), it will add us back to list.
233 		 */
234 		curthread->tlflags &= ~(TLFLAGS_IN_TDLIST | TLFLAGS_DETACHED);
235 
236 		/* child is a new kernel thread. */
237 		curthread->tid = _thr_get_tid();
238 
239 		/* clear other threads locked us. */
240 		_thr_umtx_init(&curthread->lock);
241 		_thr_umtx_init(&_thr_atfork_lock);
242 		_thr_setthreaded(0);
243 
244 		/* reinitialize libc spinlocks, this includes __malloc_lock. */
245 		_thr_spinlock_init();
246 #ifdef _PTHREADS_DEBUGGING
247 		_thr_log("fork-child\n", 11);
248 #endif
249 		_mutex_fork(curthread);
250 
251 		/* reinitalize library. */
252 		_libpthread_init(curthread);
253 
254 		/* Ready to continue, unblock signals. */
255 		_thr_signal_unblock(curthread);
256 
257 		/* Run down atfork child handlers. */
258 		TAILQ_FOREACH(af, &_thr_atfork_kern_list, qe) {
259 			if (af->child != NULL)
260 				af->child();
261 		}
262 		TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
263 			if (af->child != NULL)
264 				af->child();
265 		}
266 	} else {
267 		/* Parent process */
268 		errsave = errno;
269 
270 #ifndef __DragonFly__
271 		if (unlock_malloc)
272 			_spinunlock(__malloc_lock);
273 #endif
274 #ifdef _PTHREADS_DEBUGGING
275 		_thr_log("fork-done\n", 10);
276 #endif
277 		/* Ready to continue, unblock signals. */
278 		_thr_signal_unblock(curthread);
279 
280 		/* Run down atfork parent handlers. */
281 		TAILQ_FOREACH(af, &_thr_atfork_kern_list, qe) {
282 			if (af->parent != NULL)
283 				af->parent();
284 		}
285 		TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
286 			if (af->parent != NULL)
287 				af->parent();
288 		}
289 
290 		THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
291 		inprogress = 0;
292 		if (waiters)
293 			_thr_umtx_wake(&inprogress, waiters);
294 		THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
295 	}
296 	errno = errsave;
297 
298 	/* Return the process ID: */
299 	return (ret);
300 }
301 
302 __strong_reference(_fork, fork);
303 __strong_reference(_pthread_atfork, pthread_atfork);
304