Update copyright notices with scripts/update-copyrights
[glibc.git] / nptl / sysdeps / unix / sysv / linux / fork.c
blob961fc8a5ea4ede0197471b6b723ef467f3f78ee1
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/>. */
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 "fork.h"
27 #include <hp-timing.h>
28 #include <ldsodefs.h>
29 #include <bits/stdio-lock.h>
30 #include <atomic.h>
31 #include <pthreadP.h>
34 unsigned long int *__fork_generation_pointer;
38 /* The single linked list of all currently registered fork handlers. */
39 struct fork_handler *__fork_handlers;
42 static void
43 fresetlockfiles (void)
45 _IO_ITER i;
47 for (i = _IO_iter_begin(); i != _IO_iter_end(); i = _IO_iter_next(i))
48 _IO_lock_init (*((_IO_lock_t *) _IO_iter_file(i)->_lock));
52 pid_t
53 __libc_fork (void)
55 pid_t pid;
56 struct used_handler
58 struct fork_handler *handler;
59 struct used_handler *next;
60 } *allp = NULL;
62 /* Run all the registered preparation handlers. In reverse order.
63 While doing this we build up a list of all the entries. */
64 struct fork_handler *runp;
65 while ((runp = __fork_handlers) != NULL)
67 /* Make sure we read from the current RUNP pointer. */
68 atomic_full_barrier ();
70 unsigned int oldval = runp->refcntr;
72 if (oldval == 0)
73 /* This means some other thread removed the list just after
74 the pointer has been loaded. Try again. Either the list
75 is empty or we can retry it. */
76 continue;
78 /* Bump the reference counter. */
79 if (atomic_compare_and_exchange_bool_acq (&__fork_handlers->refcntr,
80 oldval + 1, oldval))
81 /* The value changed, try again. */
82 continue;
84 /* We bumped the reference counter for the first entry in the
85 list. That means that none of the following entries will
86 just go away. The unloading code works in the order of the
87 list.
89 While executing the registered handlers we are building a
90 list of all the entries so that we can go backward later on. */
91 while (1)
93 /* Execute the handler if there is one. */
94 if (runp->prepare_handler != NULL)
95 runp->prepare_handler ();
97 /* Create a new element for the list. */
98 struct used_handler *newp
99 = (struct used_handler *) alloca (sizeof (*newp));
100 newp->handler = runp;
101 newp->next = allp;
102 allp = newp;
104 /* Advance to the next handler. */
105 runp = runp->next;
106 if (runp == NULL)
107 break;
109 /* Bump the reference counter for the next entry. */
110 atomic_increment (&runp->refcntr);
113 /* We are done. */
114 break;
117 _IO_list_lock ();
119 #ifndef NDEBUG
120 pid_t ppid = THREAD_GETMEM (THREAD_SELF, tid);
121 #endif
123 /* We need to prevent the getpid() code to update the PID field so
124 that, if a signal arrives in the child very early and the signal
125 handler uses getpid(), the value returned is correct. */
126 pid_t parentpid = THREAD_GETMEM (THREAD_SELF, pid);
127 THREAD_SETMEM (THREAD_SELF, pid, -parentpid);
129 #ifdef ARCH_FORK
130 pid = ARCH_FORK ();
131 #else
132 # error "ARCH_FORK must be defined so that the CLONE_SETTID flag is used"
133 pid = INLINE_SYSCALL (fork, 0);
134 #endif
137 if (pid == 0)
139 struct pthread *self = THREAD_SELF;
141 assert (THREAD_GETMEM (self, tid) != ppid);
143 if (__fork_generation_pointer != NULL)
144 *__fork_generation_pointer += 4;
146 /* Adjust the PID field for the new process. */
147 THREAD_SETMEM (self, pid, THREAD_GETMEM (self, tid));
149 #if HP_TIMING_AVAIL
150 /* The CPU clock of the thread and process have to be set to zero. */
151 hp_timing_t now;
152 HP_TIMING_NOW (now);
153 THREAD_SETMEM (self, cpuclock_offset, now);
154 GL(dl_cpuclock_offset) = now;
155 #endif
157 #ifdef __NR_set_robust_list
158 /* Initialize the robust mutex list which has been reset during
159 the fork. We do not check for errors since if it fails here
160 it failed at process start as well and noone could have used
161 robust mutexes. We also do not have to set
162 self->robust_head.futex_offset since we inherit the correct
163 value from the parent. */
164 # ifdef SHARED
165 if (__builtin_expect (__libc_pthread_functions_init, 0))
166 PTHFCT_CALL (ptr_set_robust, (self));
167 # else
168 extern __typeof (__nptl_set_robust) __nptl_set_robust
169 __attribute__((weak));
170 if (__builtin_expect (__nptl_set_robust != NULL, 0))
171 __nptl_set_robust (self);
172 # endif
173 #endif
175 /* Reset the file list. These are recursive mutexes. */
176 fresetlockfiles ();
178 /* Reset locks in the I/O code. */
179 _IO_list_resetlock ();
181 /* Reset the lock the dynamic loader uses to protect its data. */
182 __rtld_lock_initialize (GL(dl_load_lock));
184 /* Run the handlers registered for the child. */
185 while (allp != NULL)
187 if (allp->handler->child_handler != NULL)
188 allp->handler->child_handler ();
190 /* Note that we do not have to wake any possible waiter.
191 This is the only thread in the new process. The count
192 may have been bumped up by other threads doing a fork.
193 We reset it to 1, to avoid waiting for non-existing
194 thread(s) to release the count. */
195 allp->handler->refcntr = 1;
197 /* XXX We could at this point look through the object pool
198 and mark all objects not on the __fork_handlers list as
199 unused. This is necessary in case the fork() happened
200 while another thread called dlclose() and that call had
201 to create a new list. */
203 allp = allp->next;
206 /* Initialize the fork lock. */
207 __fork_lock = LLL_LOCK_INITIALIZER;
209 else
211 assert (THREAD_GETMEM (THREAD_SELF, tid) == ppid);
213 /* Restore the PID value. */
214 THREAD_SETMEM (THREAD_SELF, pid, parentpid);
216 /* We execute this even if the 'fork' call failed. */
217 _IO_list_unlock ();
219 /* Run the handlers registered for the parent. */
220 while (allp != NULL)
222 if (allp->handler->parent_handler != NULL)
223 allp->handler->parent_handler ();
225 if (atomic_decrement_and_test (&allp->handler->refcntr)
226 && allp->handler->need_signal)
227 lll_futex_wake (allp->handler->refcntr, 1, LLL_PRIVATE);
229 allp = allp->next;
233 return pid;
235 weak_alias (__libc_fork, __fork)
236 libc_hidden_def (__fork)
237 weak_alias (__libc_fork, fork)