[v7,5/8] posix: Add pidfd_spawn and pidfd_spawnp (BZ 30349)

Message ID 20230803163558.991832-6-adhemerval.zanella@linaro.org
State Superseded
Headers
Series Add pidfd and cgroupv2 support for process creation |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 fail Testing failed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm fail Testing failed

Commit Message

Adhemerval Zanella Netto Aug. 3, 2023, 4:35 p.m. UTC
  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 the old racy interface base
on pid_t.

The implementation makes sure that kernel must support the complete
pidfd interface, meaning that waitid (P_PIDFD) should be supported
(added on Linux 5.4).  It ensures that non racy workaround is required
(such as reading procfs fdinfo pid to use along with wait interfaces).

These interfaces are similar to the posix_spawn and posix_spawnp, with
the only different diferent being it returns a process file descriptor
(int) instead of process ID (pid_t).  Their prototypes are:

  int pidfd_spawn (int *restrict pidfd,
 		   const char *restrict file,
  		   const posix_spawn_file_actions_t *restrict facts,
  		   const posix_spawnattr_t *restrict attrp,
  		   char *const argv[restrict],
  		   char *const envp[restrict])

  int pidfd_spawnp (int *restrict pidfd,
 		    const char *restrict path,
  		    const posix_spawn_file_actions_t *restrict facts,
  		    const posix_spawnattr_t *restrict attrp,
  		    char *const argv[restrict_arr],
  		    char *const envp[restrict_arr]);

A new symbol is used instead of a posix_spawn extension to avoid possible
issue with language bindings that might track the return argument
lifetime.  Although, on Linux pid_t and int are interchangeable, POSIX
only state that pid_t should be a signed interger.

Both symbols reuse the posix_spawn posix_spawn_file_actions_t and
posix_spawnattr_t, to void rehash posix_spawn API or add a new one.
It also mean that both interfaces support the same attribute and
file actions, and a new flag or file actions on posix_spawn is also
added automatically for pidfd_spawn.

Also, using posix_spawn plumbering allows to reuse most of the current
testing with some changes:

  - waitid is used instead of waitpid, since it is a more generic
    interface.

  - tst-posix_spawn-setsid.c is adapted to take in consideration that
    caller can check for session id directly.  The test now spawn itself
    and write the session id a file instead.

  - tst-spawn3.c need to know where pidfd_spawn is used so it keep
    an extra file description ununsed.

Checked on x86_64-linux-gnu on Linux 4.15 (no CLONE_PID or waitid
support), Linux 5.15 (only clone3 support), and Linux 5.19 (full
support).
---
 NEWS                                          |   7 +
 include/clone_internal.h                      |   4 +
 manual/process.texi                           |  14 +-
 posix/Makefile                                |   1 +
 posix/spawn_int.h                             |   3 +-
 posix/tst-posix_spawn-setsid.c                | 168 +++++++++++++-----
 posix/tst-spawn-chdir.c                       |  15 +-
 posix/tst-spawn.c                             |  24 +--
 posix/tst-spawn.h                             |  36 ++++
 posix/tst-spawn2.c                            |  17 +-
 posix/tst-spawn3.c                            | 100 ++++++-----
 posix/tst-spawn4.c                            |   7 +-
 posix/tst-spawn5.c                            |  14 +-
 posix/tst-spawn6.c                            |  15 +-
 posix/tst-spawn7.c                            |  13 +-
 sysdeps/unix/sysv/linux/Makefile              |  18 ++
 sysdeps/unix/sysv/linux/Versions              |   2 +
 sysdeps/unix/sysv/linux/aarch64/libc.abilist  |   2 +
 sysdeps/unix/sysv/linux/alpha/libc.abilist    |   2 +
 sysdeps/unix/sysv/linux/arc/libc.abilist      |   2 +
 sysdeps/unix/sysv/linux/arm/be/libc.abilist   |   2 +
 sysdeps/unix/sysv/linux/arm/le/libc.abilist   |   2 +
 sysdeps/unix/sysv/linux/bits/spawn_ext.h      |  31 ++++
 sysdeps/unix/sysv/linux/clone-pidfd-support.c |  58 ++++++
 sysdeps/unix/sysv/linux/csky/libc.abilist     |   2 +
 sysdeps/unix/sysv/linux/hppa/libc.abilist     |   2 +
 sysdeps/unix/sysv/linux/i386/libc.abilist     |   2 +
 sysdeps/unix/sysv/linux/ia64/libc.abilist     |   2 +
 .../sysv/linux/loongarch/lp64/libc.abilist    |   2 +
 .../sysv/linux/m68k/coldfire/libc.abilist     |   2 +
 .../unix/sysv/linux/m68k/m680x0/libc.abilist  |   2 +
 .../sysv/linux/microblaze/be/libc.abilist     |   2 +
 .../sysv/linux/microblaze/le/libc.abilist     |   2 +
 .../sysv/linux/mips/mips32/fpu/libc.abilist   |   2 +
 .../sysv/linux/mips/mips32/nofpu/libc.abilist |   2 +
 .../sysv/linux/mips/mips64/n32/libc.abilist   |   2 +
 .../sysv/linux/mips/mips64/n64/libc.abilist   |   2 +
 sysdeps/unix/sysv/linux/nios2/libc.abilist    |   2 +
 sysdeps/unix/sysv/linux/or1k/libc.abilist     |   2 +
 sysdeps/unix/sysv/linux/pidfd_spawn.c         |  30 ++++
 sysdeps/unix/sysv/linux/pidfd_spawnp.c        |  30 ++++
 .../linux/powerpc/powerpc32/fpu/libc.abilist  |   2 +
 .../powerpc/powerpc32/nofpu/libc.abilist      |   2 +
 .../linux/powerpc/powerpc64/be/libc.abilist   |   2 +
 .../linux/powerpc/powerpc64/le/libc.abilist   |   2 +
 .../unix/sysv/linux/riscv/rv32/libc.abilist   |   2 +
 .../unix/sysv/linux/riscv/rv64/libc.abilist   |   2 +
 .../unix/sysv/linux/s390/s390-32/libc.abilist |   2 +
 .../unix/sysv/linux/s390/s390-64/libc.abilist |   2 +
 sysdeps/unix/sysv/linux/sh/be/libc.abilist    |   2 +
 sysdeps/unix/sysv/linux/sh/le/libc.abilist    |   2 +
 .../sysv/linux/sparc/sparc32/libc.abilist     |   2 +
 .../sysv/linux/sparc/sparc64/libc.abilist     |   2 +
 sysdeps/unix/sysv/linux/spawni.c              |  20 ++-
 .../sysv/linux/tst-posix_spawn-setsid-pidfd.c |  20 +++
 .../unix/sysv/linux/tst-spawn-chdir-pidfd.c   |  20 +++
 sysdeps/unix/sysv/linux/tst-spawn-pidfd.c     |  20 +++
 sysdeps/unix/sysv/linux/tst-spawn-pidfd.h     |  63 +++++++
 sysdeps/unix/sysv/linux/tst-spawn2-pidfd.c    |  20 +++
 sysdeps/unix/sysv/linux/tst-spawn3-pidfd.c    |  20 +++
 sysdeps/unix/sysv/linux/tst-spawn4-pidfd.c    |  20 +++
 sysdeps/unix/sysv/linux/tst-spawn5-pidfd.c    |  20 +++
 sysdeps/unix/sysv/linux/tst-spawn6-pidfd.c    |  20 +++
 sysdeps/unix/sysv/linux/tst-spawn7-pidfd.c    |  20 +++
 .../unix/sysv/linux/x86_64/64/libc.abilist    |   2 +
 .../unix/sysv/linux/x86_64/x32/libc.abilist   |   2 +
 66 files changed, 787 insertions(+), 151 deletions(-)
 create mode 100644 posix/tst-spawn.h
 create mode 100644 sysdeps/unix/sysv/linux/clone-pidfd-support.c
 create mode 100644 sysdeps/unix/sysv/linux/pidfd_spawn.c
 create mode 100644 sysdeps/unix/sysv/linux/pidfd_spawnp.c
 create mode 100644 sysdeps/unix/sysv/linux/tst-posix_spawn-setsid-pidfd.c
 create mode 100644 sysdeps/unix/sysv/linux/tst-spawn-chdir-pidfd.c
 create mode 100644 sysdeps/unix/sysv/linux/tst-spawn-pidfd.c
 create mode 100644 sysdeps/unix/sysv/linux/tst-spawn-pidfd.h
 create mode 100644 sysdeps/unix/sysv/linux/tst-spawn2-pidfd.c
 create mode 100644 sysdeps/unix/sysv/linux/tst-spawn3-pidfd.c
 create mode 100644 sysdeps/unix/sysv/linux/tst-spawn4-pidfd.c
 create mode 100644 sysdeps/unix/sysv/linux/tst-spawn5-pidfd.c
 create mode 100644 sysdeps/unix/sysv/linux/tst-spawn6-pidfd.c
 create mode 100644 sysdeps/unix/sysv/linux/tst-spawn7-pidfd.c
  

Comments

Florian Weimer Aug. 11, 2023, 11:45 a.m. UTC | #1
* Adhemerval Zanella via Libc-alpha:

> diff --git a/NEWS b/NEWS
> index 99824eab95..ff41443896 100644
> --- a/NEWS
> +++ b/NEWS
> @@ -15,6 +15,13 @@ Major new features:
>    set the cgroupv2 in the new process in a race free manner.  These functions
>    are GNU extensions and require a kernel with clone3 support.
>  
> +* On Linux, the pidfd_spawn and pidfd_spawp functions have been added.
> +  They have similar prototype and semantic as posix_spawn, but instead of
> +  returning a process ID, they return a file descriptor that can be used
> +  along other pidfd functions (like pidfd_send_signal, poll, or waitid).
> +  The pidfd functionality avoid the issue of PID reuse with traditional
> +  posix_spawn interface.

“avoid[s]”

> diff --git a/posix/tst-posix_spawn-setsid.c b/posix/tst-posix_spawn-setsid.c
> index 124d878ce2..751674165c 100644
> --- a/posix/tst-posix_spawn-setsid.c
> +++ b/posix/tst-posix_spawn-setsid.c
> @@ -18,78 +18,158 @@

> +/* Called on process re-execution, write down the session id on PIDFILE.  */
> +_Noreturn static void
> +handle_restart (const char *pidfile)
> +{
> +  int pidfd = xopen (pidfile, O_WRONLY, 0);
> +
> +  char buf[INT_STRLEN_BOUND (pid_t)];
> +  int s = snprintf (buf, sizeof buf, "%d", getsid (0));
> +  size_t n = write (pidfd, buf, s);
> +  TEST_VERIFY (n == s);
> +
> +  xclose (pidfd);
> +
> +  exit (EXIT_SUCCESS);
> +}

I suspect this has an issue with hiding test failures (mapping not
shared after execve).
> diff --git a/posix/tst-spawn3.c b/posix/tst-spawn3.c
> index e7ce0fb386..bd21ac6c4b 100644
> --- a/posix/tst-spawn3.c
> +++ b/posix/tst-spawn3.c
> @@ -16,6 +16,7 @@

> +  char buf[INT_STRLEN_BOUND (pid_t)];

This should be INT_BUFSIZE_BOUND.

> diff --git a/posix/tst-spawn6.c b/posix/tst-spawn6.c
> index 4e29d78168..ff36351cd6 100644
> --- a/posix/tst-spawn6.c
> +++ b/posix/tst-spawn6.c

> @@ -202,7 +201,7 @@ do_test (int argc, char *argv[])
>    if (restart)
>      return handle_restart (argv[1], argv[2]);
>  
> -  pid_t pid = xfork ();
> +  PID_T_TYPE pid = xfork ();
>    if (pid == 0)
>      {
>        /* Create a pseudo-terminal to avoid interfering with the one using by

I think the result of xfork remains pid_t, so that switch seems wrong?


> diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions
> index 6d8a67039e..bd96ad12ad 100644
> --- a/sysdeps/unix/sysv/linux/Versions
> +++ b/sysdeps/unix/sysv/linux/Versions
> @@ -324,6 +324,8 @@ libc {
>    GLIBC_2.39 {
>      posix_spawnattr_getcgroup_np;
>      posix_spawnattr_setcgroup_np;
> +    pidfd_spawn;
> +    pidfd_spawnp;
>    }

I'd prefer to maintain lexicographic order.

> diff --git a/sysdeps/unix/sysv/linux/bits/spawn_ext.h b/sysdeps/unix/sysv/linux/bits/spawn_ext.h
> index 3bc10ab477..ff8550f264 100644
> --- a/sysdeps/unix/sysv/linux/bits/spawn_ext.h
> +++ b/sysdeps/unix/sysv/linux/bits/spawn_ext.h
> @@ -37,4 +37,35 @@ extern int posix_spawnattr_setcgroup_np
>  (posix_spawnattr_t
> +/* Spawn a new process executing PATH with the attributes describes in *ATTRP.
> +   Before running the process perform the actions described in FACTS.  Return
> +   a PID file descriptor in PIDFD if process creation was successful and the
> +   argument is non-null.
> +
> +   This function is a possible cancellation point and therefore not
> +   marked with __THROW. */

Missing space after .

> +extern int pidfd_spawn (int *__restrict __pidfd,
> +			const char *__restrict __path,
> +			const posix_spawn_file_actions_t *__restrict __facts,
> +			const posix_spawnattr_t *__restrict __attrp,
> +			char *const __argv[__restrict_arr],
> +			char *const __envp[__restrict_arr])
> +    __nonnull ((2, 5));
> +
> +/* Similar to `pidfd_spawn' but search for FILE in the PATH.
> +
> +   This function is a possible cancellation point and therefore not
> +   marked with __THROW. */

Missing space after .

> +extern int pidfd_spawnp (int *__restrict __pidfd,
> +			 const char *__restrict __file,
> +			 const posix_spawn_file_actions_t *__restrict __facts,
> +			 const posix_spawnattr_t *__restrict __attrp,
> +			 char *const __argv[__restrict_arr],
> +			 char *const __envp[__restrict_arr])
> +    __nonnull ((2, 5));

I think we should mark PIDFD as nonnull.  If the caller ignores the
descriptor, it just leaks.  In that case, the caller should use the
non-descriptor variant of posix_spawn.

> diff --git a/sysdeps/unix/sysv/linux/clone-pidfd-support.c b/sysdeps/unix/sysv/linux/clone-pidfd-support.c
> new file mode 100644
> index 0000000000..af2d213cc5
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/clone-pidfd-support.c

> +bool
> +__clone_pidfd_supported (void)
> +{
> +  static int supported = 0;

I suggest to make this a file-level static with a non-colliding name,
this way it's easier to print its value with a debugger.

> +  int state = atomic_load_relaxed (&supported);
> +  if (state == 0)
> +    {

> +    }
> +
> +  return state == 1;

“return state > 0;” is probably more efficient.

> index f0d4c62ae6..844abf1b0b 100644
> --- a/sysdeps/unix/sysv/linux/spawni.c
> +++ b/sysdeps/unix/sysv/linux/spawni.c

> @@ -319,6 +320,15 @@ __spawnix (pid_t * pid, const char *file,
>    struct posix_spawn_args args;
>    int ec;
>  
> +  bool use_pidfd = xflags & SPAWN_XFLAGS_RET_PIDFD;
> +
> +  /* For CLONE_PIDFD, older kernels might not fail with unsupported flags or
> +     some versions might not support waitid (P_PIDFD).  So to avoid the need
> +     to handle the error on the helper process, check for full pidfd
> +     support.  */
> +  if (use_pidfd && !__clone_pidfd_supported ())
> +    return ENOSYS;

Why not EOPNOTSUPP?  I think ENOSYS can be justified because the pidfd
functions are a separate family of functions, and not a sub-operation
that is failing,  Maybe add this to the comment?

Thanks,
Florian
  
Adhemerval Zanella Netto Aug. 11, 2023, 4:14 p.m. UTC | #2
On 11/08/23 08:45, Florian Weimer wrote:
> * Adhemerval Zanella via Libc-alpha:
> 
>> diff --git a/NEWS b/NEWS
>> index 99824eab95..ff41443896 100644
>> --- a/NEWS
>> +++ b/NEWS
>> @@ -15,6 +15,13 @@ Major new features:
>>    set the cgroupv2 in the new process in a race free manner.  These functions
>>    are GNU extensions and require a kernel with clone3 support.
>>  
>> +* On Linux, the pidfd_spawn and pidfd_spawp functions have been added.
>> +  They have similar prototype and semantic as posix_spawn, but instead of
>> +  returning a process ID, they return a file descriptor that can be used
>> +  along other pidfd functions (like pidfd_send_signal, poll, or waitid).
>> +  The pidfd functionality avoid the issue of PID reuse with traditional
>> +  posix_spawn interface.
> 
> “avoid[s]”

Ack.

> 
>> diff --git a/posix/tst-posix_spawn-setsid.c b/posix/tst-posix_spawn-setsid.c
>> index 124d878ce2..751674165c 100644
>> --- a/posix/tst-posix_spawn-setsid.c
>> +++ b/posix/tst-posix_spawn-setsid.c
>> @@ -18,78 +18,158 @@
> 
>> +/* Called on process re-execution, write down the session id on PIDFILE.  */
>> +_Noreturn static void
>> +handle_restart (const char *pidfile)
>> +{
>> +  int pidfd = xopen (pidfile, O_WRONLY, 0);
>> +
>> +  char buf[INT_STRLEN_BOUND (pid_t)];
>> +  int s = snprintf (buf, sizeof buf, "%d", getsid (0));
>> +  size_t n = write (pidfd, buf, s);
>> +  TEST_VERIFY (n == s);
>> +
>> +  xclose (pidfd);
>> +
>> +  exit (EXIT_SUCCESS);
>> +}
> 

Indeed, I removed the exit.

> I suspect this has an issue with hiding test failures (mapping not
> shared after execve).
>> diff --git a/posix/tst-spawn3.c b/posix/tst-spawn3.c
>> index e7ce0fb386..bd21ac6c4b 100644
>> --- a/posix/tst-spawn3.c
>> +++ b/posix/tst-spawn3.c
>> @@ -16,6 +16,7 @@
> 
>> +  char buf[INT_STRLEN_BOUND (pid_t)];
> 
> This should be INT_BUFSIZE_BOUND.

Ack.

> 
>> diff --git a/posix/tst-spawn6.c b/posix/tst-spawn6.c
>> index 4e29d78168..ff36351cd6 100644
>> --- a/posix/tst-spawn6.c
>> +++ b/posix/tst-spawn6.c
> 
>> @@ -202,7 +201,7 @@ do_test (int argc, char *argv[])
>>    if (restart)
>>      return handle_restart (argv[1], argv[2]);
>>  
>> -  pid_t pid = xfork ();
>> +  PID_T_TYPE pid = xfork ();
>>    if (pid == 0)
>>      {
>>        /* Create a pseudo-terminal to avoid interfering with the one using by
> 
> I think the result of xfork remains pid_t, so that switch seems wrong?
> 

Indeed, I reverted that.

> 
>> diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions
>> index 6d8a67039e..bd96ad12ad 100644
>> --- a/sysdeps/unix/sysv/linux/Versions
>> +++ b/sysdeps/unix/sysv/linux/Versions
>> @@ -324,6 +324,8 @@ libc {
>>    GLIBC_2.39 {
>>      posix_spawnattr_getcgroup_np;
>>      posix_spawnattr_setcgroup_np;
>> +    pidfd_spawn;
>> +    pidfd_spawnp;
>>    }
> 
> I'd prefer to maintain lexicographic order.

Ack.

> 
>> diff --git a/sysdeps/unix/sysv/linux/bits/spawn_ext.h b/sysdeps/unix/sysv/linux/bits/spawn_ext.h
>> index 3bc10ab477..ff8550f264 100644
>> --- a/sysdeps/unix/sysv/linux/bits/spawn_ext.h
>> +++ b/sysdeps/unix/sysv/linux/bits/spawn_ext.h
>> @@ -37,4 +37,35 @@ extern int posix_spawnattr_setcgroup_np
>>  (posix_spawnattr_t
>> +/* Spawn a new process executing PATH with the attributes describes in *ATTRP.
>> +   Before running the process perform the actions described in FACTS.  Return
>> +   a PID file descriptor in PIDFD if process creation was successful and the
>> +   argument is non-null.
>> +
>> +   This function is a possible cancellation point and therefore not
>> +   marked with __THROW. */
> 
> Missing space after .

Ack.

> 
>> +extern int pidfd_spawn (int *__restrict __pidfd,
>> +			const char *__restrict __path,
>> +			const posix_spawn_file_actions_t *__restrict __facts,
>> +			const posix_spawnattr_t *__restrict __attrp,
>> +			char *const __argv[__restrict_arr],
>> +			char *const __envp[__restrict_arr])
>> +    __nonnull ((2, 5));
>> +
>> +/* Similar to `pidfd_spawn' but search for FILE in the PATH.
>> +
>> +   This function is a possible cancellation point and therefore not
>> +   marked with __THROW. */
> 
> Missing space after .

Ack.

> 
>> +extern int pidfd_spawnp (int *__restrict __pidfd,
>> +			 const char *__restrict __file,
>> +			 const posix_spawn_file_actions_t *__restrict __facts,
>> +			 const posix_spawnattr_t *__restrict __attrp,
>> +			 char *const __argv[__restrict_arr],
>> +			 char *const __envp[__restrict_arr])
>> +    __nonnull ((2, 5));
> 
> I think we should mark PIDFD as nonnull.  If the caller ignores the
> descriptor, it just leaks.  In that case, the caller should use the
> non-descriptor variant of posix_spawn.

Sounds reasonable.  Another options would to just close the file descriptor
if clone is successful and pidfd is NULL.

> 
>> diff --git a/sysdeps/unix/sysv/linux/clone-pidfd-support.c b/sysdeps/unix/sysv/linux/clone-pidfd-support.c
>> new file mode 100644
>> index 0000000000..af2d213cc5
>> --- /dev/null
>> +++ b/sysdeps/unix/sysv/linux/clone-pidfd-support.c
> 
>> +bool
>> +__clone_pidfd_supported (void)
>> +{
>> +  static int supported = 0;
> 
> I suggest to make this a file-level static with a non-colliding name,
> this way it's easier to print its value with a debugger.

Ack.

> 
>> +  int state = atomic_load_relaxed (&supported);
>> +  if (state == 0)
>> +    {
> 
>> +    }
>> +
>> +  return state == 1;
> 
> “return state > 0;” is probably more efficient.
> 

Ack.

>> index f0d4c62ae6..844abf1b0b 100644
>> --- a/sysdeps/unix/sysv/linux/spawni.c
>> +++ b/sysdeps/unix/sysv/linux/spawni.c
> 
>> @@ -319,6 +320,15 @@ __spawnix (pid_t * pid, const char *file,
>>    struct posix_spawn_args args;
>>    int ec;
>>  
>> +  bool use_pidfd = xflags & SPAWN_XFLAGS_RET_PIDFD;
>> +
>> +  /* For CLONE_PIDFD, older kernels might not fail with unsupported flags or
>> +     some versions might not support waitid (P_PIDFD).  So to avoid the need
>> +     to handle the error on the helper process, check for full pidfd
>> +     support.  */
>> +  if (use_pidfd && !__clone_pidfd_supported ())
>> +    return ENOSYS;
> 
> Why not EOPNOTSUPP?  I think ENOSYS can be justified because the pidfd
> functions are a separate family of functions, and not a sub-operation
> that is failing,  Maybe add this to the comment?

I think ENOSYS fits better here, since the ideia is that if pidfd waitid
is not supported there posix_spawn can not be used regardless of its
arguments.  This is different than posix_spawn with cgroupv2, that
depends where the kernel supports clone with CLONE_INTO_CGROUP.

I have added a comment about it.
  

Patch

diff --git a/NEWS b/NEWS
index 99824eab95..ff41443896 100644
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,13 @@  Major new features:
   set the cgroupv2 in the new process in a race free manner.  These functions
   are GNU extensions and require a kernel with clone3 support.
 
+* On Linux, the pidfd_spawn and pidfd_spawp functions have been added.
+  They have similar prototype and semantic as posix_spawn, but instead of
+  returning a process ID, they return a file descriptor that can be used
+  along other pidfd functions (like pidfd_send_signal, poll, or waitid).
+  The pidfd functionality avoid the issue of PID reuse with traditional
+  posix_spawn interface.
+
 Deprecated and removed features, and other changes affecting compatibility:
 
   [Add deprecations, removals and changes affecting compatibility here]
diff --git a/include/clone_internal.h b/include/clone_internal.h
index ad7b170f58..567160ebb5 100644
--- a/include/clone_internal.h
+++ b/include/clone_internal.h
@@ -35,6 +35,10 @@  extern int __clone_internal_fallback (struct clone_args *__cl_args,
 				      void *__arg)
      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;
+
 #ifndef _ISOMAC
 libc_hidden_proto (__clone3)
 libc_hidden_proto (__clone_internal)
diff --git a/manual/process.texi b/manual/process.texi
index c8413a5a58..68361c3f61 100644
--- a/manual/process.texi
+++ b/manual/process.texi
@@ -136,13 +136,13 @@  creating a process and making it run another program.
 @cindex parent process
 @cindex subprocess
 A new processes is created when one of the functions
-@code{posix_spawn}, @code{fork}, @code{_Fork} or @code{vfork} 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{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}.
 
 After forking a child process, both the parent and child processes
 continue to execute normally.  If you want your program to wait for a
diff --git a/posix/Makefile b/posix/Makefile
index 70faad4b63..905cf9fb54 100644
--- a/posix/Makefile
+++ b/posix/Makefile
@@ -602,6 +602,7 @@  tst-spawn-static-ARGS = $(tst-spawn-ARGS)
 tst-spawn5-ARGS = -- $(host-test-program-cmd)
 tst-spawn6-ARGS = -- $(host-test-program-cmd)
 tst-spawn7-ARGS = -- $(host-test-program-cmd)
+tst-posix_spawn-setsid-ARGS = -- $(host-test-program-cmd)
 tst-dir-ARGS = `pwd` `cd $(common-objdir)/$(subdir); pwd` `cd $(common-objdir); pwd` $(objpfx)tst-dir
 tst-chmod-ARGS = $(objdir)
 tst-vfork3-ARGS = --test-dir=$(objpfx)
diff --git a/posix/spawn_int.h b/posix/spawn_int.h
index aeb066c44f..64ee03e62d 100644
--- a/posix/spawn_int.h
+++ b/posix/spawn_int.h
@@ -76,12 +76,13 @@  struct __spawn_action
 
 #define SPAWN_XFLAGS_USE_PATH	0x1
 #define SPAWN_XFLAGS_TRY_SHELL	0x2
+#define SPAWN_XFLAGS_RET_PIDFD  0x4
 
 extern int __posix_spawn_file_actions_realloc (posix_spawn_file_actions_t *
 					       file_actions)
      attribute_hidden;
 
-extern int __spawni (pid_t *pid, const char *path,
+extern int __spawni (int *pid, const char *path,
 		     const posix_spawn_file_actions_t *file_actions,
 		     const posix_spawnattr_t *attrp, char *const argv[],
 		     char *const envp[], int xflags) attribute_hidden;
diff --git a/posix/tst-posix_spawn-setsid.c b/posix/tst-posix_spawn-setsid.c
index 124d878ce2..751674165c 100644
--- a/posix/tst-posix_spawn-setsid.c
+++ b/posix/tst-posix_spawn-setsid.c
@@ -18,78 +18,158 @@ 
 
 #include <errno.h>
 #include <fcntl.h>
+#include <getopt.h>
+#include <intprops.h>
+#include <paths.h>
 #include <spawn.h>
 #include <stdbool.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <sys/resource.h>
+#include <sys/wait.h>
 #include <unistd.h>
 
 #include <support/check.h>
+#include <support/xunistd.h>
+#include <support/temp_file.h>
+#include <tst-spawn.h>
+
+/* Nonzero if the program gets called via `exec'.  */
+static int restart;
+
+/* Hold the four initial argument used to respawn the process, plus
+   the extra '--direct' and '--restart', and a final NULL.  */
+static char *initial_argv[7];
+static int initial_argv_count;
+
+#define CMDLINE_OPTIONS \
+  { "restart", no_argument, &restart, 1 },
+
+static char *pidfile;
+
+static pid_t
+read_child_sid (void)
+{
+  int pidfd = xopen (pidfile, O_RDONLY, 0);
+
+  char buf[INT_STRLEN_BOUND (pid_t)];
+  ssize_t n = read (pidfd, buf, sizeof (buf));
+  TEST_VERIFY (n < sizeof buf && n >= 0);
+  buf[n] = '\0';
+
+  /* We only expect to read the PID.  */
+  char *endp;
+  long int rpid = strtol (buf, &endp, 10);
+  TEST_VERIFY (endp != buf);
+
+  xclose (pidfd);
+
+  return rpid;
+}
+
+/* Called on process re-execution, write down the session id on PIDFILE.  */
+_Noreturn static void
+handle_restart (const char *pidfile)
+{
+  int pidfd = xopen (pidfile, O_WRONLY, 0);
+
+  char buf[INT_STRLEN_BOUND (pid_t)];
+  int s = snprintf (buf, sizeof buf, "%d", getsid (0));
+  size_t n = write (pidfd, buf, s);
+  TEST_VERIFY (n == s);
+
+  xclose (pidfd);
+
+  exit (EXIT_SUCCESS);
+}
 
 static void
 do_test_setsid (bool test_setsid)
 {
-  pid_t sid, child_sid;
-  int res;
-
   /* Current session ID.  */
-  sid = getsid(0);
-  if (sid == (pid_t) -1)
-    FAIL_EXIT1 ("getsid (0): %m");
+  pid_t sid = getsid (0);
+  TEST_VERIFY (sid != (pid_t) -1);
 
   posix_spawnattr_t attrp;
-  /* posix_spawnattr_init should not fail (it basically memset the
-     attribute).  */
-  posix_spawnattr_init (&attrp);
+  TEST_COMPARE (posix_spawnattr_init (&attrp), 0);
   if (test_setsid)
-    {
-      res = posix_spawnattr_setflags (&attrp, POSIX_SPAWN_SETSID);
-      if (res != 0)
-	{
-	  errno = res;
-	  FAIL_EXIT1 ("posix_spawnattr_setflags: %m");
-	}
-    }
-
-  /* Program to run.  */
-  char *args[2] = { (char *) "true", NULL };
-  pid_t child;
-
-  res = posix_spawnp (&child, "true", NULL, &attrp, args, environ);
-  /* posix_spawnattr_destroy is noop.  */
-  posix_spawnattr_destroy (&attrp);
-
-  if (res != 0)
-    {
-      errno = res;
-      FAIL_EXIT1 ("posix_spawnp: %m");
-    }
+    TEST_COMPARE (posix_spawnattr_setflags (&attrp, POSIX_SPAWN_SETSID), 0);
+
+  /* 1 or 4 elements from initial_argv:
+       + path to ld.so          optional
+       + --library-path         optional
+       + the library path       optional
+       + application name
+       + --direct
+       + --restart
+       + pidfile  */
+  int argv_size = initial_argv_count + 2;
+  char *args[argv_size];
+  int argc = 0;
+
+  for (char **arg = initial_argv; *arg != NULL; arg++)
+    args[argc++] = *arg;
+  args[argc++] = pidfile;
+  args[argc] = NULL;
+  TEST_VERIFY (argc < argv_size);
+
+  PID_T_TYPE pid;
+  TEST_COMPARE (POSIX_SPAWN (&pid, args[0], NULL, &attrp, args, environ), 0);
+  TEST_COMPARE (posix_spawnattr_destroy (&attrp), 0);
+
+  siginfo_t sinfo;
+  TEST_COMPARE (WAITID (P_PID, pid, &sinfo, WEXITED), 0);
+  TEST_COMPARE (sinfo.si_code, CLD_EXITED);
+  TEST_COMPARE (sinfo.si_status, 0);
+
+  pid_t child_sid = read_child_sid ();
 
   /* Child should have a different session ID than parent.  */
-  child_sid = getsid (child);
-
-  if (child_sid == (pid_t) -1)
-    FAIL_EXIT1 ("getsid (%i): %m", child);
+  TEST_VERIFY (child_sid != (pid_t) -1);
 
   if (test_setsid)
-    {
-      if (child_sid == sid)
-	FAIL_EXIT1 ("child session ID matched parent one");
-    }
+    TEST_VERIFY (child_sid != sid);
   else
-    {
-      if (child_sid != sid)
-	FAIL_EXIT1 ("child session ID did not match parent one");
-    }
+    TEST_VERIFY (child_sid == sid);
 }
 
 static int
-do_test (void)
+do_test (int argc, char *argv[])
 {
+  /* We must have either:
+
+     - one or four parameters if called initially:
+       + argv[1]: path for ld.so        optional
+       + argv[2]: "--library-path"      optional
+       + argv[3]: the library path      optional
+       + argv[4]: the application name
+
+     - six parameters left if called through re-execution:
+       + argv[5/1]: the application name
+       + argv[6/2]: the pidfile
+
+     * When built with --enable-hardcoded-path-in-tests or issued without
+       using the loader directly.  */
+
+  if (restart)
+    handle_restart (argv[1]);
+
+  TEST_VERIFY_EXIT (argc == 2 || argc == 5);
+
+  int i;
+  for (i = 0; i < argc - 1; i++)
+    initial_argv[i] = argv[i + 1];
+  initial_argv[i++] = (char *) "--direct";
+  initial_argv[i++] = (char *) "--restart";
+  initial_argv_count = i;
+
+  create_temp_file ("tst-posix_spawn-setsid-", &pidfile);
+
   do_test_setsid (false);
   do_test_setsid (true);
 
   return 0;
 }
 
+#define TEST_FUNCTION_ARGV do_test
 #include <support/test-driver.c>
diff --git a/posix/tst-spawn-chdir.c b/posix/tst-spawn-chdir.c
index b335092d7f..c01ca6692d 100644
--- a/posix/tst-spawn-chdir.c
+++ b/posix/tst-spawn-chdir.c
@@ -29,7 +29,9 @@ 
 #include <support/test-driver.h>
 #include <support/xstdio.h>
 #include <support/xunistd.h>
+#include <sys/wait.h>
 #include <unistd.h>
+#include <tst-spawn.h>
 
 /* Reads the file at PATH, which must consist of exactly one line.
    Removes the line terminator at the end of the file.  */
@@ -169,17 +171,18 @@  do_test (void)
 
           char *const argv[] = { (char *) "pwd", NULL };
           char *const envp[] = { NULL } ;
-          pid_t pid;
+          PID_T_TYPE pid;
           if (do_spawnp)
-            TEST_COMPARE (posix_spawnp (&pid, "pwd", &actions,
+            TEST_COMPARE (POSIX_SPAWNP (&pid, "pwd", &actions,
                                         NULL, argv, envp), 0);
           else
-            TEST_COMPARE (posix_spawn (&pid, "subdir/pwd-symlink", &actions,
+            TEST_COMPARE (POSIX_SPAWN (&pid, "subdir/pwd-symlink", &actions,
                                        NULL, argv, envp), 0);
           TEST_VERIFY (pid > 0);
-          int status;
-          xwaitpid (pid, &status, 0);
-          TEST_COMPARE (status, 0);
+          siginfo_t sinfo;
+          TEST_COMPARE (WAITID (P_ALL, 0, &sinfo, WEXITED), 0);
+          TEST_COMPARE (sinfo.si_code, CLD_EXITED);
+          TEST_COMPARE (sinfo.si_status, 0);
 
           /* Check that the current directory did not change.  */
           {
diff --git a/posix/tst-spawn.c b/posix/tst-spawn.c
index 6782a322fc..c44d90756a 100644
--- a/posix/tst-spawn.c
+++ b/posix/tst-spawn.c
@@ -25,11 +25,13 @@ 
 #include <stdlib.h>
 #include <string.h>
 #include <sys/param.h>
+#include <sys/wait.h>
 
 #include <support/check.h>
 #include <support/xunistd.h>
 #include <support/temp_file.h>
 #include <support/support.h>
+#include <tst-spawn.h>
 
 
 /* Nonzero if the program gets called via `exec'.  */
@@ -143,9 +145,9 @@  handle_restart (const char *fd1s, const char *fd2s, const char *fd3s,
 static int
 do_test (int argc, char *argv[])
 {
-  pid_t pid;
+  PID_T_TYPE pid;
   int fd4;
-  int status;
+  siginfo_t sinfo;
   posix_spawn_file_actions_t actions;
   char fd1name[18];
   char fd2name[18];
@@ -233,17 +235,16 @@  do_test (int argc, char *argv[])
   spargv[i++] = fd5name;
   spargv[i] = NULL;
 
-  TEST_COMPARE (posix_spawn (&pid, argv[1], &actions, NULL, spargv, environ),
+  TEST_COMPARE (POSIX_SPAWN (&pid, argv[1], &actions, NULL, spargv, environ),
 		0);
 
   /* Wait for the children.  */
-  TEST_COMPARE (xwaitpid (pid, &status, 0), pid);
-  TEST_VERIFY (WIFEXITED (status));
-  TEST_VERIFY (!WIFSIGNALED (status));
-  TEST_COMPARE (WEXITSTATUS (status), 0);
+  TEST_COMPARE (WAITID (P_PID, pid, &sinfo, WEXITED), 0);
+  TEST_COMPARE (sinfo.si_code, CLD_EXITED);
+  TEST_COMPARE (sinfo.si_status, 0);
 
   /* Same test but with a NULL pid argument.  */
-  TEST_COMPARE (posix_spawn (NULL, argv[1], &actions, NULL, spargv, environ),
+  TEST_COMPARE (POSIX_SPAWN (NULL, argv[1], &actions, NULL, spargv, environ),
 		0);
 
   /* Cleanup.  */
@@ -251,10 +252,9 @@  do_test (int argc, char *argv[])
   free (name3_copy);
 
   /* Wait for the children.  */
-  xwaitpid (-1, &status, 0);
-  TEST_VERIFY (WIFEXITED (status));
-  TEST_VERIFY (!WIFSIGNALED (status));
-  TEST_COMPARE (WEXITSTATUS (status), 0);
+  TEST_COMPARE (WAITID (P_ALL, 0, &sinfo, WEXITED), 0);
+  TEST_COMPARE (sinfo.si_code, CLD_EXITED);
+  TEST_COMPARE (sinfo.si_status, 0);
 
   return 0;
 }
diff --git a/posix/tst-spawn.h b/posix/tst-spawn.h
new file mode 100644
index 0000000000..a6f2dc8680
--- /dev/null
+++ b/posix/tst-spawn.h
@@ -0,0 +1,36 @@ 
+/* Generic definitions for posix_spawn tests.
+   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
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef PID_T_TYPE
+# define PID_T_TYPE pid_t
+#endif
+
+#ifndef POSIX_SPAWN
+# define POSIX_SPAWN(__child, __path, __actions, __attr, __argv, __envp) \
+  posix_spawn (__child, __path, __actions, __attr, __argv, __envp)
+#endif
+
+#ifndef POSIX_SPAWNP
+# define POSIX_SPAWNP(__child, __path, __actions, __attr, __argv, __envp) \
+  posix_spawnp (__child, __path, __actions, __attr, __argv, __envp)
+#endif
+
+#ifndef WAITID
+# define WAITID(__idtype, __id, __info, __opts) \
+  waitid (__idtype, __id, __info, __opts)
+#endif
diff --git a/posix/tst-spawn2.c b/posix/tst-spawn2.c
index 40dc692488..f5c1f13039 100644
--- a/posix/tst-spawn2.c
+++ b/posix/tst-spawn2.c
@@ -26,6 +26,7 @@ 
 #include <stdio.h>
 
 #include <support/check.h>
+#include <tst-spawn.h>
 
 int
 do_test (void)
@@ -35,9 +36,9 @@  do_test (void)
 
   const char *program = "/path/to/invalid/binary";
   char * const args[] = { 0 };
-  pid_t pid = -1;
+  PID_T_TYPE pid = -1;
 
-  int ret = posix_spawn (&pid, program, 0, 0, args, environ);
+  int ret = POSIX_SPAWN (&pid, program, 0, 0, args, environ);
   if (ret != ENOENT)
     {
       errno = ret;
@@ -51,14 +52,13 @@  do_test (void)
     FAIL_EXIT1 ("posix_spawn returned pid != -1 (%i)", (int) pid);
 
   /* Check if no child is actually created.  */
-  ret = waitpid (-1, NULL, 0);
-  if (ret != -1 || errno != ECHILD)
-    FAIL_EXIT1 ("waitpid: %m)");
+  TEST_COMPARE (WAITID (P_ALL, 0, NULL, WEXITED), -1);
+  TEST_COMPARE (errno, ECHILD);
 
   /* Same as before, but with posix_spawnp.  */
   char *args2[] = { (char*) program, 0 };
 
-  ret = posix_spawnp (&pid, args2[0], 0, 0, args2, environ);
+  ret = POSIX_SPAWNP (&pid, args2[0], 0, 0, args2, environ);
   if (ret != ENOENT)
     {
       errno = ret;
@@ -68,9 +68,8 @@  do_test (void)
   if (pid != -1)
     FAIL_EXIT1 ("posix_spawnp returned pid != -1 (%i)", (int) pid);
 
-  ret = waitpid (-1, NULL, 0);
-  if (ret != -1 || errno != ECHILD)
-    FAIL_EXIT1 ("waitpid: %m)");
+  TEST_COMPARE (WAITID (P_ALL, 0, NULL, WEXITED), -1);
+  TEST_COMPARE (errno, ECHILD);
 
   return 0;
 }
diff --git a/posix/tst-spawn3.c b/posix/tst-spawn3.c
index e7ce0fb386..bd21ac6c4b 100644
--- a/posix/tst-spawn3.c
+++ b/posix/tst-spawn3.c
@@ -16,6 +16,7 @@ 
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <assert.h>
 #include <stdio.h>
 #include <spawn.h>
 #include <error.h>
@@ -27,9 +28,12 @@ 
 #include <sys/resource.h>
 #include <fcntl.h>
 #include <paths.h>
+#include <intprops.h>
 
 #include <support/check.h>
 #include <support/temp_file.h>
+#include <support/xunistd.h>
+#include <tst-spawn.h>
 
 static int
 do_test (void)
@@ -48,7 +52,6 @@  do_test (void)
 
   struct rlimit rl;
   int max_fd = 24;
-  int ret;
 
   /* Set maximum number of file descriptor to a low value to avoid open
      too many files in environments where RLIMIT_NOFILE is large and to
@@ -66,7 +69,7 @@  do_test (void)
   /* Exhauste the file descriptor limit with temporary files.  */
   int files[max_fd];
   int nfiles = 0;
-  for (;;)
+  for (; nfiles < max_fd; nfiles++)
     {
       int fd = create_temp_file ("tst-spawn3.", NULL);
       if (fd == -1)
@@ -75,75 +78,82 @@  do_test (void)
 	    FAIL_EXIT1 ("create_temp_file: %m");
 	  break;
 	}
-      files[nfiles++] = fd;
+      files[nfiles] = fd;
     }
+  assert (nfiles != 0);
 
   posix_spawn_file_actions_t a;
-  if (posix_spawn_file_actions_init (&a) != 0)
-    FAIL_EXIT1 ("posix_spawn_file_actions_init");
+  TEST_COMPARE (posix_spawn_file_actions_init (&a), 0);
 
   /* Executes a /bin/sh echo $$ 2>&1 > ${objpfx}tst-spawn3.pid .  */
   const char pidfile[] = OBJPFX "tst-spawn3.pid";
-  if (posix_spawn_file_actions_addopen (&a, STDOUT_FILENO, pidfile, O_WRONLY
-					| O_CREAT | O_TRUNC, 0644) != 0)
-    FAIL_EXIT1 ("posix_spawn_file_actions_addopen");
+  TEST_COMPARE (posix_spawn_file_actions_addopen (&a, STDOUT_FILENO, pidfile,
+						  O_WRONLY| O_CREAT | O_TRUNC,
+						  0644),
+		0);
 
-  if (posix_spawn_file_actions_adddup2 (&a, STDOUT_FILENO, STDERR_FILENO) != 0)
-    FAIL_EXIT1 ("posix_spawn_file_actions_adddup2");
+  TEST_COMPARE (posix_spawn_file_actions_adddup2 (&a, STDOUT_FILENO,
+						  STDERR_FILENO),
+		0);
 
   /* Since execve (called by posix_spawn) might require to open files to
      actually execute the shell script, setup to close the temporary file
      descriptors.  */
-  for (int i=0; i<nfiles; i++)
-    {
-      if (posix_spawn_file_actions_addclose (&a, files[i]))
-	FAIL_EXIT1 ("posix_spawn_file_actions_addclose");
-    }
+  int maxnfiles =
+#ifdef TST_SPAWN_PIDFD
+    /* The sparing file descriptor will be returned as the pid descriptor,
+       otherwise clone fail with EMFILE.  */
+    nfiles - 1;
+#else
+    nfiles;
+#endif
+
+  for (int i=0; i<maxnfiles; i++)
+    TEST_COMPARE (posix_spawn_file_actions_addclose (&a, files[i]), 0);
 
   char *spawn_argv[] = { (char *) _PATH_BSHELL, (char *) "-c",
 			 (char *) "echo $$", NULL };
-  pid_t pid;
-  if ((ret = posix_spawn (&pid, _PATH_BSHELL, &a, NULL, spawn_argv, NULL))
-       != 0)
-    {
-      errno = ret;
-      FAIL_EXIT1 ("posix_spawn: %m");
-    }
-
-  int status;
-  int err = waitpid (pid, &status, 0);
-  if (err != pid)
-    FAIL_EXIT1 ("waitpid: %m");
+  PID_T_TYPE pid;
+
+  {
+    int r = POSIX_SPAWN (&pid, _PATH_BSHELL, &a, NULL, spawn_argv, NULL);
+    if (r == ENOSYS)
+      FAIL_UNSUPPORTED ("kernel does not support CLONE_PIDFD clone flag");
+#ifdef TST_SPAWN_PIDFD
+    TEST_COMPARE (r, EMFILE);
+
+    /* Free up one file descriptor, so posix_spawn_pidfd_ex can return it.  */
+    xclose (files[nfiles-1]);
+    nfiles--;
+    r = POSIX_SPAWN (&pid, _PATH_BSHELL, &a, NULL, spawn_argv, NULL);
+#endif
+    TEST_COMPARE (r, 0);
+  }
+
+  siginfo_t sinfo;
+  TEST_COMPARE (WAITID (P_PID, pid, &sinfo, WEXITED), 0);
+  TEST_COMPARE (sinfo.si_code, CLD_EXITED);
+  TEST_COMPARE (sinfo.si_status, 0);
 
   /* Close the temporary files descriptor so it can check posix_spawn
      output.  */
   for (int i=0; i<nfiles; i++)
-    {
-      if (close (files[i]))
-	FAIL_EXIT1 ("close: %m");
-    }
+    xclose (files[i]);
 
-  int pidfd = open (pidfile, O_RDONLY);
-  if (pidfd == -1)
-    FAIL_EXIT1 ("open: %m");
+  int pidfd = xopen (pidfile, O_RDONLY, 0);
 
-  char buf[64];
-  ssize_t n;
-  if ((n = read (pidfd, buf, sizeof (buf))) < 0)
-    FAIL_EXIT1 ("read: %m");
+  char buf[INT_STRLEN_BOUND (pid_t)];
+  ssize_t n = read (pidfd, buf, sizeof (buf));
+  TEST_VERIFY (n < sizeof buf && n >= 0);
 
-  unlink (pidfile);
+  xunlink (pidfile);
 
   /* We only expect to read the PID.  */
   char *endp;
   long int rpid = strtol (buf, &endp, 10);
-  if (*endp != '\n')
-    FAIL_EXIT1 ("*endp != \'n\'");
-  if (endp == buf)
-    FAIL_EXIT1 ("read empty line");
+  TEST_VERIFY (*endp == '\n' && endp != buf);
 
-  if (rpid != pid)
-    FAIL_EXIT1 ("found \"%s\", expected pid %ld\n", buf, (long int) pid);
+  TEST_COMPARE (rpid, sinfo.si_pid);
 
   return 0;
 }
diff --git a/posix/tst-spawn4.c b/posix/tst-spawn4.c
index 327f04ea6c..8bf8bd52df 100644
--- a/posix/tst-spawn4.c
+++ b/posix/tst-spawn4.c
@@ -24,6 +24,7 @@ 
 #include <support/xunistd.h>
 #include <support/check.h>
 #include <support/temp_file.h>
+#include <tst-spawn.h>
 
 static int
 do_test (void)
@@ -38,15 +39,15 @@  do_test (void)
 
   TEST_VERIFY_EXIT (chmod (scriptname, 0x775) == 0);
 
-  pid_t pid;
+  PID_T_TYPE pid;
   int status;
 
   /* Check if scripts without shebang are correctly not executed.  */
-  status = posix_spawn (&pid, scriptname, NULL, NULL, (char *[]) { 0 },
+  status = POSIX_SPAWN (&pid, scriptname, NULL, NULL, (char *[]) { 0 },
                         (char *[]) { 0 });
   TEST_VERIFY_EXIT (status == ENOEXEC);
 
-  status = posix_spawnp (&pid, scriptname, NULL, NULL, (char *[]) { 0 },
+  status = POSIX_SPAWNP (&pid, scriptname, NULL, NULL, (char *[]) { 0 },
                          (char *[]) { 0 });
   TEST_VERIFY_EXIT (status == ENOEXEC);
 
diff --git a/posix/tst-spawn5.c b/posix/tst-spawn5.c
index 6b3d11cf82..7850f3d7dd 100644
--- a/posix/tst-spawn5.c
+++ b/posix/tst-spawn5.c
@@ -33,6 +33,7 @@ 
 
 #include <arch-fd_to_filename.h>
 #include <array_length.h>
+#include <tst-spawn.h>
 
 /* Nonzero if the program gets called via `exec'.  */
 static int restart;
@@ -161,14 +162,13 @@  spawn_closefrom_test (posix_spawn_file_actions_t *fa, int lowfd, int highfd,
   args[argc] = NULL;
   TEST_VERIFY (argc < argv_size);
 
-  pid_t pid;
-  int status;
+  PID_T_TYPE pid;
+  siginfo_t sinfo;
 
-  TEST_COMPARE (posix_spawn (&pid, args[0], fa, NULL, args, environ), 0);
-  TEST_COMPARE (xwaitpid (pid, &status, 0), pid);
-  TEST_VERIFY (WIFEXITED (status));
-  TEST_VERIFY (!WIFSIGNALED (status));
-  TEST_COMPARE (WEXITSTATUS (status), 0);
+  TEST_COMPARE (POSIX_SPAWN (&pid, args[0], fa, NULL, args, environ), 0);
+  TEST_COMPARE (WAITID (P_PID, pid, &sinfo, WEXITED), 0);
+  TEST_COMPARE (sinfo.si_code, CLD_EXITED);
+  TEST_COMPARE (sinfo.si_status, 0);
 }
 
 static void
diff --git a/posix/tst-spawn6.c b/posix/tst-spawn6.c
index 4e29d78168..ff36351cd6 100644
--- a/posix/tst-spawn6.c
+++ b/posix/tst-spawn6.c
@@ -32,6 +32,7 @@ 
 #include <sys/ioctl.h>
 #include <stdlib.h>
 #include <termios.h>
+#include <tst-spawn.h>
 
 #ifndef PATH_MAX
 # define PATH_MAX 1024
@@ -108,17 +109,15 @@  run_subprogram (int argc, char *argv[], const posix_spawnattr_t *attr,
   spargv[i] = NULL;
 
   pid_t pid;
-  TEST_COMPARE (posix_spawn (&pid, argv[1], actions, attr, spargv, environ),
+  TEST_COMPARE (POSIX_SPAWN (&pid, argv[1], actions, attr, spargv, environ),
 		exp_err);
   if (exp_err != 0)
     return;
 
-  int status;
-  TEST_COMPARE (xwaitpid (pid, &status, WUNTRACED), pid);
-  TEST_VERIFY (WIFEXITED (status));
-  TEST_VERIFY (!WIFSTOPPED (status));
-  TEST_VERIFY (!WIFSIGNALED (status));
-  TEST_COMPARE (WEXITSTATUS (status), 0);
+  siginfo_t sinfo;
+  TEST_COMPARE (WAITID (P_ALL, 0, &sinfo, WEXITED), 0);
+  TEST_COMPARE (sinfo.si_code, CLD_EXITED);
+  TEST_COMPARE (sinfo.si_status, 0);
 }
 
 static int
@@ -202,7 +201,7 @@  do_test (int argc, char *argv[])
   if (restart)
     return handle_restart (argv[1], argv[2]);
 
-  pid_t pid = xfork ();
+  PID_T_TYPE pid = xfork ();
   if (pid == 0)
     {
       /* Create a pseudo-terminal to avoid interfering with the one using by
diff --git a/posix/tst-spawn7.c b/posix/tst-spawn7.c
index fb06915cb7..cc4498830b 100644
--- a/posix/tst-spawn7.c
+++ b/posix/tst-spawn7.c
@@ -24,7 +24,9 @@ 
 #include <support/check.h>
 #include <support/xsignal.h>
 #include <support/xunistd.h>
+#include <sys/wait.h>
 #include <unistd.h>
+#include <tst-spawn.h>
 
 /* Nonzero if the program gets called via `exec'.  */
 #define CMDLINE_OPTIONS \
@@ -81,14 +83,13 @@  spawn_signal_test (const char *type, const posix_spawnattr_t *attr)
 {
   spargs[check_type_argc] = (char*) type;
 
-  pid_t pid;
-  int status;
+  PID_T_TYPE pid;
+  siginfo_t sinfo;
 
   TEST_COMPARE (posix_spawn (&pid, spargs[0], NULL, attr, spargs, environ), 0);
-  TEST_COMPARE (xwaitpid (pid, &status, 0), pid);
-  TEST_VERIFY (WIFEXITED (status));
-  TEST_VERIFY (!WIFSIGNALED (status));
-  TEST_COMPARE (WEXITSTATUS (status), 0);
+  TEST_COMPARE (WAITID (P_ALL, 0, &sinfo, WEXITED), 0);
+  TEST_COMPARE (sinfo.si_code, CLD_EXITED);
+  TEST_COMPARE (sinfo.si_status, 0);
 }
 
 static void
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index d7b020154a..3ecfa184d0 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -62,6 +62,7 @@  sysdep_routines += \
   clock_adjtime \
   clone \
   clone-internal \
+  clone-pidfd-support \
   clone3 \
   closefrom_fallback \
   convert_scm_timestamps \
@@ -492,6 +493,8 @@  sysdep_headers += \
 sysdep_routines += \
   getcpu \
   oldglob \
+  pidfd_spawn \
+  pidfd_spawnp \
   sched_getcpu \
   spawnattr_getcgroup_np \
   spawnattr_setcgroup_np \
@@ -500,7 +503,16 @@  sysdep_routines += \
 tests += \
   tst-affinity \
   tst-affinity-pid \
+  tst-posix_spawn-setsid-pidfd \
   tst-spawn-cgroup \
+  tst-spawn-chdir-pidfd \
+  tst-spawn-pidfd \
+  tst-spawn2-pidfd \
+  tst-spawn3-pidfd \
+  tst-spawn4-pidfd \
+  tst-spawn5-pidfd \
+  tst-spawn6-pidfd \
+  tst-spawn7-pidfd \
   # tests
 
 tests-static += \
@@ -514,8 +526,14 @@  tests += \
 CFLAGS-fork.c = $(libio-mtsafe)
 CFLAGS-getpid.o = -fomit-frame-pointer
 CFLAGS-getpid.os = -fomit-frame-pointer
+CFLAGS-tst-spawn3-pidfd.c += -DOBJPFX=\"$(objpfx)\"
 
 tst-spawn-cgroup-ARGS = -- $(host-test-program-cmd)
+tst-spawn-pidfd-ARGS = -- $(host-test-program-cmd)
+tst-spawn5-pidfd-ARGS = -- $(host-test-program-cmd)
+tst-spawn6-pidfd-ARGS = -- $(host-test-program-cmd)
+tst-spawn7-pidfd-ARGS = -- $(host-test-program-cmd)
+tst-posix_spawn-setsid-pidfd-ARGS = -- $(host-test-program-cmd)
 endif
 
 ifeq ($(subdir),inet)
diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions
index 6d8a67039e..bd96ad12ad 100644
--- a/sysdeps/unix/sysv/linux/Versions
+++ b/sysdeps/unix/sysv/linux/Versions
@@ -324,6 +324,8 @@  libc {
   GLIBC_2.39 {
     posix_spawnattr_getcgroup_np;
     posix_spawnattr_setcgroup_np;
+    pidfd_spawn;
+    pidfd_spawnp;
   }
   GLIBC_PRIVATE {
     # functions used in other libraries
diff --git a/sysdeps/unix/sysv/linux/aarch64/libc.abilist b/sysdeps/unix/sysv/linux/aarch64/libc.abilist
index 0090827e01..6f23556067 100644
--- a/sysdeps/unix/sysv/linux/aarch64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/aarch64/libc.abilist
@@ -2673,5 +2673,7 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
diff --git a/sysdeps/unix/sysv/linux/alpha/libc.abilist b/sysdeps/unix/sysv/linux/alpha/libc.abilist
index 9d099471b6..02c43beb13 100644
--- a/sysdeps/unix/sysv/linux/alpha/libc.abilist
+++ b/sysdeps/unix/sysv/linux/alpha/libc.abilist
@@ -2782,6 +2782,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 _IO_fprintf F
diff --git a/sysdeps/unix/sysv/linux/arc/libc.abilist b/sysdeps/unix/sysv/linux/arc/libc.abilist
index d7ed2f66de..dd8e5912d8 100644
--- a/sysdeps/unix/sysv/linux/arc/libc.abilist
+++ b/sysdeps/unix/sysv/linux/arc/libc.abilist
@@ -2434,5 +2434,7 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
diff --git a/sysdeps/unix/sysv/linux/arm/be/libc.abilist b/sysdeps/unix/sysv/linux/arm/be/libc.abilist
index 92e686defe..a751e5f5a9 100644
--- a/sysdeps/unix/sysv/linux/arm/be/libc.abilist
+++ b/sysdeps/unix/sysv/linux/arm/be/libc.abilist
@@ -554,6 +554,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 _Exit F
diff --git a/sysdeps/unix/sysv/linux/arm/le/libc.abilist b/sysdeps/unix/sysv/linux/arm/le/libc.abilist
index b503e642fc..0eda3459ed 100644
--- a/sysdeps/unix/sysv/linux/arm/le/libc.abilist
+++ b/sysdeps/unix/sysv/linux/arm/le/libc.abilist
@@ -551,6 +551,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 _Exit F
diff --git a/sysdeps/unix/sysv/linux/bits/spawn_ext.h b/sysdeps/unix/sysv/linux/bits/spawn_ext.h
index 3bc10ab477..ff8550f264 100644
--- a/sysdeps/unix/sysv/linux/bits/spawn_ext.h
+++ b/sysdeps/unix/sysv/linux/bits/spawn_ext.h
@@ -37,4 +37,35 @@  extern int posix_spawnattr_setcgroup_np (posix_spawnattr_t *__restrict __attr,
 
 #endif /* __USE_MISC */
 
+#ifdef __USE_GNU
+
+/* Spawn a new process executing PATH with the attributes describes in *ATTRP.
+   Before running the process perform the actions described in FACTS.  Return
+   a PID file descriptor in PIDFD if process creation was successful and the
+   argument is non-null.
+
+   This function is a possible cancellation point and therefore not
+   marked with __THROW. */
+extern int pidfd_spawn (int *__restrict __pidfd,
+			const char *__restrict __path,
+			const posix_spawn_file_actions_t *__restrict __facts,
+			const posix_spawnattr_t *__restrict __attrp,
+			char *const __argv[__restrict_arr],
+			char *const __envp[__restrict_arr])
+    __nonnull ((2, 5));
+
+/* Similar to `pidfd_spawn' but search for FILE in the PATH.
+
+   This function is a possible cancellation point and therefore not
+   marked with __THROW. */
+extern int pidfd_spawnp (int *__restrict __pidfd,
+			 const char *__restrict __file,
+			 const posix_spawn_file_actions_t *__restrict __facts,
+			 const posix_spawnattr_t *__restrict __attrp,
+			 char *const __argv[__restrict_arr],
+			 char *const __envp[__restrict_arr])
+    __nonnull ((2, 5));
+
+#endif /* __USE_GNU */
+
 __END_DECLS
diff --git a/sysdeps/unix/sysv/linux/clone-pidfd-support.c b/sysdeps/unix/sysv/linux/clone-pidfd-support.c
new file mode 100644
index 0000000000..af2d213cc5
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/clone-pidfd-support.c
@@ -0,0 +1,58 @@ 
+/* Check if kernel supports PID file descriptors.
+   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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <atomic.h>
+#include <sys/wait.h>
+#include <sysdep.h>
+
+/* The PID file descriptors was added during multiple releases:
+   - Linux 5.2 added CLONE_PIDFD support for clone and __clone_pidfd_supported
+     syscall.
+   - Linux 5.3 added support for poll and CLONE_PIDFD for clone3.
+   - Linux 5.4 added P_PIDFD support on waitid.
+
+   For internal usage on spawn and fork, it only make sense to return a file
+   descriptor if caller can actually waitid on it.  */
+bool
+__clone_pidfd_supported (void)
+{
+  static int supported = 0;
+  int state = atomic_load_relaxed (&supported);
+  if (state == 0)
+    {
+      /* Linux define the maximum allocated file descriptor value as
+	 0x7fffffc0 (from fs/file.c):
+
+         #define __const_min(x, y) ((x) < (y) ? (x) : (y))
+         unsigned int sysctl_nr_open_max =
+	   __const_min(INT_MAX, ~(size_t)0/sizeof(void *)) & -BITS_PER_LONG;
+
+	 So we can detect whether kernel supports all pidfd interfaces by
+	 using a valid but never allocated file descriptor: if is not
+	 supported waitid will return EINVAL, otherwise EBADF.
+
+         Also the waitid is a cancellation entrypoint, so issue the syscall
+	 directly.  */
+      int r = INTERNAL_SYSCALL_CALL (waitid, P_PIDFD, INT_MAX, NULL,
+				     WEXITED | WNOHANG);
+      state = r == -EBADF ? 1 : -1;
+      atomic_store_relaxed (&supported, state);
+    }
+
+  return state == 1;
+}
diff --git a/sysdeps/unix/sysv/linux/csky/libc.abilist b/sysdeps/unix/sysv/linux/csky/libc.abilist
index ec9e209b8d..4f4e99427b 100644
--- a/sysdeps/unix/sysv/linux/csky/libc.abilist
+++ b/sysdeps/unix/sysv/linux/csky/libc.abilist
@@ -2710,5 +2710,7 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
diff --git a/sysdeps/unix/sysv/linux/hppa/libc.abilist b/sysdeps/unix/sysv/linux/hppa/libc.abilist
index 961f88bf14..abc471dd0b 100644
--- a/sysdeps/unix/sysv/linux/hppa/libc.abilist
+++ b/sysdeps/unix/sysv/linux/hppa/libc.abilist
@@ -2659,6 +2659,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 __confstr_chk F
diff --git a/sysdeps/unix/sysv/linux/i386/libc.abilist b/sysdeps/unix/sysv/linux/i386/libc.abilist
index b6f5a4ab83..9f03c8a9a2 100644
--- a/sysdeps/unix/sysv/linux/i386/libc.abilist
+++ b/sysdeps/unix/sysv/linux/i386/libc.abilist
@@ -2843,6 +2843,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 __confstr_chk F
diff --git a/sysdeps/unix/sysv/linux/ia64/libc.abilist b/sysdeps/unix/sysv/linux/ia64/libc.abilist
index a404b99e68..ce1d20b722 100644
--- a/sysdeps/unix/sysv/linux/ia64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/ia64/libc.abilist
@@ -2608,6 +2608,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 __confstr_chk F
diff --git a/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist b/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist
index 2f9f6e2332..8c3640b004 100644
--- a/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist
@@ -2194,5 +2194,7 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
diff --git a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
index b7e9ab4558..a594916319 100644
--- a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
+++ b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
@@ -555,6 +555,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 _Exit F
diff --git a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
index c345da7e0a..7f61d4824d 100644
--- a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
+++ b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
@@ -2786,6 +2786,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 __confstr_chk F
diff --git a/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist b/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist
index a643d868a8..83ebb84ff3 100644
--- a/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist
+++ b/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist
@@ -2759,5 +2759,7 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
diff --git a/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist b/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist
index fed535742c..89a0ff83bf 100644
--- a/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist
+++ b/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist
@@ -2756,5 +2756,7 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_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 147bac3eaf..e21c752057 100644
--- a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
+++ b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
@@ -2751,6 +2751,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 __confstr_chk F
diff --git a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
index e550616576..42f470d397 100644
--- a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
+++ b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
@@ -2749,6 +2749,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 __confstr_chk F
diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
index 56f414dbd0..6907f5f98b 100644
--- a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
@@ -2757,6 +2757,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 __confstr_chk F
diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
index da704a2e2b..4b1f017a98 100644
--- a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
@@ -2659,6 +2659,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 __confstr_chk F
diff --git a/sysdeps/unix/sysv/linux/nios2/libc.abilist b/sysdeps/unix/sysv/linux/nios2/libc.abilist
index f5a157ea94..0d45902209 100644
--- a/sysdeps/unix/sysv/linux/nios2/libc.abilist
+++ b/sysdeps/unix/sysv/linux/nios2/libc.abilist
@@ -2798,5 +2798,7 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
diff --git a/sysdeps/unix/sysv/linux/or1k/libc.abilist b/sysdeps/unix/sysv/linux/or1k/libc.abilist
index 85b552f1cb..c59032ef14 100644
--- a/sysdeps/unix/sysv/linux/or1k/libc.abilist
+++ b/sysdeps/unix/sysv/linux/or1k/libc.abilist
@@ -2180,5 +2180,7 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
diff --git a/sysdeps/unix/sysv/linux/pidfd_spawn.c b/sysdeps/unix/sysv/linux/pidfd_spawn.c
new file mode 100644
index 0000000000..cc76bf9935
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/pidfd_spawn.c
@@ -0,0 +1,30 @@ 
+/* pidfd_spawn - Spawn a process and return a PID 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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <spawn.h>
+#include "spawn_int.h"
+
+int
+pidfd_spawn (int *pidfd, const char *path,
+	     const posix_spawn_file_actions_t *file_actions,
+	     const posix_spawnattr_t *attrp, char *const argv[],
+	     char *const envp[])
+{
+  return __spawni (pidfd, path, file_actions, attrp, argv, envp,
+		   SPAWN_XFLAGS_RET_PIDFD);
+}
diff --git a/sysdeps/unix/sysv/linux/pidfd_spawnp.c b/sysdeps/unix/sysv/linux/pidfd_spawnp.c
new file mode 100644
index 0000000000..858c0f3191
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/pidfd_spawnp.c
@@ -0,0 +1,30 @@ 
+/* pidfd_spawnp - Spawn a process and return a PID 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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <spawn.h>
+#include "spawn_int.h"
+
+int
+pidfd_spawnp (int *pidfd, const char *path,
+	      const posix_spawn_file_actions_t *file_actions,
+	      const posix_spawnattr_t *attrp, char *const argv[],
+	      char *const envp[])
+{
+  return __spawni (pidfd, path, file_actions, attrp, argv, envp,
+		   SPAWN_XFLAGS_USE_PATH | SPAWN_XFLAGS_RET_PIDFD);
+}
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
index cadb16c12f..e014314d3e 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
@@ -2825,6 +2825,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 _IO_fprintf F
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
index 50c5b99728..ac05154915 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
@@ -2858,6 +2858,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 _IO_fprintf F
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist
index 81c63385af..e13ee6e72a 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist
@@ -2579,6 +2579,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 _IO_fprintf F
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist
index af9be18108..0e8c9ab3fe 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist
@@ -2893,5 +2893,7 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
diff --git a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist
index 2266a88ad5..b0559a5a64 100644
--- a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist
@@ -2436,5 +2436,7 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
diff --git a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist
index 4776ae32b8..5f79a84016 100644
--- a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist
@@ -2636,5 +2636,7 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_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 5d1d7d07a5..498886ccb2 100644
--- a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist
@@ -2823,6 +2823,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 _IO_fprintf F
diff --git a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist
index fffc32a0f4..51679c2990 100644
--- a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist
@@ -2616,6 +2616,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 _IO_fprintf F
diff --git a/sysdeps/unix/sysv/linux/sh/be/libc.abilist b/sysdeps/unix/sysv/linux/sh/be/libc.abilist
index 43ff21447d..af7b6f5bc9 100644
--- a/sysdeps/unix/sysv/linux/sh/be/libc.abilist
+++ b/sysdeps/unix/sysv/linux/sh/be/libc.abilist
@@ -2666,6 +2666,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 __confstr_chk F
diff --git a/sysdeps/unix/sysv/linux/sh/le/libc.abilist b/sysdeps/unix/sysv/linux/sh/le/libc.abilist
index 9ea18d5886..b766299f31 100644
--- a/sysdeps/unix/sysv/linux/sh/le/libc.abilist
+++ b/sysdeps/unix/sysv/linux/sh/le/libc.abilist
@@ -2663,6 +2663,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 __confstr_chk F
diff --git a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
index c6607d5385..f5b9200a33 100644
--- a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
@@ -2818,6 +2818,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 _IO_fprintf F
diff --git a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
index a010a2bb16..f6012e6e17 100644
--- a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
@@ -2631,6 +2631,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 __confstr_chk F
diff --git a/sysdeps/unix/sysv/linux/spawni.c b/sysdeps/unix/sysv/linux/spawni.c
index f0d4c62ae6..844abf1b0b 100644
--- a/sysdeps/unix/sysv/linux/spawni.c
+++ b/sysdeps/unix/sysv/linux/spawni.c
@@ -68,6 +68,7 @@  struct posix_spawn_args
   int xflags;
   bool use_clone3;
   int err;
+  int pidfd;
 };
 
 /* Older version requires that shell script without shebang definition
@@ -309,7 +310,7 @@  fail:
 /* Spawn a new process executing PATH with the attributes describes in *ATTRP.
    Before running the process perform the actions described in FILE-ACTIONS. */
 static int
-__spawnix (pid_t * pid, const char *file,
+__spawnix (int *pid, const char *file,
 	   const posix_spawn_file_actions_t * file_actions,
 	   const posix_spawnattr_t * attrp, char *const argv[],
 	   char *const envp[], int xflags,
@@ -319,6 +320,15 @@  __spawnix (pid_t * pid, const char *file,
   struct posix_spawn_args args;
   int ec;
 
+  bool use_pidfd = xflags & SPAWN_XFLAGS_RET_PIDFD;
+
+  /* For CLONE_PIDFD, older kernels might not fail with unsupported flags or
+     some versions might not support waitid (P_PIDFD).  So to avoid the need
+     to handle the error on the helper process, check for full pidfd
+     support.  */
+  if (use_pidfd && !__clone_pidfd_supported ())
+    return ENOSYS;
+
   /* To avoid imposing hard limits on posix_spawn{p} the total number of
      arguments is first calculated to allocate a mmap to hold all possible
      values.  */
@@ -368,6 +378,7 @@  __spawnix (pid_t * pid, const char *file,
   args.argv = argv;
   args.argc = argc;
   args.envp = envp;
+  args.pidfd = 0;
   args.xflags = xflags;
 
   internal_signal_block_all (&args.oldmask);
@@ -386,13 +397,16 @@  __spawnix (pid_t * pid, const char *file,
       /* Unsupported flags like CLONE_CLEAR_SIGHAND will be cleared up by
 	 __clone_internal_fallback.  */
       .flags = (set_cgroup ? CLONE_INTO_CGROUP : 0)
+	       | (use_pidfd ? CLONE_PIDFD : 0)
 	       | CLONE_CLEAR_SIGHAND
 	       | CLONE_VM
 	       | CLONE_VFORK,
       .exit_signal = SIGCHLD,
       .stack = (uintptr_t) stack,
       .stack_size = stack_size,
-      .cgroup = (set_cgroup ? attrp->__cgroup : 0)
+      .cgroup = (set_cgroup ? attrp->__cgroup : 0),
+      .pidfd = use_pidfd ? (uintptr_t) &args.pidfd : 0,
+      .parent_tid = use_pidfd ? (uintptr_t) &args.pidfd : 0,
     };
 #ifdef HAVE_CLONE3_WRAPPER
   args.use_clone3 = true;
@@ -445,7 +459,7 @@  __spawnix (pid_t * pid, const char *file,
   __munmap (stack, stack_size);
 
   if ((ec == 0) && (pid != NULL))
-    *pid = new_pid;
+    *pid = use_pidfd ? args.pidfd : new_pid;
 
   internal_signal_restore_set (&args.oldmask);
 
diff --git a/sysdeps/unix/sysv/linux/tst-posix_spawn-setsid-pidfd.c b/sysdeps/unix/sysv/linux/tst-posix_spawn-setsid-pidfd.c
new file mode 100644
index 0000000000..4372833f07
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tst-posix_spawn-setsid-pidfd.c
@@ -0,0 +1,20 @@ 
+/* Tests for spawn pidfd extension.
+   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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <tst-spawn-pidfd.h>
+#include <posix/tst-posix_spawn-setsid.c>
diff --git a/sysdeps/unix/sysv/linux/tst-spawn-chdir-pidfd.c b/sysdeps/unix/sysv/linux/tst-spawn-chdir-pidfd.c
new file mode 100644
index 0000000000..019527b31b
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tst-spawn-chdir-pidfd.c
@@ -0,0 +1,20 @@ 
+/* Tests for spawn pidfd extension.
+   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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <tst-spawn-pidfd.h>
+#include <posix/tst-spawn-chdir.c>
diff --git a/sysdeps/unix/sysv/linux/tst-spawn-pidfd.c b/sysdeps/unix/sysv/linux/tst-spawn-pidfd.c
new file mode 100644
index 0000000000..c430995af8
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tst-spawn-pidfd.c
@@ -0,0 +1,20 @@ 
+/* Tests for spawn pidfd extension.
+   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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <tst-spawn-pidfd.h>
+#include <posix/tst-spawn.c>
diff --git a/sysdeps/unix/sysv/linux/tst-spawn-pidfd.h b/sysdeps/unix/sysv/linux/tst-spawn-pidfd.h
new file mode 100644
index 0000000000..ea51c22447
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tst-spawn-pidfd.h
@@ -0,0 +1,63 @@ 
+/* Tests for spawn pidfd extension.
+   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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <errno.h>
+#include <spawn.h>
+#include <support/check.h>
+
+#define PID_T_TYPE int
+
+/* Call posix_spawn with POSIX_SPAWN_PIDFD set.  */
+static inline int
+pidfd_spawn_check (int *pidfd, const char *path,
+		   const posix_spawn_file_actions_t *fa,
+		   const posix_spawnattr_t *attr, char *const argv[],
+		   char *const envp[])
+{
+  int r = pidfd_spawn (pidfd, path, fa, attr, argv, envp);
+  if (r == ENOSYS)
+    FAIL_UNSUPPORTED ("kernel does not support CLONE_PIDFD clone flag");
+  return r;
+}
+
+#define POSIX_SPAWN(__pidfd, __path, __actions, __attr, __argv, __envp)	     \
+  pidfd_spawn_check (__pidfd, __path, __actions, __attr, __argv, __envp)
+
+static inline int
+pidfd_spawnp_check (int *pidfd, const char *file,
+		    const posix_spawn_file_actions_t *fa,
+		    const posix_spawnattr_t *attr,
+		    char *const argv[], char *const envp[])
+{
+  int r = pidfd_spawnp (pidfd, file, fa, attr, argv, envp);
+  if (r == ENOSYS)
+    FAIL_UNSUPPORTED ("kernel does not support CLONE_PIDFD clone flag");
+  return r;
+}
+
+#define POSIX_SPAWNP(__child, __path, __actions, __attr, __argv, __envp) \
+  pidfd_spawnp_check (__child, __path, __actions, __attr, __argv, __envp)
+
+#define WAITID(__idtype, __id, __info, __opts)				     \
+  ({									     \
+     __typeof (__idtype) __new_idtype = __idtype == P_PID		     \
+					? P_PIDFD : __idtype;		     \
+     waitid (__new_idtype, __id, __info, __opts);			     \
+  })
+
+#define TST_SPAWN_PIDFD 1
diff --git a/sysdeps/unix/sysv/linux/tst-spawn2-pidfd.c b/sysdeps/unix/sysv/linux/tst-spawn2-pidfd.c
new file mode 100644
index 0000000000..03ba7a3d15
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tst-spawn2-pidfd.c
@@ -0,0 +1,20 @@ 
+/* Tests for spawn pidfd extension.
+   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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <tst-spawn-pidfd.h>
+#include <posix/tst-spawn2.c>
diff --git a/sysdeps/unix/sysv/linux/tst-spawn3-pidfd.c b/sysdeps/unix/sysv/linux/tst-spawn3-pidfd.c
new file mode 100644
index 0000000000..8ad9a16854
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tst-spawn3-pidfd.c
@@ -0,0 +1,20 @@ 
+/* Check posix_spawn add file actions.
+   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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <tst-spawn-pidfd.h>
+#include <posix/tst-spawn3.c>
diff --git a/sysdeps/unix/sysv/linux/tst-spawn4-pidfd.c b/sysdeps/unix/sysv/linux/tst-spawn4-pidfd.c
new file mode 100644
index 0000000000..83922da7d1
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tst-spawn4-pidfd.c
@@ -0,0 +1,20 @@ 
+/* Tests for spawn pidfd extension.
+   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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <tst-spawn-pidfd.h>
+#include <posix/tst-spawn4.c>
diff --git a/sysdeps/unix/sysv/linux/tst-spawn5-pidfd.c b/sysdeps/unix/sysv/linux/tst-spawn5-pidfd.c
new file mode 100644
index 0000000000..149c352bf8
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tst-spawn5-pidfd.c
@@ -0,0 +1,20 @@ 
+/* Tests for spawn pidfd extension.
+   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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <tst-spawn-pidfd.h>
+#include <posix/tst-spawn5.c>
diff --git a/sysdeps/unix/sysv/linux/tst-spawn6-pidfd.c b/sysdeps/unix/sysv/linux/tst-spawn6-pidfd.c
new file mode 100644
index 0000000000..d3f5859457
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tst-spawn6-pidfd.c
@@ -0,0 +1,20 @@ 
+/* Tests for spawn pidfd extension.
+   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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <tst-spawn-pidfd.h>
+#include <posix/tst-spawn6.c>
diff --git a/sysdeps/unix/sysv/linux/tst-spawn7-pidfd.c b/sysdeps/unix/sysv/linux/tst-spawn7-pidfd.c
new file mode 100644
index 0000000000..3aec86bec2
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tst-spawn7-pidfd.c
@@ -0,0 +1,20 @@ 
+/* Tests for spawn pidfd extension.
+   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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <tst-spawn-pidfd.h>
+#include <posix/tst-spawn7.c>
diff --git a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
index 3591b5de5e..e35bf54779 100644
--- a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
@@ -2582,6 +2582,8 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F
 GLIBC_2.4 __confstr_chk F
diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist
index ffbd8f3738..e7d7eb61c0 100644
--- a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist
@@ -2688,5 +2688,7 @@  GLIBC_2.38 strlcat F
 GLIBC_2.38 strlcpy F
 GLIBC_2.38 wcslcat F
 GLIBC_2.38 wcslcpy F
+GLIBC_2.39 pidfd_spawn F
+GLIBC_2.39 pidfd_spawnp F
 GLIBC_2.39 posix_spawnattr_getcgroup_np F
 GLIBC_2.39 posix_spawnattr_setcgroup_np F