| 1 | /* Copyright (C) 2002-2019 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 <unistd.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <sysdep.h> |
| 24 | #include <libio/libioP.h> |
| 25 | #include <tls.h> |
| 26 | #include <hp-timing.h> |
| 27 | #include <ldsodefs.h> |
| 28 | #include <stdio-lock.h> |
| 29 | #include <atomic.h> |
| 30 | #include <nptl/pthreadP.h> |
| 31 | #include <fork.h> |
| 32 | #include <arch-fork.h> |
| 33 | #include <futex-internal.h> |
| 34 | #include <malloc/malloc-internal.h> |
| 35 | |
| 36 | static void |
| 37 | fresetlockfiles (void) |
| 38 | { |
| 39 | _IO_ITER i; |
| 40 | |
| 41 | for (i = _IO_iter_begin(); i != _IO_iter_end(); i = _IO_iter_next(i)) |
| 42 | if ((_IO_iter_file (i)->_flags & _IO_USER_LOCK) == 0) |
| 43 | _IO_lock_init (*((_IO_lock_t *) _IO_iter_file(i)->_lock)); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | pid_t |
| 48 | __libc_fork (void) |
| 49 | { |
| 50 | pid_t pid; |
| 51 | |
| 52 | /* Determine if we are running multiple threads. We skip some fork |
| 53 | handlers in the single-thread case, to make fork safer to use in |
| 54 | signal handlers. POSIX requires that fork is async-signal-safe, |
| 55 | but our current fork implementation is not. */ |
| 56 | bool multiple_threads = THREAD_GETMEM (THREAD_SELF, header.multiple_threads); |
| 57 | |
| 58 | __run_fork_handlers (atfork_run_prepare); |
| 59 | |
| 60 | /* If we are not running multiple threads, we do not have to |
| 61 | preserve lock state. If fork runs from a signal handler, only |
| 62 | async-signal-safe functions can be used in the child. These data |
| 63 | structures are only used by unsafe functions, so their state does |
| 64 | not matter if fork was called from a signal handler. */ |
| 65 | if (multiple_threads) |
| 66 | { |
| 67 | _IO_list_lock (); |
| 68 | |
| 69 | /* Acquire malloc locks. This needs to come last because fork |
| 70 | handlers may use malloc, and the libio list lock has an |
| 71 | indirect malloc dependency as well (via the getdelim |
| 72 | function). */ |
| 73 | call_function_static_weak (__malloc_fork_lock_parent); |
| 74 | } |
| 75 | |
| 76 | pid = arch_fork (&THREAD_SELF->tid); |
| 77 | |
| 78 | if (pid == 0) |
| 79 | { |
| 80 | struct pthread *self = THREAD_SELF; |
| 81 | |
| 82 | /* See __pthread_once. */ |
| 83 | if (__fork_generation_pointer != NULL) |
| 84 | *__fork_generation_pointer += __PTHREAD_ONCE_FORK_GEN_INCR; |
| 85 | |
| 86 | #if HP_TIMING_AVAIL |
| 87 | /* The CPU clock of the thread and process have to be set to zero. */ |
| 88 | hp_timing_t now; |
| 89 | HP_TIMING_NOW (now); |
| 90 | THREAD_SETMEM (self, cpuclock_offset, now); |
| 91 | GL(dl_cpuclock_offset) = now; |
| 92 | #endif |
| 93 | |
| 94 | #ifdef __NR_set_robust_list |
| 95 | /* Initialize the robust mutex list setting in the kernel which has |
| 96 | been reset during the fork. We do not check for errors because if |
| 97 | it fails here, it must have failed at process startup as well and |
| 98 | nobody could have used robust mutexes. |
| 99 | Before we do that, we have to clear the list of robust mutexes |
| 100 | because we do not inherit ownership of mutexes from the parent. |
| 101 | We do not have to set self->robust_head.futex_offset since we do |
| 102 | inherit the correct value from the parent. We do not need to clear |
| 103 | the pending operation because it must have been zero when fork was |
| 104 | called. */ |
| 105 | # if __PTHREAD_MUTEX_HAVE_PREV |
| 106 | self->robust_prev = &self->robust_head; |
| 107 | # endif |
| 108 | self->robust_head.list = &self->robust_head; |
| 109 | # ifdef SHARED |
| 110 | if (__builtin_expect (__libc_pthread_functions_init, 0)) |
| 111 | PTHFCT_CALL (ptr_set_robust, (self)); |
| 112 | # else |
| 113 | extern __typeof (__nptl_set_robust) __nptl_set_robust |
| 114 | __attribute__((weak)); |
| 115 | if (__builtin_expect (__nptl_set_robust != NULL, 0)) |
| 116 | __nptl_set_robust (self); |
| 117 | # endif |
| 118 | #endif |
| 119 | |
| 120 | /* Reset the lock state in the multi-threaded case. */ |
| 121 | if (multiple_threads) |
| 122 | { |
| 123 | /* Release malloc locks. */ |
| 124 | call_function_static_weak (__malloc_fork_unlock_child); |
| 125 | |
| 126 | /* Reset the file list. These are recursive mutexes. */ |
| 127 | fresetlockfiles (); |
| 128 | |
| 129 | /* Reset locks in the I/O code. */ |
| 130 | _IO_list_resetlock (); |
| 131 | } |
| 132 | |
| 133 | /* Reset the lock the dynamic loader uses to protect its data. */ |
| 134 | __rtld_lock_initialize (GL(dl_load_lock)); |
| 135 | |
| 136 | /* Run the handlers registered for the child. */ |
| 137 | __run_fork_handlers (atfork_run_child); |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | /* Release acquired locks in the multi-threaded case. */ |
| 142 | if (multiple_threads) |
| 143 | { |
| 144 | /* Release malloc locks, parent process variant. */ |
| 145 | call_function_static_weak (__malloc_fork_unlock_parent); |
| 146 | |
| 147 | /* We execute this even if the 'fork' call failed. */ |
| 148 | _IO_list_unlock (); |
| 149 | } |
| 150 | |
| 151 | /* Run the handlers registered for the parent. */ |
| 152 | __run_fork_handlers (atfork_run_parent); |
| 153 | } |
| 154 | |
| 155 | return pid; |
| 156 | } |
| 157 | weak_alias (__libc_fork, __fork) |
| 158 | libc_hidden_def (__fork) |
| 159 | weak_alias (__libc_fork, fork) |
| 160 | |