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: src/lib/libpthread/thread/thr_atfork.c,v 1.1 2003/11/05 03:42:10 davidxu Exp $
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. All advertising materials mentioning features or use of this software
43  *    must display the following acknowledgement:
44  *	This product includes software developed by John Birrell.
45  * 4. Neither the name of the author nor the names of any co-contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  * $FreeBSD: src/lib/libpthread/thread/thr_fork.c,v 1.34 2003/11/05 18:18:45 deischen Exp $
62  * $DragonFly: src/lib/libthread_xu/thread/thr_fork.c,v 1.6 2006/04/06 13:03:09 davidxu Exp $
63  */
64 
65 #include "namespace.h"
66 #include <machine/tls.h>
67 
68 #include <errno.h>
69 #include <string.h>
70 #include <stdlib.h>
71 #include <unistd.h>
72 #include <fcntl.h>
73 #include <pthread.h>
74 #include <spinlock.h>
75 #include "un-namespace.h"
76 
77 #include "libc_private.h"
78 #include "thr_private.h"
79 
80 struct atfork_head	_thr_atfork_list;
81 umtx_t	_thr_atfork_lock;
82 
83 int
84 _pthread_atfork(void (*prepare)(void), void (*parent)(void),
85     void (*child)(void))
86 {
87 	struct pthread *curthread;
88 	struct pthread_atfork *af;
89 
90 	_thr_check_init();
91 
92 	if ((af = malloc(sizeof(struct pthread_atfork))) == NULL)
93 		return (ENOMEM);
94 
95 	curthread = tls_get_curthread();
96 	af->prepare = prepare;
97 	af->parent = parent;
98 	af->child = child;
99 	THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
100 	TAILQ_INSERT_TAIL(&_thr_atfork_list, af, qe);
101 	THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
102 	return (0);
103 }
104 
105 /*
106  * For a while, allow libpthread to work with a libc that doesn't
107  * export the malloc lock.
108  */
109 #pragma weak __malloc_lock
110 
111 pid_t _fork(void);
112 
113 pid_t
114 _fork(void)
115 {
116 	static umtx_t inprogress;
117 	static int waiters;
118 	umtx_t tmp;
119 
120 	struct pthread *curthread;
121 	struct pthread_atfork *af;
122 	pid_t ret;
123 	int errsave;
124 #ifndef __DragonFly__
125 	int unlock_malloc;
126 #endif
127 
128 	if (!_thr_is_inited())
129 		return (__sys_fork());
130 
131 	curthread = tls_get_curthread();
132 
133 	THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
134 	tmp = inprogress;
135 	while (tmp) {
136 		waiters++;
137 		THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
138 		_thr_umtx_wait(&inprogress, tmp, NULL, 0);
139 		THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
140 		waiters--;
141 		tmp = inprogress;
142 	}
143 	inprogress = 1;
144 	THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
145 
146 	/* Run down atfork prepare handlers. */
147 	TAILQ_FOREACH_REVERSE(af, &_thr_atfork_list, atfork_head, qe) {
148 		if (af->prepare != NULL)
149 			af->prepare();
150 	}
151 
152 #ifndef __DragonFly__
153 	/*
154 	 * Try our best to protect memory from being corrupted in
155 	 * child process because another thread in malloc code will
156 	 * simply be kill by fork().
157 	 */
158 	if ((_thr_isthreaded() != 0) && (__malloc_lock != NULL)) {
159 		unlock_malloc = 1;
160 		_spinlock(__malloc_lock);
161 	} else {
162 		unlock_malloc = 0;
163 	}
164 #endif
165 
166 	/*
167 	 * Block all signals until we reach a safe point.
168 	 */
169 	_thr_signal_block(curthread);
170 
171 	/* Fork a new process: */
172 	if ((ret = __sys_fork()) == 0) {
173 		/* Child process */
174 		errsave = errno;
175 		inprogress = 0;
176 		curthread->cancelflags &= ~THR_CANCEL_NEEDED;
177 		/*
178 		 * Thread list will be reinitialized, and later we call
179 		 * _libpthread_init(), it will add us back to list.
180 		 */
181 		curthread->tlflags &= ~(TLFLAGS_IN_TDLIST | TLFLAGS_DETACHED);
182 
183 		/* child is a new kernel thread. */
184 		curthread->tid = _thr_get_tid();
185 
186 		/* clear other threads locked us. */
187 		_thr_umtx_init(&curthread->lock);
188 		_thr_umtx_init(&_thr_atfork_lock);
189 		_thr_setthreaded(0);
190 
191 		/* reinitialize libc spinlocks, this includes __malloc_lock. */
192 		_thr_spinlock_init();
193 		_mutex_fork(curthread);
194 
195 		/* reinitalize library. */
196 		_libpthread_init(curthread);
197 
198 		/* Ready to continue, unblock signals. */
199 		_thr_signal_unblock(curthread);
200 
201 		/* Run down atfork child handlers. */
202 		TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
203 			if (af->child != NULL)
204 				af->child();
205 		}
206 	} else {
207 		/* Parent process */
208 		errsave = errno;
209 
210 #ifndef __DragonFly__
211 		if (unlock_malloc)
212 			_spinunlock(__malloc_lock);
213 #endif
214 
215 		/* Ready to continue, unblock signals. */
216 		_thr_signal_unblock(curthread);
217 
218 		/* Run down atfork parent handlers. */
219 		TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
220 			if (af->parent != NULL)
221 				af->parent();
222 		}
223 
224 		THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
225 		inprogress = 0;
226 		if (waiters)
227 			_thr_umtx_wake(&inprogress, waiters);
228 		THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
229 	}
230 	errno = errsave;
231 
232 	/* Return the process ID: */
233 	return (ret);
234 }
235 
236 __strong_reference(_fork, fork);
237 __strong_reference(_pthread_atfork, pthread_atfork);
238 
239