fork.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <assert.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <sys/types.h>
  21. #include <sysdep.h>
  22. #include <tls.h>
  23. #include "fork.h"
  24. #include <hp-timing.h>
  25. #include <ldsodefs.h>
  26. #include <atomic.h>
  27. #include <errno.h>
  28. unsigned long int *__fork_generation_pointer;
  29. /* The single linked list of all currently registered for handlers. */
  30. struct fork_handler *__fork_handlers;
  31. static void
  32. fresetlockfiles (void)
  33. {
  34. FILE *fp;
  35. #ifdef __USE_STDIO_FUTEXES__
  36. for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen)
  37. STDIO_INIT_MUTEX(fp->__lock);
  38. #else
  39. pthread_mutexattr_t attr;
  40. pthread_mutexattr_init(&attr);
  41. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
  42. for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen)
  43. pthread_mutex_init(&fp->__lock, &attr);
  44. pthread_mutexattr_destroy(&attr);
  45. #endif
  46. }
  47. extern __typeof(fork) __libc_fork;
  48. pid_t
  49. __libc_fork (void)
  50. {
  51. pid_t pid;
  52. struct used_handler
  53. {
  54. struct fork_handler *handler;
  55. struct used_handler *next;
  56. } *allp = NULL;
  57. /* Run all the registered preparation handlers. In reverse order.
  58. While doing this we build up a list of all the entries. */
  59. struct fork_handler *runp;
  60. while ((runp = __fork_handlers) != NULL)
  61. {
  62. /* Make sure we read from the current RUNP pointer. */
  63. atomic_full_barrier ();
  64. unsigned int oldval = runp->refcntr;
  65. if (oldval == 0)
  66. /* This means some other thread removed the list just after
  67. the pointer has been loaded. Try again. Either the list
  68. is empty or we can retry it. */
  69. continue;
  70. /* Bump the reference counter. */
  71. if (atomic_compare_and_exchange_bool_acq (&__fork_handlers->refcntr,
  72. oldval + 1, oldval))
  73. /* The value changed, try again. */
  74. continue;
  75. /* We bumped the reference counter for the first entry in the
  76. list. That means that none of the following entries will
  77. just go away. The unloading code works in the order of the
  78. list.
  79. While executing the registered handlers we are building a
  80. list of all the entries so that we can go backward later on. */
  81. while (1)
  82. {
  83. /* Execute the handler if there is one. */
  84. if (runp->prepare_handler != NULL)
  85. runp->prepare_handler ();
  86. /* Create a new element for the list. */
  87. struct used_handler *newp
  88. = (struct used_handler *) alloca (sizeof (*newp));
  89. newp->handler = runp;
  90. newp->next = allp;
  91. allp = newp;
  92. /* Advance to the next handler. */
  93. runp = runp->next;
  94. if (runp == NULL)
  95. break;
  96. /* Bump the reference counter for the next entry. */
  97. atomic_increment (&runp->refcntr);
  98. }
  99. /* We are done. */
  100. break;
  101. }
  102. __UCLIBC_IO_MUTEX_LOCK_CANCEL_UNSAFE(_stdio_openlist_add_lock);
  103. #ifndef NDEBUG
  104. pid_t ppid = THREAD_GETMEM (THREAD_SELF, tid);
  105. #endif
  106. /* We need to prevent the getpid() code to update the PID field so
  107. that, if a signal arrives in the child very early and the signal
  108. handler uses getpid(), the value returned is correct. */
  109. pid_t parentpid = THREAD_GETMEM (THREAD_SELF, pid);
  110. THREAD_SETMEM (THREAD_SELF, pid, -parentpid);
  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. /* Adjust the PID field for the new process. */
  124. THREAD_SETMEM (self, pid, THREAD_GETMEM (self, tid));
  125. #if HP_TIMING_AVAIL
  126. /* The CPU clock of the thread and process have to be set to zero. */
  127. hp_timing_t now;
  128. HP_TIMING_NOW (now);
  129. THREAD_SETMEM (self, cpuclock_offset, now);
  130. GL(dl_cpuclock_offset) = now;
  131. #endif
  132. /* Reset the file list. These are recursive mutexes. */
  133. fresetlockfiles ();
  134. /* Reset locks in the I/O code. */
  135. STDIO_INIT_MUTEX(_stdio_openlist_add_lock);
  136. /* XXX reset any locks in dynamic loader */
  137. /* Run the handlers registered for the child. */
  138. while (allp != NULL)
  139. {
  140. if (allp->handler->child_handler != NULL)
  141. allp->handler->child_handler ();
  142. /* Note that we do not have to wake any possible waiter.
  143. This is the only thread in the new process. The count
  144. may have been bumped up by other threads doing a fork.
  145. We reset it to 1, to avoid waiting for non-existing
  146. thread(s) to release the count. */
  147. allp->handler->refcntr = 1;
  148. /* XXX We could at this point look through the object pool
  149. and mark all objects not on the __fork_handlers list as
  150. unused. This is necessary in case the fork() happened
  151. while another thread called dlclose() and that call had
  152. to create a new list. */
  153. allp = allp->next;
  154. }
  155. /* Initialize the fork lock. */
  156. __fork_lock = LLL_LOCK_INITIALIZER;
  157. }
  158. else
  159. {
  160. assert (THREAD_GETMEM (THREAD_SELF, tid) == ppid);
  161. /* Restore the PID value. */
  162. THREAD_SETMEM (THREAD_SELF, pid, parentpid);
  163. /* We execute this even if the 'fork' call failed. */
  164. __UCLIBC_IO_MUTEX_UNLOCK_CANCEL_UNSAFE(_stdio_openlist_add_lock);
  165. /* Run the handlers registered for the parent. */
  166. while (allp != NULL)
  167. {
  168. if (allp->handler->parent_handler != NULL)
  169. allp->handler->parent_handler ();
  170. if (atomic_decrement_and_test (&allp->handler->refcntr)
  171. && allp->handler->need_signal)
  172. lll_futex_wake (allp->handler->refcntr, 1, LLL_PRIVATE);
  173. allp = allp->next;
  174. }
  175. }
  176. return pid;
  177. }
  178. weak_alias(__libc_fork,__fork)
  179. libc_hidden_proto(fork)
  180. weak_alias(__libc_fork,fork)
  181. libc_hidden_weak(fork)