xref: /freebsd/lib/libthr/thread/thr_fork.c (revision 9336e0699bda8a301cd2bfa37106b6ec5e32012e)
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 "thr_private.h"
71 
72 __weak_reference(_pthread_atfork, pthread_atfork);
73 
74 int
75 _pthread_atfork(void (*prepare)(void), void (*parent)(void),
76     void (*child)(void))
77 {
78 	struct pthread *curthread;
79 	struct pthread_atfork *af;
80 
81 	_thr_check_init();
82 
83 	if ((af = malloc(sizeof(struct pthread_atfork))) == NULL)
84 		return (ENOMEM);
85 
86 	curthread = _get_curthread();
87 	af->prepare = prepare;
88 	af->parent = parent;
89 	af->child = child;
90 	THR_UMUTEX_LOCK(curthread, &_thr_atfork_lock);
91 	TAILQ_INSERT_TAIL(&_thr_atfork_list, af, qe);
92 	THR_UMUTEX_UNLOCK(curthread, &_thr_atfork_lock);
93 	return (0);
94 }
95 
96 __weak_reference(_fork, fork);
97 
98 pid_t _fork(void);
99 
100 pid_t
101 _fork(void)
102 {
103 	struct pthread *curthread;
104 	struct pthread_atfork *af;
105 	pid_t ret;
106 	int errsave;
107 	int unlock_malloc;
108 
109 	if (!_thr_is_inited())
110 		return (__sys_fork());
111 
112 	curthread = _get_curthread();
113 
114 	THR_UMUTEX_LOCK(curthread, &_thr_atfork_lock);
115 
116 	/* Run down atfork prepare handlers. */
117 	TAILQ_FOREACH_REVERSE(af, &_thr_atfork_list, atfork_head, qe) {
118 		if (af->prepare != NULL)
119 			af->prepare();
120 	}
121 
122 	/*
123 	 * Try our best to protect memory from being corrupted in
124 	 * child process because another thread in malloc code will
125 	 * simply be kill by fork().
126 	 */
127 	if (_thr_isthreaded() != 0) {
128 		unlock_malloc = 1;
129 		_malloc_prefork();
130 	} else {
131 		unlock_malloc = 0;
132 	}
133 
134 	/*
135 	 * Block all signals until we reach a safe point.
136 	 */
137 	_thr_signal_block(curthread);
138 
139 	/* Fork a new process: */
140 	if ((ret = __sys_fork()) == 0) {
141 		/* Child process */
142 		errsave = errno;
143 		curthread->cancel_pending = 0;
144 		curthread->flags &= ~THR_FLAGS_NEED_SUSPEND;
145 
146 		/*
147 		 * Thread list will be reinitialized, and later we call
148 		 * _libpthread_init(), it will add us back to list.
149 		 */
150 		curthread->tlflags &= ~(TLFLAGS_IN_TDLIST | TLFLAGS_DETACHED);
151 
152 		/* child is a new kernel thread. */
153 		thr_self(&curthread->tid);
154 
155 		/* clear other threads locked us. */
156 		_thr_umutex_init(&curthread->lock);
157 		_thr_umutex_init(&_thr_atfork_lock);
158 		_thr_setthreaded(0);
159 
160 		/* reinitialize libc spinlocks. */
161 		_thr_spinlock_init();
162 		_mutex_fork(curthread);
163 
164 		/* reinitalize library. */
165 		_libpthread_init(curthread);
166 
167 		/* Ready to continue, unblock signals. */
168 		_thr_signal_unblock(curthread);
169 
170 		/* Run down atfork child handlers. */
171 		TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
172 			if (af->child != NULL)
173 				af->child();
174 		}
175 	} else {
176 		/* Parent process */
177 		errsave = errno;
178 
179 		/* Ready to continue, unblock signals. */
180 		_thr_signal_unblock(curthread);
181 
182 		if (unlock_malloc)
183 			_malloc_postfork();
184 
185 		/* Run down atfork parent handlers. */
186 		TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
187 			if (af->parent != NULL)
188 				af->parent();
189 		}
190 
191 		THR_UMUTEX_UNLOCK(curthread, &_thr_atfork_lock);
192 	}
193 	errno = errsave;
194 
195 	/* Return the process ID: */
196 	return (ret);
197 }
198