fork.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <sys/types.h>
  20. #include <sysdep.h>
  21. #include <tls.h>
  22. #include "fork.h"
  23. #include <hp-timing.h>
  24. #include <ldsodefs.h>
  25. #include <atomic.h>
  26. #include <errno.h>
  27. unsigned long int *__fork_generation_pointer;
  28. /* The single linked list of all currently registered for handlers. */
  29. struct fork_handler *__fork_handlers;
  30. static void
  31. fresetlockfiles (void)
  32. {
  33. FILE *fp;
  34. #ifdef __USE_STDIO_FUTEXES__
  35. for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen)
  36. STDIO_INIT_MUTEX(fp->__lock);
  37. #else
  38. pthread_mutexattr_t attr;
  39. pthread_mutexattr_init(&attr);
  40. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
  41. for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen)
  42. pthread_mutex_init(&fp->__lock, &attr);
  43. pthread_mutexattr_destroy(&attr);
  44. #endif
  45. }
  46. pid_t
  47. #if defined __arm__ && defined __thumb__ && __GNUC_PREREQ (4,6)
  48. /* GCC PR target/53735
  49. * In thumb1 we run out of registers when compiling with Os so relax that
  50. * to have more registers available for spilling by using O2 here.
  51. */
  52. attribute_optimize("O2")
  53. #endif
  54. fork (void)
  55. {
  56. pid_t pid;
  57. struct used_handler
  58. {
  59. struct fork_handler *handler;
  60. struct used_handler *next;
  61. } *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)
  66. {
  67. /* Make sure we read from the current RUNP pointer. */
  68. atomic_full_barrier ();
  69. unsigned int oldval = runp->refcntr;
  70. if (oldval == 0)
  71. /* This means some other thread removed the list just after
  72. the pointer has been loaded. Try again. Either the list
  73. is empty or we can retry it. */
  74. continue;
  75. /* Bump the reference counter. */
  76. if (atomic_compare_and_exchange_bool_acq (&__fork_handlers->refcntr,
  77. oldval + 1, oldval))
  78. /* The value changed, try again. */
  79. continue;
  80. /* We bumped the reference counter for the first entry in the
  81. list. That means that none of the following entries will
  82. just go away. The unloading code works in the order of the
  83. list.
  84. While executing the registered handlers we are building a
  85. list of all the entries so that we can go backward later on. */
  86. while (1)
  87. {
  88. /* Execute the handler if there is one. */
  89. if (runp->prepare_handler != NULL)
  90. runp->prepare_handler ();
  91. /* Create a new element for the list. */
  92. struct used_handler *newp
  93. = (struct used_handler *) alloca (sizeof (*newp));
  94. newp->handler = runp;
  95. newp->next = allp;
  96. allp = newp;
  97. /* Advance to the next handler. */
  98. runp = runp->next;
  99. if (runp == NULL)
  100. break;
  101. /* Bump the reference counter for the next entry. */
  102. atomic_increment (&runp->refcntr);
  103. }
  104. /* We are done. */
  105. break;
  106. }
  107. __UCLIBC_IO_MUTEX_LOCK_CANCEL_UNSAFE(_stdio_openlist_add_lock);
  108. #ifndef NDEBUG
  109. pid_t ppid = THREAD_GETMEM (THREAD_SELF, tid);
  110. #endif
  111. /* We need to prevent the getpid() code to update the PID field so
  112. that, if a signal arrives in the child very early and the signal
  113. handler uses getpid(), the value returned is correct. */
  114. pid_t parentpid = THREAD_GETMEM (THREAD_SELF, pid);
  115. THREAD_SETMEM (THREAD_SELF, pid, -parentpid);
  116. #ifdef ARCH_FORK
  117. pid = ARCH_FORK ();
  118. #else
  119. # error "ARCH_FORK must be defined so that the CLONE_SETTID flag is used"
  120. pid = INLINE_SYSCALL (fork, 0);
  121. #endif
  122. if (pid == 0)
  123. {
  124. struct pthread *self = THREAD_SELF;
  125. assert (THREAD_GETMEM (self, tid) != ppid);
  126. if (__fork_generation_pointer != NULL)
  127. *__fork_generation_pointer += 4;
  128. /* Adjust the PID field for the new process. */
  129. THREAD_SETMEM (self, pid, THREAD_GETMEM (self, tid));
  130. #if HP_TIMING_AVAIL
  131. /* The CPU clock of the thread and process have to be set to zero. */
  132. hp_timing_t now;
  133. HP_TIMING_NOW (now);
  134. THREAD_SETMEM (self, cpuclock_offset, now);
  135. GL(dl_cpuclock_offset) = now;
  136. #endif
  137. /* Reset the file list. These are recursive mutexes. */
  138. fresetlockfiles ();
  139. /* Reset locks in the I/O code. */
  140. STDIO_INIT_MUTEX(_stdio_openlist_add_lock);
  141. /* XXX reset any locks in dynamic loader */
  142. /* Run the handlers registered for the child. */
  143. while (allp != NULL)
  144. {
  145. if (allp->handler->child_handler != NULL)
  146. allp->handler->child_handler ();
  147. /* Note that we do not have to wake any possible waiter.
  148. This is the only thread in the new process. The count
  149. may have been bumped up by other threads doing a fork.
  150. We reset it to 1, to avoid waiting for non-existing
  151. thread(s) to release the count. */
  152. allp->handler->refcntr = 1;
  153. /* XXX We could at this point look through the object pool
  154. and mark all objects not on the __fork_handlers list as
  155. unused. This is necessary in case the fork() happened
  156. while another thread called dlclose() and that call had
  157. to create a new list. */
  158. allp = allp->next;
  159. }
  160. /* Initialize the fork lock. */
  161. __fork_lock = LLL_LOCK_INITIALIZER;
  162. }
  163. else
  164. {
  165. assert (THREAD_GETMEM (THREAD_SELF, tid) == ppid);
  166. /* Restore the PID value. */
  167. THREAD_SETMEM (THREAD_SELF, pid, parentpid);
  168. /* We execute this even if the 'fork' call failed. */
  169. __UCLIBC_IO_MUTEX_UNLOCK_CANCEL_UNSAFE(_stdio_openlist_add_lock);
  170. /* Run the handlers registered for the parent. */
  171. while (allp != NULL)
  172. {
  173. if (allp->handler->parent_handler != NULL)
  174. allp->handler->parent_handler ();
  175. if (atomic_decrement_and_test (&allp->handler->refcntr)
  176. && allp->handler->need_signal)
  177. lll_futex_wake (allp->handler->refcntr, 1, LLL_PRIVATE);
  178. allp = allp->next;
  179. }
  180. }
  181. return pid;
  182. }
  183. libc_hidden_def(fork)