Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / nptl / fork.c
blob5cffd82ced19c78804699ec7701103af701b8196
1 /* Copyright (C) 2002-2015 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/>. */
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 <bits/stdio-lock.h>
29 #include <atomic.h>
30 #include <nptl/pthreadP.h>
31 #include <fork.h>
32 #include <arch-fork.h>
35 static void
36 fresetlockfiles (void)
38 _IO_ITER i;
40 for (i = _IO_iter_begin(); i != _IO_iter_end(); i = _IO_iter_next(i))
41 if ((_IO_iter_file (i)->_flags & _IO_USER_LOCK) == 0)
42 _IO_lock_init (*((_IO_lock_t *) _IO_iter_file(i)->_lock));
46 pid_t
47 __libc_fork (void)
49 pid_t pid;
50 struct used_handler
52 struct fork_handler *handler;
53 struct used_handler *next;
54 } *allp = NULL;
56 /* Run all the registered preparation handlers. In reverse order.
57 While doing this we build up a list of all the entries. */
58 struct fork_handler *runp;
59 while ((runp = __fork_handlers) != NULL)
61 /* Make sure we read from the current RUNP pointer. */
62 atomic_full_barrier ();
64 unsigned int oldval = runp->refcntr;
66 if (oldval == 0)
67 /* This means some other thread removed the list just after
68 the pointer has been loaded. Try again. Either the list
69 is empty or we can retry it. */
70 continue;
72 /* Bump the reference counter. */
73 if (atomic_compare_and_exchange_bool_acq (&__fork_handlers->refcntr,
74 oldval + 1, oldval))
75 /* The value changed, try again. */
76 continue;
78 /* We bumped the reference counter for the first entry in the
79 list. That means that none of the following entries will
80 just go away. The unloading code works in the order of the
81 list.
83 While executing the registered handlers we are building a
84 list of all the entries so that we can go backward later on. */
85 while (1)
87 /* Execute the handler if there is one. */
88 if (runp->prepare_handler != NULL)
89 runp->prepare_handler ();
91 /* Create a new element for the list. */
92 struct used_handler *newp
93 = (struct used_handler *) alloca (sizeof (*newp));
94 newp->handler = runp;
95 newp->next = allp;
96 allp = newp;
98 /* Advance to the next handler. */
99 runp = runp->next;
100 if (runp == NULL)
101 break;
103 /* Bump the reference counter for the next entry. */
104 atomic_increment (&runp->refcntr);
107 /* We are done. */
108 break;
111 _IO_list_lock ();
113 #ifndef NDEBUG
114 pid_t ppid = THREAD_GETMEM (THREAD_SELF, tid);
115 #endif
117 /* We need to prevent the getpid() code to update the PID field so
118 that, if a signal arrives in the child very early and the signal
119 handler uses getpid(), the value returned is correct. */
120 pid_t parentpid = THREAD_GETMEM (THREAD_SELF, pid);
121 THREAD_SETMEM (THREAD_SELF, pid, -parentpid);
123 #ifdef ARCH_FORK
124 pid = ARCH_FORK ();
125 #else
126 # error "ARCH_FORK must be defined so that the CLONE_SETTID flag is used"
127 pid = INLINE_SYSCALL (fork, 0);
128 #endif
131 if (pid == 0)
133 struct pthread *self = THREAD_SELF;
135 assert (THREAD_GETMEM (self, tid) != ppid);
137 /* See __pthread_once. */
138 if (__fork_generation_pointer != NULL)
139 *__fork_generation_pointer += __PTHREAD_ONCE_FORK_GEN_INCR;
141 /* Adjust the PID field for the new process. */
142 THREAD_SETMEM (self, pid, THREAD_GETMEM (self, tid));
144 #if HP_TIMING_AVAIL
145 /* The CPU clock of the thread and process have to be set to zero. */
146 hp_timing_t now;
147 HP_TIMING_NOW (now);
148 THREAD_SETMEM (self, cpuclock_offset, now);
149 GL(dl_cpuclock_offset) = now;
150 #endif
152 #ifdef __NR_set_robust_list
153 /* Initialize the robust mutex list which has been reset during
154 the fork. We do not check for errors since if it fails here
155 it failed at process start as well and noone could have used
156 robust mutexes. We also do not have to set
157 self->robust_head.futex_offset since we inherit the correct
158 value from the parent. */
159 # ifdef SHARED
160 if (__builtin_expect (__libc_pthread_functions_init, 0))
161 PTHFCT_CALL (ptr_set_robust, (self));
162 # else
163 extern __typeof (__nptl_set_robust) __nptl_set_robust
164 __attribute__((weak));
165 if (__builtin_expect (__nptl_set_robust != NULL, 0))
166 __nptl_set_robust (self);
167 # endif
168 #endif
170 /* Reset the file list. These are recursive mutexes. */
171 fresetlockfiles ();
173 /* Reset locks in the I/O code. */
174 _IO_list_resetlock ();
176 /* Reset the lock the dynamic loader uses to protect its data. */
177 __rtld_lock_initialize (GL(dl_load_lock));
179 /* Run the handlers registered for the child. */
180 while (allp != NULL)
182 if (allp->handler->child_handler != NULL)
183 allp->handler->child_handler ();
185 /* Note that we do not have to wake any possible waiter.
186 This is the only thread in the new process. The count
187 may have been bumped up by other threads doing a fork.
188 We reset it to 1, to avoid waiting for non-existing
189 thread(s) to release the count. */
190 allp->handler->refcntr = 1;
192 /* XXX We could at this point look through the object pool
193 and mark all objects not on the __fork_handlers list as
194 unused. This is necessary in case the fork() happened
195 while another thread called dlclose() and that call had
196 to create a new list. */
198 allp = allp->next;
201 /* Initialize the fork lock. */
202 __fork_lock = LLL_LOCK_INITIALIZER;
204 else
206 assert (THREAD_GETMEM (THREAD_SELF, tid) == ppid);
208 /* Restore the PID value. */
209 THREAD_SETMEM (THREAD_SELF, pid, parentpid);
211 /* We execute this even if the 'fork' call failed. */
212 _IO_list_unlock ();
214 /* Run the handlers registered for the parent. */
215 while (allp != NULL)
217 if (allp->handler->parent_handler != NULL)
218 allp->handler->parent_handler ();
220 if (atomic_decrement_and_test (&allp->handler->refcntr)
221 && allp->handler->need_signal)
222 lll_futex_wake (allp->handler->refcntr, 1, LLL_PRIVATE);
224 allp = allp->next;
228 return pid;
230 weak_alias (__libc_fork, __fork)
231 libc_hidden_def (__fork)
232 weak_alias (__libc_fork, fork)