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 "namespace.h"
61 #include <sys/syscall.h>
62 
63 #include <machine/tls.h>
64 
65 #include <errno.h>
66 #include <link.h>
67 #include <string.h>
68 #include <stdlib.h>
69 #include <unistd.h>
70 #include <fcntl.h>
71 #include <pthread.h>
72 #include <spinlock.h>
73 #include "un-namespace.h"
74 
75 #include "libc_private.h"
76 #include "thr_private.h"
77 
78 struct atfork_head	_thr_atfork_list;
79 umtx_t	_thr_atfork_lock;
80 
81 int
82 _pthread_atfork(void (*prepare)(void), void (*parent)(void),
83     void (*child)(void))
84 {
85 	struct pthread *curthread;
86 	struct pthread_atfork *af;
87 
88 	if ((af = malloc(sizeof(struct pthread_atfork))) == NULL)
89 		return (ENOMEM);
90 
91 	curthread = tls_get_curthread();
92 	af->prepare = prepare;
93 	af->parent = parent;
94 	af->child = child;
95 	THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
96 	TAILQ_INSERT_TAIL(&_thr_atfork_list, af, qe);
97 	THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
98 	return (0);
99 }
100 
101 void
102 __pthread_cxa_finalize(struct dl_phdr_info *phdr_info)
103 {
104 	struct pthread *curthread;
105 	struct pthread_atfork *af, *af1;
106 
107 	curthread = tls_get_curthread();
108 	THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
109 	TAILQ_FOREACH_MUTABLE(af, &_thr_atfork_list, qe, af1) {
110 		if (__elf_phdr_match_addr(phdr_info, af->prepare) ||
111 		    __elf_phdr_match_addr(phdr_info, af->parent) ||
112 		    __elf_phdr_match_addr(phdr_info, af->child)) {
113 			TAILQ_REMOVE(&_thr_atfork_list, af, qe);
114 			free(af);
115 		}
116 	}
117 	THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
118 }
119 
120 /*
121  * For a while, allow libpthread to work with a libc that doesn't
122  * export the malloc lock.
123  */
124 #pragma weak __malloc_lock
125 
126 pid_t _fork(void);
127 
128 pid_t
129 _fork(void)
130 {
131 	static umtx_t inprogress;
132 	static int waiters;
133 	umtx_t tmp;
134 
135 	struct pthread *curthread;
136 	struct pthread_atfork *af;
137 	pid_t ret;
138 	int errsave;
139 #ifndef __DragonFly__
140 	int unlock_malloc;
141 #endif
142 
143 	if (!_thr_is_inited())
144 		return (__syscall(SYS_fork));
145 
146 	curthread = tls_get_curthread();
147 
148 	THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
149 	tmp = inprogress;
150 	while (tmp) {
151 		waiters++;
152 		THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
153 		_thr_umtx_wait(&inprogress, tmp, NULL, 0);
154 		THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
155 		waiters--;
156 		tmp = inprogress;
157 	}
158 	inprogress = 1;
159 	THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
160 
161 	/* Run down atfork prepare handlers. */
162 	TAILQ_FOREACH_REVERSE(af, &_thr_atfork_list, atfork_head, qe) {
163 		if (af->prepare != NULL)
164 			af->prepare();
165 	}
166 
167 #ifndef __DragonFly__
168 	/*
169 	 * Try our best to protect memory from being corrupted in
170 	 * child process because another thread in malloc code will
171 	 * simply be kill by fork().
172 	 */
173 	if ((_thr_isthreaded() != 0) && (__malloc_lock != NULL)) {
174 		unlock_malloc = 1;
175 		_spinlock(__malloc_lock);
176 	} else {
177 		unlock_malloc = 0;
178 	}
179 #endif
180 
181 	/*
182 	 * Block all signals until we reach a safe point.
183 	 */
184 	_thr_signal_block(curthread);
185 
186 	/* Fork a new process: */
187 	if ((ret = __syscall(SYS_fork)) == 0) {
188 		/* Child process */
189 		errsave = errno;
190 		inprogress = 0;
191 		curthread->cancelflags &= ~THR_CANCEL_NEEDED;
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 | TLFLAGS_DETACHED);
197 
198 		/* child is a new kernel thread. */
199 		curthread->tid = _thr_get_tid();
200 
201 		/* clear other threads locked us. */
202 		_thr_umtx_init(&curthread->lock);
203 		_thr_umtx_init(&_thr_atfork_lock);
204 		_thr_setthreaded(0);
205 
206 		/* reinitialize libc spinlocks, this includes __malloc_lock. */
207 		_thr_spinlock_init();
208 		_mutex_fork(curthread);
209 
210 		/* reinitalize library. */
211 		_libpthread_init(curthread);
212 
213 		/* Ready to continue, unblock signals. */
214 		_thr_signal_unblock(curthread);
215 
216 		/* Run down atfork child handlers. */
217 		TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
218 			if (af->child != NULL)
219 				af->child();
220 		}
221 	} else {
222 		/* Parent process */
223 		errsave = errno;
224 
225 #ifndef __DragonFly__
226 		if (unlock_malloc)
227 			_spinunlock(__malloc_lock);
228 #endif
229 
230 		/* Ready to continue, unblock signals. */
231 		_thr_signal_unblock(curthread);
232 
233 		/* Run down atfork parent handlers. */
234 		TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
235 			if (af->parent != NULL)
236 				af->parent();
237 		}
238 
239 		THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
240 		inprogress = 0;
241 		if (waiters)
242 			_thr_umtx_wake(&inprogress, waiters);
243 		THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
244 	}
245 	errno = errsave;
246 
247 	/* Return the process ID: */
248 	return (ret);
249 }
250 
251 __strong_reference(_fork, fork);
252 __strong_reference(_pthread_atfork, pthread_atfork);
253 
254