fork.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #ifdef ARCH_FORK
  112. pid = ARCH_FORK ();
  113. #else
  114. # error "ARCH_FORK must be defined so that the CLONE_SETTID flag is used"
  115. pid = INLINE_SYSCALL (fork, 0);
  116. #endif
  117. if (pid == 0)
  118. {
  119. struct pthread *self = THREAD_SELF;
  120. assert (THREAD_GETMEM (self, tid) != ppid);
  121. if (__fork_generation_pointer != NULL)
  122. *__fork_generation_pointer += 4;
  123. #if HP_TIMING_AVAIL
  124. /* The CPU clock of the thread and process have to be set to zero. */
  125. hp_timing_t now;
  126. HP_TIMING_NOW (now);
  127. THREAD_SETMEM (self, cpuclock_offset, now);
  128. GL(dl_cpuclock_offset) = now;
  129. #endif
  130. /* Reset the file list. These are recursive mutexes. */
  131. fresetlockfiles ();
  132. /* Reset locks in the I/O code. */
  133. STDIO_INIT_MUTEX(_stdio_openlist_add_lock);
  134. /* XXX reset any locks in dynamic loader */
  135. /* Run the handlers registered for the child. */
  136. while (allp != NULL)
  137. {
  138. if (allp->handler->child_handler != NULL)
  139. allp->handler->child_handler ();
  140. /* Note that we do not have to wake any possible waiter.
  141. This is the only thread in the new process. The count
  142. may have been bumped up by other threads doing a fork.
  143. We reset it to 1, to avoid waiting for non-existing
  144. thread(s) to release the count. */
  145. allp->handler->refcntr = 1;
  146. /* XXX We could at this point look through the object pool
  147. and mark all objects not on the __fork_handlers list as
  148. unused. This is necessary in case the fork() happened
  149. while another thread called dlclose() and that call had
  150. to create a new list. */
  151. allp = allp->next;
  152. }
  153. /* Initialize the fork lock. */
  154. __fork_lock = LLL_LOCK_INITIALIZER;
  155. }
  156. else
  157. {
  158. assert (THREAD_GETMEM (THREAD_SELF, tid) == ppid);
  159. /* We execute this even if the 'fork' call failed. */
  160. __UCLIBC_IO_MUTEX_UNLOCK_CANCEL_UNSAFE(_stdio_openlist_add_lock);
  161. /* Run the handlers registered for the parent. */
  162. while (allp != NULL)
  163. {
  164. if (allp->handler->parent_handler != NULL)
  165. allp->handler->parent_handler ();
  166. if (atomic_decrement_and_test (&allp->handler->refcntr)
  167. && allp->handler->need_signal)
  168. lll_futex_wake (allp->handler->refcntr, 1, LLL_PRIVATE);
  169. allp = allp->next;
  170. }
  171. }
  172. return pid;
  173. }
  174. libc_hidden_def(fork)