1/* fork - create a child process.
2 Copyright (C) 1991-2021 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
29static void
30fresetlockfiles (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
39pid_t
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 = __libc_single_threaded == 0;
49
50 __run_fork_handlers (atfork_run_prepare, multiple_threads);
51
52 struct nss_database_data nss_database_data;
53
54 /* If we are not running multiple threads, we do not have to
55 preserve lock state. If fork runs from a signal handler, only
56 async-signal-safe functions can be used in the child. These data
57 structures are only used by unsafe functions, so their state does
58 not matter if fork was called from a signal handler. */
59 if (multiple_threads)
60 {
61 call_function_static_weak (__nss_database_fork_prepare_parent,
62 &nss_database_data);
63
64 _IO_list_lock ();
65
66 /* Acquire malloc locks. This needs to come last because fork
67 handlers may use malloc, and the libio list lock has an
68 indirect malloc dependency as well (via the getdelim
69 function). */
70 call_function_static_weak (__malloc_fork_lock_parent);
71 }
72
73 pid_t pid = _Fork ();
74
75 if (pid == 0)
76 {
77 fork_system_setup ();
78
79 /* Reset the lock state in the multi-threaded case. */
80 if (multiple_threads)
81 {
82 __libc_unwind_link_after_fork ();
83
84 fork_system_setup_after_fork ();
85
86 /* Release malloc locks. */
87 call_function_static_weak (__malloc_fork_unlock_child);
88
89 /* Reset the file list. These are recursive mutexes. */
90 fresetlockfiles ();
91
92 /* Reset locks in the I/O code. */
93 _IO_list_resetlock ();
94
95 call_function_static_weak (__nss_database_fork_subprocess,
96 &nss_database_data);
97 }
98
99 /* Reset the lock the dynamic loader uses to protect its data. */
100 __rtld_lock_initialize (GL(dl_load_lock));
101
102 reclaim_stacks ();
103
104 /* Run the handlers registered for the child. */
105 __run_fork_handlers (atfork_run_child, multiple_threads);
106 }
107 else
108 {
109 /* If _Fork failed, preserve its errno value. */
110 int save_errno = errno;
111
112 /* Release acquired locks in the multi-threaded case. */
113 if (multiple_threads)
114 {
115 /* Release malloc locks, parent process variant. */
116 call_function_static_weak (__malloc_fork_unlock_parent);
117
118 /* We execute this even if the 'fork' call failed. */
119 _IO_list_unlock ();
120 }
121
122 /* Run the handlers registered for the parent. */
123 __run_fork_handlers (atfork_run_parent, multiple_threads);
124
125 if (pid < 0)
126 __set_errno (save_errno);
127 }
128
129 return pid;
130}
131weak_alias (__libc_fork, __fork)
132libc_hidden_def (__fork)
133weak_alias (__libc_fork, fork)
134