mirror of
https://sourceware.org/git/glibc.git
synced 2024-11-14 01:00:07 +00:00
c32c868ab8
Austin Group issue 62 [1] dropped the async-signal-safe requirement for fork and provided a async-signal-safe _Fork replacement that does not run the atfork handlers. It will be included in the next POSIX standard. It allow to close a long standing issue to make fork AS-safe (BZ#4737). As indicated on the bug, besides the internal lock for the atfork handlers itself; there is no guarantee that the handlers itself will not introduce more AS-safe issues. The idea is synchronize fork with the required internal locks to allow children in multithread processes to use mostly of standard function (even though POSIX states only AS-safe function should be used). On signal handles, _Fork should be used intead and only AS-safe functions should be used. For testing, the new tst-_Fork only check basic usage. I also added a new tst-mallocfork3 which uses the same strategy to check for deadlock of tst-mallocfork2 but using threads instead of subprocesses (and it does deadlock if it replaces _Fork with fork). [1] https://austingroupbugs.net/view.php?id=62
134 lines
4.1 KiB
C
134 lines
4.1 KiB
C
/* fork - create a child process.
|
|
Copyright (C) 1991-2021 Free Software Foundation, Inc.
|
|
This file is part of the GNU C Library.
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
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.
|
|
|
|
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
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with the GNU C Library; if not, see
|
|
<https://www.gnu.org/licenses/>. */
|
|
|
|
#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>
|
|
|
|
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));
|
|
}
|
|
|
|
pid_t
|
|
__libc_fork (void)
|
|
{
|
|
/* Determine if we are running multiple threads. We skip some fork
|
|
handlers in the single-thread case, to make fork safer to use in
|
|
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. */
|
|
bool multiple_threads = __libc_single_threaded == 0;
|
|
|
|
__run_fork_handlers (atfork_run_prepare, multiple_threads);
|
|
|
|
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));
|
|
|
|
reclaim_stacks ();
|
|
|
|
/* Run the handlers registered for the child. */
|
|
__run_fork_handlers (atfork_run_child, multiple_threads);
|
|
}
|
|
else
|
|
{
|
|
/* If _Fork failed, preserve its errno value. */
|
|
int save_errno = errno;
|
|
|
|
/* 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. */
|
|
__run_fork_handlers (atfork_run_parent, multiple_threads);
|
|
|
|
if (pid < 0)
|
|
__set_errno (save_errno);
|
|
}
|
|
|
|
return pid;
|
|
}
|
|
weak_alias (__libc_fork, __fork)
|
|
libc_hidden_def (__fork)
|
|
weak_alias (__libc_fork, fork)
|