1 /* fork - create a child process.
2 Copyright (C) 1991-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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 <fork.h>
20 #include <libio/libioP.h>
21 #include <ldsodefs.h>
22 #include <malloc/malloc-internal.h>
23 #include <nss/nss_database.h>
24 #include <register-atfork.h>
25 #include <stdio-lock.h>
26 #include <sys/single_threaded.h>
27 #include <unwind-link.h>
28
29 static void
fresetlockfiles(void)30 fresetlockfiles (void)
31 {
32 _IO_ITER i;
33
34 for (i = _IO_iter_begin(); i != _IO_iter_end(); i = _IO_iter_next(i))
35 if ((_IO_iter_file (i)->_flags & _IO_USER_LOCK) == 0)
36 _IO_lock_init (*((_IO_lock_t *) _IO_iter_file(i)->_lock));
37 }
38
39 pid_t
__libc_fork(void)40 __libc_fork (void)
41 {
42 /* Determine if we are running multiple threads. We skip some fork
43 handlers in the single-thread case, to make fork safer to use in
44 signal handlers. Although POSIX has dropped async-signal-safe
45 requirement for fork (Austin Group tracker issue #62) this is
46 best effort to make is async-signal-safe at least for single-thread
47 case. */
48 bool multiple_threads = !SINGLE_THREAD_P;
49 uint64_t lastrun;
50
51 lastrun = __run_prefork_handlers (multiple_threads);
52
53 struct nss_database_data nss_database_data;
54
55 /* If we are not running multiple threads, we do not have to
56 preserve lock state. If fork runs from a signal handler, only
57 async-signal-safe functions can be used in the child. These data
58 structures are only used by unsafe functions, so their state does
59 not matter if fork was called from a signal handler. */
60 if (multiple_threads)
61 {
62 call_function_static_weak (__nss_database_fork_prepare_parent,
63 &nss_database_data);
64
65 _IO_list_lock ();
66
67 /* Acquire malloc locks. This needs to come last because fork
68 handlers may use malloc, and the libio list lock has an
69 indirect malloc dependency as well (via the getdelim
70 function). */
71 call_function_static_weak (__malloc_fork_lock_parent);
72 }
73
74 pid_t pid = _Fork ();
75
76 if (pid == 0)
77 {
78 fork_system_setup ();
79
80 /* Reset the lock state in the multi-threaded case. */
81 if (multiple_threads)
82 {
83 __libc_unwind_link_after_fork ();
84
85 fork_system_setup_after_fork ();
86
87 /* Release malloc locks. */
88 call_function_static_weak (__malloc_fork_unlock_child);
89
90 /* Reset the file list. These are recursive mutexes. */
91 fresetlockfiles ();
92
93 /* Reset locks in the I/O code. */
94 _IO_list_resetlock ();
95
96 call_function_static_weak (__nss_database_fork_subprocess,
97 &nss_database_data);
98 }
99
100 /* Reset the lock the dynamic loader uses to protect its data. */
101 __rtld_lock_initialize (GL(dl_load_lock));
102
103 /* Reset the lock protecting dynamic TLS related data. */
104 __rtld_lock_initialize (GL(dl_load_tls_lock));
105
106 reclaim_stacks ();
107
108 /* Run the handlers registered for the child. */
109 __run_postfork_handlers (atfork_run_child, multiple_threads, lastrun);
110 }
111 else
112 {
113 /* If _Fork failed, preserve its errno value. */
114 int save_errno = errno;
115
116 /* Release acquired locks in the multi-threaded case. */
117 if (multiple_threads)
118 {
119 /* Release malloc locks, parent process variant. */
120 call_function_static_weak (__malloc_fork_unlock_parent);
121
122 /* We execute this even if the 'fork' call failed. */
123 _IO_list_unlock ();
124 }
125
126 /* Run the handlers registered for the parent. */
127 __run_postfork_handlers (atfork_run_parent, multiple_threads, lastrun);
128
129 if (pid < 0)
130 __set_errno (save_errno);
131 }
132
133 return pid;
134 }
135 weak_alias (__libc_fork, __fork)
136 libc_hidden_def (__fork)
137 weak_alias (__libc_fork, fork)
138