1 /* Copyright (C) 2002-2014 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 <bits/stdio-lock.h>
30 #include <nptl/pthreadP.h>
32 #include <arch-fork.h>
35 unsigned long int *__fork_generation_pointer
;
39 /* The single linked list of all currently registered fork handlers. */
40 struct fork_handler
*__fork_handlers
;
44 fresetlockfiles (void)
48 for (i
= _IO_iter_begin(); i
!= _IO_iter_end(); i
= _IO_iter_next(i
))
49 if ((_IO_iter_file (i
)->_flags
& _IO_USER_LOCK
) == 0)
50 _IO_lock_init (*((_IO_lock_t
*) _IO_iter_file(i
)->_lock
));
60 struct fork_handler
*handler
;
61 struct used_handler
*next
;
64 /* Run all the registered preparation handlers. In reverse order.
65 While doing this we build up a list of all the entries. */
66 struct fork_handler
*runp
;
67 while ((runp
= __fork_handlers
) != NULL
)
69 /* Make sure we read from the current RUNP pointer. */
70 atomic_full_barrier ();
72 unsigned int oldval
= runp
->refcntr
;
75 /* This means some other thread removed the list just after
76 the pointer has been loaded. Try again. Either the list
77 is empty or we can retry it. */
80 /* Bump the reference counter. */
81 if (atomic_compare_and_exchange_bool_acq (&__fork_handlers
->refcntr
,
83 /* The value changed, try again. */
86 /* We bumped the reference counter for the first entry in the
87 list. That means that none of the following entries will
88 just go away. The unloading code works in the order of the
91 While executing the registered handlers we are building a
92 list of all the entries so that we can go backward later on. */
95 /* Execute the handler if there is one. */
96 if (runp
->prepare_handler
!= NULL
)
97 runp
->prepare_handler ();
99 /* Create a new element for the list. */
100 struct used_handler
*newp
101 = (struct used_handler
*) alloca (sizeof (*newp
));
102 newp
->handler
= runp
;
106 /* Advance to the next handler. */
111 /* Bump the reference counter for the next entry. */
112 atomic_increment (&runp
->refcntr
);
122 pid_t ppid
= THREAD_GETMEM (THREAD_SELF
, tid
);
125 /* We need to prevent the getpid() code to update the PID field so
126 that, if a signal arrives in the child very early and the signal
127 handler uses getpid(), the value returned is correct. */
128 pid_t parentpid
= THREAD_GETMEM (THREAD_SELF
, pid
);
129 THREAD_SETMEM (THREAD_SELF
, pid
, -parentpid
);
134 # error "ARCH_FORK must be defined so that the CLONE_SETTID flag is used"
135 pid
= INLINE_SYSCALL (fork
, 0);
141 struct pthread
*self
= THREAD_SELF
;
143 assert (THREAD_GETMEM (self
, tid
) != ppid
);
145 /* See __pthread_once. */
146 if (__fork_generation_pointer
!= NULL
)
147 *__fork_generation_pointer
+= __PTHREAD_ONCE_FORK_GEN_INCR
;
149 /* Adjust the PID field for the new process. */
150 THREAD_SETMEM (self
, pid
, THREAD_GETMEM (self
, tid
));
153 /* The CPU clock of the thread and process have to be set to zero. */
156 THREAD_SETMEM (self
, cpuclock_offset
, now
);
157 GL(dl_cpuclock_offset
) = now
;
160 #ifdef __NR_set_robust_list
161 /* Initialize the robust mutex list which has been reset during
162 the fork. We do not check for errors since if it fails here
163 it failed at process start as well and noone could have used
164 robust mutexes. We also do not have to set
165 self->robust_head.futex_offset since we inherit the correct
166 value from the parent. */
168 if (__builtin_expect (__libc_pthread_functions_init
, 0))
169 PTHFCT_CALL (ptr_set_robust
, (self
));
171 extern __typeof (__nptl_set_robust
) __nptl_set_robust
172 __attribute__((weak
));
173 if (__builtin_expect (__nptl_set_robust
!= NULL
, 0))
174 __nptl_set_robust (self
);
178 /* Reset the file list. These are recursive mutexes. */
181 /* Reset locks in the I/O code. */
182 _IO_list_resetlock ();
184 /* Reset the lock the dynamic loader uses to protect its data. */
185 __rtld_lock_initialize (GL(dl_load_lock
));
187 /* Run the handlers registered for the child. */
190 if (allp
->handler
->child_handler
!= NULL
)
191 allp
->handler
->child_handler ();
193 /* Note that we do not have to wake any possible waiter.
194 This is the only thread in the new process. The count
195 may have been bumped up by other threads doing a fork.
196 We reset it to 1, to avoid waiting for non-existing
197 thread(s) to release the count. */
198 allp
->handler
->refcntr
= 1;
200 /* XXX We could at this point look through the object pool
201 and mark all objects not on the __fork_handlers list as
202 unused. This is necessary in case the fork() happened
203 while another thread called dlclose() and that call had
204 to create a new list. */
209 /* Initialize the fork lock. */
210 __fork_lock
= LLL_LOCK_INITIALIZER
;
214 assert (THREAD_GETMEM (THREAD_SELF
, tid
) == ppid
);
216 /* Restore the PID value. */
217 THREAD_SETMEM (THREAD_SELF
, pid
, parentpid
);
219 /* We execute this even if the 'fork' call failed. */
222 /* Run the handlers registered for the parent. */
225 if (allp
->handler
->parent_handler
!= NULL
)
226 allp
->handler
->parent_handler ();
228 if (atomic_decrement_and_test (&allp
->handler
->refcntr
)
229 && allp
->handler
->need_signal
)
230 lll_futex_wake (allp
->handler
->refcntr
, 1, LLL_PRIVATE
);
238 weak_alias (__libc_fork
, __fork
)
239 libc_hidden_def (__fork
)
240 weak_alias (__libc_fork
, fork
)