1/* Copyright (C) 2002-2016 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19#include <assert.h>
20#include <stdlib.h>
21#include <unistd.h>
22#include <sys/types.h>
23#include <sysdep.h>
24#include <libio/libioP.h>
25#include <tls.h>
26#include <hp-timing.h>
27#include <ldsodefs.h>
28#include <stdio-lock.h>
29#include <atomic.h>
30#include <nptl/pthreadP.h>
31#include <fork.h>
32#include <arch-fork.h>
33#include <futex-internal.h>
34#include <malloc/malloc-internal.h>
35
36static void
37fresetlockfiles (void)
38{
39 _IO_ITER i;
40
41 for (i = _IO_iter_begin(); i != _IO_iter_end(); i = _IO_iter_next(i))
42 if ((_IO_iter_file (i)->_flags & _IO_USER_LOCK) == 0)
43 _IO_lock_init (*((_IO_lock_t *) _IO_iter_file(i)->_lock));
44}
45
46
47pid_t
48__libc_fork (void)
49{
50 pid_t pid;
51 struct used_handler
52 {
53 struct fork_handler *handler;
54 struct used_handler *next;
55 } *allp = NULL;
56
57 /* Determine if we are running multiple threads. We skip some fork
58 handlers in the single-thread case, to make fork safer to use in
59 signal handlers. POSIX requires that fork is async-signal-safe,
60 but our current fork implementation is not. */
61 bool multiple_threads = THREAD_GETMEM (THREAD_SELF, header.multiple_threads);
62
63 /* Run all the registered preparation handlers. In reverse order.
64 While doing this we build up a list of all the entries. */
65 struct fork_handler *runp;
66 while ((runp = __fork_handlers) != NULL)
67 {
68 /* Make sure we read from the current RUNP pointer. */
69 atomic_full_barrier ();
70
71 unsigned int oldval = runp->refcntr;
72
73 if (oldval == 0)
74 /* This means some other thread removed the list just after
75 the pointer has been loaded. Try again. Either the list
76 is empty or we can retry it. */
77 continue;
78
79 /* Bump the reference counter. */
80 if (atomic_compare_and_exchange_bool_acq (&__fork_handlers->refcntr,
81 oldval + 1, oldval))
82 /* The value changed, try again. */
83 continue;
84
85 /* We bumped the reference counter for the first entry in the
86 list. That means that none of the following entries will
87 just go away. The unloading code works in the order of the
88 list.
89
90 While executing the registered handlers we are building a
91 list of all the entries so that we can go backward later on. */
92 while (1)
93 {
94 /* Execute the handler if there is one. */
95 if (runp->prepare_handler != NULL)
96 runp->prepare_handler ();
97
98 /* Create a new element for the list. */
99 struct used_handler *newp
100 = (struct used_handler *) alloca (sizeof (*newp));
101 newp->handler = runp;
102 newp->next = allp;
103 allp = newp;
104
105 /* Advance to the next handler. */
106 runp = runp->next;
107 if (runp == NULL)
108 break;
109
110 /* Bump the reference counter for the next entry. */
111 atomic_increment (&runp->refcntr);
112 }
113
114 /* We are done. */
115 break;
116 }
117
118 /* If we are not running multiple threads, we do not have to
119 preserve lock state. If fork runs from a signal handler, only
120 async-signal-safe functions can be used in the child. These data
121 structures are only used by unsafe functions, so their state does
122 not matter if fork was called from a signal handler. */
123 if (multiple_threads)
124 {
125 _IO_list_lock ();
126
127 /* Acquire malloc locks. This needs to come last because fork
128 handlers may use malloc, and the libio list lock has an
129 indirect malloc dependency as well (via the getdelim
130 function). */
131 __malloc_fork_lock_parent ();
132 }
133
134#ifndef NDEBUG
135 pid_t ppid = THREAD_GETMEM (THREAD_SELF, tid);
136#endif
137
138 /* We need to prevent the getpid() code to update the PID field so
139 that, if a signal arrives in the child very early and the signal
140 handler uses getpid(), the value returned is correct. */
141 pid_t parentpid = THREAD_GETMEM (THREAD_SELF, pid);
142 THREAD_SETMEM (THREAD_SELF, pid, -parentpid);
143
144#ifdef ARCH_FORK
145 pid = ARCH_FORK ();
146#else
147# error "ARCH_FORK must be defined so that the CLONE_SETTID flag is used"
148 pid = INLINE_SYSCALL (fork, 0);
149#endif
150
151
152 if (pid == 0)
153 {
154 struct pthread *self = THREAD_SELF;
155
156 assert (THREAD_GETMEM (self, tid) != ppid);
157
158 /* See __pthread_once. */
159 if (__fork_generation_pointer != NULL)
160 *__fork_generation_pointer += __PTHREAD_ONCE_FORK_GEN_INCR;
161
162 /* Adjust the PID field for the new process. */
163 THREAD_SETMEM (self, pid, THREAD_GETMEM (self, tid));
164
165#if HP_TIMING_AVAIL
166 /* The CPU clock of the thread and process have to be set to zero. */
167 hp_timing_t now;
168 HP_TIMING_NOW (now);
169 THREAD_SETMEM (self, cpuclock_offset, now);
170 GL(dl_cpuclock_offset) = now;
171#endif
172
173#ifdef __NR_set_robust_list
174 /* Initialize the robust mutex list which has been reset during
175 the fork. We do not check for errors since if it fails here
176 it failed at process start as well and noone could have used
177 robust mutexes. We also do not have to set
178 self->robust_head.futex_offset since we inherit the correct
179 value from the parent. */
180# ifdef SHARED
181 if (__builtin_expect (__libc_pthread_functions_init, 0))
182 PTHFCT_CALL (ptr_set_robust, (self));
183# else
184 extern __typeof (__nptl_set_robust) __nptl_set_robust
185 __attribute__((weak));
186 if (__builtin_expect (__nptl_set_robust != NULL, 0))
187 __nptl_set_robust (self);
188# endif
189#endif
190
191 /* Reset the lock state in the multi-threaded case. */
192 if (multiple_threads)
193 {
194 /* Release malloc locks. */
195 __malloc_fork_unlock_child ();
196
197 /* Reset the file list. These are recursive mutexes. */
198 fresetlockfiles ();
199
200 /* Reset locks in the I/O code. */
201 _IO_list_resetlock ();
202 }
203
204 /* Reset the lock the dynamic loader uses to protect its data. */
205 __rtld_lock_initialize (GL(dl_load_lock));
206
207 /* Run the handlers registered for the child. */
208 while (allp != NULL)
209 {
210 if (allp->handler->child_handler != NULL)
211 allp->handler->child_handler ();
212
213 /* Note that we do not have to wake any possible waiter.
214 This is the only thread in the new process. The count
215 may have been bumped up by other threads doing a fork.
216 We reset it to 1, to avoid waiting for non-existing
217 thread(s) to release the count. */
218 allp->handler->refcntr = 1;
219
220 /* XXX We could at this point look through the object pool
221 and mark all objects not on the __fork_handlers list as
222 unused. This is necessary in case the fork() happened
223 while another thread called dlclose() and that call had
224 to create a new list. */
225
226 allp = allp->next;
227 }
228
229 /* Initialize the fork lock. */
230 __fork_lock = LLL_LOCK_INITIALIZER;
231 }
232 else
233 {
234 assert (THREAD_GETMEM (THREAD_SELF, tid) == ppid);
235
236 /* Restore the PID value. */
237 THREAD_SETMEM (THREAD_SELF, pid, parentpid);
238
239 /* Release acquired locks in the multi-threaded case. */
240 if (multiple_threads)
241 {
242 /* Release malloc locks, parent process variant. */
243 __malloc_fork_unlock_parent ();
244
245 /* We execute this even if the 'fork' call failed. */
246 _IO_list_unlock ();
247 }
248
249 /* Run the handlers registered for the parent. */
250 while (allp != NULL)
251 {
252 if (allp->handler->parent_handler != NULL)
253 allp->handler->parent_handler ();
254
255 if (atomic_decrement_and_test (&allp->handler->refcntr)
256 && allp->handler->need_signal)
257 futex_wake (&allp->handler->refcntr, 1, FUTEX_PRIVATE);
258
259 allp = allp->next;
260 }
261 }
262
263 return pid;
264}
265weak_alias (__libc_fork, __fork)
266libc_hidden_def (__fork)
267weak_alias (__libc_fork, fork)
268