pthread: General pre-cleanup (style, typos etc)
[dragonfly.git] / lib / libthread_xu / thread / thr_fork.c
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: head/lib/libthr/thread/thr_fork.c 213096 2010-08-23 $
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 <sys/syscall.h>
62
63 #include <machine/tls.h>
64 #include <errno.h>
65 #include <link.h>
66 #include <string.h>
67 #include <stdlib.h>
68 #include <unistd.h>
69 #include <fcntl.h>
70 #include <pthread.h>
71 #include <spinlock.h>
72 #include "un-namespace.h"
73
74 #include "libc_private.h"
75 #include "thr_private.h"
76
77 struct atfork_head      _thr_atfork_list;
78 umtx_t  _thr_atfork_lock;
79
80 int
81 _pthread_atfork(void (*prepare)(void), void (*parent)(void),
82     void (*child)(void))
83 {
84         struct pthread *curthread;
85         struct pthread_atfork *af;
86
87         if ((af = malloc(sizeof(struct pthread_atfork))) == NULL)
88                 return (ENOMEM);
89
90         curthread = tls_get_curthread();
91         af->prepare = prepare;
92         af->parent = parent;
93         af->child = child;
94         THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
95         TAILQ_INSERT_TAIL(&_thr_atfork_list, af, qe);
96         THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
97         return (0);
98 }
99
100 void
101 __pthread_cxa_finalize(struct dl_phdr_info *phdr_info)
102 {
103         struct pthread *curthread;
104         struct pthread_atfork *af, *af1;
105
106         curthread = tls_get_curthread();
107         THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
108         TAILQ_FOREACH_MUTABLE(af, &_thr_atfork_list, qe, af1) {
109                 if (__elf_phdr_match_addr(phdr_info, af->prepare) ||
110                     __elf_phdr_match_addr(phdr_info, af->parent) ||
111                     __elf_phdr_match_addr(phdr_info, af->child)) {
112                         TAILQ_REMOVE(&_thr_atfork_list, af, qe);
113                         free(af);
114                 }
115         }
116         THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
117 }
118
119 /*
120  * For a while, allow libpthread to work with a libc that doesn't
121  * export the malloc lock.
122  */
123 #pragma weak __malloc_lock
124
125 pid_t _fork(void);
126
127 pid_t
128 _fork(void)
129 {
130         static umtx_t inprogress;
131         static int waiters;
132         umtx_t tmp;
133
134         struct pthread *curthread;
135         struct pthread_atfork *af;
136         pid_t ret;
137         int errsave;
138 #ifndef __DragonFly__
139         int unlock_malloc;
140 #endif
141
142         if (!_thr_is_inited())
143                 return (__syscall(SYS_fork));
144
145         curthread = tls_get_curthread();
146
147         THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
148         tmp = inprogress;
149         while (tmp) {
150                 waiters++;
151                 THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
152                 _thr_umtx_wait(&inprogress, tmp, NULL, 0);
153                 THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
154                 waiters--;
155                 tmp = inprogress;
156         }
157         inprogress = 1;
158         THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
159
160         /* Run down atfork prepare handlers. */
161         TAILQ_FOREACH_REVERSE(af, &_thr_atfork_list, atfork_head, qe) {
162                 if (af->prepare != NULL)
163                         af->prepare();
164         }
165
166 #ifndef __DragonFly__
167         /*
168          * Try our best to protect memory from being corrupted in
169          * child process because another thread in malloc code will
170          * simply be kill by fork().
171          */
172         if ((_thr_isthreaded() != 0) && (__malloc_lock != NULL)) {
173                 unlock_malloc = 1;
174                 _spinlock(__malloc_lock);
175         } else {
176                 unlock_malloc = 0;
177         }
178 #endif
179
180         /*
181          * Block all signals until we reach a safe point.
182          */
183         _thr_signal_block(curthread);
184
185         /* Fork a new process: */
186         if ((ret = __syscall(SYS_fork)) == 0) {
187                 /* Child process */
188                 errsave = errno;
189                 inprogress = 0;
190                 curthread->cancelflags &= ~THR_CANCEL_NEEDED;
191                 /*
192                  * Thread list will be reinitialized, and later we call
193                  * _libpthread_init(), it will add us back to list.
194                  */
195                 curthread->tlflags &= ~(TLFLAGS_IN_TDLIST | TLFLAGS_DETACHED);
196
197                 /* child is a new kernel thread. */
198                 curthread->tid = _thr_get_tid();
199
200                 /* clear other threads locked us. */
201                 _thr_umtx_init(&curthread->lock);
202                 _thr_umtx_init(&_thr_atfork_lock);
203                 _thr_setthreaded(0);
204
205                 /* reinitialize libc spinlocks, this includes __malloc_lock. */
206                 _thr_spinlock_init();
207                 _mutex_fork(curthread);
208
209                 /* reinitalize library. */
210                 _libpthread_init(curthread);
211
212                 /* Ready to continue, unblock signals. */
213                 _thr_signal_unblock(curthread);
214
215                 /* Run down atfork child handlers. */
216                 TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
217                         if (af->child != NULL)
218                                 af->child();
219                 }
220         } else {
221                 /* Parent process */
222                 errsave = errno;
223
224 #ifndef __DragonFly__
225                 if (unlock_malloc)
226                         _spinunlock(__malloc_lock);
227 #endif
228
229                 /* Ready to continue, unblock signals. */
230                 _thr_signal_unblock(curthread);
231
232                 /* Run down atfork parent handlers. */
233                 TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
234                         if (af->parent != NULL)
235                                 af->parent();
236                 }
237
238                 THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
239                 inprogress = 0;
240                 if (waiters)
241                         _thr_umtx_wake(&inprogress, waiters);
242                 THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
243         }
244         errno = errsave;
245
246         /* Return the process ID: */
247         return (ret);
248 }
249
250 __strong_reference(_fork, fork);
251 __strong_reference(_pthread_atfork, pthread_atfork);