1 /* Copyright (C) 2002-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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.
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.
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/>. */
22 #include <sys/types.h>
24 #include <libio/libioP.h>
26 #include <hp-timing.h>
28 #include <stdio-lock.h>
30 #include <nptl/pthreadP.h>
32 #include <arch-fork.h>
33 #include <futex-internal.h>
34 #include <malloc/malloc-internal.h>
37 fresetlockfiles (void)
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
));
53 struct fork_handler
*handler
;
54 struct used_handler
*next
;
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
);
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
)
68 /* Make sure we read from the current RUNP pointer. */
69 atomic_full_barrier ();
71 unsigned int oldval
= runp
->refcntr
;
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. */
79 /* Bump the reference counter. */
80 if (atomic_compare_and_exchange_bool_acq (&__fork_handlers
->refcntr
,
82 /* The value changed, try again. */
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
90 While executing the registered handlers we are building a
91 list of all the entries so that we can go backward later on. */
94 /* Execute the handler if there is one. */
95 if (runp
->prepare_handler
!= NULL
)
96 runp
->prepare_handler ();
98 /* Create a new element for the list. */
99 struct used_handler
*newp
100 = (struct used_handler
*) alloca (sizeof (*newp
));
101 newp
->handler
= runp
;
105 /* Advance to the next handler. */
110 /* Bump the reference counter for the next entry. */
111 atomic_increment (&runp
->refcntr
);
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
)
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
131 call_function_static_weak (__malloc_fork_lock_parent
);
135 pid_t ppid
= THREAD_GETMEM (THREAD_SELF
, tid
);
141 # error "ARCH_FORK must be defined so that the CLONE_SETTID flag is used"
142 pid
= INLINE_SYSCALL (fork
, 0);
148 struct pthread
*self
= THREAD_SELF
;
150 assert (THREAD_GETMEM (self
, tid
) != ppid
);
152 /* See __pthread_once. */
153 if (__fork_generation_pointer
!= NULL
)
154 *__fork_generation_pointer
+= __PTHREAD_ONCE_FORK_GEN_INCR
;
157 /* The CPU clock of the thread and process have to be set to zero. */
160 THREAD_SETMEM (self
, cpuclock_offset
, now
);
161 GL(dl_cpuclock_offset
) = now
;
164 #ifdef __NR_set_robust_list
165 /* Initialize the robust mutex list which has been reset during
166 the fork. We do not check for errors since if it fails here
167 it failed at process start as well and noone could have used
168 robust mutexes. We also do not have to set
169 self->robust_head.futex_offset since we inherit the correct
170 value from the parent. */
172 if (__builtin_expect (__libc_pthread_functions_init
, 0))
173 PTHFCT_CALL (ptr_set_robust
, (self
));
175 extern __typeof (__nptl_set_robust
) __nptl_set_robust
176 __attribute__((weak
));
177 if (__builtin_expect (__nptl_set_robust
!= NULL
, 0))
178 __nptl_set_robust (self
);
182 /* Reset the lock state in the multi-threaded case. */
183 if (multiple_threads
)
185 /* Release malloc locks. */
186 call_function_static_weak (__malloc_fork_unlock_child
);
188 /* Reset the file list. These are recursive mutexes. */
191 /* Reset locks in the I/O code. */
192 _IO_list_resetlock ();
195 /* Reset the lock the dynamic loader uses to protect its data. */
196 __rtld_lock_initialize (GL(dl_load_lock
));
198 /* Run the handlers registered for the child. */
201 if (allp
->handler
->child_handler
!= NULL
)
202 allp
->handler
->child_handler ();
204 /* Note that we do not have to wake any possible waiter.
205 This is the only thread in the new process. The count
206 may have been bumped up by other threads doing a fork.
207 We reset it to 1, to avoid waiting for non-existing
208 thread(s) to release the count. */
209 allp
->handler
->refcntr
= 1;
211 /* XXX We could at this point look through the object pool
212 and mark all objects not on the __fork_handlers list as
213 unused. This is necessary in case the fork() happened
214 while another thread called dlclose() and that call had
215 to create a new list. */
220 /* Initialize the fork lock. */
221 __fork_lock
= LLL_LOCK_INITIALIZER
;
225 assert (THREAD_GETMEM (THREAD_SELF
, tid
) == ppid
);
227 /* Release acquired locks in the multi-threaded case. */
228 if (multiple_threads
)
230 /* Release malloc locks, parent process variant. */
231 call_function_static_weak (__malloc_fork_unlock_parent
);
233 /* We execute this even if the 'fork' call failed. */
237 /* Run the handlers registered for the parent. */
240 if (allp
->handler
->parent_handler
!= NULL
)
241 allp
->handler
->parent_handler ();
243 if (atomic_decrement_and_test (&allp
->handler
->refcntr
)
244 && allp
->handler
->need_signal
)
245 futex_wake (&allp
->handler
->refcntr
, 1, FUTEX_PRIVATE
);
253 weak_alias (__libc_fork
, __fork
)
254 libc_hidden_def (__fork
)
255 weak_alias (__libc_fork
, fork
)