xref: /freebsd/lib/libthr/thread/thr_fork.c (revision 3157ba21)
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 <string.h>
63 #include <stdlib.h>
64 #include <unistd.h>
65 #include <pthread.h>
66 #include <spinlock.h>
67 #include "un-namespace.h"
68 
69 #include "libc_private.h"
70 #include "rtld_lock.h"
71 #include "thr_private.h"
72 
73 __weak_reference(_pthread_atfork, pthread_atfork);
74 
75 int
76 _pthread_atfork(void (*prepare)(void), void (*parent)(void),
77     void (*child)(void))
78 {
79 	struct pthread *curthread;
80 	struct pthread_atfork *af;
81 
82 	_thr_check_init();
83 
84 	if ((af = malloc(sizeof(struct pthread_atfork))) == NULL)
85 		return (ENOMEM);
86 
87 	curthread = _get_curthread();
88 	af->prepare = prepare;
89 	af->parent = parent;
90 	af->child = child;
91 	THR_UMUTEX_LOCK(curthread, &_thr_atfork_lock);
92 	TAILQ_INSERT_TAIL(&_thr_atfork_list, af, qe);
93 	THR_UMUTEX_UNLOCK(curthread, &_thr_atfork_lock);
94 	return (0);
95 }
96 
97 __weak_reference(_fork, fork);
98 
99 pid_t _fork(void);
100 
101 pid_t
102 _fork(void)
103 {
104 	struct pthread *curthread;
105 	struct pthread_atfork *af;
106 	pid_t ret;
107 	int errsave;
108 	int was_threaded;
109 	int rtld_locks[MAX_RTLD_LOCKS];
110 
111 	if (!_thr_is_inited())
112 		return (__sys_fork());
113 
114 	curthread = _get_curthread();
115 
116 	THR_UMUTEX_LOCK(curthread, &_thr_atfork_lock);
117 
118 	/* Run down atfork prepare handlers. */
119 	TAILQ_FOREACH_REVERSE(af, &_thr_atfork_list, atfork_head, qe) {
120 		if (af->prepare != NULL)
121 			af->prepare();
122 	}
123 
124 	/*
125 	 * All bets are off as to what should happen soon if the parent
126 	 * process was not so kindly as to set up pthread fork hooks to
127 	 * relinquish all running threads.
128 	 */
129 	if (_thr_isthreaded() != 0) {
130 		was_threaded = 1;
131 		_malloc_prefork();
132 		_rtld_atfork_pre(rtld_locks);
133 	} else {
134 		was_threaded = 0;
135 	}
136 
137 	/*
138 	 * Block all signals until we reach a safe point.
139 	 */
140 	_thr_signal_block(curthread);
141 
142 	/* Fork a new process: */
143 	if ((ret = __sys_fork()) == 0) {
144 		/* Child process */
145 		errsave = errno;
146 		curthread->cancel_pending = 0;
147 		curthread->flags &= ~THR_FLAGS_NEED_SUSPEND;
148 
149 		/*
150 		 * Thread list will be reinitialized, and later we call
151 		 * _libpthread_init(), it will add us back to list.
152 		 */
153 		curthread->tlflags &= ~(TLFLAGS_IN_TDLIST | TLFLAGS_DETACHED);
154 
155 		/* child is a new kernel thread. */
156 		thr_self(&curthread->tid);
157 
158 		/* clear other threads locked us. */
159 		_thr_umutex_init(&curthread->lock);
160 		_thr_umutex_init(&_thr_atfork_lock);
161 
162 		if (was_threaded)
163 			_rtld_atfork_post(rtld_locks);
164 		_thr_setthreaded(0);
165 
166 		/* reinitialize libc spinlocks. */
167 		_thr_spinlock_init();
168 		_mutex_fork(curthread);
169 
170 		/* reinitalize library. */
171 		_libpthread_init(curthread);
172 
173 		/* Ready to continue, unblock signals. */
174 		_thr_signal_unblock(curthread);
175 
176 		if (was_threaded) {
177 			__isthreaded = 1;
178 			_malloc_postfork();
179 			__isthreaded = 0;
180 		}
181 
182 		/* Run down atfork child handlers. */
183 		TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
184 			if (af->child != NULL)
185 				af->child();
186 		}
187 	} else {
188 		/* Parent process */
189 		errsave = errno;
190 
191 		/* Ready to continue, unblock signals. */
192 		_thr_signal_unblock(curthread);
193 
194 		if (was_threaded) {
195 			_rtld_atfork_post(rtld_locks);
196 			_malloc_postfork();
197 		}
198 
199 		/* Run down atfork parent handlers. */
200 		TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
201 			if (af->parent != NULL)
202 				af->parent();
203 		}
204 
205 		THR_UMUTEX_UNLOCK(curthread, &_thr_atfork_lock);
206 	}
207 	errno = errsave;
208 
209 	/* Return the process ID: */
210 	return (ret);
211 }
212