1 /* Copyright (C) 2002, 2003, 2007, 2008 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 #include <sys/types.h>
25 #include <libio/libioP.h>
28 #include <hp-timing.h>
30 #include <bits/stdio-lock.h>
34 unsigned long int *__fork_generation_pointer;
38 /* The single linked list of all currently registered for handlers. */
39 struct fork_handler *__fork_handlers;
43 fresetlockfiles (void)
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));
58 struct fork_handler *handler;
59 struct used_handler *next;
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 unsigned int oldval = runp->refcntr;
70 /* This means some other thread removed the list just after
71 the pointer has been loaded. Try again. Either the list
72 is empty or we can retry it. */
75 /* Bump the reference counter. */
76 if (atomic_compare_and_exchange_bool_acq (&__fork_handlers->refcntr,
78 /* The value changed, try again. */
81 /* We bumped the reference counter for the first entry in the
82 list. That means that none of the following entries will
83 just go away. The unloading code works in the order of the
86 While executing the registered handlers we are building a
87 list of all the entries so that we can go backward later on. */
90 /* Execute the handler if there is one. */
91 if (runp->prepare_handler != NULL)
92 runp->prepare_handler ();
94 /* Create a new element for the list. */
95 struct used_handler *newp
96 = (struct used_handler *) alloca (sizeof (*newp));
101 /* Advance to the next handler. */
106 /* Bump the reference counter for the next entry. */
107 atomic_increment (&runp->refcntr);
116 #ifndef PTHREAD_T_IS_TID
118 pid_t ppid = THREAD_GETMEM (THREAD_SELF, tid);
122 /* We need to prevent the getpid() code to update the PID field so
123 that, if a signal arrives in the child very early and the signal
124 handler uses getpid(), the value returned is correct. */
125 pid_t parentpid = THREAD_GETMEM (THREAD_SELF, pid);
126 THREAD_SETMEM (THREAD_SELF, pid, -parentpid);
131 # error "ARCH_FORK must be defined so that the CLONE_SETTID flag is used"
132 pid = INLINE_SYSCALL (fork, 0);
138 struct pthread *self = THREAD_SELF;
140 #ifndef PTHREAD_T_IS_TID
141 assert (THREAD_GETMEM (self, tid) != ppid);
144 if (__fork_generation_pointer != NULL)
145 *__fork_generation_pointer += 4;
147 /* Adjust the PID field for the new process. */
148 #ifndef PTHREAD_T_IS_TID
149 THREAD_SETMEM (self, pid, THREAD_GETMEM (self, tid));
151 THREAD_SETMEM (self, pid, getpid ());
155 /* The CPU clock of the thread and process have to be set to zero. */
158 THREAD_SETMEM (self, cpuclock_offset, now);
159 GL(dl_cpuclock_offset) = now;
162 /* Reset the file list. These are recursive mutexes. */
165 /* Reset locks in the I/O code. */
166 _IO_list_resetlock ();
168 /* Reset the lock the dynamic loader uses to protect its data. */
169 __rtld_lock_initialize (GL(dl_load_lock));
171 /* Run the handlers registered for the child. */
174 if (allp->handler->child_handler != NULL)
175 allp->handler->child_handler ();
177 /* Note that we do not have to wake any possible waiter.
178 This is the only thread in the new process. The count
179 may have been bumped up by other threads doing a fork.
180 We reset it to 1, to avoid waiting for non-existing
181 thread(s) to release the count. */
182 allp->handler->refcntr = 1;
184 /* XXX We could at this point look through the object pool
185 and mark all objects not on the __fork_handlers list as
186 unused. This is necessary in case the fork() happened
187 while another thread called dlclose() and that call had
188 to create a new list. */
193 /* Initialize the fork lock. */
195 __fork_lock = LLL_LOCK_INITIALIZER;
197 lll_init (__fork_lock);
202 #ifndef PTHREAD_T_IS_TID
203 assert (THREAD_GETMEM (THREAD_SELF, tid) == ppid);
206 /* Restore the PID value. */
207 THREAD_SETMEM (THREAD_SELF, pid, parentpid);
209 /* We execute this even if the 'fork' call failed. */
212 /* Run the handlers registered for the parent. */
215 if (allp->handler->parent_handler != NULL)
216 allp->handler->parent_handler ();
218 if (atomic_decrement_and_test (&allp->handler->refcntr)
219 && allp->handler->need_signal)
220 lll_futex_wake (allp->handler->refcntr, 1, LLL_PRIVATE);
228 weak_alias (__libc_fork, __fork)
229 libc_hidden_def (__fork)
230 weak_alias (__libc_fork, fork)