xref: /dflybsd-src/lib/libthread_xu/thread/thr_fork.c (revision 23c32883e759b0ea42fdaff39e661bd1a12e3b9f)
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.2 2005/03/15 11:24:23 davidxu Exp $
63  */
64 #include <errno.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 
72 #include "libc_private.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_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 /*
100  * For a while, allow libpthread to work with a libc that doesn't
101  * export the malloc lock.
102  */
103 #pragma weak __malloc_lock
104 
105 __weak_reference(_fork, fork);
106 
107 pid_t
108 _fork(void)
109 {
110 	static umtx_t inprogress;
111 	static int waiters;
112 	umtx_t tmp;
113 
114 	struct pthread *curthread;
115 	struct pthread_atfork *af;
116 	pid_t ret;
117 	int errsave;
118 #ifndef __DragonFly__
119 	int unlock_malloc;
120 #endif
121 
122 	if (!_thr_is_inited())
123 		return (__sys_fork());
124 
125 	curthread = _get_curthread();
126 
127 	THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
128 	tmp = inprogress;
129 	while (tmp) {
130 		waiters++;
131 		THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
132 		_thr_umtx_wait(&inprogress, tmp, NULL, 0);
133 		THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
134 		waiters--;
135 		tmp = inprogress;
136 	}
137 	inprogress = 1;
138 	THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
139 
140 	/* Run down atfork prepare handlers. */
141 	TAILQ_FOREACH_REVERSE(af, &_thr_atfork_list, atfork_head, qe) {
142 		if (af->prepare != NULL)
143 			af->prepare();
144 	}
145 
146 #ifndef __DragonFly__
147 	/*
148 	 * Try our best to protect memory from being corrupted in
149 	 * child process because another thread in malloc code will
150 	 * simply be kill by fork().
151 	 */
152 	if ((_thr_isthreaded() != 0) && (__malloc_lock != NULL)) {
153 		unlock_malloc = 1;
154 		_spinlock(__malloc_lock);
155 	} else {
156 		unlock_malloc = 0;
157 	}
158 #endif
159 
160 	/*
161 	 * Block all signals until we reach a safe point.
162 	 */
163 	_thr_signal_block(curthread);
164 
165 	/* Fork a new process: */
166 	if ((ret = __sys_fork()) == 0) {
167 		/* Child process */
168 		errsave = errno;
169 		inprogress = 0;
170 		curthread->cancelflags &= ~THR_CANCEL_NEEDED;
171 		/*
172 		 * Thread list will be reinitialized, and later we call
173 		 * _libpthread_init(), it will add us back to list.
174 		 */
175 		curthread->tlflags &= ~(TLFLAGS_IN_TDLIST | TLFLAGS_DETACHED);
176 
177 		/* child is a new kernel thread. */
178 		curthread->tid = _thr_get_tid();
179 
180 		/* clear other threads locked us. */
181 		_thr_umtx_init(&curthread->lock);
182 		_thr_umtx_init(&_thr_atfork_lock);
183 		_thr_setthreaded(0);
184 
185 		/* reinitialize libc spinlocks, this includes __malloc_lock. */
186 		_thr_spinlock_init();
187 		_mutex_fork(curthread);
188 
189 		/* reinitalize library. */
190 		_libpthread_init(curthread);
191 
192 		/* Ready to continue, unblock signals. */
193 		_thr_signal_unblock(curthread);
194 
195 		/* Run down atfork child handlers. */
196 		TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
197 			if (af->child != NULL)
198 				af->child();
199 		}
200 	} else {
201 		/* Parent process */
202 		errsave = errno;
203 
204 #ifndef __DragonFly__
205 		if (unlock_malloc)
206 			_spinunlock(__malloc_lock);
207 #endif
208 
209 		/* Ready to continue, unblock signals. */
210 		_thr_signal_unblock(curthread);
211 
212 		/* Run down atfork parent handlers. */
213 		TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
214 			if (af->parent != NULL)
215 				af->parent();
216 		}
217 
218 		THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
219 		inprogress = 0;
220 		if (waiters)
221 			_thr_umtx_wake(&inprogress, waiters);
222 		THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
223 	}
224 	errno = errsave;
225 
226 	/* Return the process ID: */
227 	return (ret);
228 }
229