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