1/* fork - create a child process.
2 Copyright (C) 1991-2024 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 = !SINGLE_THREAD_P;
49 uint64_t lastrun;
50
51 lastrun = __run_prefork_handlers (do_locking: 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_proc_file_chain_lock ();
66 _IO_list_lock ();
67
68 /* Acquire malloc locks. This needs to come last because fork
69 handlers may use malloc, and the libio list lock has an
70 indirect malloc dependency as well (via the getdelim
71 function). */
72 call_function_static_weak (__malloc_fork_lock_parent);
73 }
74
75 pid_t pid = _Fork ();
76
77 if (pid == 0)
78 {
79 fork_system_setup ();
80
81 /* Reset the lock state in the multi-threaded case. */
82 if (multiple_threads)
83 {
84 __libc_unwind_link_after_fork ();
85
86 fork_system_setup_after_fork ();
87
88 /* Release malloc locks. */
89 call_function_static_weak (__malloc_fork_unlock_child);
90
91 /* Reset the file list. These are recursive mutexes. */
92 fresetlockfiles ();
93
94 /* Reset locks in the I/O code. */
95 _IO_list_resetlock ();
96 _IO_proc_file_chain_resetlock ();
97
98 call_function_static_weak (__nss_database_fork_subprocess,
99 &nss_database_data);
100 }
101
102 /* Reset the lock the dynamic loader uses to protect its data. */
103 __rtld_lock_initialize (GL(dl_load_lock));
104
105 /* Reset the lock protecting dynamic TLS related data. */
106 __rtld_lock_initialize (GL(dl_load_tls_lock));
107
108 reclaim_stacks ();
109
110 /* Run the handlers registered for the child. */
111 __run_postfork_handlers (who: atfork_run_child, do_locking: multiple_threads, lastrun);
112 }
113 else
114 {
115 /* If _Fork failed, preserve its errno value. */
116 int save_errno = errno;
117
118 /* Release acquired locks in the multi-threaded case. */
119 if (multiple_threads)
120 {
121 /* Release malloc locks, parent process variant. */
122 call_function_static_weak (__malloc_fork_unlock_parent);
123
124 /* We execute this even if the 'fork' call failed. */
125 _IO_list_unlock ();
126 _IO_proc_file_chain_unlock ();
127 }
128
129 /* Run the handlers registered for the parent. */
130 __run_postfork_handlers (who: atfork_run_parent, do_locking: multiple_threads, lastrun);
131
132 if (pid < 0)
133 __set_errno (save_errno);
134 }
135
136 return pid;
137}
138weak_alias (__libc_fork, __fork)
139libc_hidden_def (__fork)
140weak_alias (__libc_fork, fork)
141

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of glibc/posix/fork.c