[v2,3/3] linux: Add pidfd_getpid

Message ID 20230420142037.4063169-4-adhemerval.zanella@linaro.org
State Superseded
Headers
Series Add pidfd_spawn, pidfd_spawnp, pidfd_fork, and pidfd_getpid |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

Adhemerval Zanella Netto April 20, 2023, 2:20 p.m. UTC
  This interface allows to obtain the associated pid ID from the
process file descriptor.  It is done by parsing the procps fdinfo
information.  Its prototype is:

   pid_t pidfd_getpid (int fd)

It returns the associated pid or -1 in case of an error and set the
errno accordingly.  The possible errno values are the smae from
open, read, and close (used on procps parsing), along with:

   - EINVAL if the FP is negative (similar to fexecve).

   - EBADF if the FD does not have a PID associated of if the fdinfo
     fields contains a value larger than pid_t.

   - EREMOTE if the PID is in a separate namespace.

   - ESRCH if the process is already terminated.

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                                          |  4 +
 sysdeps/unix/sysv/linux/Makefile              |  2 +
 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/arm/be/libc.abilist   |  1 +
 sysdeps/unix/sysv/linux/arm/le/libc.abilist   |  1 +
 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_getpid.c        | 94 ++++++++++++++++++
 .../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 +
 sysdeps/unix/sysv/linux/procutils.c           | 99 +++++++++++++++++++
 sysdeps/unix/sysv/linux/procutils.h           | 37 +++++++
 .../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           |  4 +
 sysdeps/unix/sysv/linux/tst-pidfd.c           | 45 +++++++++
 .../unix/sysv/linux/x86_64/64/libc.abilist    |  1 +
 .../unix/sysv/linux/x86_64/x32/libc.abilist   |  1 +
 42 files changed, 320 insertions(+)
 create mode 100644 sysdeps/unix/sysv/linux/pidfd_getpid.c
 create mode 100644 sysdeps/unix/sysv/linux/procutils.c
 create mode 100644 sysdeps/unix/sysv/linux/procutils.h
  

Comments

Luca Boccassi April 20, 2023, 2:24 p.m. UTC | #1
On Thu, 20 Apr 2023 at 15:20, Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
> This interface allows to obtain the associated pid ID from the
> process file descriptor.  It is done by parsing the procps fdinfo
> information.  Its prototype is:
>
>    pid_t pidfd_getpid (int fd)
>
> It returns the associated pid or -1 in case of an error and set the
> errno accordingly.  The possible errno values are the smae from
> open, read, and close (used on procps parsing), along with:
>
>    - EINVAL if the FP is negative (similar to fexecve).
>
>    - EBADF if the FD does not have a PID associated of if the fdinfo
>      fields contains a value larger than pid_t.
>
>    - EREMOTE if the PID is in a separate namespace.
>
>    - ESRCH if the process is already terminated.
>
> 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).

Acked-by: Luca Boccassi <bluca@debian.org>
  
Florian Weimer May 2, 2023, 4:07 p.m. UTC | #2
* Adhemerval Zanella via Libc-alpha:

> This interface allows to obtain the associated pid ID from the
> process file descriptor.  It is done by parsing the procps fdinfo
> information.  Its prototype is:
>
>    pid_t pidfd_getpid (int fd)
>
> It returns the associated pid or -1 in case of an error and set the
> errno accordingly.  The possible errno values are the smae from
> open, read, and close (used on procps parsing), along with:
>
>    - EINVAL if the FP is negative (similar to fexecve).
>
>    - EBADF if the FD does not have a PID associated of if the fdinfo
>      fields contains a value larger than pid_t.
>
>    - EREMOTE if the PID is in a separate namespace.
>
>    - ESRCH if the process is already terminated.

Could you add a manual entry for this?

The documentation for EREMOTE (also in the comment in the code) should
probably say that this is returned if there is no PID to denote the
process in the current namespace.  I assume that the PID is available
from an outer namespace (even across PID namespace boundaries), but not
necessarily in the other direction.

> diff --git a/sysdeps/unix/sysv/linux/pidfd_getpid.c b/sysdeps/unix/sysv/linux/pidfd_getpid.c
> new file mode 100644
> index 0000000000..d0c7987791
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/pidfd_getpid.c
> @@ -0,0 +1,94 @@

> +static bool
> +parse_fdinfo (const char *l, void *arg)
> +{
> +  enum { fieldlen = sizeof ("Pid:") - 1 };
> +  if (strncmp (l, "Pid:", fieldlen) != 0)
> +    return true;
> +
> +  l += fieldlen;
> +
> +  char *endp;
> +  long int n = strtol (l, &endp, 10);
> +  if (l == endp || n > INT_MAX)
> +    return true;

Should this use strtoul?  Otherwise I'm not sure the overflow check will
work on 32-bit.

Rest of the implementation looks okay, but the kernel should really
provide an ioctl or similar for this.

> diff --git a/sysdeps/unix/sysv/linux/procutils.c b/sysdeps/unix/sysv/linux/procutils.c
> new file mode 100644
> index 0000000000..4909afeae1
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/procutils.c
> @@ -0,0 +1,99 @@
> +/* Utilities functions to read/parse Linux procfs and sysfs.

> +int
> +procutils_read_file (const char *filename, procutils_closure_t closure,
> +		     void *arg)
> +{
> +  enum { buffer_size = 1024 };
> +  char buffer[buffer_size];
> +  char *buffer_end = buffer + buffer_size;
> +  char *cp = buffer_end;
> +  char *re = buffer_end;
> +
> +  int fd = __open64_nocancel (filename, O_RDONLY | O_CLOEXEC);
> +  if (fd == -1)
> +    return -1;
> +
> +  char *l;
> +  while ((l = next_line (fd, buffer, &cp, &re, buffer_end)) != NULL)
> +    if (!closure (l, arg))
> +      break;
> +
> +  __close_nocancel_nostatus (fd);
> +
> +  return 0;
> +}

Incomplete error reporting?  Any read failure doesn't make it to the
caller.

The callback interface will be easier to use if you have it return int
and return the callback return value if it is non-zero, I think.

> diff --git a/sysdeps/unix/sysv/linux/tst-pidfd.c b/sysdeps/unix/sysv/linux/tst-pidfd.c
> index 64d8a2ef40..2a73328ef1 100644
> --- a/sysdeps/unix/sysv/linux/tst-pidfd.c
> +++ b/sysdeps/unix/sysv/linux/tst-pidfd.c
> @@ -18,6 +18,7 @@

It would be nice to have a check for the EREMOTE case.

Thanks,
Florian
  
Luca Boccassi May 2, 2023, 4:19 p.m. UTC | #3
On Tue, 2 May 2023 at 17:07, Florian Weimer <fweimer@redhat.com> wrote:
>
> * Adhemerval Zanella via Libc-alpha:
> > diff --git a/sysdeps/unix/sysv/linux/pidfd_getpid.c b/sysdeps/unix/sysv/linux/pidfd_getpid.c
> > new file mode 100644
> > index 0000000000..d0c7987791
> > --- /dev/null
> > +++ b/sysdeps/unix/sysv/linux/pidfd_getpid.c
> > @@ -0,0 +1,94 @@
>
> > +static bool
> > +parse_fdinfo (const char *l, void *arg)
> > +{
> > +  enum { fieldlen = sizeof ("Pid:") - 1 };
> > +  if (strncmp (l, "Pid:", fieldlen) != 0)
> > +    return true;
> > +
> > +  l += fieldlen;
> > +
> > +  char *endp;
> > +  long int n = strtol (l, &endp, 10);
> > +  if (l == endp || n > INT_MAX)
> > +    return true;
>
> Should this use strtoul?  Otherwise I'm not sure the overflow check will
> work on 32-bit.
>
> Rest of the implementation looks okay, but the kernel should really
> provide an ioctl or similar for this.

This can be -1 when the process is gone, and it is important to get
that distinction (with certainty) to tell the caller ESRCH. We need
this distinction in callers.

Kind regards,
Luca Boccassi
  
Florian Weimer May 2, 2023, 5:13 p.m. UTC | #4
* Luca Boccassi:

> On Tue, 2 May 2023 at 17:07, Florian Weimer <fweimer@redhat.com> wrote:
>>
>> * Adhemerval Zanella via Libc-alpha:
>> > diff --git a/sysdeps/unix/sysv/linux/pidfd_getpid.c b/sysdeps/unix/sysv/linux/pidfd_getpid.c
>> > new file mode 100644
>> > index 0000000000..d0c7987791
>> > --- /dev/null
>> > +++ b/sysdeps/unix/sysv/linux/pidfd_getpid.c
>> > @@ -0,0 +1,94 @@
>>
>> > +static bool
>> > +parse_fdinfo (const char *l, void *arg)
>> > +{
>> > +  enum { fieldlen = sizeof ("Pid:") - 1 };
>> > +  if (strncmp (l, "Pid:", fieldlen) != 0)
>> > +    return true;
>> > +
>> > +  l += fieldlen;
>> > +
>> > +  char *endp;
>> > +  long int n = strtol (l, &endp, 10);
>> > +  if (l == endp || n > INT_MAX)
>> > +    return true;
>>
>> Should this use strtoul?  Otherwise I'm not sure the overflow check will
>> work on 32-bit.
>>
>> Rest of the implementation looks okay, but the kernel should really
>> provide an ioctl or similar for this.
>
> This can be -1 when the process is gone, and it is important to get
> that distinction (with certainty) to tell the caller ESRCH. We need
> this distinction in callers.

That shouldn't be a problem, strotul processes leading signs, too.  The
entire error checking is a bit dubious because it doesn't check much,
considering strtol's general flexibility.

Thanks,
Florian
  
Adhemerval Zanella Netto May 15, 2023, 5:39 p.m. UTC | #5
On 02/05/23 13:07, Florian Weimer wrote:
> * Adhemerval Zanella via Libc-alpha:
> 
>> This interface allows to obtain the associated pid ID from the
>> process file descriptor.  It is done by parsing the procps fdinfo
>> information.  Its prototype is:
>>
>>    pid_t pidfd_getpid (int fd)
>>
>> It returns the associated pid or -1 in case of an error and set the
>> errno accordingly.  The possible errno values are the smae from
>> open, read, and close (used on procps parsing), along with:
>>
>>    - EINVAL if the FP is negative (similar to fexecve).
>>
>>    - EBADF if the FD does not have a PID associated of if the fdinfo
>>      fields contains a value larger than pid_t.
>>
>>    - EREMOTE if the PID is in a separate namespace.
>>
>>    - ESRCH if the process is already terminated.
> 
> Could you add a manual entry for this?
> 
> The documentation for EREMOTE (also in the comment in the code) should
> probably say that this is returned if there is no PID to denote the
> process in the current namespace.  I assume that the PID is available
> from an outer namespace (even across PID namespace boundaries), but not
> necessarily in the other direction.

Ok, I will update the patch with a manual entry. I will also add a testcase
for EREMOTE.

> 
>> diff --git a/sysdeps/unix/sysv/linux/pidfd_getpid.c b/sysdeps/unix/sysv/linux/pidfd_getpid.c
>> new file mode 100644
>> index 0000000000..d0c7987791
>> --- /dev/null
>> +++ b/sysdeps/unix/sysv/linux/pidfd_getpid.c
>> @@ -0,0 +1,94 @@
> 
>> +static bool
>> +parse_fdinfo (const char *l, void *arg)
>> +{
>> +  enum { fieldlen = sizeof ("Pid:") - 1 };
>> +  if (strncmp (l, "Pid:", fieldlen) != 0)
>> +    return true;
>> +
>> +  l += fieldlen;
>> +
>> +  char *endp;
>> +  long int n = strtol (l, &endp, 10);
>> +  if (l == endp || n > INT_MAX)
>> +    return true;
> 
> Should this use strtoul?  Otherwise I'm not sure the overflow check will
> work on 32-bit.

Ack.

> 
> Rest of the implementation looks okay, but the kernel should really
> provide an ioctl or similar for this.

It would be the best option, although assuming procps support it is just
a mater to correct parsing it (although such things is frequently subject
to subtle bugs).

> 
>> diff --git a/sysdeps/unix/sysv/linux/procutils.c b/sysdeps/unix/sysv/linux/procutils.c
>> new file mode 100644
>> index 0000000000..4909afeae1
>> --- /dev/null
>> +++ b/sysdeps/unix/sysv/linux/procutils.c
>> @@ -0,0 +1,99 @@
>> +/* Utilities functions to read/parse Linux procfs and sysfs.
> 
>> +int
>> +procutils_read_file (const char *filename, procutils_closure_t closure,
>> +		     void *arg)
>> +{
>> +  enum { buffer_size = 1024 };
>> +  char buffer[buffer_size];
>> +  char *buffer_end = buffer + buffer_size;
>> +  char *cp = buffer_end;
>> +  char *re = buffer_end;
>> +
>> +  int fd = __open64_nocancel (filename, O_RDONLY | O_CLOEXEC);
>> +  if (fd == -1)
>> +    return -1;
>> +
>> +  char *l;
>> +  while ((l = next_line (fd, buffer, &cp, &re, buffer_end)) != NULL)
>> +    if (!closure (l, arg))
>> +      break;
>> +
>> +  __close_nocancel_nostatus (fd);
>> +
>> +  return 0;
>> +}
> 
> Incomplete error reporting?  Any read failure doesn't make it to the
> caller.

Ack, I will fix to report read errors.

> 
> The callback interface will be easier to use if you have it return int
> and return the callback return value if it is non-zero, I think.

Ack.

> 
>> diff --git a/sysdeps/unix/sysv/linux/tst-pidfd.c b/sysdeps/unix/sysv/linux/tst-pidfd.c
>> index 64d8a2ef40..2a73328ef1 100644
>> --- a/sysdeps/unix/sysv/linux/tst-pidfd.c
>> +++ b/sysdeps/unix/sysv/linux/tst-pidfd.c
>> @@ -18,6 +18,7 @@
> 
> It would be nice to have a check for the EREMOTE case.
> 
> Thanks,
> Florian
>
  

Patch

diff --git a/NEWS b/NEWS
index 2ac69a8a2c..ae4fcc1bc1 100644
--- a/NEWS
+++ b/NEWS
@@ -33,6 +33,10 @@  Major new features:
   return a process ID, it returns a file descriptor that can be used along
   other pidfd functions.
 
+* On Linux, the pidfd_getpid has been added.  It allows to retrieve the
+  process ID associated with process file descriptor created with
+  pid_spawn, pidfd_fork, or pidfd_open.
+
 Deprecated and removed features, and other changes affecting compatibility:
 
 * In the Linux kernel for the hppa/parisc architecture some of the
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index c5cef27244..e3f15014af 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -492,8 +492,10 @@  sysdep_routines += \
   getcpu \
   oldglob \
   pidfd_fork \
+  pidfd_getpid \
   pidfd_spawn \
   pidfd_spawnp \
+  procutils \
   sched_getcpu \
   sched_getcpu \
   # sysdep_routines
diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions
index 7e4109e9c5..047bc0c92a 100644
--- a/sysdeps/unix/sysv/linux/Versions
+++ b/sysdeps/unix/sysv/linux/Versions
@@ -323,6 +323,7 @@  libc {
   }
   GLIBC_2.38 {
     pidfd_fork;
+    pidfd_getpid;
     pidfd_spawn;
     pidfd_spawnp;
   }
diff --git a/sysdeps/unix/sysv/linux/aarch64/libc.abilist b/sysdeps/unix/sysv/linux/aarch64/libc.abilist
index dbf7c44319..febfc78f29 100644
--- a/sysdeps/unix/sysv/linux/aarch64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/aarch64/libc.abilist
@@ -2666,5 +2666,6 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp F
diff --git a/sysdeps/unix/sysv/linux/alpha/libc.abilist b/sysdeps/unix/sysv/linux/alpha/libc.abilist
index a9d6ffce14..87f93beeb8 100644
--- a/sysdeps/unix/sysv/linux/alpha/libc.abilist
+++ b/sysdeps/unix/sysv/linux/alpha/libc.abilist
@@ -2775,6 +2775,7 @@  GLIBC_2.38 __nldbl___isoc23_vswscanf F
 GLIBC_2.38 __nldbl___isoc23_vwscanf F
 GLIBC_2.38 __nldbl___isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp 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 c08d548556..dd33567a75 100644
--- a/sysdeps/unix/sysv/linux/arc/libc.abilist
+++ b/sysdeps/unix/sysv/linux/arc/libc.abilist
@@ -2427,5 +2427,6 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp F
diff --git a/sysdeps/unix/sysv/linux/arm/be/libc.abilist b/sysdeps/unix/sysv/linux/arm/be/libc.abilist
index f348336ee7..2344b8ae48 100644
--- a/sysdeps/unix/sysv/linux/arm/be/libc.abilist
+++ b/sysdeps/unix/sysv/linux/arm/be/libc.abilist
@@ -547,6 +547,7 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp 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 5aed593521..b34ec3e9cc 100644
--- a/sysdeps/unix/sysv/linux/arm/le/libc.abilist
+++ b/sysdeps/unix/sysv/linux/arm/le/libc.abilist
@@ -544,6 +544,7 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp F
 GLIBC_2.4 _Exit F
diff --git a/sysdeps/unix/sysv/linux/csky/libc.abilist b/sysdeps/unix/sysv/linux/csky/libc.abilist
index 8a7022aaa8..f59d84fdb0 100644
--- a/sysdeps/unix/sysv/linux/csky/libc.abilist
+++ b/sysdeps/unix/sysv/linux/csky/libc.abilist
@@ -2703,5 +2703,6 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp F
diff --git a/sysdeps/unix/sysv/linux/hppa/libc.abilist b/sysdeps/unix/sysv/linux/hppa/libc.abilist
index e2ac6949df..f6527bb9db 100644
--- a/sysdeps/unix/sysv/linux/hppa/libc.abilist
+++ b/sysdeps/unix/sysv/linux/hppa/libc.abilist
@@ -2652,6 +2652,7 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp 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 5e8d49fc1f..28ccb54206 100644
--- a/sysdeps/unix/sysv/linux/i386/libc.abilist
+++ b/sysdeps/unix/sysv/linux/i386/libc.abilist
@@ -2836,6 +2836,7 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp 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 3cd3c80334..aabf217819 100644
--- a/sysdeps/unix/sysv/linux/ia64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/ia64/libc.abilist
@@ -2601,6 +2601,7 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp 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 0965db2ff0..8b53d82e8c 100644
--- a/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist
@@ -2187,5 +2187,6 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp F
diff --git a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
index 1ec24886a3..66bf4532ab 100644
--- a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
+++ b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
@@ -548,6 +548,7 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp 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 239b1d70be..0ee9fef825 100644
--- a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
+++ b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
@@ -2779,6 +2779,7 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp 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 dd41d63394..8861ebb4ca 100644
--- a/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist
+++ b/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist
@@ -2752,5 +2752,6 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp F
diff --git a/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist b/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist
index 55c3dcd471..008dda3746 100644
--- a/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist
+++ b/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist
@@ -2749,5 +2749,6 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp F
diff --git a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
index 295e4d8608..2d0af493ed 100644
--- a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
+++ b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
@@ -2744,6 +2744,7 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp 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 19fb1aebe7..ae82d18edb 100644
--- a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
+++ b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
@@ -2742,6 +2742,7 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp 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 2dd6ef5416..87aee7f86d 100644
--- a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
@@ -2750,6 +2750,7 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp 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 6445d27220..c0d1fd370a 100644
--- a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
@@ -2652,6 +2652,7 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp 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 c1cc5134c9..06092621b9 100644
--- a/sysdeps/unix/sysv/linux/nios2/libc.abilist
+++ b/sysdeps/unix/sysv/linux/nios2/libc.abilist
@@ -2791,5 +2791,6 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp F
diff --git a/sysdeps/unix/sysv/linux/or1k/libc.abilist b/sysdeps/unix/sysv/linux/or1k/libc.abilist
index 293643346a..88dcdd8b98 100644
--- a/sysdeps/unix/sysv/linux/or1k/libc.abilist
+++ b/sysdeps/unix/sysv/linux/or1k/libc.abilist
@@ -2173,5 +2173,6 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp F
diff --git a/sysdeps/unix/sysv/linux/pidfd_getpid.c b/sysdeps/unix/sysv/linux/pidfd_getpid.c
new file mode 100644
index 0000000000..d0c7987791
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/pidfd_getpid.c
@@ -0,0 +1,94 @@ 
+/* pidfd_getpid - Get the associated pid from the 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 <_itoa.h>
+#include <errno.h>
+#include <intprops.h>
+#include <procutils.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysdep.h>
+#include <unistd.h>
+
+#define FDINFO_TO_FILENAME_PREFIX "/proc/self/fdinfo/"
+
+#define FDINFO_FILENAME_LEN \
+  (sizeof (FDINFO_TO_FILENAME_PREFIX) + INT_STRLEN_BOUND (int))
+
+struct parse_fdinfo_t
+{
+  bool found;
+  pid_t pid;
+};
+
+static bool
+parse_fdinfo (const char *l, void *arg)
+{
+  enum { fieldlen = sizeof ("Pid:") - 1 };
+  if (strncmp (l, "Pid:", fieldlen) != 0)
+    return true;
+
+  l += fieldlen;
+
+  char *endp;
+  long int n = strtol (l, &endp, 10);
+  if (l == endp || n > INT_MAX)
+    return true;
+
+  struct parse_fdinfo_t *fdinfo = arg;
+  fdinfo->found = true;
+  fdinfo->pid = n;
+
+  return false;
+}
+
+pid_t
+pidfd_getpid (int fd)
+{
+  if (__glibc_unlikely (fd < 0))
+    {
+      __set_errno (EINVAL);
+      return -1;
+    }
+
+  char fdinfoname[FDINFO_FILENAME_LEN];
+
+  char *p = mempcpy (fdinfoname, FDINFO_TO_FILENAME_PREFIX,
+		     strlen (FDINFO_TO_FILENAME_PREFIX));
+  *_fitoa_word (fd, p, 10, 0) = '\0';
+
+  struct parse_fdinfo_t fdinfo = { .found = false, .pid = -1 };
+  if (procutils_read_file (fdinfoname, parse_fdinfo, &fdinfo) == -1)
+    /* The fdinfo contains an invalid 'Pid:' value.  */
+    return INLINE_SYSCALL_ERROR_RETURN_VALUE (EBADF);
+
+  /* The FD does not have a 'Pid:' entry associated.  */
+  if (!fdinfo.found)
+    return INLINE_SYSCALL_ERROR_RETURN_VALUE (EBADF);
+
+  /* The pidfd cannot be resolved because it is in a separate pid
+     namespace.  */
+  if (fdinfo.pid == 0)
+    return INLINE_SYSCALL_ERROR_RETURN_VALUE (EREMOTE);
+
+  /* A negative value means the process is terminated.  */
+  if (fdinfo.pid < 0)
+    return INLINE_SYSCALL_ERROR_RETURN_VALUE (ESRCH);
+
+  return fdinfo.pid;
+}
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
index 03a35e860d..ce885f9136 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
@@ -2818,6 +2818,7 @@  GLIBC_2.38 __nldbl___isoc23_vswscanf F
 GLIBC_2.38 __nldbl___isoc23_vwscanf F
 GLIBC_2.38 __nldbl___isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp 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 a91af4245b..8063a0606e 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
@@ -2851,6 +2851,7 @@  GLIBC_2.38 __nldbl___isoc23_vswscanf F
 GLIBC_2.38 __nldbl___isoc23_vwscanf F
 GLIBC_2.38 __nldbl___isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp 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 c15137b9ad..21fb95a352 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist
@@ -2572,6 +2572,7 @@  GLIBC_2.38 __nldbl___isoc23_vswscanf F
 GLIBC_2.38 __nldbl___isoc23_vwscanf F
 GLIBC_2.38 __nldbl___isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp 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 12c0f4cfba..14bda5b5b8 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist
@@ -2886,5 +2886,6 @@  GLIBC_2.38 __nldbl___isoc23_vswscanf F
 GLIBC_2.38 __nldbl___isoc23_vwscanf F
 GLIBC_2.38 __nldbl___isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp F
diff --git a/sysdeps/unix/sysv/linux/procutils.c b/sysdeps/unix/sysv/linux/procutils.c
new file mode 100644
index 0000000000..4909afeae1
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/procutils.c
@@ -0,0 +1,99 @@ 
+/* Utilities functions to read/parse Linux procfs and sysfs.
+   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 <assert.h>
+#include <not-cancel.h>
+#include <procutils.h>
+#include <string.h>
+
+static char *
+next_line (int fd, char *const buffer, char **cp, char **re,
+           char *const buffer_end)
+{
+  char *res = *cp;
+  char *nl = memchr (*cp, '\n', *re - *cp);
+  if (nl == NULL)
+    {
+      if (*cp != buffer)
+        {
+          if (*re == buffer_end)
+            {
+              memmove (buffer, *cp, *re - *cp);
+              *re = buffer + (*re - *cp);
+              *cp = buffer;
+
+              ssize_t n = __read_nocancel (fd, *re, buffer_end - *re);
+              if (n < 0)
+                return NULL;
+
+              *re += n;
+
+              nl = memchr (*cp, '\n', *re - *cp);
+              while (nl == NULL && *re == buffer_end)
+                {
+                  /* Truncate too long lines.  */
+                  *re = buffer + 3 * (buffer_end - buffer) / 4;
+                  n = __read_nocancel (fd, *re, buffer_end - *re);
+                  if (n < 0)
+                    return NULL;
+
+                  nl = memchr (*re, '\n', n);
+                  **re = '\0';
+                  *re += n;
+                }
+            }
+          else
+            nl = memchr (*cp, '\n', *re - *cp);
+
+          res = *cp;
+        }
+
+      if (nl == NULL)
+        nl = *re - 1;
+    }
+
+  *nl = '\0';
+  *cp = nl + 1;
+  assert (*cp <= *re);
+
+  return res == *re ? NULL : res;
+}
+
+int
+procutils_read_file (const char *filename, procutils_closure_t closure,
+		     void *arg)
+{
+  enum { buffer_size = 1024 };
+  char buffer[buffer_size];
+  char *buffer_end = buffer + buffer_size;
+  char *cp = buffer_end;
+  char *re = buffer_end;
+
+  int fd = __open64_nocancel (filename, O_RDONLY | O_CLOEXEC);
+  if (fd == -1)
+    return -1;
+
+  char *l;
+  while ((l = next_line (fd, buffer, &cp, &re, buffer_end)) != NULL)
+    if (!closure (l, arg))
+      break;
+
+  __close_nocancel_nostatus (fd);
+
+  return 0;
+}
diff --git a/sysdeps/unix/sysv/linux/procutils.h b/sysdeps/unix/sysv/linux/procutils.h
new file mode 100644
index 0000000000..ba564ef102
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/procutils.h
@@ -0,0 +1,37 @@ 
+/* Utilities functions to read/parse Linux procfs and sysfs.
+   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 _PROCUTILS_H
+#define _PROCUTILS_H
+
+#include <stdbool.h>
+
+typedef bool (*procutils_closure_t)(const char *line, void *arg);
+
+/* Open and read the path FILENAME, line per line, and call CLOSURE with
+   argument ARG on each line.  The read is done with a static buffer,
+   with non-cancellable calls, and the line is null terminated.
+
+   The CLOSURE should return true if the read should continue, or false
+   if the function should stop.
+
+   It returns 0 in case of success, or -1 otherwise.  */
+int procutils_read_file (const char *filename, procutils_closure_t closure,
+			 void *arg) attribute_hidden;
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist
index 9537dac42d..b294ec1021 100644
--- a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist
@@ -2429,5 +2429,6 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp F
diff --git a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist
index ea72e6b648..61921a02d4 100644
--- a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist
@@ -2629,5 +2629,6 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp F
diff --git a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist
index d6c895b487..fbc2d5826f 100644
--- a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist
@@ -2816,6 +2816,7 @@  GLIBC_2.38 __nldbl___isoc23_vswscanf F
 GLIBC_2.38 __nldbl___isoc23_vwscanf F
 GLIBC_2.38 __nldbl___isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp 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 b375f5a7ab..45ab26d7a7 100644
--- a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist
@@ -2609,6 +2609,7 @@  GLIBC_2.38 __nldbl___isoc23_vswscanf F
 GLIBC_2.38 __nldbl___isoc23_vwscanf F
 GLIBC_2.38 __nldbl___isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp 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 7b2266ecb3..8de69e0057 100644
--- a/sysdeps/unix/sysv/linux/sh/be/libc.abilist
+++ b/sysdeps/unix/sysv/linux/sh/be/libc.abilist
@@ -2659,6 +2659,7 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp 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 2f9f732044..3821ce3676 100644
--- a/sysdeps/unix/sysv/linux/sh/le/libc.abilist
+++ b/sysdeps/unix/sysv/linux/sh/le/libc.abilist
@@ -2656,6 +2656,7 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp 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 144d54ebf2..7be5b0c125 100644
--- a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
@@ -2811,6 +2811,7 @@  GLIBC_2.38 __nldbl___isoc23_vswscanf F
 GLIBC_2.38 __nldbl___isoc23_vwscanf F
 GLIBC_2.38 __nldbl___isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp 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 bc8bd8ff54..a09b2efc90 100644
--- a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
@@ -2624,6 +2624,7 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp F
 GLIBC_2.4 __confstr_chk F
diff --git a/sysdeps/unix/sysv/linux/sys/pidfd.h b/sysdeps/unix/sysv/linux/sys/pidfd.h
index e5c3bdb788..0188a16953 100644
--- a/sysdeps/unix/sysv/linux/sys/pidfd.h
+++ b/sysdeps/unix/sysv/linux/sys/pidfd.h
@@ -59,4 +59,8 @@  extern int pidfd_send_signal (int __pidfd, int __sig, siginfo_t *__info,
    of the new process to the parent process.  */
 extern int pidfd_fork (unsigned int __flags) __THROW;
 
+/* Query the process ID (PID) from process descriptor __FD.  Return the PID
+   or -1 in case of an error.  */
+extern pid_t pidfd_getpid (int __fd) __THROW;
+
 #endif /* _PIDFD_H  */
diff --git a/sysdeps/unix/sysv/linux/tst-pidfd.c b/sysdeps/unix/sysv/linux/tst-pidfd.c
index 64d8a2ef40..2a73328ef1 100644
--- a/sysdeps/unix/sysv/linux/tst-pidfd.c
+++ b/sysdeps/unix/sysv/linux/tst-pidfd.c
@@ -18,6 +18,7 @@ 
 
 #include <errno.h>
 #include <fcntl.h>
+#include <limits.h>
 #include <support/capture_subprocess.h>
 #include <support/check.h>
 #include <support/process_state.h>
@@ -27,6 +28,9 @@ 
 #include <support/xsocket.h>
 #include <sys/pidfd.h>
 #include <sys/wait.h>
+#include <stdlib.h>
+
+#include <string.h>
 
 #define REMOTE_PATH "/dev/null"
 
@@ -102,6 +106,41 @@  do_test (void)
   ppid = getpid ();
   puid = getuid ();
 
+  /* Sanity check for invalid inputs.  */
+  TEST_COMPARE (pidfd_getpid (-1), -1);
+  TEST_COMPARE (errno, EINVAL);
+
+  {
+    pid_t pid = pidfd_getpid (STDOUT_FILENO);
+    TEST_COMPARE (pid, -1);
+    TEST_COMPARE (errno, EBADF);
+  }
+
+  /* Check if pidfd_getpid return ESRCH for exited subprocess.  */
+  {
+    int pidfd = pidfd_fork (0);
+    if (pidfd == 0)
+      exit (EXIT_SUCCESS);
+
+    /* The process might be still running or already in zombie state, in any
+       case the PID is still allocated to the process.  */
+    pid_t pid = pidfd_getpid (pidfd);
+    TEST_VERIFY (pid > 0);
+    support_process_state_wait (pid, support_process_state_zombie);
+
+    siginfo_t info;
+    TEST_COMPARE (waitid (P_PIDFD, pidfd, &info, WEXITED), 0);
+    TEST_COMPARE (info.si_status, 0);
+    TEST_COMPARE (info.si_code, CLD_EXITED);
+
+    /* Once the process is reaped the associated PID is not available.  */
+    pid = pidfd_getpid (pidfd);
+    TEST_COMPARE (pid, -1);
+    TEST_COMPARE (errno, ESRCH);
+
+    xclose (pidfd);
+  }
+
   TEST_COMPARE (socketpair (AF_UNIX, SOCK_STREAM, 0, sockets), 0);
 
   pid_t pid = xfork ();
@@ -118,6 +157,12 @@  do_test (void)
   int pidfd = pidfd_open (pid, 0);
   TEST_VERIFY (pidfd != -1);
 
+  TEST_COMPARE (pidfd_getpid (INT_MAX), -1);
+  {
+    pid_t querypid = pidfd_getpid (pidfd);
+    TEST_COMPARE (querypid, pid);
+  }
+
   /* Wait for first sigtimedwait.  */
   support_process_state_wait (pid, support_process_state_sleeping);
   TEST_COMPARE (pidfd_send_signal (pidfd, SIGUSR1, NULL, 0), 0);
diff --git a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
index 045205de0c..0726b6e8d0 100644
--- a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
@@ -2575,6 +2575,7 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp 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 5a67604948..9c1f6830ba 100644
--- a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist
@@ -2681,5 +2681,6 @@  GLIBC_2.38 __isoc23_wcstoull_l F
 GLIBC_2.38 __isoc23_wcstoumax F
 GLIBC_2.38 __isoc23_wscanf F
 GLIBC_2.38 pidfd_fork F
+GLIBC_2.38 pidfd_getpid F
 GLIBC_2.38 pidfd_spawn F
 GLIBC_2.38 pidfd_spawnp F