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
>
@@ -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
@@ -492,8 +492,10 @@ sysdep_routines += \
getcpu \
oldglob \
pidfd_fork \
+ pidfd_getpid \
pidfd_spawn \
pidfd_spawnp \
+ procutils \
sched_getcpu \
sched_getcpu \
# sysdep_routines
@@ -323,6 +323,7 @@ libc {
}
GLIBC_2.38 {
pidfd_fork;
+ pidfd_getpid;
pidfd_spawn;
pidfd_spawnp;
}
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
new file mode 100644
@@ -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;
+}
@@ -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
@@ -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
@@ -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
@@ -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
new file mode 100644
@@ -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;
+}
new file mode 100644
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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 */
@@ -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);
@@ -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
@@ -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