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.
4 
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.
9 
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.
14 
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/>.  */
18 
19 #include <assert.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <sysdep.h>
25 #include <tls.h>
26 #include "fork.h"
27 #include <ldsodefs.h>
28 #include <atomic.h>
29 #include <errno.h>
30 
31 #ifdef __ARCH_USE_MMU__
32 unsigned long int *__fork_generation_pointer;
33 
34 
35 
36 /* The single linked list of all currently registered for handlers.  */
37 struct fork_handler *__fork_handlers;
38 
39 
40 static void
fresetlockfiles(void)41 fresetlockfiles (void)
42 {
43   FILE *fp;
44 #ifdef __USE_STDIO_FUTEXES__
45   for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen)
46     STDIO_INIT_MUTEX(fp->__lock);
47 #else
48   pthread_mutexattr_t attr;
49 
50   pthread_mutexattr_init(&attr);
51   pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
52 
53   for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen)
54     pthread_mutex_init(&fp->__lock, &attr);
55 
56   pthread_mutexattr_destroy(&attr);
57 #endif
58 }
59 
60 pid_t
61 #if defined __arm__ && defined __thumb__ && __GNUC_PREREQ (4,6)
62 /* GCC PR target/53735
63  * In thumb1 we run out of registers when compiling with Os so relax that
64  * to have more registers available for spilling by using O2 here.
65  */
66 attribute_optimize("O2")
67 #endif
fork(void)68 fork (void)
69 {
70   pid_t pid;
71   struct used_handler
72   {
73     struct fork_handler *handler;
74     struct used_handler *next;
75   } *allp = NULL;
76 
77   /* Run all the registered preparation handlers.  In reverse order.
78      While doing this we build up a list of all the entries.  */
79   struct fork_handler *runp;
80   while ((runp = __fork_handlers) != NULL)
81     {
82       /* Make sure we read from the current RUNP pointer.  */
83       atomic_full_barrier ();
84 
85       unsigned int oldval = runp->refcntr;
86 
87       if (oldval == 0)
88 	/* This means some other thread removed the list just after
89 	   the pointer has been loaded.  Try again.  Either the list
90 	   is empty or we can retry it.  */
91 	continue;
92 
93       /* Bump the reference counter.  */
94       if (atomic_compare_and_exchange_bool_acq (&__fork_handlers->refcntr,
95 						oldval + 1, oldval))
96 	/* The value changed, try again.  */
97 	continue;
98 
99       /* We bumped the reference counter for the first entry in the
100 	 list.  That means that none of the following entries will
101 	 just go away.  The unloading code works in the order of the
102 	 list.
103 
104          While executing the registered handlers we are building a
105          list of all the entries so that we can go backward later on.  */
106       while (1)
107 	{
108 	  /* Execute the handler if there is one.  */
109 	  if (runp->prepare_handler != NULL)
110 	    runp->prepare_handler ();
111 
112 	  /* Create a new element for the list.  */
113 	  struct used_handler *newp
114 	    = (struct used_handler *) alloca (sizeof (*newp));
115 	  newp->handler = runp;
116 	  newp->next = allp;
117 	  allp = newp;
118 
119 	  /* Advance to the next handler.  */
120 	  runp = runp->next;
121 	  if (runp == NULL)
122 	    break;
123 
124 	  /* Bump the reference counter for the next entry.  */
125 	  atomic_increment (&runp->refcntr);
126 	}
127 
128       /* We are done.  */
129       break;
130     }
131 
132   __UCLIBC_IO_MUTEX_LOCK_CANCEL_UNSAFE(_stdio_openlist_add_lock);
133 
134 #ifndef NDEBUG
135   pid_t ppid = THREAD_GETMEM (THREAD_SELF, tid);
136 #endif
137 
138 #ifdef ARCH_FORK
139   pid = ARCH_FORK ();
140 #else
141 # error "ARCH_FORK must be defined so that the CLONE_SETTID flag is used"
142   pid = INLINE_SYSCALL (fork, 0);
143 #endif
144 
145 
146   if (pid == 0)
147     {
148       assert (THREAD_GETMEM (THREAD_SELF, tid) != ppid);
149 
150       if (__fork_generation_pointer != NULL)
151 	*__fork_generation_pointer += 4;
152 
153       /* Reset the file list.  These are recursive mutexes.  */
154       fresetlockfiles ();
155 
156       /* Reset locks in the I/O code.  */
157       STDIO_INIT_MUTEX(_stdio_openlist_add_lock);
158 
159       /* XXX reset any locks in dynamic loader */
160 
161       /* Run the handlers registered for the child.  */
162       while (allp != NULL)
163 	{
164 	  if (allp->handler->child_handler != NULL)
165 	    allp->handler->child_handler ();
166 
167 	  /* Note that we do not have to wake any possible waiter.
168  	     This is the only thread in the new process.  The count
169  	     may have been bumped up by other threads doing a fork.
170  	     We reset it to 1, to avoid waiting for non-existing
171  	     thread(s) to release the count.  */
172 	  allp->handler->refcntr = 1;
173 
174 	  /* XXX We could at this point look through the object pool
175 	     and mark all objects not on the __fork_handlers list as
176 	     unused.  This is necessary in case the fork() happened
177 	     while another thread called dlclose() and that call had
178 	     to create a new list.  */
179 
180 	  allp = allp->next;
181 	}
182 
183       /* Initialize the fork lock.  */
184       __fork_lock = LLL_LOCK_INITIALIZER;
185     }
186   else
187     {
188       assert (THREAD_GETMEM (THREAD_SELF, tid) == ppid);
189 
190       /* We execute this even if the 'fork' call failed.  */
191       __UCLIBC_IO_MUTEX_UNLOCK_CANCEL_UNSAFE(_stdio_openlist_add_lock);
192 
193       /* Run the handlers registered for the parent.  */
194       while (allp != NULL)
195 	{
196 	  if (allp->handler->parent_handler != NULL)
197 	    allp->handler->parent_handler ();
198 
199 	  if (atomic_decrement_and_test (&allp->handler->refcntr)
200 	      && allp->handler->need_signal)
201 	    lll_futex_wake (allp->handler->refcntr, 1, LLL_PRIVATE);
202 
203 	  allp = allp->next;
204 	}
205     }
206 
207   return pid;
208 }
209 libc_hidden_def(fork)
210 
211 #endif /* __ARCH_USE_MMU__ */
212