/* Copyright (C) 1994-2025 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 <errno.h>
#include <unistd.h>
#include <hurd.h>
#include <hurd/signal.h>
#include <hurd/threadvar.h>
#include <setjmp.h>
#include <thread_state.h>
#include <sysdep.h> /* For stack growth direction. */
#include "set-hooks.h"
#include <assert.h>
#include "hurdmalloc.h" /* XXX */
#include <tls.h>
#include <malloc/malloc-internal.h>
#include <nss/nss_database.h>
#include <unwind-link.h>
#include <register-atfork.h>
#undef __fork
/* Things that want to be locked while forking. */
symbol_set_declare (_hurd_fork_locks)
/* 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;
ss = _hurd_self_sigstate ();
retry:
__spin_lock (&ss->critical_section_lock);
#undef LOSE
#define LOSE do { assert_perror (err); goto lose; } while (0) /* XXX */
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;
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;
}
/* Run things that prepare for forking before we create the task. */
RUN_HOOK (_hurd_fork_prepare_hook, ());
/* Lock things that want to be locked before we fork. */
{
void *const *p;
for (p = symbol_set_first_element (_hurd_fork_locks);
! symbol_set_end_p (_hurd_fork_locks, p);
++p)
__mutex_lock (*p);
}
__mutex_lock (&