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