1PIDFD_OPEN(2) Linux Programmer's Manual PIDFD_OPEN(2)
2
3
4
6 pidfd_open - obtain a file descriptor that refers to a process
7
9 #include <sys/types.h>
10
11 int pidfd_open(pid_t pid, unsigned int flags);
12
14 The pidfd_open() system call creates a file descriptor that refers to
15 the process whose PID is specified in pid. The file descriptor is
16 returned as the function result; the close-on-exec flag is set on the
17 file descriptor.
18
19 The flags argument is reserved for future use; currently, this argument
20 must be specified as 0.
21
23 On success, pidfd_open() returns a file descriptor (a nonnegative inte‐
24 ger). On error, -1 is returned and errno is set to indicate the cause
25 of the error.
26
28 EINVAL flags is not 0.
29
30 EINVAL pid is not valid.
31
32 EMFILE The per-process limit on the number of open file descriptors has
33 been reached (see the description of RLIMIT_NOFILE in getr‐
34 limit(2)).
35
36 ENFILE The system-wide limit on the total number of open files has been
37 reached.
38
39 ENODEV The anonymous inode filesystem is not available in this kernel.
40
41 ENOMEM Insufficient kernel memory was available.
42
43 ESRCH The process specified by pid does not exist.
44
46 pidfd_open() first appeared in Linux 5.3.
47
49 pidfd_open() is Linux specific.
50
52 Currently, there is no glibc wrapper for this system call; call it
53 using syscall(2).
54
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.,
78 either 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
94 interfaces indicate the file descriptor as readable. Note, however,
95 that in the current implementation, nothing can be read from the
96 file descriptor (read(2) on the file descriptor fails with the error
97 EINVAL).
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
103 a file descriptor of another process referred to by a PID file
104 descriptor.
105
106 The pidfd_open() system call is the preferred way of obtaining a PID
107 file descriptor for an already existing process. The alternative is to
108 obtain a file descriptor by opening a /proc/[pid] directory. However,
109 the latter technique is possible only if the proc(5) filesystem is
110 mounted; furthermore, the file descriptor obtained in this way is not
111 pollable and can't be waited on with waitid(2).
112
114 The program below opens a PID file descriptor for the process whose PID
115 is specified as its command-line argument. It then uses poll(2) to
116 monitor the file descriptor for process exit, as indicated by an
117 EPOLLIN event.
118
119 Program source
120
121 #define _GNU_SOURCE
122 #include <sys/types.h>
123 #include <sys/syscall.h>
124 #include <unistd.h>
125 #include <poll.h>
126 #include <stdlib.h>
127 #include <stdio.h>
128
129 #ifndef __NR_pidfd_open
130 #define __NR_pidfd_open 434 /* System call # on most architectures */
131 #endif
132
133 static int
134 pidfd_open(pid_t pid, unsigned int flags)
135 {
136 return syscall(__NR_pidfd_open, pid, flags);
137 }
138
139 int
140 main(int argc, char *argv[])
141 {
142 struct pollfd pollfd;
143 int pidfd, ready;
144
145 if (argc != 2) {
146 fprintf(stderr, "Usage: %s <pid>\n", argv[0]);
147 exit(EXIT_SUCCESS);
148 }
149
150 pidfd = pidfd_open(atoi(argv[1]), 0);
151 if (pidfd == -1) {
152 perror("pidfd_open");
153 exit(EXIT_FAILURE);
154 }
155
156 pollfd.fd = pidfd;
157 pollfd.events = POLLIN;
158
159 ready = poll(&pollfd, 1, -1);
160 if (ready == -1) {
161 perror("poll");
162 exit(EXIT_FAILURE);
163 }
164
165 printf("Events (0x%x): POLLIN is %sset\n", pollfd.revents,
166 (pollfd.revents & POLLIN) ? "" : "not ");
167
168 exit(EXIT_SUCCESS);
169 }
170
172 clone(2), kill(2), pidfd_getfd(2), pidfd_send_signal(2), poll(2),
173 select(2), waitid(2), epoll(7)
174
176 This page is part of release 5.07 of the Linux man-pages project. A
177 description of the project, information about reporting bugs, and the
178 latest version of this page, can be found at
179 https://www.kernel.org/doc/man-pages/.
180
181
182
183Linux 2020-04-11 PIDFD_OPEN(2)