1 .\" Copyright (c) 2019 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH PIDFD_OPEN 2 2021-08-27 "Linux" "Linux Programmer's Manual"
27 pidfd_open \- obtain a file descriptor that refers to a process
30 .BR "#include <sys/syscall.h>" " /* Definition of " SYS_* " constants */"
31 .B #include <unistd.h>
33 .BI "int syscall(SYS_pidfd_open, pid_t " pid ", unsigned int " flags );
37 glibc provides no wrapper for
39 necessitating the use of
44 system call creates a file descriptor that refers to
45 the process whose PID is specified in
47 The file descriptor is returned as the function result;
48 the close-on-exec flag is set on the file descriptor.
52 argument either has the value 0, or contains the following flag:
54 .BR PIDFD_NONBLOCK " (since Linux 5.10)"
55 .\" commit 4da9af0014b51c8b015ed8c622440ef28912efe6
56 Return a nonblocking file descriptor.
57 If the process referred to by the file descriptor has not yet terminated,
58 then an attempt to wait on the file descriptor using
60 will immediately return the error
66 returns a file descriptor (a nonnegative integer).
67 On error, \-1 is returned and
69 is set to indicate the error.
81 The per-process limit on the number of open file descriptors has been reached
82 (see the description of
88 The system-wide limit on the total number of open files has been reached.
91 The anonymous inode filesystem is not available in this kernel.
94 Insufficient kernel memory was available.
97 The process specified by
102 first appeared in Linux 5.3.
107 The following code sequence can be used to obtain a file descriptor
114 if (pid > 0) { /* If parent */
115 pidfd = pidfd_open(pid, 0);
121 Even if the child has already terminated by the time of the
123 call, its PID will not have been recycled and the returned
124 file descriptor will refer to the resulting zombie process.
125 Note, however, that this is guaranteed only if the following
126 conditions hold true:
130 has not been explicitly set to
137 flag was not specified while establishing a handler for
139 or while setting the disposition of that signal to
145 the zombie process was not reaped elsewhere in the program
146 (e.g., either by an asynchronously executed signal handler or by
148 or similar in another thread).
150 If any of these conditions does not hold,
151 then the child process (along with a PID file descriptor that refers to it)
152 should instead be created using
158 .SS Use cases for PID file descriptors
159 A PID file descriptor returned by
165 flag) can be used for the following purposes:
168 .BR pidfd_send_signal (2)
169 system call can be used to send a signal to the process referred to by
170 a PID file descriptor.
172 A PID file descriptor can be monitored using
177 When the process that it refers to terminates,
178 these interfaces indicate the file descriptor as readable.
179 Note, however, that in the current implementation,
180 nothing can be read from the file descriptor
182 on the file descriptor fails with the error
185 If the PID file descriptor refers to a child of the calling process,
186 then it can be waited on using
191 system call can be used to obtain a duplicate of a file descriptor
192 of another process referred to by a PID file descriptor.
194 A PID file descriptor can be used as the argument of
196 in order to move into one or more of the same namespaces as the process
197 referred to by the file descriptor.
199 A PID file descriptor can be used as the argument of
200 .BR process_madvise (2)
201 in order to provide advice on the memory usage patterns of the process
202 referred to by the file descriptor.
206 system call is the preferred way of obtaining a PID file descriptor
207 for an already existing process.
208 The alternative is to obtain a file descriptor by opening a
211 However, the latter technique is possible only if the
213 filesystem is mounted;
214 furthermore, the file descriptor obtained in this way is
216 pollable and can't be waited on with
219 The program below opens a PID file descriptor for the
220 process whose PID is specified as its command-line argument.
223 to monitor the file descriptor for process exit, as indicated by an
231 #include <sys/types.h>
232 #include <sys/syscall.h>
238 #ifndef __NR_pidfd_open
239 #define __NR_pidfd_open 434 /* System call # on most architectures */
243 pidfd_open(pid_t pid, unsigned int flags)
245 return syscall(__NR_pidfd_open, pid, flags);
249 main(int argc, char *argv[])
251 struct pollfd pollfd;
255 fprintf(stderr, "Usage: %s <pid>\en", argv[0]);
259 pidfd = pidfd_open(atoi(argv[1]), 0);
261 perror("pidfd_open");
266 pollfd.events = POLLIN;
268 ready = poll(&pollfd, 1, \-1);
274 printf("Events (%#x): POLLIN is %sset\en", pollfd.revents,
275 (pollfd.revents & POLLIN) ? "" : "not ");
285 .BR pidfd_send_signal (2),
287 .BR process_madvise (2),