1 | /* Copyright (C) 2002-2020 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 | <https://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, multiple_threads); |
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 | /* Initialize the robust mutex list setting in the kernel which has |
87 | been reset during the fork. We do not check for errors because if |
88 | it fails here, it must have failed at process startup as well and |
89 | nobody could have used robust mutexes. |
90 | Before we do that, we have to clear the list of robust mutexes |
91 | because we do not inherit ownership of mutexes from the parent. |
92 | We do not have to set self->robust_head.futex_offset since we do |
93 | inherit the correct value from the parent. We do not need to clear |
94 | the pending operation because it must have been zero when fork was |
95 | called. */ |
96 | #if __PTHREAD_MUTEX_HAVE_PREV |
97 | self->robust_prev = &self->robust_head; |
98 | #endif |
99 | self->robust_head.list = &self->robust_head; |
100 | #ifdef SHARED |
101 | if (__builtin_expect (__libc_pthread_functions_init, 0)) |
102 | PTHFCT_CALL (ptr_set_robust, (self)); |
103 | #else |
104 | extern __typeof (__nptl_set_robust) __nptl_set_robust |
105 | __attribute__((weak)); |
106 | if (__builtin_expect (__nptl_set_robust != NULL, 0)) |
107 | __nptl_set_robust (self); |
108 | #endif |
109 | |
110 | /* Reset the lock state in the multi-threaded case. */ |
111 | if (multiple_threads) |
112 | { |
113 | /* Release malloc locks. */ |
114 | call_function_static_weak (__malloc_fork_unlock_child); |
115 | |
116 | /* Reset the file list. These are recursive mutexes. */ |
117 | fresetlockfiles (); |
118 | |
119 | /* Reset locks in the I/O code. */ |
120 | _IO_list_resetlock (); |
121 | } |
122 | |
123 | /* Reset the lock the dynamic loader uses to protect its data. */ |
124 | __rtld_lock_initialize (GL(dl_load_lock)); |
125 | |
126 | /* Run the handlers registered for the child. */ |
127 | __run_fork_handlers (atfork_run_child, multiple_threads); |
128 | } |
129 | else |
130 | { |
131 | /* Release acquired locks in the multi-threaded case. */ |
132 | if (multiple_threads) |
133 | { |
134 | /* Release malloc locks, parent process variant. */ |
135 | call_function_static_weak (__malloc_fork_unlock_parent); |
136 | |
137 | /* We execute this even if the 'fork' call failed. */ |
138 | _IO_list_unlock (); |
139 | } |
140 | |
141 | /* Run the handlers registered for the parent. */ |
142 | __run_fork_handlers (atfork_run_parent, multiple_threads); |
143 | } |
144 | |
145 | return pid; |
146 | } |
147 | weak_alias (__libc_fork, __fork) |
148 | libc_hidden_def (__fork) |
149 | weak_alias (__libc_fork, fork) |
150 | |