2021-01-18 18:18:13 +00:00
|
|
|
/* fork - create a child process.
|
2023-01-06 21:08:04 +00:00
|
|
|
Copyright (C) 1991-2023 Free Software Foundation, Inc.
|
1997-06-23 21:55:26 +00:00
|
|
|
This file is part of the GNU C Library.
|
1995-02-18 01:27:10 +00:00
|
|
|
|
1997-06-23 21:55:26 +00:00
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 04:58:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
1995-02-18 01:27:10 +00:00
|
|
|
|
1997-06-23 21:55:26 +00:00
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2001-07-06 04:58:11 +00:00
|
|
|
Lesser General Public License for more details.
|
1995-02-18 01:27:10 +00:00
|
|
|
|
2001-07-06 04:58:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
1995-02-18 01:27:10 +00:00
|
|
|
|
2021-01-18 18:18:13 +00:00
|
|
|
#include <fork.h>
|
|
|
|
#include <libio/libioP.h>
|
|
|
|
#include <ldsodefs.h>
|
|
|
|
#include <malloc/malloc-internal.h>
|
|
|
|
#include <nss/nss_database.h>
|
|
|
|
#include <register-atfork.h>
|
|
|
|
#include <stdio-lock.h>
|
|
|
|
#include <sys/single_threaded.h>
|
|
|
|
#include <unwind-link.h>
|
1995-02-18 01:27:10 +00:00
|
|
|
|
2021-01-18 18:18:13 +00:00
|
|
|
static void
|
|
|
|
fresetlockfiles (void)
|
|
|
|
{
|
|
|
|
_IO_ITER i;
|
|
|
|
|
|
|
|
for (i = _IO_iter_begin(); i != _IO_iter_end(); i = _IO_iter_next(i))
|
|
|
|
if ((_IO_iter_file (i)->_flags & _IO_USER_LOCK) == 0)
|
|
|
|
_IO_lock_init (*((_IO_lock_t *) _IO_iter_file(i)->_lock));
|
|
|
|
}
|
1995-02-18 01:27:10 +00:00
|
|
|
|
2021-01-18 18:18:13 +00:00
|
|
|
pid_t
|
|
|
|
__libc_fork (void)
|
1995-02-18 01:27:10 +00:00
|
|
|
{
|
2021-01-18 18:18:13 +00:00
|
|
|
/* Determine if we are running multiple threads. We skip some fork
|
|
|
|
handlers in the single-thread case, to make fork safer to use in
|
2021-01-19 12:26:31 +00:00
|
|
|
signal handlers. Although POSIX has dropped async-signal-safe
|
|
|
|
requirement for fork (Austin Group tracker issue #62) this is
|
|
|
|
best effort to make is async-signal-safe at least for single-thread
|
|
|
|
case. */
|
2022-06-07 16:09:40 +00:00
|
|
|
bool multiple_threads = !SINGLE_THREAD_P;
|
Fix deadlock when pthread_atfork handler calls pthread_atfork or dlclose
In multi-threaded programs, registering via pthread_atfork,
de-registering implicitly via dlclose, or running pthread_atfork
handlers during fork was protected by an internal lock. This meant
that a pthread_atfork handler attempting to register another handler or
dlclose a dynamically loaded library would lead to a deadlock.
This commit fixes the deadlock in the following way:
During the execution of handlers at fork time, the atfork lock is
released prior to the execution of each handler and taken again upon its
return. Any handler registrations or de-registrations that occurred
during the execution of the handler are accounted for before proceeding
with further handler execution.
If a handler that hasn't been executed yet gets de-registered by another
handler during fork, it will not be executed. If a handler gets
registered by another handler during fork, it will not be executed
during that particular fork.
The possibility that handlers may now be registered or deregistered
during handler execution means that identifying the next handler to be
run after a given handler may register/de-register others requires some
bookkeeping. The fork_handler struct has an additional field, 'id',
which is assigned sequentially during registration. Thus, handlers are
executed in ascending order of 'id' during 'prepare', and descending
order of 'id' during parent/child handler execution after the fork.
Two tests are included:
* tst-atfork3: Adhemerval Zanella <adhemerval.zanella@linaro.org>
This test exercises calling dlclose from prepare, parent, and child
handlers.
* tst-atfork4: This test exercises calling pthread_atfork and dlclose
from the prepare handler.
[BZ #24595, BZ #27054]
Co-authored-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2022-05-24 15:57:36 +00:00
|
|
|
uint64_t lastrun;
|
2021-01-18 18:18:13 +00:00
|
|
|
|
Fix deadlock when pthread_atfork handler calls pthread_atfork or dlclose
In multi-threaded programs, registering via pthread_atfork,
de-registering implicitly via dlclose, or running pthread_atfork
handlers during fork was protected by an internal lock. This meant
that a pthread_atfork handler attempting to register another handler or
dlclose a dynamically loaded library would lead to a deadlock.
This commit fixes the deadlock in the following way:
During the execution of handlers at fork time, the atfork lock is
released prior to the execution of each handler and taken again upon its
return. Any handler registrations or de-registrations that occurred
during the execution of the handler are accounted for before proceeding
with further handler execution.
If a handler that hasn't been executed yet gets de-registered by another
handler during fork, it will not be executed. If a handler gets
registered by another handler during fork, it will not be executed
during that particular fork.
The possibility that handlers may now be registered or deregistered
during handler execution means that identifying the next handler to be
run after a given handler may register/de-register others requires some
bookkeeping. The fork_handler struct has an additional field, 'id',
which is assigned sequentially during registration. Thus, handlers are
executed in ascending order of 'id' during 'prepare', and descending
order of 'id' during parent/child handler execution after the fork.
Two tests are included:
* tst-atfork3: Adhemerval Zanella <adhemerval.zanella@linaro.org>
This test exercises calling dlclose from prepare, parent, and child
handlers.
* tst-atfork4: This test exercises calling pthread_atfork and dlclose
from the prepare handler.
[BZ #24595, BZ #27054]
Co-authored-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2022-05-24 15:57:36 +00:00
|
|
|
lastrun = __run_prefork_handlers (multiple_threads);
|
2021-01-18 18:18:13 +00:00
|
|
|
|
|
|
|
struct nss_database_data nss_database_data;
|
|
|
|
|
|
|
|
/* If we are not running multiple threads, we do not have to
|
|
|
|
preserve lock state. If fork runs from a signal handler, only
|
|
|
|
async-signal-safe functions can be used in the child. These data
|
|
|
|
structures are only used by unsafe functions, so their state does
|
|
|
|
not matter if fork was called from a signal handler. */
|
|
|
|
if (multiple_threads)
|
|
|
|
{
|
|
|
|
call_function_static_weak (__nss_database_fork_prepare_parent,
|
|
|
|
&nss_database_data);
|
|
|
|
|
|
|
|
_IO_list_lock ();
|
|
|
|
|
|
|
|
/* Acquire malloc locks. This needs to come last because fork
|
|
|
|
handlers may use malloc, and the libio list lock has an
|
|
|
|
indirect malloc dependency as well (via the getdelim
|
|
|
|
function). */
|
|
|
|
call_function_static_weak (__malloc_fork_lock_parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t pid = _Fork ();
|
|
|
|
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
|
|
|
fork_system_setup ();
|
|
|
|
|
|
|
|
/* Reset the lock state in the multi-threaded case. */
|
|
|
|
if (multiple_threads)
|
|
|
|
{
|
|
|
|
__libc_unwind_link_after_fork ();
|
|
|
|
|
|
|
|
fork_system_setup_after_fork ();
|
|
|
|
|
|
|
|
/* Release malloc locks. */
|
|
|
|
call_function_static_weak (__malloc_fork_unlock_child);
|
|
|
|
|
|
|
|
/* Reset the file list. These are recursive mutexes. */
|
|
|
|
fresetlockfiles ();
|
|
|
|
|
|
|
|
/* Reset locks in the I/O code. */
|
|
|
|
_IO_list_resetlock ();
|
|
|
|
|
|
|
|
call_function_static_weak (__nss_database_fork_subprocess,
|
|
|
|
&nss_database_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset the lock the dynamic loader uses to protect its data. */
|
|
|
|
__rtld_lock_initialize (GL(dl_load_lock));
|
|
|
|
|
2021-09-15 14:16:19 +00:00
|
|
|
/* Reset the lock protecting dynamic TLS related data. */
|
|
|
|
__rtld_lock_initialize (GL(dl_load_tls_lock));
|
|
|
|
|
2021-01-18 18:18:13 +00:00
|
|
|
reclaim_stacks ();
|
|
|
|
|
|
|
|
/* Run the handlers registered for the child. */
|
Fix deadlock when pthread_atfork handler calls pthread_atfork or dlclose
In multi-threaded programs, registering via pthread_atfork,
de-registering implicitly via dlclose, or running pthread_atfork
handlers during fork was protected by an internal lock. This meant
that a pthread_atfork handler attempting to register another handler or
dlclose a dynamically loaded library would lead to a deadlock.
This commit fixes the deadlock in the following way:
During the execution of handlers at fork time, the atfork lock is
released prior to the execution of each handler and taken again upon its
return. Any handler registrations or de-registrations that occurred
during the execution of the handler are accounted for before proceeding
with further handler execution.
If a handler that hasn't been executed yet gets de-registered by another
handler during fork, it will not be executed. If a handler gets
registered by another handler during fork, it will not be executed
during that particular fork.
The possibility that handlers may now be registered or deregistered
during handler execution means that identifying the next handler to be
run after a given handler may register/de-register others requires some
bookkeeping. The fork_handler struct has an additional field, 'id',
which is assigned sequentially during registration. Thus, handlers are
executed in ascending order of 'id' during 'prepare', and descending
order of 'id' during parent/child handler execution after the fork.
Two tests are included:
* tst-atfork3: Adhemerval Zanella <adhemerval.zanella@linaro.org>
This test exercises calling dlclose from prepare, parent, and child
handlers.
* tst-atfork4: This test exercises calling pthread_atfork and dlclose
from the prepare handler.
[BZ #24595, BZ #27054]
Co-authored-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2022-05-24 15:57:36 +00:00
|
|
|
__run_postfork_handlers (atfork_run_child, multiple_threads, lastrun);
|
2021-01-18 18:18:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-21 13:20:54 +00:00
|
|
|
/* If _Fork failed, preserve its errno value. */
|
|
|
|
int save_errno = errno;
|
|
|
|
|
2021-01-18 18:18:13 +00:00
|
|
|
/* Release acquired locks in the multi-threaded case. */
|
|
|
|
if (multiple_threads)
|
|
|
|
{
|
|
|
|
/* Release malloc locks, parent process variant. */
|
|
|
|
call_function_static_weak (__malloc_fork_unlock_parent);
|
|
|
|
|
|
|
|
/* We execute this even if the 'fork' call failed. */
|
|
|
|
_IO_list_unlock ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Run the handlers registered for the parent. */
|
Fix deadlock when pthread_atfork handler calls pthread_atfork or dlclose
In multi-threaded programs, registering via pthread_atfork,
de-registering implicitly via dlclose, or running pthread_atfork
handlers during fork was protected by an internal lock. This meant
that a pthread_atfork handler attempting to register another handler or
dlclose a dynamically loaded library would lead to a deadlock.
This commit fixes the deadlock in the following way:
During the execution of handlers at fork time, the atfork lock is
released prior to the execution of each handler and taken again upon its
return. Any handler registrations or de-registrations that occurred
during the execution of the handler are accounted for before proceeding
with further handler execution.
If a handler that hasn't been executed yet gets de-registered by another
handler during fork, it will not be executed. If a handler gets
registered by another handler during fork, it will not be executed
during that particular fork.
The possibility that handlers may now be registered or deregistered
during handler execution means that identifying the next handler to be
run after a given handler may register/de-register others requires some
bookkeeping. The fork_handler struct has an additional field, 'id',
which is assigned sequentially during registration. Thus, handlers are
executed in ascending order of 'id' during 'prepare', and descending
order of 'id' during parent/child handler execution after the fork.
Two tests are included:
* tst-atfork3: Adhemerval Zanella <adhemerval.zanella@linaro.org>
This test exercises calling dlclose from prepare, parent, and child
handlers.
* tst-atfork4: This test exercises calling pthread_atfork and dlclose
from the prepare handler.
[BZ #24595, BZ #27054]
Co-authored-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2022-05-24 15:57:36 +00:00
|
|
|
__run_postfork_handlers (atfork_run_parent, multiple_threads, lastrun);
|
2021-01-21 13:20:54 +00:00
|
|
|
|
|
|
|
if (pid < 0)
|
|
|
|
__set_errno (save_errno);
|
2021-01-18 18:18:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pid;
|
1995-02-18 01:27:10 +00:00
|
|
|
}
|
2021-01-18 18:18:13 +00:00
|
|
|
weak_alias (__libc_fork, __fork)
|
2002-12-31 11:12:11 +00:00
|
|
|
libc_hidden_def (__fork)
|
2021-01-18 18:18:13 +00:00
|
|
|
weak_alias (__libc_fork, fork)
|