1 /* fork - create a child process.
2 Copyright (C) 1991-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
20 #include <libio/libioP.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>
30 fresetlockfiles (void)
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
));
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
48 bool multiple_threads
= !SINGLE_THREAD_P
;
51 lastrun
= __run_prefork_handlers (multiple_threads
);
53 struct nss_database_data nss_database_data
;
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. */
62 call_function_static_weak (__nss_database_fork_prepare_parent
,
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
71 call_function_static_weak (__malloc_fork_lock_parent
);
80 /* Reset the lock state in the multi-threaded case. */
83 __libc_unwind_link_after_fork ();
85 fork_system_setup_after_fork ();
87 /* Release malloc locks. */
88 call_function_static_weak (__malloc_fork_unlock_child
);
90 /* Reset the file list. These are recursive mutexes. */
93 /* Reset locks in the I/O code. */
94 _IO_list_resetlock ();
96 call_function_static_weak (__nss_database_fork_subprocess
,
100 /* Reset the lock the dynamic loader uses to protect its data. */
101 __rtld_lock_initialize (GL(dl_load_lock
));
103 /* Reset the lock protecting dynamic TLS related data. */
104 __rtld_lock_initialize (GL(dl_load_tls_lock
));
108 /* Run the handlers registered for the child. */
109 __run_postfork_handlers (atfork_run_child
, multiple_threads
, lastrun
);
113 /* If _Fork failed, preserve its errno value. */
114 int save_errno
= errno
;
116 /* Release acquired locks in the multi-threaded case. */
117 if (multiple_threads
)
119 /* Release malloc locks, parent process variant. */
120 call_function_static_weak (__malloc_fork_unlock_parent
);
122 /* We execute this even if the 'fork' call failed. */
126 /* Run the handlers registered for the parent. */
127 __run_postfork_handlers (atfork_run_parent
, multiple_threads
, lastrun
);
130 __set_errno (save_errno
);
135 weak_alias (__libc_fork
, __fork
)
136 libc_hidden_def (__fork
)
137 weak_alias (__libc_fork
, fork
)