1PIDFD_OPEN(2)              Linux Programmer's Manual             PIDFD_OPEN(2)
2
3
4

NAME

6       pidfd_open - obtain a file descriptor that refers to a process
7

SYNOPSIS

9       #include <sys/syscall.h>      /* Definition of SYS_* constants */
10       #include <unistd.h>
11
12       int syscall(SYS_pidfd_open, pid_t pid, unsigned int flags);
13
14       Note: glibc provides no wrapper for pidfd_open(), necessitating the use
15       of syscall(2).
16

DESCRIPTION

18       The pidfd_open() system call creates a file descriptor that  refers  to
19       the  process whose PID is specified in pid.  The file descriptor is re‐
20       turned as the function result; the close-on-exec flag  is  set  on  the
21       file descriptor.
22
23       The flags argument is reserved for future use; currently, this argument
24       must be specified as 0.
25

RETURN VALUE

27       On success, pidfd_open() returns a file descriptor (a nonnegative inte‐
28       ger).  On error, -1 is returned and errno is set to indicate the error.
29

ERRORS

31       EINVAL flags is not 0.
32
33       EINVAL pid is not valid.
34
35       EMFILE The per-process limit on the number of open file descriptors has
36              been reached (see the  description  of  RLIMIT_NOFILE  in  getr‐
37              limit(2)).
38
39       ENFILE The system-wide limit on the total number of open files has been
40              reached.
41
42       ENODEV The anonymous inode filesystem is not available in this kernel.
43
44       ENOMEM Insufficient kernel memory was available.
45
46       ESRCH  The process specified by pid does not exist.
47

VERSIONS

49       pidfd_open() first appeared in Linux 5.3.
50

CONFORMING TO

52       pidfd_open() is Linux specific.
53

NOTES

55       The following code sequence can be used to obtain a file descriptor for
56       the child of fork(2):
57
58           pid = fork();
59           if (pid > 0) {     /* If parent */
60               pidfd = pidfd_open(pid, 0);
61               ...
62           }
63
64       Even   if  the  child  has  already  terminated  by  the  time  of  the
65       pidfd_open() call, its PID will not have been recycled and the returned
66       file descriptor will refer to the resulting zombie process.  Note, how‐
67       ever, that this is guaranteed only if  the  following  conditions  hold
68       true:
69
70       • the  disposition  of  SIGCHLD  has not been explicitly set to SIG_IGN
71         (see sigaction(2));
72
73       • the SA_NOCLDWAIT flag was not specified while establishing a  handler
74         for  SIGCHLD  or  while  setting  the  disposition  of that signal to
75         SIG_DFL (see sigaction(2)); and
76
77       • the zombie process was not reaped elsewhere in the program (e.g., ei‐
78         ther  by  an  asynchronously executed signal handler or by wait(2) or
79         similar in another thread).
80
81       If any of these conditions does not hold, then the child process (along
82       with a PID file descriptor that refers to it) should instead be created
83       using clone(2) with the CLONE_PIDFD flag.
84
85   Use cases for PID file descriptors
86       A PID file descriptor returned by pidfd_open() (or by clone(2) with the
87       CLONE_PID flag) can be used for the following purposes:
88
89       • The  pidfd_send_signal(2) system call can be used to send a signal to
90         the process referred to by a PID file descriptor.
91
92       • A PID file descriptor can be monitored using poll(2), select(2),  and
93         epoll(7).   When  the process that it refers to terminates, these in‐
94         terfaces indicate the file descriptor as  readable.   Note,  however,
95         that in the current implementation, nothing can be read from the file
96         descriptor (read(2) on the file descriptor fails with the error  EIN‐
97         VAL).
98
99       • If  the PID file descriptor refers to a child of the calling process,
100         then it can be waited on using waitid(2).
101
102       • The pidfd_getfd(2) system call can be used to obtain a duplicate of a
103         file descriptor of another process referred to by a PID file descrip‐
104         tor.
105
106       • A PID file descriptor can be used as the argument of setns(2) in  or‐
107         der  to  move  into one or more of the same namespaces as the process
108         referred to by the file descriptor.
109
110       • A PID file descriptor can be used as  the  argument  of  process_mad‐
111         vise(2)  in  order  to provide advice on the memory usage patterns of
112         the process referred to by the file descriptor.
113
114       The pidfd_open() system call is the preferred way of  obtaining  a  PID
115       file descriptor for an already existing process.  The alternative is to
116       obtain a file descriptor by opening a /proc/[pid] directory.   However,
117       the  latter  technique  is  possible  only if the proc(5) filesystem is
118       mounted; furthermore, the file descriptor obtained in this way  is  not
119       pollable and can't be waited on with waitid(2).
120

EXAMPLES

122       The program below opens a PID file descriptor for the process whose PID
123       is specified as its command-line argument.  It  then  uses  poll(2)  to
124       monitor  the  file  descriptor  for  process  exit,  as indicated by an
125       EPOLLIN event.
126
127   Program source
128
129       #define _GNU_SOURCE
130       #include <sys/types.h>
131       #include <sys/syscall.h>
132       #include <unistd.h>
133       #include <poll.h>
134       #include <stdlib.h>
135       #include <stdio.h>
136
137       #ifndef __NR_pidfd_open
138       #define __NR_pidfd_open 434   /* System call # on most architectures */
139       #endif
140
141       static int
142       pidfd_open(pid_t pid, unsigned int flags)
143       {
144           return syscall(__NR_pidfd_open, pid, flags);
145       }
146
147       int
148       main(int argc, char *argv[])
149       {
150           struct pollfd pollfd;
151           int pidfd, ready;
152
153           if (argc != 2) {
154               fprintf(stderr, "Usage: %s <pid>\n", argv[0]);
155               exit(EXIT_SUCCESS);
156           }
157
158           pidfd = pidfd_open(atoi(argv[1]), 0);
159           if (pidfd == -1) {
160               perror("pidfd_open");
161               exit(EXIT_FAILURE);
162           }
163
164           pollfd.fd = pidfd;
165           pollfd.events = POLLIN;
166
167           ready = poll(&pollfd, 1, -1);
168           if (ready == -1) {
169               perror("poll");
170               exit(EXIT_FAILURE);
171           }
172
173           printf("Events (%#x): POLLIN is %sset\n", pollfd.revents,
174                   (pollfd.revents & POLLIN) ? "" : "not ");
175
176           close(pidfd);
177           exit(EXIT_SUCCESS);
178       }
179

SEE ALSO

181       clone(2),  kill(2),  pidfd_getfd(2),   pidfd_send_signal(2),   poll(2),
182       process_madvise(2), select(2), setns(2), waitid(2), epoll(7)
183

COLOPHON

185       This  page  is  part of release 5.12 of the Linux man-pages project.  A
186       description of the project, information about reporting bugs,  and  the
187       latest     version     of     this    page,    can    be    found    at
188       https://www.kernel.org/doc/man-pages/.
189
190
191
192Linux                             2021-03-22                     PIDFD_OPEN(2)
Impressum