Update translations.
[glibc.git] / posix / fork.c
blob6b50c091f92d37d0bfe9043352c223915f114cad
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.
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/>. */
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>
29 static void
30 fresetlockfiles (void)
32 _IO_ITER i;
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));
39 pid_t
40 __libc_fork (void)
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;
50 __run_fork_handlers (atfork_run_prepare, multiple_threads);
52 struct nss_database_data nss_database_data;
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)
61 call_function_static_weak (__nss_database_fork_prepare_parent,
62 &nss_database_data);
64 _IO_list_lock ();
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);
73 pid_t pid = _Fork ();
75 if (pid == 0)
77 fork_system_setup ();
79 /* Reset the lock state in the multi-threaded case. */
80 if (multiple_threads)
82 __libc_unwind_link_after_fork ();
84 fork_system_setup_after_fork ();
86 /* Release malloc locks. */
87 call_function_static_weak (__malloc_fork_unlock_child);
89 /* Reset the file list. These are recursive mutexes. */
90 fresetlockfiles ();
92 /* Reset locks in the I/O code. */
93 _IO_list_resetlock ();
95 call_function_static_weak (__nss_database_fork_subprocess,
96 &nss_database_data);
99 /* Reset the lock the dynamic loader uses to protect its data. */
100 __rtld_lock_initialize (GL(dl_load_lock));
102 /* Reset the lock protecting dynamic TLS related data. */
103 __rtld_lock_initialize (GL(dl_load_tls_lock));
105 reclaim_stacks ();
107 /* Run the handlers registered for the child. */
108 __run_fork_handlers (atfork_run_child, multiple_threads);
110 else
112 /* If _Fork failed, preserve its errno value. */
113 int save_errno = errno;
115 /* Release acquired locks in the multi-threaded case. */
116 if (multiple_threads)
118 /* Release malloc locks, parent process variant. */
119 call_function_static_weak (__malloc_fork_unlock_parent);
121 /* We execute this even if the 'fork' call failed. */
122 _IO_list_unlock ();
125 /* Run the handlers registered for the parent. */
126 __run_fork_handlers (atfork_run_parent, multiple_threads);
128 if (pid < 0)
129 __set_errno (save_errno);
132 return pid;
134 weak_alias (__libc_fork, __fork)
135 libc_hidden_def (__fork)
136 weak_alias (__libc_fork, fork)