From patchwork Wed Jul 5 20:43:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 72212 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E053F385697B for ; Wed, 5 Jul 2023 20:44:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E053F385697B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1688589898; bh=c2Ob33Qap+SvExB+RIf33/3onamsNaG0HTsijYKgOY8=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=uPRY0NdDqOpEFT3G3+/zXfOh3RXaFsGSbIPkLA2BU9MbQXnPRBiPNjZxmzaPPqCzm 2vbO2Uyw1BdpySaAlE5JOVuP7hFZAEVzgzQoaFBRQZ0aPdzgra/GTQ5tBGIgfMIKdO xdz/WoxlhRhOjC82zf5//10H2I04uwdRZcX5vL7c= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x235.google.com (mail-oi1-x235.google.com [IPv6:2607:f8b0:4864:20::235]) by sourceware.org (Postfix) with ESMTPS id D5C483856967 for ; Wed, 5 Jul 2023 20:43:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D5C483856967 Received: by mail-oi1-x235.google.com with SMTP id 5614622812f47-3a337ddff16so101251b6e.0 for ; Wed, 05 Jul 2023 13:43:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688589819; x=1691181819; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=c2Ob33Qap+SvExB+RIf33/3onamsNaG0HTsijYKgOY8=; b=Zu0fXl0Zrtv2lpt0vW1l/uwg433fL46H3djJdoqA4adfMkPFw2jM3XxVJA7PT0L8E+ qH+Ao/IsV0eePAGp+4zj98sO/n+v1F1aaPZaX7tfWBUvf0gwo+HZGeRk/ZsS4OQnbyVz 6v8eAXUF63UBkwU+/W20VvkhUP1dtXYH5Nz0IZMhLzk7vc+i7zHd3ZfkpJhMZ6ZVFp4N v0FaLPNWXMbPz8w5LoSaJ6my/xCUKYHkAWcn3DzPMblDXwz8kilrb/ryoxLRu+UiGshe TyuRwjMrUfzC73GL8qjT0e+hSoz4aViZt3BTvACEAH2IpPrjDXK7qZuXeo6WYTa433Kt lQ6g== X-Gm-Message-State: AC+VfDyscXVatVPxCzNvTs/TiQHgtwiXogfcKuqf7C75sfT+Ja6Z1F6v ernQmG/gIJINbNfIVebJnKz1CHgFLrzQXefthI0UyQ== X-Google-Smtp-Source: ACHHUZ7vaBpw4xJelRKkFw5LueyVzh7l/51ia6JHsx2S4riroC37QUXDa1m4/RSpYw42a+j2CCOqTw== X-Received: by 2002:a05:6808:11ce:b0:3a3:7e62:fca2 with SMTP id p14-20020a05680811ce00b003a37e62fca2mr18166730oiv.0.1688589818759; Wed, 05 Jul 2023 13:43:38 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c3:e0c8:4c8a:4b26:2eb9:add8]) by smtp.gmail.com with ESMTPSA id u10-20020a056808114a00b003a3860b375esm5637858oiu.34.2023.07.05.13.43.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jul 2023 13:43:38 -0700 (PDT) To: libc-alpha@sourceware.org, Luca Boccassi , Philip Withnall Subject: [PATCH v6 3/5] posix: Add pidfd_fork (BZ 26371) Date: Wed, 5 Jul 2023 17:43:26 -0300 Message-Id: <20230705204328.4067751-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230705204328.4067751-1-adhemerval.zanella@linaro.org> References: <20230705204328.4067751-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Returning a pidfd allows a process to keep a race-free handle to a child process, otherwise the caller will need to either use pidfd_open (which still might be subject to TOCTOU) or keep using the old racy interface. The implementation makes sure that kernel must support the complete pidfd interface, meaning that waitid (P_PIDFD) should be supported. It ensure that non racy workaround is required (such as reading procfs fdinfo pid to use along with old wait interfaces). If kernel does not have the required support the interface returns -1 and set errno to ENOSYS. The interface is: pid_t pidfd_fork (int *pidfd, int cgroup, unsigned int flags) If PIDFD is set to NULL, no file descriptor is returned and pidfd_fork acts as fork. Otherwise, a new file descriptor is returned and the kernel already sets O_CLOEXEC as default. The pidfd_fork follows fork/_Fork convention on returning a positive or negative value to the parent (with negative indicating an error) and zero to the child. If cgroup is 0 or positive value, it is interpreted as a different cgroup to be place the new process (check CLONE_INTO_CGROUP clone flag). Similar to fork, pidfd_fork also runs the pthread_atfork handlers It can be change by using PIDFDFORK_ASYNCSAFE flag, which make pidfd_fork acts a _Fork. It also send SIGCHLD to parent when process terminates. Checked on x86_64-linux-gnu on Linux 4.15 (no CLONE_PID or waitid support), Linux 5.15 (only clone support), and Linux 5.19 (full support including clone3). --- NEWS | 5 + include/clone_internal.h | 16 ++ manual/process.texi | 53 ++++- posix/Makefile | 3 +- posix/fork-internal.c | 127 ++++++++++++ posix/fork-internal.h | 36 ++++ posix/fork.c | 107 +--------- sysdeps/nptl/_Fork.c | 2 +- sysdeps/unix/sysv/linux/Makefile | 3 + sysdeps/unix/sysv/linux/Versions | 1 + sysdeps/unix/sysv/linux/aarch64/libc.abilist | 1 + sysdeps/unix/sysv/linux/alpha/libc.abilist | 1 + sysdeps/unix/sysv/linux/arc/libc.abilist | 1 + sysdeps/unix/sysv/linux/arch-fork.h | 16 +- sysdeps/unix/sysv/linux/arm/be/libc.abilist | 1 + sysdeps/unix/sysv/linux/arm/le/libc.abilist | 1 + sysdeps/unix/sysv/linux/clone-internal.c | 56 +++++- sysdeps/unix/sysv/linux/csky/libc.abilist | 1 + sysdeps/unix/sysv/linux/hppa/libc.abilist | 1 + sysdeps/unix/sysv/linux/i386/libc.abilist | 1 + sysdeps/unix/sysv/linux/ia64/libc.abilist | 1 + .../sysv/linux/loongarch/lp64/libc.abilist | 1 + .../sysv/linux/m68k/coldfire/libc.abilist | 1 + .../unix/sysv/linux/m68k/m680x0/libc.abilist | 1 + .../sysv/linux/microblaze/be/libc.abilist | 1 + .../sysv/linux/microblaze/le/libc.abilist | 1 + .../sysv/linux/mips/mips32/fpu/libc.abilist | 1 + .../sysv/linux/mips/mips32/nofpu/libc.abilist | 1 + .../sysv/linux/mips/mips64/n32/libc.abilist | 1 + .../sysv/linux/mips/mips64/n64/libc.abilist | 1 + sysdeps/unix/sysv/linux/nios2/libc.abilist | 1 + sysdeps/unix/sysv/linux/or1k/libc.abilist | 1 + sysdeps/unix/sysv/linux/pidfd_fork.c | 81 ++++++++ .../linux/powerpc/powerpc32/fpu/libc.abilist | 1 + .../powerpc/powerpc32/nofpu/libc.abilist | 1 + .../linux/powerpc/powerpc64/be/libc.abilist | 1 + .../linux/powerpc/powerpc64/le/libc.abilist | 1 + .../unix/sysv/linux/riscv/rv32/libc.abilist | 1 + .../unix/sysv/linux/riscv/rv64/libc.abilist | 1 + .../unix/sysv/linux/s390/s390-32/libc.abilist | 1 + .../unix/sysv/linux/s390/s390-64/libc.abilist | 1 + sysdeps/unix/sysv/linux/sh/be/libc.abilist | 1 + sysdeps/unix/sysv/linux/sh/le/libc.abilist | 1 + .../sysv/linux/sparc/sparc32/libc.abilist | 1 + .../sysv/linux/sparc/sparc64/libc.abilist | 1 + sysdeps/unix/sysv/linux/sys/pidfd.h | 19 ++ .../unix/sysv/linux/tst-pidfd_fork-cgroup.c | 162 +++++++++++++++ sysdeps/unix/sysv/linux/tst-pidfd_fork.c | 186 ++++++++++++++++++ .../unix/sysv/linux/x86_64/64/libc.abilist | 1 + .../unix/sysv/linux/x86_64/x32/libc.abilist | 1 + 50 files changed, 788 insertions(+), 119 deletions(-) create mode 100644 posix/fork-internal.c create mode 100644 posix/fork-internal.h create mode 100644 sysdeps/unix/sysv/linux/pidfd_fork.c create mode 100644 sysdeps/unix/sysv/linux/tst-pidfd_fork-cgroup.c create mode 100644 sysdeps/unix/sysv/linux/tst-pidfd_fork.c diff --git a/NEWS b/NEWS index 65562e75e7..462d8f511d 100644 --- a/NEWS +++ b/NEWS @@ -61,6 +61,11 @@ Major new features: The pidfd functionality avoid the issue of PID reuse with traditional posix_spawn interface. +* On Linux, the pidfd_fork has been added. It has a similar semantic + as fork or _Fork, where it clones the calling process. However instead + of return a process ID, it returns a file descriptor that can be used + along other pidfd functions. + Deprecated and removed features, and other changes affecting compatibility: * In the Linux kernel for the hppa/parisc architecture some of the diff --git a/include/clone_internal.h b/include/clone_internal.h index 567160ebb5..d9b5509f78 100644 --- a/include/clone_internal.h +++ b/include/clone_internal.h @@ -2,6 +2,8 @@ #define _CLONE_INTERNAL_H #include +#include +#include /* The clone3 syscall provides a superset of the functionality of the clone interface. The kernel might extend __CL_ARGS struct in the future, with @@ -35,6 +37,20 @@ extern int __clone_internal_fallback (struct clone_args *__cl_args, void *__arg) attribute_hidden; +/* Call the clone3/clone syscall with fork semantic (i.e. no stack setting + required). The EXTRA_FLAGS define any additional flag to be used besides + CLONE_CHILD_SETTID and CLONE_CHILD_CLEARTID, the PIDFD indicates where + the process file descriptor (set with CLONE_PIDFD) should be returned, + and the CGROUP specifies the cgroupsv2 (set with CLONE_INTO_CGROUP). + + Similar to __clone3_internal, it uses the stick check to avoid re-issue + the clone3 syscall if kernel does not support it. + + It does not provide CLONE_INTO_CGROUP/CGROUP fallback if clone3 is not + supported, in this case the function returns -1/ENOTSUP. */ +extern int __clone_fork (uint64_t __extra_flags, void *__pidfd, int __cgroup) + attribute_hidden; + /* Return whether the kernel supports pid file descriptor, including clone with CLONE_PIDFD and waitid with P_PIDFD. */ extern bool __clone_pidfd_supported (void) attribute_hidden; diff --git a/manual/process.texi b/manual/process.texi index 68361c3f61..a656df425b 100644 --- a/manual/process.texi +++ b/manual/process.texi @@ -137,12 +137,12 @@ creating a process and making it run another program. @cindex subprocess A new processes is created when one of the functions @code{posix_spawn}, @code{fork}, @code{_Fork}, @code{vfork}, or -@code{pidfd_spawn} is called. (The @code{system} and @code{popen} also -create new processes internally.) Due to the name of the @code{fork} -function, the act of creating a new process is sometimes called -@dfn{forking} a process. Each new process (the @dfn{child process} or -@dfn{subprocess}) is allocated a process ID, distinct from the process -ID of the parent process. @xref{Process Identification}. +@code{pidfd_spawn}, or @code{pidfd_fork} is called. (The @code{system} +and @code{popen} also create new processes internally.) Due to the name +of the @code{fork} function, the act of creating a new process is +sometimes called @dfn{forking} a process. Each new process (the +@dfn{child process} or @dfn{subprocess}) is allocated a process ID, +distinct from the process ID of the parent process. @xref{Process Identification}. After forking a child process, both the parent and child processes continue to execute normally. If you want your program to wait for a @@ -153,10 +153,10 @@ limited information about why the child terminated---for example, its exit status code. A newly forked child process continues to execute the same program as -its parent process, at the point where the @code{fork} or @code{_Fork} -call returns. You can use the return value from @code{fork} or -@code{_Fork} to tell whether the program is running in the parent process -or the child. +its parent process, at the point where the @code{fork}, @code{_Fork}, +or @code{pidfd_fork} call returns. You can use the return value from +@code{fork}, @code{_Fork}, or @code{pidfd_fork} to tell whether the +program is running in the parent process or the child. @cindex process image Having several processes run the same program is only occasionally @@ -362,6 +362,39 @@ the proper precautions for using @code{vfork}, your program will still work even if the system uses @code{fork} instead. @end deftypefun +@deftypefun pid_t pidfd_fork (int *@var{pidfd}, int @var{cgroup}, unsigned int @var{flags}) +@standards{GNU, sys/pidfd.h} +@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} +The @code{fork} function is similar to @code{fork} but return a file +descriptor instead of process ID. + +If the operation is sucessful, there are both parent and child processes +and both see @code{pidfd_fork} return, but with different values: it return +a value of @code{0} in the child process and returns the child's process ID +in the parent process. + +Also, if the process is correctly created and @code{pidfd} is non @code{NULL} +the input argument will contain a file descriptor that can be used along other +pidfd functions (like @code{pidfd_send_signal} or with @code{waitid} along with +@code{P_PIDFD}. + +The @var{cgroup} argument should either -1 or a file descriptor to a cgroup v2 +directory used on process creation. There is no fallback implementation, meaning +If the kernel does not provide the required support an error is returned. + +The @var{flags} argument should be either zero, or the bitwise OR of some of the +following flags: + +@table @code +@item PIDFDFORK_ASYNCSAFE +Acts as @code{_Fork}, where it does not invoke any callbacks registered with +@code{pthread_atfork}, nor does it reset internal state or locks (such as the +@code{malloc} locks). +@end table +@end deftypefun + +This function is specific to Linux. + @node Executing a File @section Executing a File @cindex executing a file diff --git a/posix/Makefile b/posix/Makefile index e3c78d3d65..974a369339 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -84,6 +84,7 @@ routines := \ fexecve \ fnmatch \ fork \ + fork-internal \ fpathconf \ gai_strerror \ get_child_max \ @@ -580,7 +581,7 @@ CFLAGS-execl.os = -fomit-frame-pointer CFLAGS-execvp.os = -fomit-frame-pointer CFLAGS-execlp.os = -fomit-frame-pointer CFLAGS-nanosleep.c += -fexceptions -fasynchronous-unwind-tables -CFLAGS-fork.c = $(libio-mtsafe) $(config-cflags-wno-ignored-attributes) +CFLAGS-fork-internal.c = $(libio-mtsafe) $(config-cflags-wno-ignored-attributes) tstgetopt-ARGS = -a -b -cfoobar --required foobar --optional=bazbug \ --none random --col --color --colour diff --git a/posix/fork-internal.c b/posix/fork-internal.c new file mode 100644 index 0000000000..a5e47cbe53 --- /dev/null +++ b/posix/fork-internal.c @@ -0,0 +1,127 @@ +/* Internal fork definitions. + Copyright (C) 2023 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 + . */ + +#include +#include +#include +#include +#include +#include +#include +#include + +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)); +} + +uint64_t +__fork_pre (bool multiple_threads, struct nss_database_data *nss_database_data) +{ + uint64_t lastrun = __run_prefork_handlers (multiple_threads); + + /* 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); + } + + return lastrun; +} + +void +__fork_post (struct fork_post_state_t *state, + struct nss_database_data *nss_database_data) +{ + if (state->pid == 0) + { + fork_system_setup (); + + /* Reset the lock state in the multi-threaded case. */ + if (state->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)); + + /* Reset the lock protecting dynamic TLS related data. */ + __rtld_lock_initialize (GL(dl_load_tls_lock)); + + reclaim_stacks (); + + /* Run the handlers registered for the child. */ + __run_postfork_handlers (atfork_run_child, state->multiple_threads, + state->lastrun); + } + else + { + /* If _Fork failed, preserve its errno value. */ + int save_errno = errno; + + /* Release acquired locks in the multi-threaded case. */ + if (state->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_postfork_handlers (atfork_run_parent, state->multiple_threads, + state->lastrun); + + if (state->pid < 0) + __set_errno (save_errno); + } +} diff --git a/posix/fork-internal.h b/posix/fork-internal.h new file mode 100644 index 0000000000..5017061e1e --- /dev/null +++ b/posix/fork-internal.h @@ -0,0 +1,36 @@ +/* Internal fork definitions. + Copyright (C) 2023 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 + . */ + +#ifndef _FORK_INTERNAL_H +#define _FORK_INTERNAL_H + +#include +#include + +struct fork_post_state_t +{ + bool multiple_threads; + pid_t pid; + uint64_t lastrun; +}; + +uint64_t __fork_pre (bool, struct nss_database_data *) attribute_hidden; +void __fork_post (struct fork_post_state_t *, struct nss_database_data *) + attribute_hidden; + +#endif diff --git a/posix/fork.c b/posix/fork.c index b4aaa9fa6d..1708473e72 100644 --- a/posix/fork.c +++ b/posix/fork.c @@ -16,25 +16,10 @@ License along with the GNU C Library; if not, see . */ -#include -#include -#include -#include -#include -#include -#include +#include #include #include - -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)); -} +#include pid_t __libc_fork (void) @@ -45,92 +30,18 @@ __libc_fork (void) 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 = !SINGLE_THREAD_P; - uint64_t lastrun; - - lastrun = __run_prefork_handlers (multiple_threads); - + struct fork_post_state_t state = { + .multiple_threads = !SINGLE_THREAD_P + }; 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)); - - /* Reset the lock protecting dynamic TLS related data. */ - __rtld_lock_initialize (GL(dl_load_tls_lock)); - - reclaim_stacks (); - - /* Run the handlers registered for the child. */ - __run_postfork_handlers (atfork_run_child, multiple_threads, lastrun); - } - 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 (); - } + state.lastrun = __fork_pre (state.multiple_threads, &nss_database_data); - /* Run the handlers registered for the parent. */ - __run_postfork_handlers (atfork_run_parent, multiple_threads, lastrun); + state.pid = _Fork (); - if (pid < 0) - __set_errno (save_errno); - } + __fork_post (&state, &nss_database_data); - return pid; + return state.pid; } weak_alias (__libc_fork, __fork) libc_hidden_def (__fork) diff --git a/sysdeps/nptl/_Fork.c b/sysdeps/nptl/_Fork.c index f8322ae557..aa99e05b5b 100644 --- a/sysdeps/nptl/_Fork.c +++ b/sysdeps/nptl/_Fork.c @@ -22,7 +22,7 @@ pid_t _Fork (void) { - pid_t pid = arch_fork (&THREAD_SELF->tid); + pid_t pid = arch_fork (0, NULL, &THREAD_SELF->tid); if (pid == 0) { struct pthread *self = THREAD_SELF; diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 1bfd114d5d..1fc6785f28 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -490,6 +490,7 @@ sysdep_headers += \ sysdep_routines += \ getcpu \ oldglob \ + pidfd_fork \ pidfd_spawn \ pidfd_spawnp \ sched_getcpu \ @@ -500,6 +501,8 @@ sysdep_routines += \ tests += \ tst-affinity \ tst-affinity-pid \ + tst-pidfd_fork \ + tst-pidfd_fork-cgroup \ tst-posix_spawn-setsid-pidfd \ tst-spawn-cgroup \ tst-spawn-chdir-pidfd \ diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions index 95ad896850..e9eecfecc0 100644 --- a/sysdeps/unix/sysv/linux/Versions +++ b/sysdeps/unix/sysv/linux/Versions @@ -324,6 +324,7 @@ libc { GLIBC_2.38 { posix_spawnattr_getcgroup_np; posix_spawnattr_setcgroup_np; + pidfd_fork; pidfd_spawn; pidfd_spawnp; } diff --git a/sysdeps/unix/sysv/linux/aarch64/libc.abilist b/sysdeps/unix/sysv/linux/aarch64/libc.abilist index 26483dbc4e..e81e56f88c 100644 --- a/sysdeps/unix/sysv/linux/aarch64/libc.abilist +++ b/sysdeps/unix/sysv/linux/aarch64/libc.abilist @@ -2669,6 +2669,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/alpha/libc.abilist b/sysdeps/unix/sysv/linux/alpha/libc.abilist index b2cafe896e..0640fd71b9 100644 --- a/sysdeps/unix/sysv/linux/alpha/libc.abilist +++ b/sysdeps/unix/sysv/linux/alpha/libc.abilist @@ -2778,6 +2778,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/arc/libc.abilist b/sysdeps/unix/sysv/linux/arc/libc.abilist index 7138d480c1..4c9dd624ca 100644 --- a/sysdeps/unix/sysv/linux/arc/libc.abilist +++ b/sysdeps/unix/sysv/linux/arc/libc.abilist @@ -2430,6 +2430,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/arch-fork.h b/sysdeps/unix/sysv/linux/arch-fork.h index 0e0eccbf38..9e8a449e2c 100644 --- a/sysdeps/unix/sysv/linux/arch-fork.h +++ b/sysdeps/unix/sysv/linux/arch-fork.h @@ -32,24 +32,24 @@ override it with one of the supported calling convention (check generic kernel-features.h for the clone abi variants). */ static inline pid_t -arch_fork (void *ctid) +arch_fork (int flags, void *ptid, void *ctid) { - const int flags = CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID | SIGCHLD; long int ret; + flags |= CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID | SIGCHLD; #ifdef __ASSUME_CLONE_BACKWARDS # ifdef INLINE_CLONE_SYSCALL - ret = INLINE_CLONE_SYSCALL (flags, 0, NULL, 0, ctid); + ret = INLINE_CLONE_SYSCALL (flags, 0, ptid, 0, ctid); # else - ret = INLINE_SYSCALL_CALL (clone, flags, 0, NULL, 0, ctid); + ret = INLINE_SYSCALL_CALL (clone, flags, 0, ptid, 0, ctid); # endif #elif defined(__ASSUME_CLONE_BACKWARDS2) - ret = INLINE_SYSCALL_CALL (clone, 0, flags, NULL, ctid, 0); + ret = INLINE_SYSCALL_CALL (clone, 0, flags, ptid, ctid, 0); #elif defined(__ASSUME_CLONE_BACKWARDS3) - ret = INLINE_SYSCALL_CALL (clone, flags, 0, 0, NULL, ctid, 0); + ret = INLINE_SYSCALL_CALL (clone, flags, 0, 0, ptid, ctid, 0); #elif defined(__ASSUME_CLONE2) - ret = INLINE_SYSCALL_CALL (clone2, flags, 0, 0, NULL, ctid, 0); + ret = INLINE_SYSCALL_CALL (clone2, flags, 0, 0, ptid, ctid, 0); #elif defined(__ASSUME_CLONE_DEFAULT) - ret = INLINE_SYSCALL_CALL (clone, flags, 0, NULL, ctid, 0); + ret = INLINE_SYSCALL_CALL (clone, flags, 0, ptid, ctid, 0); #else # error "Undefined clone variant" #endif diff --git a/sysdeps/unix/sysv/linux/arm/be/libc.abilist b/sysdeps/unix/sysv/linux/arm/be/libc.abilist index 4d92c041dd..e45af835ff 100644 --- a/sysdeps/unix/sysv/linux/arm/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/arm/be/libc.abilist @@ -550,6 +550,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/arm/le/libc.abilist b/sysdeps/unix/sysv/linux/arm/le/libc.abilist index 8595044924..17abecc580 100644 --- a/sysdeps/unix/sysv/linux/arm/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/arm/le/libc.abilist @@ -547,6 +547,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/clone-internal.c b/sysdeps/unix/sysv/linux/clone-internal.c index 790739cfce..6f5d65d98c 100644 --- a/sysdeps/unix/sysv/linux/clone-internal.c +++ b/sysdeps/unix/sysv/linux/clone-internal.c @@ -16,6 +16,7 @@ License along with the GNU C Library. If not, see . */ +#include #include #include #include @@ -43,6 +44,11 @@ _Static_assert (offsetofend (struct clone_args, cgroup) == CLONE_ARGS_SIZE_VER2, _Static_assert (sizeof (struct clone_args) == CLONE_ARGS_SIZE_VER2, "sizeof (struct clone_args) != CLONE_ARGS_SIZE_VER2"); +#if !__ASSUME_CLONE3 && defined __NR_clone3 +/* Set to 0 if kernel does not support clone3 syscall. */ +static int clone3_supported = 1; +#endif + int __clone_internal_fallback (struct clone_args *cl_args, int (*func) (void *arg), void *arg) @@ -84,7 +90,6 @@ __clone3_internal (struct clone_args *cl_args, int (*func) (void *args), # if __ASSUME_CLONE3 return __clone3 (cl_args, sizeof (*cl_args), func, arg); # else - static int clone3_supported = 1; if (atomic_load_relaxed (&clone3_supported) == 1) { int ret = __clone3 (cl_args, sizeof (*cl_args), func, arg); @@ -118,3 +123,52 @@ __clone_internal (struct clone_args *cl_args, } libc_hidden_def (__clone_internal) + +int +__clone_fork (uint64_t extra_flags, void *pidfd, int cgroup) +{ +#ifdef __NR_clone3 + struct clone_args clone_args = + { + .flags = extra_flags + | CLONE_CHILD_SETTID + | CLONE_CHILD_CLEARTID, + .exit_signal = SIGCHLD, + .cgroup = cgroup, + .child_tid = (uintptr_t) &THREAD_SELF->tid, + .pidfd = (uintptr_t) pidfd, + .parent_tid = (uintptr_t) pidfd + }; +#endif + +#if __ASSUME_CLONE3 + return INLINE_SYSCALL_CALL (clone3, &clone_args, sizeof (clone_args)); +#else + /* Some architecture still does not export clone3. */ + pid_t pid; +# ifdef __NR_clone3 + if (atomic_load_relaxed (&clone3_supported) == 1) + { + pid = INLINE_SYSCALL_CALL (clone3, &clone_args, sizeof (clone_args)); + if (pid != -1 || errno != ENOSYS) + return pid; + + atomic_store_relaxed (&clone3_supported, 0); + } +# endif + + bool set_cgroup = cgroup != -1; + bool use_pidfd = pidfd != NULL; + + if (!set_cgroup) + pid = arch_fork (use_pidfd ? CLONE_PIDFD : 0, pidfd, &THREAD_SELF->tid); + else + { + /* No fallback for POSIX_SPAWN_SETCGROUP if clone3 is not supported. */ + pid = -1; + if (errno == ENOSYS) + errno = ENOTSUP; + } + return pid; +#endif +} diff --git a/sysdeps/unix/sysv/linux/csky/libc.abilist b/sysdeps/unix/sysv/linux/csky/libc.abilist index 388db91231..360a60980d 100644 --- a/sysdeps/unix/sysv/linux/csky/libc.abilist +++ b/sysdeps/unix/sysv/linux/csky/libc.abilist @@ -2706,6 +2706,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/hppa/libc.abilist b/sysdeps/unix/sysv/linux/hppa/libc.abilist index aa21ca135b..2aa63e2860 100644 --- a/sysdeps/unix/sysv/linux/hppa/libc.abilist +++ b/sysdeps/unix/sysv/linux/hppa/libc.abilist @@ -2655,6 +2655,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/i386/libc.abilist b/sysdeps/unix/sysv/linux/i386/libc.abilist index 31d34bd2cc..2ee3d027ac 100644 --- a/sysdeps/unix/sysv/linux/i386/libc.abilist +++ b/sysdeps/unix/sysv/linux/i386/libc.abilist @@ -2839,6 +2839,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/ia64/libc.abilist b/sysdeps/unix/sysv/linux/ia64/libc.abilist index 6b09d6bddb..262e0b3f59 100644 --- a/sysdeps/unix/sysv/linux/ia64/libc.abilist +++ b/sysdeps/unix/sysv/linux/ia64/libc.abilist @@ -2604,6 +2604,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist b/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist index 65c5050c24..0e5b4da990 100644 --- a/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist +++ b/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist @@ -2190,6 +2190,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist index 104f1d9e7d..b33dc8f04a 100644 --- a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist +++ b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist @@ -551,6 +551,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist index b1d44b697c..0b8bfb07d3 100644 --- a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist +++ b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist @@ -2782,6 +2782,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist b/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist index be7b0c59b9..d70ae3c2d3 100644 --- a/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist @@ -2755,6 +2755,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist b/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist index 44171c5bcc..c9dea106b8 100644 --- a/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist @@ -2752,6 +2752,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist index 672d142d2e..542d9b464e 100644 --- a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist @@ -2747,6 +2747,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist index 6a494ab102..5839437940 100644 --- a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist @@ -2745,6 +2745,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist index 38d3ed399d..3d5a63c979 100644 --- a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist @@ -2753,6 +2753,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist index 4f6f2040b9..3aa747d1b8 100644 --- a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist @@ -2655,6 +2655,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/nios2/libc.abilist b/sysdeps/unix/sysv/linux/nios2/libc.abilist index dbdeab7a7a..ed7da52383 100644 --- a/sysdeps/unix/sysv/linux/nios2/libc.abilist +++ b/sysdeps/unix/sysv/linux/nios2/libc.abilist @@ -2794,6 +2794,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/or1k/libc.abilist b/sysdeps/unix/sysv/linux/or1k/libc.abilist index 8801c867b1..e75c55efa0 100644 --- a/sysdeps/unix/sysv/linux/or1k/libc.abilist +++ b/sysdeps/unix/sysv/linux/or1k/libc.abilist @@ -2176,6 +2176,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/pidfd_fork.c b/sysdeps/unix/sysv/linux/pidfd_fork.c new file mode 100644 index 0000000000..983f8ade98 --- /dev/null +++ b/sysdeps/unix/sysv/linux/pidfd_fork.c @@ -0,0 +1,81 @@ +/* pidfd_fork - Duplicated calling process and return a process file + descriptor. + Copyright (C) 2023 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 + . */ + +#include +#include +#include + +static pid_t +forkfd (int *pidfd, int cgroup) +{ + bool use_pidfd = pidfd != NULL; + bool set_cgroup = cgroup != -1; + + uint64_t extra_flags = (use_pidfd ? CLONE_PIDFD : 0) + | (set_cgroup ? CLONE_INTO_CGROUP : 0); + pid_t pid = __clone_fork (extra_flags, use_pidfd ? pidfd : NULL, + set_cgroup ? cgroup: 0); + + if (pid == 0) + { + struct pthread *self = THREAD_SELF; + + /* Initialize the robust mutex, check _Fork implementation for a full + description why this is required. */ +#if __PTHREAD_MUTEX_HAVE_PREV + self->robust_prev = &self->robust_head; +#endif + self->robust_head.list = &self->robust_head; + INTERNAL_SYSCALL_CALL (set_robust_list, &self->robust_head, + sizeof (struct robust_list_head)); + } + return pid; +} + +pid_t +pidfd_fork (int *pidfd, int cgroup, unsigned int flags) +{ + if (!__clone_pidfd_supported ()) + return INLINE_SYSCALL_ERROR_RETURN_VALUE (ENOSYS); + + if (flags & ~(PIDFDFORK_ASYNCSAFE)) + return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL); + + pid_t pid; + if (!(flags & PIDFDFORK_ASYNCSAFE)) + { + bool multiple_threads = !SINGLE_THREAD_P; + struct fork_post_state_t state = { + .multiple_threads = !SINGLE_THREAD_P + }; + struct nss_database_data nss_database_data; + + state.lastrun = __fork_pre (multiple_threads, &nss_database_data); + state.pid = forkfd (pidfd, cgroup); + /* It follow the usual fork semantic, where a positive or negative + value is returned to parent, and 0 for the child. */ + __fork_post (&state, &nss_database_data); + + pid = state.pid; + } + else + pid = forkfd (pidfd, cgroup); + + return pid; +} diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist index d4927da36e..82eb6e1be0 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist @@ -2821,6 +2821,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist index 934ebcc495..c188bb00f2 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist @@ -2854,6 +2854,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist index 7dee513a82..f68077e425 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist @@ -2575,6 +2575,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist index 1f733560b9..8aa9ce6859 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist @@ -2889,6 +2889,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist index 6f0799c25a..3a1e55073a 100644 --- a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist +++ b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist @@ -2432,6 +2432,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist index c359dc2b29..312b0860b3 100644 --- a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist +++ b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist @@ -2632,6 +2632,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist index c49704b77e..b702ceb160 100644 --- a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist +++ b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist @@ -2819,6 +2819,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist index 389a451762..0d9f2c0ea7 100644 --- a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist +++ b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist @@ -2612,6 +2612,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/sh/be/libc.abilist b/sysdeps/unix/sysv/linux/sh/be/libc.abilist index 01adcabee8..a99bd972e5 100644 --- a/sysdeps/unix/sysv/linux/sh/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/sh/be/libc.abilist @@ -2662,6 +2662,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/sh/le/libc.abilist b/sysdeps/unix/sysv/linux/sh/le/libc.abilist index 83a4359dab..76fdafd7df 100644 --- a/sysdeps/unix/sysv/linux/sh/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/sh/le/libc.abilist @@ -2659,6 +2659,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist index ba2f588fc1..9201f21b4e 100644 --- a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist +++ b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist @@ -2814,6 +2814,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist index 2e63d242c6..5337df989d 100644 --- a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist +++ b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist @@ -2627,6 +2627,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/sys/pidfd.h b/sysdeps/unix/sysv/linux/sys/pidfd.h index 342e593288..3e6d009ce7 100644 --- a/sysdeps/unix/sysv/linux/sys/pidfd.h +++ b/sysdeps/unix/sysv/linux/sys/pidfd.h @@ -46,4 +46,23 @@ extern int pidfd_getfd (int __pidfd, int __targetfd, extern int pidfd_send_signal (int __pidfd, int __sig, siginfo_t *__info, unsigned int __flags) __THROW; + +/* Do not issue the pthread_atfork on pidfd_fork. */ +#define PIDFDFORK_ASYNCSAFE (1U << 1) + +/* Clone the calling process, creating an exact copy and return a file + descriptor that can be used along other pidfd functions. + + THE __CGROUP can be used to specify a different cgroup2 than the default + one. This is done with the CLONE_INTO_CGROUP clone3 flag, and passing an + value -1 disables it. If clone3 is not supported the call will fail. + + The __FLAGS can be used to specify whether to run pthread_atfork handlers + and reset internal states. The default is to run it, similar to fork. + + Return -1 for errors, 0 to the new process, and the process ID of the new + process to the parent process. */ +extern pid_t pidfd_fork (int *__pidfd, int __cgroup, unsigned int __flags) + __THROW; + #endif /* _PIDFD_H */ diff --git a/sysdeps/unix/sysv/linux/tst-pidfd_fork-cgroup.c b/sysdeps/unix/sysv/linux/tst-pidfd_fork-cgroup.c new file mode 100644 index 0000000000..124a70d7f5 --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-pidfd_fork-cgroup.c @@ -0,0 +1,162 @@ +/* pidfd_fork test using cgroupsv2. + Copyright (C) 2023 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 + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define CGROUPFS "/sys/fs/cgroup/" +#ifndef CGROUP2_SUPER_MAGIC +# define CGROUP2_SUPER_MAGIC 0x63677270 +#endif + +#define F_TYPE_EQUAL(a, b) (a == (typeof(a)) b) + +static inline char * +startswith(const char *s, const char *prefix) +{ + size_t l = strlen (prefix); + if (strncmp (s, prefix, l) == 0) + return (char*) s + l; + return NULL; +} + +static char * +get_cgroup (void) +{ + FILE *f = xfopen ("/proc/self/cgroup", "re"); + + char *cgroup = NULL; + + char *line = NULL; + size_t linesiz = 0; + while (xgetline (&line, &linesiz, f) > 0) + { + char *entry = startswith (line, "0:"); + if (entry == NULL) + continue; + + entry = strchr (entry, ':'); + if (entry == NULL) + continue; + + cgroup = entry + 1; + size_t l = strlen (cgroup); + if (cgroup[l - 1] == '\n') + cgroup[l - 1] = '\0'; + + cgroup = xstrdup (entry + 1); + break; + } + + xfclose (f); + free (line); + + return cgroup; +} + +static int +do_test (void) +{ + struct statfs fs; + if (statfs (CGROUPFS, &fs) < 0) + { + if (errno == ENOENT) + FAIL_UNSUPPORTED ("not cgroupv2 mount found"); + FAIL_EXIT1 ("statfs (%s): %m\n", CGROUPFS); + } + + if (!F_TYPE_EQUAL (fs.f_type, CGROUP2_SUPER_MAGIC)) + FAIL_UNSUPPORTED ("%s is not a cgroupv2", CGROUPFS); + + char *cgroup = get_cgroup (); + TEST_VERIFY_EXIT (cgroup != NULL); + char *newcgroup = xasprintf ("%s/%s", cgroup, "test-pidfd_fork-cgroup"); + char *cgpath = xasprintf ("%s%s/test-pidfd_fork-cgroup", CGROUPFS, cgroup); + free (cgroup); + + if (mkdir (cgpath, 0755) == -1 && errno != EEXIST) + { + if (errno == EACCES || errno == EPERM) + FAIL_UNSUPPORTED ("can not create a new cgroupv2 group"); + FAIL_EXIT1 ("mkdir (%s): %m", cgpath); + } + add_temp_file (cgpath); + + int dfd = xopen (cgpath, O_DIRECTORY | O_RDONLY | O_CLOEXEC, 0666); + + /* Check if the cgroup used at creation is the same returned by the kernel + and not as the parent. */ + { + pid_t pid = pidfd_fork (NULL, dfd, 0); + if (pid == -1 && errno == ENOSYS) + FAIL_UNSUPPORTED ("kernel does not support CLONE_PIDFD clone flag"); + TEST_VERIFY_EXIT (pid != -1); + if (pid == 0) + { + char *child_cgroup = get_cgroup (); + TEST_VERIFY_EXIT (child_cgroup != NULL); + TEST_COMPARE_STRING (newcgroup, child_cgroup); + _exit (EXIT_SUCCESS); + } + + siginfo_t sinfo; + TEST_COMPARE (waitid (P_PID, pid, &sinfo, WEXITED), 0); + TEST_COMPARE (sinfo.si_signo, SIGCHLD); + TEST_COMPARE (sinfo.si_code, CLD_EXITED); + TEST_COMPARE (sinfo.si_status, 0); + } + + /* Same as before, but also check along with process file descriptor. */ + { + int pidfd; + pid_t pid = pidfd_fork (&pidfd, dfd, 0); + TEST_VERIFY_EXIT (pid != -1); + if (pid == 0) + { + char *child_cgroup = get_cgroup (); + TEST_VERIFY_EXIT (child_cgroup != NULL); + TEST_COMPARE_STRING (newcgroup, child_cgroup); + _exit (EXIT_SUCCESS); + } + + siginfo_t sinfo; + TEST_COMPARE (waitid (P_PIDFD, pidfd, &sinfo, WEXITED), 0); + TEST_COMPARE (sinfo.si_signo, SIGCHLD); + TEST_COMPARE (sinfo.si_code, CLD_EXITED); + TEST_COMPARE (sinfo.si_status, 0); + } + + free (cgpath); + free (newcgroup); + + return 0; +} + +#include diff --git a/sysdeps/unix/sysv/linux/tst-pidfd_fork.c b/sysdeps/unix/sysv/linux/tst-pidfd_fork.c new file mode 100644 index 0000000000..3e09c55d54 --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-pidfd_fork.c @@ -0,0 +1,186 @@ +/* Basic tests for pidfd_fork. + Copyright (C) 2023 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 + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SIG_PID_EXIT_CODE 20 + +static bool atfork_prepare_var; +static bool atfork_parent_var; +static bool atfork_child_var; + +static void +atfork_prepare (void) +{ + atfork_prepare_var = true; +} + +static void +atfork_parent (void) +{ + atfork_parent_var = true; +} + +static void +atfork_child (void) +{ + atfork_child_var = true; +} + +static int +singlethread_test (unsigned int flags, bool wait_with_pid) +{ + const char testdata1[] = "abcdefghijklmnopqrtuvwxz"; + enum { testdatalen1 = array_length (testdata1) }; + const char testdata2[] = "01234567890"; + enum { testdatalen2 = array_length (testdata2) }; + + pid_t ppid = getpid (); + + int tempfd = create_temp_file ("tst-pidfd_fork", NULL); + + /* Check if the opened file is shared between process by read and write + some data on parent and child processes. */ + xwrite (tempfd, testdata1, testdatalen1); + off_t off = xlseek (tempfd, 0, SEEK_CUR); + TEST_COMPARE (off, testdatalen1); + + int pidfd; + pid_t pid = pidfd_fork (&pidfd, -1, flags); + TEST_VERIFY_EXIT (pid != -1); + + if (pid == 0) + { + if (flags & PIDFDFORK_ASYNCSAFE) + TEST_VERIFY (!atfork_child_var); + else + TEST_VERIFY (atfork_child_var); + + TEST_VERIFY_EXIT (getpid () != ppid); + TEST_COMPARE (getppid(), ppid); + + TEST_COMPARE (xlseek (tempfd, 0, SEEK_CUR), testdatalen1); + + xlseek (tempfd, 0, SEEK_SET); + char buf[testdatalen1]; + TEST_COMPARE (read (tempfd, buf, sizeof (buf)), testdatalen1); + TEST_COMPARE_BLOB (buf, testdatalen1, testdata1, testdatalen1); + + xlseek (tempfd, 0, SEEK_SET); + xwrite (tempfd, testdata2, testdatalen2); + + xclose (tempfd); + + _exit (EXIT_SUCCESS); + } + + { + siginfo_t sinfo; + if (wait_with_pid) + TEST_COMPARE (waitid (P_PID, pid, &sinfo, WEXITED), 0); + else + TEST_COMPARE (waitid (P_PIDFD, pidfd, &sinfo, WEXITED), 0); + TEST_COMPARE (sinfo.si_signo, SIGCHLD); + TEST_COMPARE (sinfo.si_code, CLD_EXITED); + TEST_COMPARE (sinfo.si_status, 0); + } + + TEST_COMPARE (xlseek (tempfd, 0, SEEK_CUR), testdatalen2); + + xlseek (tempfd, 0, SEEK_SET); + char buf[testdatalen2]; + TEST_COMPARE (read (tempfd, buf, sizeof (buf)), testdatalen2); + + TEST_COMPARE_BLOB (buf, testdatalen2, testdata2, testdatalen2); + + return 0; +} + +static int +do_test (void) +{ + /* Sanity check for pidfd support and check if passing NULL as the argument + make pidfd_fork acts as fork. */ + { + pid_t pid = pidfd_fork (NULL, -1, 0); + if (pid == -1 && errno == ENOSYS) + FAIL_UNSUPPORTED ("kernel does not support CLONE_PIDFD clone flag"); + TEST_VERIFY_EXIT (pid != -1); + if (pid == 0) + _exit (EXIT_SUCCESS); + + siginfo_t sinfo; + TEST_COMPARE (waitid (P_PID, pid, &sinfo, WEXITED), 0); + TEST_COMPARE (sinfo.si_signo, SIGCHLD); + TEST_COMPARE (sinfo.si_code, CLD_EXITED); + TEST_COMPARE (sinfo.si_status, 0); + } + + pthread_atfork (atfork_prepare, atfork_parent, atfork_child); + + /* With default flags, pidfd_fork acts as fork and run the pthread_atfork + handlers. */ + { + atfork_prepare_var = atfork_parent_var = atfork_child_var = false; + singlethread_test (0, false); + TEST_VERIFY (atfork_prepare_var); + TEST_VERIFY (atfork_parent_var); + TEST_VERIFY (!atfork_child_var); + } + + /* Same as before, but also wait using the PID instead of pidfd. */ + { + atfork_prepare_var = atfork_parent_var = atfork_child_var = false; + singlethread_test (0, true); + TEST_VERIFY (atfork_prepare_var); + TEST_VERIFY (atfork_parent_var); + TEST_VERIFY (!atfork_child_var); + } + + /* With PIDFDFORK_ASYNCSAFE, pidfd_fork acts as _Fork. */ + { + atfork_prepare_var = atfork_parent_var = atfork_child_var = false; + pthread_atfork (atfork_prepare, atfork_parent, atfork_child); + singlethread_test (PIDFDFORK_ASYNCSAFE, false); + TEST_VERIFY (!atfork_prepare_var); + TEST_VERIFY (!atfork_parent_var); + TEST_VERIFY (!atfork_child_var); + } + + { + atfork_prepare_var = atfork_parent_var = atfork_child_var = false; + pthread_atfork (atfork_prepare, atfork_parent, atfork_child); + singlethread_test (PIDFDFORK_ASYNCSAFE, true); + TEST_VERIFY (!atfork_prepare_var); + TEST_VERIFY (!atfork_parent_var); + TEST_VERIFY (!atfork_child_var); + } + + return 0; +} + +#include diff --git a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist index f7d5b23888..fa0ffd975f 100644 --- a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist +++ b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist @@ -2578,6 +2578,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist index ddc1f04eb1..cf4d2b2573 100644 --- a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist +++ b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist @@ -2684,6 +2684,7 @@ GLIBC_2.38 __strlcat_chk F GLIBC_2.38 __strlcpy_chk F GLIBC_2.38 __wcslcat_chk F GLIBC_2.38 __wcslcpy_chk F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.38 posix_spawnattr_getcgroup_np F