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 re‐
16 turned 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 us‐
53 ing 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., 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 The pidfd_open() system call is the preferred way of obtaining a PID
111 file descriptor for an already existing process. The alternative is to
112 obtain a file descriptor by opening a /proc/[pid] directory. However,
113 the latter technique is possible only if the proc(5) filesystem is
114 mounted; furthermore, the file descriptor obtained in this way is not
115 pollable and can't be waited on with waitid(2).
116
118 The program below opens a PID file descriptor for the process whose PID
119 is specified as its command-line argument. It then uses poll(2) to
120 monitor the file descriptor for process exit, as indicated by an
121 EPOLLIN event.
122
123 Program source
124
125 #define _GNU_SOURCE
126 #include <sys/types.h>
127 #include <sys/syscall.h>
128 #include <unistd.h>
129 #include <poll.h>
130 #include <stdlib.h>
131 #include <stdio.h>
132
133 #ifndef __NR_pidfd_open
134 #define __NR_pidfd_open 434 /* System call # on most architectures */
135 #endif
136
137 static int
138 pidfd_open(pid_t pid, unsigned int flags)
139 {
140 return syscall(__NR_pidfd_open, pid, flags);
141 }
142
143 int
144 main(int argc, char *argv[])
145 {
146 struct pollfd pollfd;
147 int pidfd, ready;
148
149 if (argc != 2) {
150 fprintf(stderr, "Usage: %s <pid>\n", argv[0]);
151 exit(EXIT_SUCCESS);
152 }
153
154 pidfd = pidfd_open(atoi(argv[1]), 0);
155 if (pidfd == -1) {
156 perror("pidfd_open");
157 exit(EXIT_FAILURE);
158 }
159
160 pollfd.fd = pidfd;
161 pollfd.events = POLLIN;
162
163 ready = poll(&pollfd, 1, -1);
164 if (ready == -1) {
165 perror("poll");
166 exit(EXIT_FAILURE);
167 }
168
169 printf("Events (%#x): POLLIN is %sset\n", pollfd.revents,
170 (pollfd.revents & POLLIN) ? "" : "not ");
171
172 close(pidfd);
173 exit(EXIT_SUCCESS);
174 }
175
177 clone(2), kill(2), pidfd_getfd(2), pidfd_send_signal(2), poll(2), se‐
178 lect(2), setns(2), waitid(2), epoll(7)
179
181 This page is part of release 5.10 of the Linux man-pages project. A
182 description of the project, information about reporting bugs, and the
183 latest version of this page, can be found at
184 https://www.kernel.org/doc/man-pages/.
185
186
187
188Linux 2020-08-13 PIDFD_OPEN(2)