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