From e7190fc73dbc8a1f8f94f8ccacd9a190fa5e609c Mon Sep 17 00:00:00 2001 From: Adhemerval Zanella Netto Date: Thu, 24 Aug 2023 13:42:19 -0300 Subject: linux: Add pidfd_getpid This interface allows to obtain the associated process 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 sets the errno accordingly. The possible errno values are those from open, read, and close (used on procps parsing), along with: - EBADF if the FD is negative, does not have a PID associated, or if the fdinfo fields contain 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_PIDFD or waitid support), Linux 5.4 (full support), and Linux 6.2. Reviewed-by: Florian Weimer --- manual/process.texi | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'manual') diff --git a/manual/process.texi b/manual/process.texi index 68361c3f61..8254e5ee86 100644 --- a/manual/process.texi +++ b/manual/process.texi @@ -33,6 +33,7 @@ primitive functions to do each step individually instead. * Process Creation Concepts:: An overview of the hard way to do it. * Process Identification:: How to get the process ID of a process. * Creating a Process:: How to fork a child process. +* Querying a Process:: How to query a child process. * Executing a File:: How to make a process execute another program. * Process Completion:: How to tell when a child process has completed. * Process Completion Status:: How to interpret the status value @@ -362,6 +363,43 @@ the proper precautions for using @code{vfork}, your program will still work even if the system uses @code{fork} instead. @end deftypefun +@node Querying a Process +@section Querying a Process + +The file descriptor returned by the @code{pidfd_fork} function can be used to +query process extra information. + +@deftypefun pid_t pidfd_getpid (int @var{fd}) +@standards{GNU, sys/pidfd.h} +@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} + +The @code{pidfd_getpid} function retrieves the process ID associated with process +file descriptor created with @code{pid_spawn}, @code{pidfd_fork}, or +@code{pidfd_open}. + +If the operation fails, @code{pidfd_getpid} return @code{-1} and the following +@code{errno} error conditionas are defined: + +@table @code +@item EBADF +The input file descriptor is invalid, does not have a pidfd associated, or an +error has occurred parsing the kernel data. +@item EREMOTE +There is no process ID to denote the process in the current namespace. +@item ESRCH +The process for which the file descriptor refers to is terminated. +@item ENOENT +The procfs is not mounted. +@item ENFILE. +Too many open files in system (@code{pidfd_open} tries to open a procfs file and +read its contents). +@item ENOMEM +Insufficient kernel memory was available. +@end table + +This function is specific to Linux. +@end deftypefun + @node Executing a File @section Executing a File @cindex executing a file -- cgit 1.4.1