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
24 #include <sys/types.h>
28 #include <hp-timing.h>
33 unsigned long int *__fork_generation_pointer;
37 /* The single linked list of all currently registered for handlers. */
38 struct fork_handler *__fork_handlers;
42 fresetlockfiles (void)
45 #ifdef __USE_STDIO_FUTEXES__
46 for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen)
47 STDIO_INIT_MUTEX(fp->__lock);
49 pthread_mutexattr_t attr;
51 pthread_mutexattr_init(&attr);
52 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
54 for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen)
55 pthread_mutex_init(&fp->__lock, &attr);
57 pthread_mutexattr_destroy(&attr);
61 extern __typeof(fork) __libc_fork;
68 struct fork_handler *handler;
69 struct used_handler *next;
72 /* Run all the registered preparation handlers. In reverse order.
73 While doing this we build up a list of all the entries. */
74 struct fork_handler *runp;
75 while ((runp = __fork_handlers) != NULL)
77 /* Make sure we read from the current RUNP pointer. */
78 atomic_full_barrier ();
80 unsigned int oldval = runp->refcntr;
83 /* This means some other thread removed the list just after
84 the pointer has been loaded. Try again. Either the list
85 is empty or we can retry it. */
88 /* Bump the reference counter. */
89 if (atomic_compare_and_exchange_bool_acq (&__fork_handlers->refcntr,
91 /* The value changed, try again. */
94 /* We bumped the reference counter for the first entry in the
95 list. That means that none of the following entries will
96 just go away. The unloading code works in the order of the
99 While executing the registered handlers we are building a
100 list of all the entries so that we can go backward later on. */
103 /* Execute the handler if there is one. */
104 if (runp->prepare_handler != NULL)
105 runp->prepare_handler ();
107 /* Create a new element for the list. */
108 struct used_handler *newp
109 = (struct used_handler *) alloca (sizeof (*newp));
110 newp->handler = runp;
114 /* Advance to the next handler. */
119 /* Bump the reference counter for the next entry. */
120 atomic_increment (&runp->refcntr);
127 __UCLIBC_IO_MUTEX_LOCK_CANCEL_UNSAFE(_stdio_openlist_add_lock);
130 pid_t ppid = THREAD_GETMEM (THREAD_SELF, tid);
133 /* We need to prevent the getpid() code to update the PID field so
134 that, if a signal arrives in the child very early and the signal
135 handler uses getpid(), the value returned is correct. */
136 pid_t parentpid = THREAD_GETMEM (THREAD_SELF, pid);
137 THREAD_SETMEM (THREAD_SELF, pid, -parentpid);
142 # error "ARCH_FORK must be defined so that the CLONE_SETTID flag is used"
143 pid = INLINE_SYSCALL (fork, 0);
149 struct pthread *self = THREAD_SELF;
151 assert (THREAD_GETMEM (self, tid) != ppid);
153 if (__fork_generation_pointer != NULL)
154 *__fork_generation_pointer += 4;
156 /* Adjust the PID field for the new process. */
157 THREAD_SETMEM (self, pid, THREAD_GETMEM (self, tid));
160 /* The CPU clock of the thread and process have to be set to zero. */
163 THREAD_SETMEM (self, cpuclock_offset, now);
164 GL(dl_cpuclock_offset) = now;
167 /* Reset the file list. These are recursive mutexes. */
170 /* Reset locks in the I/O code. */
171 STDIO_INIT_MUTEX(_stdio_openlist_add_lock);
173 /* XXX reset any locks in dynamic loader */
175 /* Run the handlers registered for the child. */
178 if (allp->handler->child_handler != NULL)
179 allp->handler->child_handler ();
181 /* Note that we do not have to wake any possible waiter.
182 This is the only thread in the new process. The count
183 may have been bumped up by other threads doing a fork.
184 We reset it to 1, to avoid waiting for non-existing
185 thread(s) to release the count. */
186 allp->handler->refcntr = 1;
188 /* XXX We could at this point look through the object pool
189 and mark all objects not on the __fork_handlers list as
190 unused. This is necessary in case the fork() happened
191 while another thread called dlclose() and that call had
192 to create a new list. */
197 /* Initialize the fork lock. */
198 __fork_lock = LLL_LOCK_INITIALIZER;
202 assert (THREAD_GETMEM (THREAD_SELF, tid) == ppid);
204 /* Restore the PID value. */
205 THREAD_SETMEM (THREAD_SELF, pid, parentpid);
207 /* We execute this even if the 'fork' call failed. */
208 __UCLIBC_IO_MUTEX_UNLOCK_CANCEL_UNSAFE(_stdio_openlist_add_lock);
210 /* Run the handlers registered for the parent. */
213 if (allp->handler->parent_handler != NULL)
214 allp->handler->parent_handler ();
216 if (atomic_decrement_and_test (&allp->handler->refcntr)
217 && allp->handler->need_signal)
218 lll_futex_wake (allp->handler->refcntr, 1, LLL_PRIVATE);
226 weak_alias(__libc_fork,__fork)
227 libc_hidden_proto(fork)
228 weak_alias(__libc_fork,fork)
229 libc_hidden_weak(fork)