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