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