2021-01-02 19:32:25 +00:00
|
|
|
/* Copyright (C) 1994-2021 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
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <hurd.h>
|
|
|
|
#include <hurd/signal.h>
|
hurd: Replace threadvars with TLS
This gets rid of a lot of kludge and gets closer to other ports.
* hurd/Makefile (headers): Remove threadvar.h.
(inline-headers): Remove threadvar.h.
* hurd/Versions (GLIBC_2.0: Remove __hurd_sigthread_stack_base,
__hurd_sigthread_stack_end, __hurd_sigthread_variables,
__hurd_threadvar_max, __hurd_errno_location.
(HURD_CTHREADS_0.3): Add pthread_getattr_np, pthread_attr_getstack.
* hurd/hurd/signal.h: Do not include <hurd/threadvar.h>.
(_hurd_self_sigstate): Use THREAD_SELF to get _hurd_sigstate.
(_HURD_SIGNAL_H_EXTERN_INLINE): Use THREAD_SELF to get _hurd_sigstate,
unless TLS is not initialized yet, in which case we do not need a
critical section yet anyway.
* hurd/hurd/threadvar.h: Include <tls.h>, do not include
<machine-sp.h>.
(__hurd_sigthread_variables, __hurd_threadvar_max): Remove variables
declarations.
(__hurd_threadvar_index): Remove enum.
(_HURD_THREADVAR_H_EXTERN_INLINE): Remove macro.
(__hurd_threadvar_location_from_sp,__hurd_threadvar_location): Remove
inlines.
(__hurd_reply_port0): New variable declaration.
(__hurd_local_reply_port): New macro.
* hurd/hurdsig.c (__hurd_sigthread_variables): Remove variable.
(interrupted_reply_port_location): Add thread_t parameter. Use it
with THREAD_TCB to access thread-local variables.
(_hurdsig_abort_rpcs): Pass ss->thread to
interrupted_reply_port_location.
(_hurd_internal_post_signal): Likewise.
(_hurdsig_init): Use presence of cthread_fork instead of
__hurd_threadvar_stack_mask to start signal thread by hand.
Remove signal thread threadvar initialization.
* hurd/hurdstartup.c: Do not include <hurd/threadvar.h>
* hurd/sigunwind.c: Include <hurd/threadvar.h>
(_hurdsig_longjmp_from_handler): Use __hurd_local_reply_port instead
of threadvar.
* sysdeps/mach/hurd/Versions (libc.GLIBC_PRIVATE): Add
__libc_lock_self0.
(ld.GLIBC_2.0): Remove __hurd_sigthread_stack_base,
__hurd_sigthread_stack_end, __hurd_sigthread_variables.
(ld.GLIBC_PRIVATE): Add __libc_lock_self0.
* sysdeps/mach/hurd/cthreads.c: Add __libc_lock_self0.
* sysdeps/mach/hurd/dl-sysdep.c (errno, __hurd_sigthread_stack_base,
__hurd_sigthread_stack_end, __hurd_sigthread_variables, threadvars,
__hurd_threadvar_stack_offset, __hurd_threadvar_stack_mask): Do not
define variables.
* sysdeps/mach/hurd/errno-loc.c: Do not include <errno.h> and
<hurd/threadvar.h>.
[IS_IN(rtld)] (rtld_errno): New variable.
[IS_IN(rtld)] (__errno_location): New weak function.
[!IS_IN(rtld)]: Include "../../../csu/errno-loc.c".
* sysdeps/mach/hurd/errno.c: Remove file.
* sysdeps/mach/hurd/fork.c: Include <hurd/threadvar.h>
(__fork): Remove THREADVAR_SPACE macro and its use.
* sysdeps/mach/hurd/i386/init-first.c (__hurd_threadvar_max): Remove
variable.
(init): Do not initialize threadvar.
* sysdeps/mach/hurd/i386/libc.abilist (__hurd_threadvar_max): Remove
symbol.
* sysdeps/mach/hurd/i386/sigreturn.c (__sigreturn): Use
__hurd_local_reply_port instead of threadvar.
* sysdeps/mach/hurd/i386/tls.h (tcbhead_t): Add reply_port and
_hurd_sigstate fields.
(HURD_DESC_TLS, __LIBC_NO_TLS, THREAD_TCB): New macro.
* sysdeps/mach/hurd/i386/trampoline.c: Remove outdated comment.
* sysdeps/mach/hurd/libc-lock.h: Do not include <hurd/threadvar.h>.
(__libc_lock_owner_self): Use &__libc_lock_self0 and THREAD_SELF
instead of threadvar.
* sysdeps/mach/hurd/libc-tsd.h: Remove file.
* sysdeps/mach/hurd/mig-reply.c (GETPORT, reply_port): Remove macros.
(use_threadvar, global_reply_port): Remove variables.
(__hurd_reply_port0): New variable.
(__mig_get_reply_port): Use __hurd_local_reply_port and
__hurd_reply_port0 instead of threadvar.
(__mig_dealloc_reply_port): Likewise.
(__mig_init): Do not initialize threadvar.
* sysdeps/mach/hurd/profil.c: Fix comment.
2018-03-17 22:27:34 +00:00
|
|
|
#include <hurd/threadvar.h>
|
1995-02-18 01:27:10 +00:00
|
|
|
#include <setjmp.h>
|
2005-12-21 22:16:34 +00:00
|
|
|
#include <thread_state.h>
|
1995-02-18 01:27:10 +00:00
|
|
|
#include <sysdep.h> /* For stack growth direction. */
|
|
|
|
#include "set-hooks.h"
|
|
|
|
#include <assert.h>
|
|
|
|
#include "hurdmalloc.h" /* XXX */
|
2004-10-27 07:48:08 +00:00
|
|
|
#include <tls.h>
|
2016-04-14 07:17:02 +00:00
|
|
|
#include <malloc/malloc-internal.h>
|
2020-02-20 12:20:32 +00:00
|
|
|
#include <nss/nss_database.h>
|
2021-03-01 14:56:36 +00:00
|
|
|
#include <unwind-link.h>
|
2021-01-18 18:10:02 +00:00
|
|
|
#include <register-atfork.h>
|
1995-02-18 01:27:10 +00:00
|
|
|
|
Update.
* sysdeps/mach/hurd/chown.c: Use INTDEF for __chown.
* sysdeps/unix/sysv/aix/chown.c: Likewise.
* sysdeps/unix/grantpt.c: Use INTUSE for __chown calls.
* sysdeps/unix/sysv/linux/m68k/chown.c: Likewise.
* sysdeps/unix/sysv/linux/powerpc/chown.c: Likewise.
* sysdeps/unix/sysv/linux/i386/chown.c: Use INTDEF2 to define
__chown_internal.
* sysdeps/unix/sysv/linux/s390/s390-32//chown.c: Likewise.
* intl/dcngettext.c [_LIBC]: Use INTUSE for __dcngettext.
* intl/dngettext.c [_LIBC] (DCNGETTEXT): Use INTUSE.
* intl/ngettext.c: Likewise.
* include/sys/socket.h: Declare __connect_internal and define
__connect macro if not NOT_IN_libc.
* sysdeps/mach/hurd/connect.c: Use INTDEF for __connect.
* sysdeps/unix/sysv/aix/connect.c: Likewise.
* sysdeps/unix/sysv/linux/connect.S: Add __connect_internal alias.
* include/unistd.h: Declare __close_internal and define __close macro
if not NOT_IN_libc.
* libio/libioP.h (JUMO0, JUMP1, JUMP2, JUMP3, WJUMP0, WJUMP1, WJUMP2,
WJUMP3): Add extra parenthesis to avoid expanding element names with
macors like __close.
* sysdeps/unix/syscalls.list: Add __close_internal alias.
* include/unistd.h: Declare __dup2_internal and define __dup2 macro
if not NOT_IN_libc.
* sysdeps/mach/hurd/dup2.c: Use INTDEF for __dup2.
* sysdeps/posix/dup2.c: Use INTDEF for __dup2.
* sysdeps/unix/syscalls.list: Add __dup2_internal alias.
* include/unistd.h: Declare __fork_internal and define __fork macro
if not NOT_IN_libc.
* sysdeps/mach/hurd/fork.c: Use INTDEF for __fork.
* sysdeps/unix/sysv/aix/fork.c: Likewise.
* sysdeps/unix/sysv/linux/syscalls.list: Add __fork_internal alias.
* include/stdio_ext.h: Declare __fsetlocking_internal and define
__fsetlocking macro to use it if not NOT_IN_libc.
* libio/__fsetlocking.c: Use INTDEF for __fsetlocking.
* libio/__fbufsize.c: Correct copyright.
* libio/__flbf.c: Likewise.
* libio/__fpending.c: Likewise.
* libio/__fpurge.c: Likewise.
* libio/__freadable.c: Likewise.
* libio/__freading.c: Likewise.
* libio/__fsetlocking.c: Likewise.
* libio/__fwritable.c: Likewise.
* libio/__fwriting.c: Likewise.
* include/stdio.h: Declare __asprintf_internal and define __asprintf
macro to use it if not NOT_IN_libc.
* stdio-common/asprintf.c: Use INTDEF for __asprintf.
* include/mntent.h: Declare __setmntent_internal,
__getmntent_r_internal, __endmntent_internal. Define __setmntent,
__getmntent_r, and __endmntent macros to use these functions if not
NOT_IN_libc.
* misc/mntent_r.c: Use INTDEF for __setmntent, __getmntent_r, and
__endmntent.
* include/math.h: Declare __finite_internal, __finitef_internal, and
__finitel_internal. Redefine isfinite macro if in libc or libm using
these functions.
* sysdeps/generic/s_ldexp.c: Use INTUSE for __finite calls.
* sysdeps/generic/s_ldexpf.c: Use INTUSE for __finitef calls.
* sysdeps/generic/s_ldexpl.c: Use INTUSE for __finitel calls.
* sysdeps/i386/fpu/s_finite.S: Define _internal alias.
* sysdeps/i386/fpu/s_finitef.S: Likewise.
* sysdeps/i386/fpu/s_finitel.S: Likewise.
* sysdeps/ieee754/dbl-64/s_finite.c: Likewise.
* sysdeps/ieee754/flt-32/s_finitef.c: Likewise.
* sysdeps/ieee754/ldbl-128/s_finitel.c: Likewise.
* sysdeps/ieee754/ldbl-96/s_finitel.c: Likewise.
* include/fcntl.h: Declare __fcntl_internal. Define __fcntl macro
to use this function if not NOT_IN_libc.
* libio/iofdopen.c (_IO_fcntl): Use INTUSE.
* sysdeps/generic/fcntl.c: Use INTDEF for __fcntl.
* sysdeps/mach/hurd/fcntl.c: Likewise.
* sysdeps/unix/sysv/aix/fcntl.c: Likewise.
* sysdeps/unix/sysv/linux/i386/fcntl.c: Likewise.
* include/argz.h: Declare __argz_count_internal and
__argz_stringify_internal.
* intl/l10nflist.c [_LIBC]: Use INTUSE for __argz_count and
__argz_stringify.
* string/argz-count.c: Use INTDEF for __argz_count.
* string/argz-stringify.c: Use INTDEF for __argz_stringify.
* include/stdlib.h: Declare __cxa_atexit_internal and define
__cxa_atexit macro if not NOT_IN_libc.
* stdlib/cxa_atexit.c: Use INTDEF for __cxa_atexit.
* dlfcn/Makefile: Define NOT_IN_libc when compiling modcxaatexit.c.
* assert/assert.c: Use INTDEF for __assert_fail.
* assert/__assert.c: Use INTUSE for call to __assert_fail.
* include/assert.h: Declare __assert_fail_internal.
2002-04-09 20:36:24 +00:00
|
|
|
#undef __fork
|
|
|
|
|
1995-02-18 01:27:10 +00:00
|
|
|
|
|
|
|
/* Things that want to be locked while forking. */
|
1995-08-17 22:55:22 +00:00
|
|
|
symbol_set_declare (_hurd_fork_locks)
|
1995-02-18 01:27:10 +00:00
|
|
|
|
|
|
|
/* Things that want to be called before we fork, to prepare the parent for
|
|
|
|
task_create, when the new child task will inherit our address space. */
|
|
|
|
DEFINE_HOOK (_hurd_fork_prepare_hook, (void));
|
|
|
|
|
|
|
|
/* Things that want to be called when we are forking, with the above all
|
|
|
|
locked. They are passed the task port of the child. The child process
|
|
|
|
is all set up except for doing proc_child, and has no threads yet. */
|
|
|
|
DEFINE_HOOK (_hurd_fork_setup_hook, (void));
|
|
|
|
|
|
|
|
/* Things to be run in the child fork. */
|
|
|
|
DEFINE_HOOK (_hurd_fork_child_hook, (void));
|
|
|
|
|
|
|
|
/* Things to be run in the parent fork. */
|
|
|
|
DEFINE_HOOK (_hurd_fork_parent_hook, (void));
|
|
|
|
|
|
|
|
|
|
|
|
/* Clone the calling process, creating an exact copy.
|
|
|
|
Return -1 for errors, 0 to the new process,
|
|
|
|
and the process ID of the new process to the old process. */
|
|
|
|
pid_t
|
|
|
|
__fork (void)
|
|
|
|
{
|
|
|
|
jmp_buf env;
|
|
|
|
pid_t pid;
|
|
|
|
size_t i;
|
|
|
|
error_t err;
|
|
|
|
struct hurd_sigstate *volatile ss;
|
2020-02-20 12:20:32 +00:00
|
|
|
struct nss_database_data nss_database_data;
|
1995-02-18 01:27:10 +00:00
|
|
|
|
2021-01-18 18:10:02 +00:00
|
|
|
__run_fork_handlers (atfork_run_prepare, true);
|
2013-09-24 21:08:15 +00:00
|
|
|
|
1995-02-18 01:27:10 +00:00
|
|
|
ss = _hurd_self_sigstate ();
|
2021-03-23 22:38:54 +00:00
|
|
|
retry:
|
1996-05-04 09:46:57 +00:00
|
|
|
__spin_lock (&ss->critical_section_lock);
|
1995-04-24 09:00:07 +00:00
|
|
|
|
|
|
|
#undef LOSE
|
2001-06-14 05:32:26 +00:00
|
|
|
#define LOSE do { assert_perror (err); goto lose; } while (0) /* XXX */
|
1995-02-18 01:27:10 +00:00
|
|
|
|
|
|
|
if (! setjmp (env))
|
|
|
|
{
|
|
|
|
process_t newproc;
|
|
|
|
task_t newtask;
|
|
|
|
thread_t thread, sigthread;
|
|
|
|
mach_port_urefs_t thread_refs, sigthread_refs;
|
|
|
|
struct machine_thread_state state;
|
|
|
|
mach_msg_type_number_t statecount;
|
|
|
|
mach_port_t *portnames = NULL;
|
|
|
|
mach_msg_type_number_t nportnames = 0;
|
|
|
|
mach_port_type_t *porttypes = NULL;
|
|
|
|
mach_msg_type_number_t nporttypes = 0;
|
|
|
|
thread_t *threads = NULL;
|
|
|
|
mach_msg_type_number_t nthreads = 0;
|
1995-08-17 22:55:22 +00:00
|
|
|
int ports_locked = 0, stopped = 0;
|
|
|
|
|
|
|
|
void resume_threads (void)
|
|
|
|
{
|
|
|
|
if (! stopped)
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert (threads);
|
|
|
|
|
|
|
|
for (i = 0; i < nthreads; ++i)
|
|
|
|
if (threads[i] != ss->thread)
|
|
|
|
__thread_resume (threads[i]);
|
|
|
|
stopped = 0;
|
|
|
|
}
|
1995-02-18 01:27:10 +00:00
|
|
|
|
|
|
|
/* Run things that prepare for forking before we create the task. */
|
|
|
|
RUN_HOOK (_hurd_fork_prepare_hook, ());
|
|
|
|
|
2020-02-20 12:20:32 +00:00
|
|
|
call_function_static_weak (__nss_database_fork_prepare_parent,
|
|
|
|
&nss_database_data);
|
|
|
|
|
1995-02-18 01:27:10 +00:00
|
|
|
/* Lock things that want to be locked before we fork. */
|
1995-08-17 22:55:22 +00:00
|
|
|
{
|
|
|
|
void *const *p;
|
|
|
|
for (p = symbol_set_first_element (_hurd_fork_locks);
|
|
|
|
! symbol_set_end_p (_hurd_fork_locks, p);
|
|
|
|
++p)
|
|
|
|
__mutex_lock (*p);
|
|
|
|
}
|
1995-04-24 09:00:07 +00:00
|
|
|
__mutex_lock (&_hurd_siglock);
|
1995-12-22 05:57:24 +00:00
|
|
|
|
2016-09-21 23:45:48 +00:00
|
|
|
/* 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);
|
|
|
|
_hurd_malloc_fork_prepare ();
|
|
|
|
|
1995-02-18 01:27:10 +00:00
|
|
|
newtask = MACH_PORT_NULL;
|
|
|
|
thread = sigthread = MACH_PORT_NULL;
|
|
|
|
newproc = MACH_PORT_NULL;
|
|
|
|
|
|
|
|
/* Lock all the port cells for the standard ports while we copy the
|
|
|
|
address space. We want to insert all the send rights into the
|
|
|
|
child with the same names. */
|
|
|
|
for (i = 0; i < _hurd_nports; ++i)
|
|
|
|
__spin_lock (&_hurd_ports[i].lock);
|
|
|
|
ports_locked = 1;
|
|
|
|
|
1995-12-22 05:57:24 +00:00
|
|
|
|
2014-08-28 20:08:39 +00:00
|
|
|
/* Keep our SS locked while stopping other threads, so they don't get a
|
|
|
|
chance to have it locked in the copied space. */
|
|
|
|
__spin_lock (&ss->lock);
|
1995-08-17 22:55:22 +00:00
|
|
|
/* Stop all other threads while copying the address space,
|
|
|
|
so nothing changes. */
|
|
|
|
err = __proc_dostop (_hurd_ports[INIT_PORT_PROC].port, ss->thread);
|
2014-08-28 20:08:39 +00:00
|
|
|
__spin_unlock (&ss->lock);
|
1995-08-17 22:55:22 +00:00
|
|
|
if (!err)
|
1997-06-23 21:55:26 +00:00
|
|
|
{
|
|
|
|
stopped = 1;
|
1995-08-17 22:55:22 +00:00
|
|
|
|
1996-01-04 00:06:01 +00:00
|
|
|
#define XXX_KERNEL_PAGE_FAULT_BUG /* XXX work around page fault bug in mk */
|
|
|
|
|
|
|
|
#ifdef XXX_KERNEL_PAGE_FAULT_BUG
|
|
|
|
/* Gag me with a pitchfork.
|
|
|
|
The bug scenario is this:
|
|
|
|
|
|
|
|
- The page containing __mach_task_self_ is paged out.
|
|
|
|
- The signal thread was faulting on that page when we
|
|
|
|
suspended it via proc_dostop. It holds some lock, or set
|
|
|
|
some busy bit, or somesuch.
|
|
|
|
- Now this thread faults on that same page.
|
|
|
|
- GRATUIOUS DEADLOCK
|
|
|
|
|
|
|
|
We can break the deadlock by aborting the thread that faulted
|
|
|
|
first, which if the bug happened was the signal thread because
|
|
|
|
it is the only other thread and we just suspended it.
|
|
|
|
*/
|
|
|
|
__thread_abort (_hurd_msgport_thread);
|
|
|
|
#endif
|
1995-08-17 22:55:22 +00:00
|
|
|
/* Create the child task. It will inherit a copy of our memory. */
|
2002-01-02 10:23:33 +00:00
|
|
|
err = __task_create (__mach_task_self (),
|
|
|
|
#ifdef KERN_INVALID_LEDGER
|
|
|
|
NULL, 0, /* OSF Mach */
|
|
|
|
#endif
|
|
|
|
1, &newtask);
|
1997-06-23 21:55:26 +00:00
|
|
|
}
|
1995-04-24 09:00:07 +00:00
|
|
|
|
|
|
|
/* Unlock the global signal state lock, so we do not
|
|
|
|
block the signal thread any longer than necessary. */
|
|
|
|
__mutex_unlock (&_hurd_siglock);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
|
|
|
|
/* Fetch the names of all ports used in this task. */
|
|
|
|
if (err = __mach_port_names (__mach_task_self (),
|
|
|
|
&portnames, &nportnames,
|
|
|
|
&porttypes, &nporttypes))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
if (nportnames != nporttypes)
|
|
|
|
{
|
|
|
|
err = EGRATUITOUS;
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get send rights for all the threads in this task.
|
|
|
|
We want to avoid giving these rights to the child. */
|
|
|
|
if (err = __task_threads (__mach_task_self (), &threads, &nthreads))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
|
|
|
|
/* Get the child process's proc server port. We will insert it into
|
|
|
|
the child with the same name as we use for our own proc server
|
|
|
|
port; and we will need it to set the child's message port. */
|
|
|
|
if (err = __proc_task2proc (_hurd_ports[INIT_PORT_PROC].port,
|
|
|
|
newtask, &newproc))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
|
|
|
|
/* Insert all our port rights into the child task. */
|
|
|
|
thread_refs = sigthread_refs = 0;
|
|
|
|
for (i = 0; i < nportnames; ++i)
|
|
|
|
{
|
|
|
|
if (porttypes[i] & MACH_PORT_TYPE_RECEIVE)
|
|
|
|
{
|
|
|
|
/* This is a receive right. We want to give the child task
|
|
|
|
its own new receive right under the same name. */
|
2018-01-27 20:40:08 +00:00
|
|
|
if (err = __mach_port_allocate_name (newtask,
|
|
|
|
MACH_PORT_RIGHT_RECEIVE,
|
|
|
|
portnames[i]))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
if (porttypes[i] & MACH_PORT_TYPE_SEND)
|
|
|
|
{
|
|
|
|
/* Give the child as many send rights for its receive
|
|
|
|
right as we have for ours. */
|
|
|
|
mach_port_urefs_t refs;
|
|
|
|
mach_port_t port;
|
|
|
|
mach_msg_type_name_t poly;
|
|
|
|
if (err = __mach_port_get_refs (__mach_task_self (),
|
|
|
|
portnames[i],
|
|
|
|
MACH_PORT_RIGHT_SEND,
|
|
|
|
&refs))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
if (err = __mach_port_extract_right (newtask,
|
|
|
|
portnames[i],
|
|
|
|
MACH_MSG_TYPE_MAKE_SEND,
|
|
|
|
&port, &poly))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
if (portnames[i] == _hurd_msgport)
|
|
|
|
{
|
|
|
|
/* We just created a receive right for the child's
|
|
|
|
message port and are about to insert send rights
|
|
|
|
for it. Now, while we happen to have a send right
|
|
|
|
for it, give it to the proc server. */
|
|
|
|
mach_port_t old;
|
|
|
|
if (err = __proc_setmsgport (newproc, port, &old))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
if (old != MACH_PORT_NULL)
|
|
|
|
/* XXX what to do here? */
|
|
|
|
__mach_port_deallocate (__mach_task_self (), old);
|
1996-02-08 02:10:15 +00:00
|
|
|
/* The new task will receive its own exceptions
|
|
|
|
on its message port. */
|
2002-01-02 10:23:33 +00:00
|
|
|
if (err =
|
|
|
|
#ifdef TASK_EXCEPTION_PORT
|
|
|
|
__task_set_special_port (newtask,
|
|
|
|
TASK_EXCEPTION_PORT,
|
|
|
|
port)
|
|
|
|
#elif defined (EXC_MASK_ALL)
|
|
|
|
__task_set_exception_ports
|
|
|
|
(newtask, EXC_MASK_ALL & ~(EXC_MASK_SYSCALL
|
|
|
|
| EXC_MASK_MACH_SYSCALL
|
|
|
|
| EXC_MASK_RPC_ALERT),
|
|
|
|
port, EXCEPTION_DEFAULT, MACHINE_THREAD_STATE)
|
|
|
|
#else
|
|
|
|
# error task_set_exception_port?
|
|
|
|
#endif
|
|
|
|
)
|
1996-02-08 02:10:15 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
}
|
|
|
|
if (err = __mach_port_insert_right (newtask,
|
|
|
|
portnames[i],
|
|
|
|
port,
|
|
|
|
MACH_MSG_TYPE_MOVE_SEND))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
2019-02-26 15:01:50 +00:00
|
|
|
if (refs > 1
|
|
|
|
&& (err = __mach_port_mod_refs (newtask,
|
|
|
|
portnames[i],
|
|
|
|
MACH_PORT_RIGHT_SEND,
|
|
|
|
refs - 1)))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
}
|
|
|
|
if (porttypes[i] & MACH_PORT_TYPE_SEND_ONCE)
|
|
|
|
{
|
|
|
|
/* Give the child a send-once right for its receive right,
|
|
|
|
since we have one for ours. */
|
|
|
|
mach_port_t port;
|
|
|
|
mach_msg_type_name_t poly;
|
|
|
|
if (err = __mach_port_extract_right
|
|
|
|
(newtask,
|
|
|
|
portnames[i],
|
|
|
|
MACH_MSG_TYPE_MAKE_SEND_ONCE,
|
|
|
|
&port, &poly))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
if (err = __mach_port_insert_right
|
|
|
|
(newtask,
|
|
|
|
portnames[i], port,
|
|
|
|
MACH_MSG_TYPE_MOVE_SEND_ONCE))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-26 15:01:50 +00:00
|
|
|
else if (porttypes[i]
|
|
|
|
& (MACH_PORT_TYPE_SEND|MACH_PORT_TYPE_DEAD_NAME))
|
1995-02-18 01:27:10 +00:00
|
|
|
{
|
|
|
|
/* This is a send right or a dead name.
|
|
|
|
Give the child as many references for it as we have. */
|
2011-09-08 16:37:42 +00:00
|
|
|
mach_port_urefs_t refs = 0, *record_refs = NULL;
|
1995-02-18 01:27:10 +00:00
|
|
|
mach_port_t insert;
|
1996-04-07 17:44:31 +00:00
|
|
|
mach_msg_type_name_t insert_type = MACH_MSG_TYPE_COPY_SEND;
|
2001-04-09 21:24:34 +00:00
|
|
|
if (portnames[i] == newtask || portnames[i] == newproc)
|
|
|
|
/* Skip the name we use for the child's task or proc ports. */
|
1995-02-18 01:27:10 +00:00
|
|
|
continue;
|
|
|
|
if (portnames[i] == __mach_task_self ())
|
|
|
|
/* For the name we use for our own task port,
|
|
|
|
insert the child's task port instead. */
|
|
|
|
insert = newtask;
|
|
|
|
else if (portnames[i] == _hurd_ports[INIT_PORT_PROC].port)
|
|
|
|
{
|
2001-04-09 21:24:34 +00:00
|
|
|
/* Use the proc server port for the new task. */
|
|
|
|
insert = newproc;
|
|
|
|
insert_type = MACH_MSG_TYPE_COPY_SEND;
|
1995-02-18 01:27:10 +00:00
|
|
|
}
|
1995-08-17 22:55:22 +00:00
|
|
|
else if (portnames[i] == ss->thread)
|
1995-02-18 01:27:10 +00:00
|
|
|
{
|
|
|
|
/* For the name we use for our own thread port, we will
|
|
|
|
insert the thread port for the child main user thread
|
|
|
|
after we create it. */
|
|
|
|
insert = MACH_PORT_NULL;
|
|
|
|
record_refs = &thread_refs;
|
|
|
|
/* Allocate a dead name right for this name as a
|
1997-06-23 21:55:26 +00:00
|
|
|
placeholder, so the kernel will not chose this name
|
|
|
|
for any other new port (it might use it for one of the
|
|
|
|
rights created when a thread is created). */
|
1995-02-18 01:27:10 +00:00
|
|
|
if (err = __mach_port_allocate_name
|
|
|
|
(newtask, MACH_PORT_RIGHT_DEAD_NAME, portnames[i]))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
}
|
|
|
|
else if (portnames[i] == _hurd_msgport_thread)
|
|
|
|
/* For the name we use for our signal thread's thread port,
|
|
|
|
we will insert the thread port for the child's signal
|
|
|
|
thread after we create it. */
|
|
|
|
{
|
|
|
|
insert = MACH_PORT_NULL;
|
|
|
|
record_refs = &sigthread_refs;
|
|
|
|
/* Allocate a dead name right as a placeholder. */
|
|
|
|
if (err = __mach_port_allocate_name
|
|
|
|
(newtask, MACH_PORT_RIGHT_DEAD_NAME, portnames[i]))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Skip the name we use for any of our own thread ports. */
|
|
|
|
mach_msg_type_number_t j;
|
|
|
|
for (j = 0; j < nthreads; ++j)
|
|
|
|
if (portnames[i] == threads[j])
|
|
|
|
break;
|
|
|
|
if (j < nthreads)
|
|
|
|
continue;
|
|
|
|
|
1996-04-07 17:44:31 +00:00
|
|
|
/* Copy our own send right. */
|
1995-02-18 01:27:10 +00:00
|
|
|
insert = portnames[i];
|
|
|
|
}
|
|
|
|
/* Find out how many user references we have for
|
|
|
|
the send right with this name. */
|
|
|
|
if (err = __mach_port_get_refs (__mach_task_self (),
|
|
|
|
portnames[i],
|
|
|
|
MACH_PORT_RIGHT_SEND,
|
|
|
|
record_refs ?: &refs))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
if (insert == MACH_PORT_NULL)
|
|
|
|
continue;
|
2019-02-26 15:01:50 +00:00
|
|
|
if (insert == portnames[i]
|
|
|
|
&& (porttypes[i] & MACH_PORT_TYPE_DEAD_NAME))
|
1995-04-24 09:00:07 +00:00
|
|
|
/* This is a dead name; allocate another dead name
|
|
|
|
with the same name in the child. */
|
|
|
|
allocate_dead_name:
|
|
|
|
err = __mach_port_allocate_name (newtask,
|
|
|
|
MACH_PORT_RIGHT_DEAD_NAME,
|
|
|
|
portnames[i]);
|
|
|
|
else
|
|
|
|
/* Insert the chosen send right into the child. */
|
|
|
|
err = __mach_port_insert_right (newtask,
|
|
|
|
portnames[i],
|
1999-06-13 09:53:59 +00:00
|
|
|
insert, insert_type);
|
1995-04-24 09:00:07 +00:00
|
|
|
switch (err)
|
1995-02-18 01:27:10 +00:00
|
|
|
{
|
1995-04-24 09:00:07 +00:00
|
|
|
case KERN_NAME_EXISTS:
|
|
|
|
{
|
|
|
|
/* It already has a send right under this name (?!).
|
|
|
|
Well, it starts out with a send right for its task
|
|
|
|
port, and inherits the bootstrap and exception ports
|
|
|
|
from us. */
|
|
|
|
mach_port_t childport;
|
|
|
|
mach_msg_type_name_t poly;
|
|
|
|
assert (__mach_port_extract_right (newtask, portnames[i],
|
|
|
|
MACH_MSG_TYPE_COPY_SEND,
|
|
|
|
&childport,
|
2019-02-26 15:01:50 +00:00
|
|
|
&poly) == 0
|
|
|
|
&& childport == insert
|
|
|
|
&& __mach_port_deallocate (__mach_task_self (),
|
|
|
|
childport) == 0);
|
1995-04-24 09:00:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case KERN_INVALID_CAPABILITY:
|
|
|
|
/* The port just died. It was a send right,
|
|
|
|
and now it's a dead name. */
|
|
|
|
goto allocate_dead_name;
|
|
|
|
|
|
|
|
default:
|
|
|
|
LOSE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KERN_SUCCESS:
|
|
|
|
/* Give the child as many user references as we have. */
|
2019-02-26 15:01:50 +00:00
|
|
|
if (refs > 1
|
|
|
|
&& (err = __mach_port_mod_refs (newtask,
|
|
|
|
portnames[i],
|
|
|
|
MACH_PORT_RIGHT_SEND,
|
|
|
|
refs - 1)))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlock the standard port cells. The child must unlock its own
|
|
|
|
copies too. */
|
|
|
|
for (i = 0; i < _hurd_nports; ++i)
|
|
|
|
__spin_unlock (&_hurd_ports[i].lock);
|
|
|
|
ports_locked = 0;
|
|
|
|
|
1995-08-17 22:55:22 +00:00
|
|
|
/* All state has now been copied from the parent. It is safe to
|
1997-06-23 21:55:26 +00:00
|
|
|
resume other parent threads. */
|
1995-08-17 22:55:22 +00:00
|
|
|
resume_threads ();
|
|
|
|
|
1995-02-18 01:27:10 +00:00
|
|
|
/* Create the child main user thread and signal thread. */
|
2019-02-26 15:01:50 +00:00
|
|
|
if ((err = __thread_create (newtask, &thread))
|
|
|
|
|| (err = __thread_create (newtask, &sigthread)))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
|
|
|
|
/* Insert send rights for those threads. We previously allocated
|
1997-06-23 21:55:26 +00:00
|
|
|
dead name rights with the names we want to give the thread ports
|
|
|
|
in the child as placeholders. Now deallocate them so we can use
|
|
|
|
the names. */
|
2019-02-26 15:01:50 +00:00
|
|
|
if ((err = __mach_port_deallocate (newtask, ss->thread))
|
|
|
|
|| (err = __mach_port_insert_right (newtask, ss->thread,
|
|
|
|
thread,
|
|
|
|
MACH_MSG_TYPE_COPY_SEND)))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
hurd: Global signal disposition
This adds _hurd_sigstate_set_global_rcv used by libpthread to enable
POSIX-confirming behavior of signals on a per-thread basis.
This also provides a sigstate destructor _hurd_sigstate_delete, and a
global process signal state, which needs to be locked and check when
global disposition is enabled, thus the addition of _hurd_sigstate_lock
_hurd_sigstate_actions _hurd_sigstate_pending _hurd_sigstate_unlock helpers.
This also updates all the glibc code accordingly.
This also drops support for get_int(INIT_SIGMASK), which did not make sense
any more since we do not have a single signal thread any more.
During fork/spawn, this also reinitializes the child global sigstate's
lock. That cures an issue that would very rarely cause a deadlock in the
child in fork, tries to unlock ss' critical section lock at the end of
fork. This will typically (always?) be observed in /bin/sh, which is not
surprising as that is the foremost caller of fork.
To reproduce an intermediate state, add an endless loop if
_hurd_global_sigstate is locked after __proc_dostop (cast through
volatile); that is, while still being in the fork's parent process.
When that triggers (use the libtool testsuite), the signal thread has
already locked ss (which is _hurd_global_sigstate), and is stuck at
hurdsig.c:685 in post_signal, trying to lock _hurd_siglock (which the
main thread already has locked and keeps locked until after
__task_create). This is the case that ss->thread == MACH_PORT_NULL, that
is, a global signal. In the main thread, between __proc_dostop and
__task_create is the __thread_abort call on the signal thread which would
abort any current kernel operation (but leave ss locked). Later in fork,
in the parent, when _hurd_siglock is unlocked in fork, the parent's
signal thread can proceed and will unlock eventually the global sigstate.
In the client, _hurd_siglock will likewise be unlocked, but the global
sigstate never will be, as the client's signal thread has been configured
to restart execution from _hurd_msgport_receive. Thus, when the child
tries to unlock ss' critical section lock at the end of fork, it will
first lock the global sigstate, will spin trying to lock it, which can
never be successful, and we get our deadlock.
Options seem to be:
* Move the locking of _hurd_siglock earlier in post_signal -- but that
may generally impact performance, if this locking isn't generally
needed anyway?
On the other hand, would it actually make sense to wait here until we
are not any longer in a critical section (which is meant to disable
signal delivery anyway (but not for preempted signals?))?
* Clear the global sigstate in the fork's child with the rationale that
we're anyway restarting the signal thread from a clean state. This
has now been implemented.
Why has this problem not been observed before Jérémie's patches? (Or has
it? Perhaps even more rarely?) In _S_msg_sig_post, the signal is now
posted to a *global receiver thread*, whereas previously it was posted to
the *designated signal-receiving thread*. The latter one was in a
critical section in fork, so didn't try to handle the signal until after
leaving the critical section? (Not completely analyzed and verified.)
Another question is what the signal is that is being received
during/around the time __proc_dostop executes.
2019-12-29 16:59:55 +00:00
|
|
|
/* XXX consumed? (_hurd_sigthread is no more) */
|
2019-02-26 15:01:50 +00:00
|
|
|
if (thread_refs > 1
|
|
|
|
&& (err = __mach_port_mod_refs (newtask, ss->thread,
|
|
|
|
MACH_PORT_RIGHT_SEND,
|
|
|
|
thread_refs - 1)))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
if ((_hurd_msgport_thread != MACH_PORT_NULL) /* Let user have none. */
|
2019-02-26 15:01:50 +00:00
|
|
|
&& ((err = __mach_port_deallocate (newtask, _hurd_msgport_thread))
|
|
|
|
|| (err = __mach_port_insert_right (newtask,
|
|
|
|
_hurd_msgport_thread,
|
|
|
|
sigthread,
|
|
|
|
MACH_MSG_TYPE_COPY_SEND))))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
2019-02-26 15:01:50 +00:00
|
|
|
if (sigthread_refs > 1
|
|
|
|
&& (err = __mach_port_mod_refs (newtask, _hurd_msgport_thread,
|
|
|
|
MACH_PORT_RIGHT_SEND,
|
|
|
|
sigthread_refs - 1)))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
|
|
|
|
/* This seems like a convenient juncture to copy the proc server's
|
|
|
|
idea of what addresses our argv and envp are found at from the
|
|
|
|
parent into the child. Since we happen to know that the child
|
|
|
|
shares our memory image, it is we who should do this copying. */
|
|
|
|
{
|
|
|
|
vm_address_t argv, envp;
|
|
|
|
err = (__USEPORT (PROC, __proc_get_arg_locations (port, &argv, &envp))
|
|
|
|
?: __proc_set_arg_locations (newproc, argv, envp));
|
|
|
|
if (err)
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
}
|
1995-12-22 05:57:24 +00:00
|
|
|
|
1995-02-18 01:27:10 +00:00
|
|
|
/* Set the child signal thread up to run the msgport server function
|
|
|
|
using the same signal thread stack copied from our address space.
|
|
|
|
We fetch the state before longjmp'ing it so that miscellaneous
|
|
|
|
registers not affected by longjmp (such as i386 segment registers)
|
|
|
|
are in their normal default state. */
|
|
|
|
statecount = MACHINE_THREAD_STATE_COUNT;
|
|
|
|
if (err = __thread_get_state (_hurd_msgport_thread,
|
|
|
|
MACHINE_THREAD_STATE_FLAVOR,
|
|
|
|
(natural_t *) &state, &statecount))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
2015-03-21 03:49:44 +00:00
|
|
|
#ifdef STACK_GROWTH_UP
|
1999-11-18 Roland McGrath <roland@baalperazim.frob.com>
* hurd/hurdsig.c (_hurdsig_init): If __hurd_threadvar_stack_mask is
nonzero, use cthread_fork to create the signal thread.
* hurd/msgportdemux.c (_hurd_msgport_receive): Initialize
_hurd_msgport_thread here (to self).
* sysdeps/mach/hurd/fork.c (__fork): When __hurd_sigthread_stack_end
is zero, instead compute child signal thread's starting SP from parent
signal thread's current SP and the threadvar_stack variables.
* hurd/Versions (GLIBC_2.1.3): Add cthread_fork, cthread_detach.
These are now referenced weakly by _hurdsig_init.
* hurd/report-wait.c (_S_msg_report_wait): Fix typo:
&_hurd_itimer_thread not &_hurd_msgport_thread.
1999-10-01 Roland McGrath <roland@baalperazim.frob.com>
* hurd/hurdfchdir.c (_hurd_change_directory_port_from_fd): Rewrite
without HURD_DPORT_USE to clean up warnings.
* hurd/dtable.c (get_dtable_port): Likewise.
* hurd/hurdioctl.c (rectty_dtable): Renamed to install_ctty.
(install_ctty): Do the changing of the cttyid port cell here, inside
the critical section while we holding the dtable lock.
(_hurd_setcttyid, tiocsctty, tiocnotty): Use that instead of changing
the port cell and calling rectty_dtable.
(_hurd_locked_install_cttyid): New function, split out of install_ctty.
(install_ctty): Use it inside a critical section, with the lock held.
* sysdeps/mach/hurd/setsid.c (__setsid): Use
_hurd_locked_install_cttyid to effect the cttyid and dtable changes
after proc_setsid, having held the dtable lock throughout.
* hurd/dtable.c (ctty_new_pgrp): With the dtable lock held, check the
cttyid port for null and bail out early if so. The dtable lock
serializes us after any cttyid change and its associated dtable update.
1999-12-03 05:01:23 +00:00
|
|
|
if (__hurd_sigthread_stack_base == 0)
|
|
|
|
{
|
|
|
|
state.SP &= __hurd_threadvar_stack_mask;
|
hurd: Replace threadvars with TLS
This gets rid of a lot of kludge and gets closer to other ports.
* hurd/Makefile (headers): Remove threadvar.h.
(inline-headers): Remove threadvar.h.
* hurd/Versions (GLIBC_2.0: Remove __hurd_sigthread_stack_base,
__hurd_sigthread_stack_end, __hurd_sigthread_variables,
__hurd_threadvar_max, __hurd_errno_location.
(HURD_CTHREADS_0.3): Add pthread_getattr_np, pthread_attr_getstack.
* hurd/hurd/signal.h: Do not include <hurd/threadvar.h>.
(_hurd_self_sigstate): Use THREAD_SELF to get _hurd_sigstate.
(_HURD_SIGNAL_H_EXTERN_INLINE): Use THREAD_SELF to get _hurd_sigstate,
unless TLS is not initialized yet, in which case we do not need a
critical section yet anyway.
* hurd/hurd/threadvar.h: Include <tls.h>, do not include
<machine-sp.h>.
(__hurd_sigthread_variables, __hurd_threadvar_max): Remove variables
declarations.
(__hurd_threadvar_index): Remove enum.
(_HURD_THREADVAR_H_EXTERN_INLINE): Remove macro.
(__hurd_threadvar_location_from_sp,__hurd_threadvar_location): Remove
inlines.
(__hurd_reply_port0): New variable declaration.
(__hurd_local_reply_port): New macro.
* hurd/hurdsig.c (__hurd_sigthread_variables): Remove variable.
(interrupted_reply_port_location): Add thread_t parameter. Use it
with THREAD_TCB to access thread-local variables.
(_hurdsig_abort_rpcs): Pass ss->thread to
interrupted_reply_port_location.
(_hurd_internal_post_signal): Likewise.
(_hurdsig_init): Use presence of cthread_fork instead of
__hurd_threadvar_stack_mask to start signal thread by hand.
Remove signal thread threadvar initialization.
* hurd/hurdstartup.c: Do not include <hurd/threadvar.h>
* hurd/sigunwind.c: Include <hurd/threadvar.h>
(_hurdsig_longjmp_from_handler): Use __hurd_local_reply_port instead
of threadvar.
* sysdeps/mach/hurd/Versions (libc.GLIBC_PRIVATE): Add
__libc_lock_self0.
(ld.GLIBC_2.0): Remove __hurd_sigthread_stack_base,
__hurd_sigthread_stack_end, __hurd_sigthread_variables.
(ld.GLIBC_PRIVATE): Add __libc_lock_self0.
* sysdeps/mach/hurd/cthreads.c: Add __libc_lock_self0.
* sysdeps/mach/hurd/dl-sysdep.c (errno, __hurd_sigthread_stack_base,
__hurd_sigthread_stack_end, __hurd_sigthread_variables, threadvars,
__hurd_threadvar_stack_offset, __hurd_threadvar_stack_mask): Do not
define variables.
* sysdeps/mach/hurd/errno-loc.c: Do not include <errno.h> and
<hurd/threadvar.h>.
[IS_IN(rtld)] (rtld_errno): New variable.
[IS_IN(rtld)] (__errno_location): New weak function.
[!IS_IN(rtld)]: Include "../../../csu/errno-loc.c".
* sysdeps/mach/hurd/errno.c: Remove file.
* sysdeps/mach/hurd/fork.c: Include <hurd/threadvar.h>
(__fork): Remove THREADVAR_SPACE macro and its use.
* sysdeps/mach/hurd/i386/init-first.c (__hurd_threadvar_max): Remove
variable.
(init): Do not initialize threadvar.
* sysdeps/mach/hurd/i386/libc.abilist (__hurd_threadvar_max): Remove
symbol.
* sysdeps/mach/hurd/i386/sigreturn.c (__sigreturn): Use
__hurd_local_reply_port instead of threadvar.
* sysdeps/mach/hurd/i386/tls.h (tcbhead_t): Add reply_port and
_hurd_sigstate fields.
(HURD_DESC_TLS, __LIBC_NO_TLS, THREAD_TCB): New macro.
* sysdeps/mach/hurd/i386/trampoline.c: Remove outdated comment.
* sysdeps/mach/hurd/libc-lock.h: Do not include <hurd/threadvar.h>.
(__libc_lock_owner_self): Use &__libc_lock_self0 and THREAD_SELF
instead of threadvar.
* sysdeps/mach/hurd/libc-tsd.h: Remove file.
* sysdeps/mach/hurd/mig-reply.c (GETPORT, reply_port): Remove macros.
(use_threadvar, global_reply_port): Remove variables.
(__hurd_reply_port0): New variable.
(__mig_get_reply_port): Use __hurd_local_reply_port and
__hurd_reply_port0 instead of threadvar.
(__mig_dealloc_reply_port): Likewise.
(__mig_init): Do not initialize threadvar.
* sysdeps/mach/hurd/profil.c: Fix comment.
2018-03-17 22:27:34 +00:00
|
|
|
state.SP += __hurd_threadvar_stack_offset;
|
1999-11-18 Roland McGrath <roland@baalperazim.frob.com>
* hurd/hurdsig.c (_hurdsig_init): If __hurd_threadvar_stack_mask is
nonzero, use cthread_fork to create the signal thread.
* hurd/msgportdemux.c (_hurd_msgport_receive): Initialize
_hurd_msgport_thread here (to self).
* sysdeps/mach/hurd/fork.c (__fork): When __hurd_sigthread_stack_end
is zero, instead compute child signal thread's starting SP from parent
signal thread's current SP and the threadvar_stack variables.
* hurd/Versions (GLIBC_2.1.3): Add cthread_fork, cthread_detach.
These are now referenced weakly by _hurdsig_init.
* hurd/report-wait.c (_S_msg_report_wait): Fix typo:
&_hurd_itimer_thread not &_hurd_msgport_thread.
1999-10-01 Roland McGrath <roland@baalperazim.frob.com>
* hurd/hurdfchdir.c (_hurd_change_directory_port_from_fd): Rewrite
without HURD_DPORT_USE to clean up warnings.
* hurd/dtable.c (get_dtable_port): Likewise.
* hurd/hurdioctl.c (rectty_dtable): Renamed to install_ctty.
(install_ctty): Do the changing of the cttyid port cell here, inside
the critical section while we holding the dtable lock.
(_hurd_setcttyid, tiocsctty, tiocnotty): Use that instead of changing
the port cell and calling rectty_dtable.
(_hurd_locked_install_cttyid): New function, split out of install_ctty.
(install_ctty): Use it inside a critical section, with the lock held.
* sysdeps/mach/hurd/setsid.c (__setsid): Use
_hurd_locked_install_cttyid to effect the cttyid and dtable changes
after proc_setsid, having held the dtable lock throughout.
* hurd/dtable.c (ctty_new_pgrp): With the dtable lock held, check the
cttyid port for null and bail out early if so. The dtable lock
serializes us after any cttyid change and its associated dtable update.
1999-12-03 05:01:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
state.SP = __hurd_sigthread_stack_base;
|
1995-02-18 01:27:10 +00:00
|
|
|
#else
|
1999-11-18 Roland McGrath <roland@baalperazim.frob.com>
* hurd/hurdsig.c (_hurdsig_init): If __hurd_threadvar_stack_mask is
nonzero, use cthread_fork to create the signal thread.
* hurd/msgportdemux.c (_hurd_msgport_receive): Initialize
_hurd_msgport_thread here (to self).
* sysdeps/mach/hurd/fork.c (__fork): When __hurd_sigthread_stack_end
is zero, instead compute child signal thread's starting SP from parent
signal thread's current SP and the threadvar_stack variables.
* hurd/Versions (GLIBC_2.1.3): Add cthread_fork, cthread_detach.
These are now referenced weakly by _hurdsig_init.
* hurd/report-wait.c (_S_msg_report_wait): Fix typo:
&_hurd_itimer_thread not &_hurd_msgport_thread.
1999-10-01 Roland McGrath <roland@baalperazim.frob.com>
* hurd/hurdfchdir.c (_hurd_change_directory_port_from_fd): Rewrite
without HURD_DPORT_USE to clean up warnings.
* hurd/dtable.c (get_dtable_port): Likewise.
* hurd/hurdioctl.c (rectty_dtable): Renamed to install_ctty.
(install_ctty): Do the changing of the cttyid port cell here, inside
the critical section while we holding the dtable lock.
(_hurd_setcttyid, tiocsctty, tiocnotty): Use that instead of changing
the port cell and calling rectty_dtable.
(_hurd_locked_install_cttyid): New function, split out of install_ctty.
(install_ctty): Use it inside a critical section, with the lock held.
* sysdeps/mach/hurd/setsid.c (__setsid): Use
_hurd_locked_install_cttyid to effect the cttyid and dtable changes
after proc_setsid, having held the dtable lock throughout.
* hurd/dtable.c (ctty_new_pgrp): With the dtable lock held, check the
cttyid port for null and bail out early if so. The dtable lock
serializes us after any cttyid change and its associated dtable update.
1999-12-03 05:01:23 +00:00
|
|
|
if (__hurd_sigthread_stack_end == 0)
|
|
|
|
{
|
2020-11-14 23:35:35 +00:00
|
|
|
/* The signal thread has a stack assigned by pthread.
|
1999-11-18 Roland McGrath <roland@baalperazim.frob.com>
* hurd/hurdsig.c (_hurdsig_init): If __hurd_threadvar_stack_mask is
nonzero, use cthread_fork to create the signal thread.
* hurd/msgportdemux.c (_hurd_msgport_receive): Initialize
_hurd_msgport_thread here (to self).
* sysdeps/mach/hurd/fork.c (__fork): When __hurd_sigthread_stack_end
is zero, instead compute child signal thread's starting SP from parent
signal thread's current SP and the threadvar_stack variables.
* hurd/Versions (GLIBC_2.1.3): Add cthread_fork, cthread_detach.
These are now referenced weakly by _hurdsig_init.
* hurd/report-wait.c (_S_msg_report_wait): Fix typo:
&_hurd_itimer_thread not &_hurd_msgport_thread.
1999-10-01 Roland McGrath <roland@baalperazim.frob.com>
* hurd/hurdfchdir.c (_hurd_change_directory_port_from_fd): Rewrite
without HURD_DPORT_USE to clean up warnings.
* hurd/dtable.c (get_dtable_port): Likewise.
* hurd/hurdioctl.c (rectty_dtable): Renamed to install_ctty.
(install_ctty): Do the changing of the cttyid port cell here, inside
the critical section while we holding the dtable lock.
(_hurd_setcttyid, tiocsctty, tiocnotty): Use that instead of changing
the port cell and calling rectty_dtable.
(_hurd_locked_install_cttyid): New function, split out of install_ctty.
(install_ctty): Use it inside a critical section, with the lock held.
* sysdeps/mach/hurd/setsid.c (__setsid): Use
_hurd_locked_install_cttyid to effect the cttyid and dtable changes
after proc_setsid, having held the dtable lock throughout.
* hurd/dtable.c (ctty_new_pgrp): With the dtable lock held, check the
cttyid port for null and bail out early if so. The dtable lock
serializes us after any cttyid change and its associated dtable update.
1999-12-03 05:01:23 +00:00
|
|
|
The threadvar_stack variables conveniently tell us how
|
|
|
|
to get to the highest address in the stack, just below
|
|
|
|
the per-thread variables. */
|
|
|
|
state.SP &= __hurd_threadvar_stack_mask;
|
|
|
|
state.SP += __hurd_threadvar_stack_offset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
state.SP = __hurd_sigthread_stack_end;
|
1995-12-22 05:57:24 +00:00
|
|
|
#endif
|
1995-02-18 01:27:10 +00:00
|
|
|
MACHINE_THREAD_STATE_SET_PC (&state,
|
|
|
|
(unsigned long int) _hurd_msgport_receive);
|
2018-03-17 02:17:36 +00:00
|
|
|
|
|
|
|
/* Do special signal thread setup for TLS if needed. */
|
|
|
|
if (err = _hurd_tls_fork (sigthread, _hurd_msgport_thread, &state))
|
|
|
|
LOSE;
|
|
|
|
|
1995-02-18 01:27:10 +00:00
|
|
|
if (err = __thread_set_state (sigthread, MACHINE_THREAD_STATE_FLAVOR,
|
|
|
|
(natural_t *) &state, statecount))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
/* We do not thread_resume SIGTHREAD here because the child
|
|
|
|
fork needs to do more setup before it can take signals. */
|
|
|
|
|
|
|
|
/* Set the child user thread up to return 1 from the setjmp above. */
|
|
|
|
_hurd_longjmp_thread_state (&state, env, 1);
|
2004-10-27 07:48:08 +00:00
|
|
|
|
|
|
|
/* Do special thread setup for TLS if needed. */
|
2018-03-17 02:17:36 +00:00
|
|
|
if (err = _hurd_tls_fork (thread, ss->thread, &state))
|
2004-10-27 07:48:08 +00:00
|
|
|
LOSE;
|
|
|
|
|
1995-02-18 01:27:10 +00:00
|
|
|
if (err = __thread_set_state (thread, MACHINE_THREAD_STATE_FLAVOR,
|
|
|
|
(natural_t *) &state, statecount))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
|
|
|
|
/* Get the PID of the child from the proc server. We must do this
|
|
|
|
before calling proc_child below, because at that point any
|
|
|
|
authorized POSIX.1 process may kill the child task with SIGKILL. */
|
|
|
|
if (err = __USEPORT (PROC, __proc_task2pid (port, newtask, &pid)))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
|
|
|
|
/* Register the child with the proc server. It is important that
|
|
|
|
this be that last thing we do before starting the child thread
|
|
|
|
running. Once proc_child has been done for the task, it appears
|
|
|
|
as a POSIX.1 process. Any errors we get must be detected before
|
|
|
|
this point, and the child must have a message port so it responds
|
|
|
|
to POSIX.1 signals. */
|
|
|
|
if (err = __USEPORT (PROC, __proc_child (port, newtask)))
|
1995-04-24 09:00:07 +00:00
|
|
|
LOSE;
|
1995-02-18 01:27:10 +00:00
|
|
|
|
|
|
|
/* This must be the absolutely last thing we do; we can't assume that
|
|
|
|
the child will remain alive for even a moment once we do this. We
|
|
|
|
ignore errors because we have committed to the fork and are not
|
|
|
|
allowed to return them after the process becomes visible to
|
|
|
|
POSIX.1 (which happened right above when we called proc_child). */
|
|
|
|
(void) __thread_resume (thread);
|
|
|
|
|
|
|
|
lose:
|
|
|
|
if (ports_locked)
|
|
|
|
for (i = 0; i < _hurd_nports; ++i)
|
|
|
|
__spin_unlock (&_hurd_ports[i].lock);
|
|
|
|
|
1995-08-17 22:55:22 +00:00
|
|
|
resume_threads ();
|
|
|
|
|
1995-02-18 01:27:10 +00:00
|
|
|
if (newtask != MACH_PORT_NULL)
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
__task_terminate (newtask);
|
|
|
|
__mach_port_deallocate (__mach_task_self (), newtask);
|
|
|
|
}
|
|
|
|
if (thread != MACH_PORT_NULL)
|
|
|
|
__mach_port_deallocate (__mach_task_self (), thread);
|
|
|
|
if (sigthread != MACH_PORT_NULL)
|
|
|
|
__mach_port_deallocate (__mach_task_self (), sigthread);
|
|
|
|
if (newproc != MACH_PORT_NULL)
|
|
|
|
__mach_port_deallocate (__mach_task_self (), newproc);
|
|
|
|
|
|
|
|
if (portnames)
|
|
|
|
__vm_deallocate (__mach_task_self (),
|
|
|
|
(vm_address_t) portnames,
|
|
|
|
nportnames * sizeof (*portnames));
|
|
|
|
if (porttypes)
|
|
|
|
__vm_deallocate (__mach_task_self (),
|
|
|
|
(vm_address_t) porttypes,
|
|
|
|
nporttypes * sizeof (*porttypes));
|
|
|
|
if (threads)
|
|
|
|
{
|
|
|
|
for (i = 0; i < nthreads; ++i)
|
|
|
|
__mach_port_deallocate (__mach_task_self (), threads[i]);
|
|
|
|
__vm_deallocate (__mach_task_self (),
|
|
|
|
(vm_address_t) threads,
|
|
|
|
nthreads * sizeof (*threads));
|
|
|
|
}
|
|
|
|
|
2016-04-14 07:17:02 +00:00
|
|
|
/* Release malloc locks. */
|
2016-09-21 23:45:48 +00:00
|
|
|
_hurd_malloc_fork_parent ();
|
2016-08-26 20:40:27 +00:00
|
|
|
call_function_static_weak (__malloc_fork_unlock_parent);
|
2016-04-14 07:17:02 +00:00
|
|
|
|
1995-02-18 01:27:10 +00:00
|
|
|
/* Run things that want to run in the parent to restore it to
|
|
|
|
normality. Usually prepare hooks and parent hooks are
|
|
|
|
symmetrical: the prepare hook arrests state in some way for the
|
|
|
|
fork, and the parent hook restores the state for the parent to
|
|
|
|
continue executing normally. */
|
|
|
|
RUN_HOOK (_hurd_fork_parent_hook, ());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct hurd_sigstate *oldstates;
|
|
|
|
|
|
|
|
/* We are the child task. Unlock the standard port cells, which were
|
1997-06-23 21:55:26 +00:00
|
|
|
locked in the parent when we copied its memory. The parent has
|
|
|
|
inserted send rights with the names that were in the cells then. */
|
1995-02-18 01:27:10 +00:00
|
|
|
for (i = 0; i < _hurd_nports; ++i)
|
|
|
|
__spin_unlock (&_hurd_ports[i].lock);
|
|
|
|
|
2002-09-20 03:31:58 +00:00
|
|
|
/* Claim our sigstate structure and unchain the rest: the
|
|
|
|
threads existed in the parent task but don't exist in this
|
|
|
|
task (the child process). Delay freeing them until later
|
|
|
|
because some of the further setup and unlocking might be
|
|
|
|
required for free to work. Before we finish cleaning up,
|
|
|
|
we will reclaim the signal thread's sigstate structure (if
|
|
|
|
it had one). */
|
1995-02-18 01:27:10 +00:00
|
|
|
oldstates = _hurd_sigstates;
|
|
|
|
if (oldstates == ss)
|
|
|
|
oldstates = ss->next;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (_hurd_sigstates->next != ss)
|
|
|
|
_hurd_sigstates = _hurd_sigstates->next;
|
|
|
|
_hurd_sigstates->next = ss->next;
|
|
|
|
}
|
|
|
|
ss->next = NULL;
|
|
|
|
_hurd_sigstates = ss;
|
1995-04-24 09:00:07 +00:00
|
|
|
__mutex_unlock (&_hurd_siglock);
|
hurd: Global signal disposition
This adds _hurd_sigstate_set_global_rcv used by libpthread to enable
POSIX-confirming behavior of signals on a per-thread basis.
This also provides a sigstate destructor _hurd_sigstate_delete, and a
global process signal state, which needs to be locked and check when
global disposition is enabled, thus the addition of _hurd_sigstate_lock
_hurd_sigstate_actions _hurd_sigstate_pending _hurd_sigstate_unlock helpers.
This also updates all the glibc code accordingly.
This also drops support for get_int(INIT_SIGMASK), which did not make sense
any more since we do not have a single signal thread any more.
During fork/spawn, this also reinitializes the child global sigstate's
lock. That cures an issue that would very rarely cause a deadlock in the
child in fork, tries to unlock ss' critical section lock at the end of
fork. This will typically (always?) be observed in /bin/sh, which is not
surprising as that is the foremost caller of fork.
To reproduce an intermediate state, add an endless loop if
_hurd_global_sigstate is locked after __proc_dostop (cast through
volatile); that is, while still being in the fork's parent process.
When that triggers (use the libtool testsuite), the signal thread has
already locked ss (which is _hurd_global_sigstate), and is stuck at
hurdsig.c:685 in post_signal, trying to lock _hurd_siglock (which the
main thread already has locked and keeps locked until after
__task_create). This is the case that ss->thread == MACH_PORT_NULL, that
is, a global signal. In the main thread, between __proc_dostop and
__task_create is the __thread_abort call on the signal thread which would
abort any current kernel operation (but leave ss locked). Later in fork,
in the parent, when _hurd_siglock is unlocked in fork, the parent's
signal thread can proceed and will unlock eventually the global sigstate.
In the client, _hurd_siglock will likewise be unlocked, but the global
sigstate never will be, as the client's signal thread has been configured
to restart execution from _hurd_msgport_receive. Thus, when the child
tries to unlock ss' critical section lock at the end of fork, it will
first lock the global sigstate, will spin trying to lock it, which can
never be successful, and we get our deadlock.
Options seem to be:
* Move the locking of _hurd_siglock earlier in post_signal -- but that
may generally impact performance, if this locking isn't generally
needed anyway?
On the other hand, would it actually make sense to wait here until we
are not any longer in a critical section (which is meant to disable
signal delivery anyway (but not for preempted signals?))?
* Clear the global sigstate in the fork's child with the rationale that
we're anyway restarting the signal thread from a clean state. This
has now been implemented.
Why has this problem not been observed before Jérémie's patches? (Or has
it? Perhaps even more rarely?) In _S_msg_sig_post, the signal is now
posted to a *global receiver thread*, whereas previously it was posted to
the *designated signal-receiving thread*. The latter one was in a
critical section in fork, so didn't try to handle the signal until after
leaving the critical section? (Not completely analyzed and verified.)
Another question is what the signal is that is being received
during/around the time __proc_dostop executes.
2019-12-29 16:59:55 +00:00
|
|
|
/* Earlier on, the global sigstate may have been tainted and now needs to
|
|
|
|
be reinitialized. Nobody is interested in its present state anymore:
|
|
|
|
we're not, the signal thread will be restarted, and there are no other
|
|
|
|
threads.
|
|
|
|
|
|
|
|
We can't simply allocate a fresh global sigstate here, as
|
|
|
|
_hurd_thread_sigstate will call malloc and that will deadlock trying
|
|
|
|
to determine the current thread's sigstate. */
|
|
|
|
#if 0
|
|
|
|
_hurd_thread_sigstate_init (_hurd_global_sigstate, MACH_PORT_NULL);
|
|
|
|
#else
|
|
|
|
/* Only reinitialize the lock -- otherwise we might have to do additional
|
|
|
|
setup as done in hurdsig.c:_hurdsig_init. */
|
|
|
|
__spin_lock_init (&_hurd_global_sigstate->lock);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* We are one of the (exactly) two threads in this new task, we
|
|
|
|
will take the task-global signals. */
|
|
|
|
_hurd_sigstate_set_global_rcv (ss);
|
1995-02-18 01:27:10 +00:00
|
|
|
|
|
|
|
/* Fetch our new process IDs from the proc server. No need to
|
|
|
|
refetch our pgrp; it is always inherited from the parent (so
|
|
|
|
_hurd_pgrp is already correct), and the proc server will send us a
|
|
|
|
proc_newids notification when it changes. */
|
|
|
|
err = __USEPORT (PROC, __proc_getpids (port, &_hurd_pid, &_hurd_ppid,
|
|
|
|
&_hurd_orphaned));
|
|
|
|
|
hurd: Global signal disposition
This adds _hurd_sigstate_set_global_rcv used by libpthread to enable
POSIX-confirming behavior of signals on a per-thread basis.
This also provides a sigstate destructor _hurd_sigstate_delete, and a
global process signal state, which needs to be locked and check when
global disposition is enabled, thus the addition of _hurd_sigstate_lock
_hurd_sigstate_actions _hurd_sigstate_pending _hurd_sigstate_unlock helpers.
This also updates all the glibc code accordingly.
This also drops support for get_int(INIT_SIGMASK), which did not make sense
any more since we do not have a single signal thread any more.
During fork/spawn, this also reinitializes the child global sigstate's
lock. That cures an issue that would very rarely cause a deadlock in the
child in fork, tries to unlock ss' critical section lock at the end of
fork. This will typically (always?) be observed in /bin/sh, which is not
surprising as that is the foremost caller of fork.
To reproduce an intermediate state, add an endless loop if
_hurd_global_sigstate is locked after __proc_dostop (cast through
volatile); that is, while still being in the fork's parent process.
When that triggers (use the libtool testsuite), the signal thread has
already locked ss (which is _hurd_global_sigstate), and is stuck at
hurdsig.c:685 in post_signal, trying to lock _hurd_siglock (which the
main thread already has locked and keeps locked until after
__task_create). This is the case that ss->thread == MACH_PORT_NULL, that
is, a global signal. In the main thread, between __proc_dostop and
__task_create is the __thread_abort call on the signal thread which would
abort any current kernel operation (but leave ss locked). Later in fork,
in the parent, when _hurd_siglock is unlocked in fork, the parent's
signal thread can proceed and will unlock eventually the global sigstate.
In the client, _hurd_siglock will likewise be unlocked, but the global
sigstate never will be, as the client's signal thread has been configured
to restart execution from _hurd_msgport_receive. Thus, when the child
tries to unlock ss' critical section lock at the end of fork, it will
first lock the global sigstate, will spin trying to lock it, which can
never be successful, and we get our deadlock.
Options seem to be:
* Move the locking of _hurd_siglock earlier in post_signal -- but that
may generally impact performance, if this locking isn't generally
needed anyway?
On the other hand, would it actually make sense to wait here until we
are not any longer in a critical section (which is meant to disable
signal delivery anyway (but not for preempted signals?))?
* Clear the global sigstate in the fork's child with the rationale that
we're anyway restarting the signal thread from a clean state. This
has now been implemented.
Why has this problem not been observed before Jérémie's patches? (Or has
it? Perhaps even more rarely?) In _S_msg_sig_post, the signal is now
posted to a *global receiver thread*, whereas previously it was posted to
the *designated signal-receiving thread*. The latter one was in a
critical section in fork, so didn't try to handle the signal until after
leaving the critical section? (Not completely analyzed and verified.)
Another question is what the signal is that is being received
during/around the time __proc_dostop executes.
2019-12-29 16:59:55 +00:00
|
|
|
/* Forking clears the trace flag and pending masks. */
|
1996-05-04 09:46:57 +00:00
|
|
|
__sigemptyset (&_hurdsig_traced);
|
hurd: Global signal disposition
This adds _hurd_sigstate_set_global_rcv used by libpthread to enable
POSIX-confirming behavior of signals on a per-thread basis.
This also provides a sigstate destructor _hurd_sigstate_delete, and a
global process signal state, which needs to be locked and check when
global disposition is enabled, thus the addition of _hurd_sigstate_lock
_hurd_sigstate_actions _hurd_sigstate_pending _hurd_sigstate_unlock helpers.
This also updates all the glibc code accordingly.
This also drops support for get_int(INIT_SIGMASK), which did not make sense
any more since we do not have a single signal thread any more.
During fork/spawn, this also reinitializes the child global sigstate's
lock. That cures an issue that would very rarely cause a deadlock in the
child in fork, tries to unlock ss' critical section lock at the end of
fork. This will typically (always?) be observed in /bin/sh, which is not
surprising as that is the foremost caller of fork.
To reproduce an intermediate state, add an endless loop if
_hurd_global_sigstate is locked after __proc_dostop (cast through
volatile); that is, while still being in the fork's parent process.
When that triggers (use the libtool testsuite), the signal thread has
already locked ss (which is _hurd_global_sigstate), and is stuck at
hurdsig.c:685 in post_signal, trying to lock _hurd_siglock (which the
main thread already has locked and keeps locked until after
__task_create). This is the case that ss->thread == MACH_PORT_NULL, that
is, a global signal. In the main thread, between __proc_dostop and
__task_create is the __thread_abort call on the signal thread which would
abort any current kernel operation (but leave ss locked). Later in fork,
in the parent, when _hurd_siglock is unlocked in fork, the parent's
signal thread can proceed and will unlock eventually the global sigstate.
In the client, _hurd_siglock will likewise be unlocked, but the global
sigstate never will be, as the client's signal thread has been configured
to restart execution from _hurd_msgport_receive. Thus, when the child
tries to unlock ss' critical section lock at the end of fork, it will
first lock the global sigstate, will spin trying to lock it, which can
never be successful, and we get our deadlock.
Options seem to be:
* Move the locking of _hurd_siglock earlier in post_signal -- but that
may generally impact performance, if this locking isn't generally
needed anyway?
On the other hand, would it actually make sense to wait here until we
are not any longer in a critical section (which is meant to disable
signal delivery anyway (but not for preempted signals?))?
* Clear the global sigstate in the fork's child with the rationale that
we're anyway restarting the signal thread from a clean state. This
has now been implemented.
Why has this problem not been observed before Jérémie's patches? (Or has
it? Perhaps even more rarely?) In _S_msg_sig_post, the signal is now
posted to a *global receiver thread*, whereas previously it was posted to
the *designated signal-receiving thread*. The latter one was in a
critical section in fork, so didn't try to handle the signal until after
leaving the critical section? (Not completely analyzed and verified.)
Another question is what the signal is that is being received
during/around the time __proc_dostop executes.
2019-12-29 16:59:55 +00:00
|
|
|
__sigemptyset (&_hurd_global_sigstate->pending);
|
|
|
|
__sigemptyset (&ss->pending);
|
1996-02-02 18:41:42 +00:00
|
|
|
|
2021-03-01 14:56:36 +00:00
|
|
|
__libc_unwind_link_after_fork ();
|
|
|
|
|
2016-04-14 07:17:02 +00:00
|
|
|
/* Release malloc locks. */
|
2016-09-21 23:45:48 +00:00
|
|
|
_hurd_malloc_fork_child ();
|
2016-08-26 20:40:27 +00:00
|
|
|
call_function_static_weak (__malloc_fork_unlock_child);
|
2016-04-14 07:17:02 +00:00
|
|
|
|
2020-02-20 12:20:32 +00:00
|
|
|
call_function_static_weak (__nss_database_fork_subprocess,
|
|
|
|
&nss_database_data);
|
|
|
|
|
1995-02-18 01:27:10 +00:00
|
|
|
/* Run things that want to run in the child task to set up. */
|
|
|
|
RUN_HOOK (_hurd_fork_child_hook, ());
|
|
|
|
|
|
|
|
/* Set up proc server-assisted fault recovery for the signal thread. */
|
|
|
|
_hurdsig_fault_init ();
|
|
|
|
|
|
|
|
/* Start the signal thread listening on the message port. */
|
|
|
|
if (!err)
|
|
|
|
err = __thread_resume (_hurd_msgport_thread);
|
|
|
|
|
2002-09-20 03:31:58 +00:00
|
|
|
/* Reclaim the signal thread's sigstate structure and free the
|
|
|
|
other old sigstate structures. */
|
1995-02-18 01:27:10 +00:00
|
|
|
while (oldstates != NULL)
|
|
|
|
{
|
|
|
|
struct hurd_sigstate *next = oldstates->next;
|
2002-09-20 03:31:58 +00:00
|
|
|
|
|
|
|
if (oldstates->thread == _hurd_msgport_thread)
|
|
|
|
{
|
|
|
|
/* If we have a second signal state structure then we
|
|
|
|
must have been through here before--not good. */
|
|
|
|
assert (_hurd_sigstates->next == 0);
|
|
|
|
_hurd_sigstates->next = oldstates;
|
|
|
|
oldstates->next = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
free (oldstates);
|
|
|
|
|
1995-02-18 01:27:10 +00:00
|
|
|
oldstates = next;
|
|
|
|
}
|
2002-09-20 03:31:58 +00:00
|
|
|
|
1995-02-18 01:27:10 +00:00
|
|
|
/* XXX what to do if we have any errors here? */
|
|
|
|
|
|
|
|
pid = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlock things we locked before creating the child task.
|
|
|
|
They are locked in both the parent and child tasks. */
|
1995-08-17 22:55:22 +00:00
|
|
|
{
|
|
|
|
void *const *p;
|
|
|
|
for (p = symbol_set_first_element (_hurd_fork_locks);
|
|
|
|
! symbol_set_end_p (_hurd_fork_locks, p);
|
|
|
|
++p)
|
|
|
|
__mutex_unlock (*p);
|
|
|
|
}
|
1995-02-18 01:27:10 +00:00
|
|
|
|
1995-04-24 09:00:07 +00:00
|
|
|
_hurd_critical_section_unlock (ss);
|
2021-03-23 22:38:54 +00:00
|
|
|
if (err == EINTR)
|
|
|
|
/* Got a signal while inside an RPC of the critical section, retry again */
|
|
|
|
goto retry;
|
1995-02-18 01:27:10 +00:00
|
|
|
|
2013-09-24 21:08:15 +00:00
|
|
|
if (!err)
|
|
|
|
{
|
2021-01-18 18:10:02 +00:00
|
|
|
__run_fork_handlers (pid == 0 ? atfork_run_child : atfork_run_parent,
|
|
|
|
true);
|
2013-09-24 21:08:15 +00:00
|
|
|
}
|
|
|
|
|
1995-02-18 01:27:10 +00:00
|
|
|
return err ? __hurd_fail (err) : pid;
|
|
|
|
}
|
2002-12-31 11:12:11 +00:00
|
|
|
libc_hidden_def (__fork)
|
1995-02-18 01:27:10 +00:00
|
|
|
|
|
|
|
weak_alias (__fork, fork)
|