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, see
17 <http://www.gnu.org/licenses/>. */
23 #include <sys/types.h>
31 unsigned long int *__fork_generation_pointer
;
35 /* The single linked list of all currently registered for handlers. */
36 struct fork_handler
*__fork_handlers
;
40 fresetlockfiles (void)
43 #ifdef __USE_STDIO_FUTEXES__
44 for (fp
= _stdio_openlist
; fp
!= NULL
; fp
= fp
->__nextopen
)
45 STDIO_INIT_MUTEX(fp
->__lock
);
47 pthread_mutexattr_t attr
;
49 pthread_mutexattr_init(&attr
);
50 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE_NP
);
52 for (fp
= _stdio_openlist
; fp
!= NULL
; fp
= fp
->__nextopen
)
53 pthread_mutex_init(&fp
->__lock
, &attr
);
55 pthread_mutexattr_destroy(&attr
);
60 #if defined __arm__ && defined __thumb__ && __GNUC_PREREQ (4,6)
61 /* GCC PR target/53735
62 * In thumb1 we run out of registers when compiling with Os so relax that
63 * to have more registers available for spilling by using O2 here.
65 attribute_optimize("O2")
72 struct fork_handler
*handler
;
73 struct used_handler
*next
;
76 /* Run all the registered preparation handlers. In reverse order.
77 While doing this we build up a list of all the entries. */
78 struct fork_handler
*runp
;
79 while ((runp
= __fork_handlers
) != NULL
)
81 /* Make sure we read from the current RUNP pointer. */
82 atomic_full_barrier ();
84 unsigned int oldval
= runp
->refcntr
;
87 /* This means some other thread removed the list just after
88 the pointer has been loaded. Try again. Either the list
89 is empty or we can retry it. */
92 /* Bump the reference counter. */
93 if (atomic_compare_and_exchange_bool_acq (&__fork_handlers
->refcntr
,
95 /* The value changed, try again. */
98 /* We bumped the reference counter for the first entry in the
99 list. That means that none of the following entries will
100 just go away. The unloading code works in the order of the
103 While executing the registered handlers we are building a
104 list of all the entries so that we can go backward later on. */
107 /* Execute the handler if there is one. */
108 if (runp
->prepare_handler
!= NULL
)
109 runp
->prepare_handler ();
111 /* Create a new element for the list. */
112 struct used_handler
*newp
113 = (struct used_handler
*) alloca (sizeof (*newp
));
114 newp
->handler
= runp
;
118 /* Advance to the next handler. */
123 /* Bump the reference counter for the next entry. */
124 atomic_increment (&runp
->refcntr
);
131 __UCLIBC_IO_MUTEX_LOCK_CANCEL_UNSAFE(_stdio_openlist_add_lock
);
134 pid_t ppid
= THREAD_GETMEM (THREAD_SELF
, tid
);
140 # error "ARCH_FORK must be defined so that the CLONE_SETTID flag is used"
141 pid
= INLINE_SYSCALL (fork
, 0);
147 struct pthread
*self
= THREAD_SELF
;
149 assert (THREAD_GETMEM (self
, tid
) != ppid
);
151 if (__fork_generation_pointer
!= NULL
)
152 *__fork_generation_pointer
+= 4;
154 /* Reset the file list. These are recursive mutexes. */
157 /* Reset locks in the I/O code. */
158 STDIO_INIT_MUTEX(_stdio_openlist_add_lock
);
160 /* XXX reset any locks in dynamic loader */
162 /* Run the handlers registered for the child. */
165 if (allp
->handler
->child_handler
!= NULL
)
166 allp
->handler
->child_handler ();
168 /* Note that we do not have to wake any possible waiter.
169 This is the only thread in the new process. The count
170 may have been bumped up by other threads doing a fork.
171 We reset it to 1, to avoid waiting for non-existing
172 thread(s) to release the count. */
173 allp
->handler
->refcntr
= 1;
175 /* XXX We could at this point look through the object pool
176 and mark all objects not on the __fork_handlers list as
177 unused. This is necessary in case the fork() happened
178 while another thread called dlclose() and that call had
179 to create a new list. */
184 /* Initialize the fork lock. */
185 __fork_lock
= LLL_LOCK_INITIALIZER
;
189 assert (THREAD_GETMEM (THREAD_SELF
, tid
) == ppid
);
191 /* We execute this even if the 'fork' call failed. */
192 __UCLIBC_IO_MUTEX_UNLOCK_CANCEL_UNSAFE(_stdio_openlist_add_lock
);
194 /* Run the handlers registered for the parent. */
197 if (allp
->handler
->parent_handler
!= NULL
)
198 allp
->handler
->parent_handler ();
200 if (atomic_decrement_and_test (&allp
->handler
->refcntr
)
201 && allp
->handler
->need_signal
)
202 lll_futex_wake (allp
->handler
->refcntr
, 1, LLL_PRIVATE
);
210 libc_hidden_def(fork
)