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-03-22 "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 is reserved for future use;
53 currently, this argument must be specified as 0.
57 returns a file descriptor (a nonnegative integer).
58 On error, \-1 is returned and
60 is set to indicate the error.
72 The per-process limit on the number of open file descriptors has been reached
73 (see the description of
79 The system-wide limit on the total number of open files has been reached.
82 The anonymous inode filesystem is not available in this kernel.
85 Insufficient kernel memory was available.
88 The process specified by
93 first appeared in Linux 5.3.
98 The following code sequence can be used to obtain a file descriptor
105 if (pid > 0) { /* If parent */
106 pidfd = pidfd_open(pid, 0);
112 Even if the child has already terminated by the time of the
114 call, its PID will not have been recycled and the returned
115 file descriptor will refer to the resulting zombie process.
116 Note, however, that this is guaranteed only if the following
117 conditions hold true:
121 has not been explicitly set to
128 flag was not specified while establishing a handler for
130 or while setting the disposition of that signal to
136 the zombie process was not reaped elsewhere in the program
137 (e.g., either by an asynchronously executed signal handler or by
139 or similar in another thread).
141 If any of these conditions does not hold,
142 then the child process (along with a PID file descriptor that refers to it)
143 should instead be created using
149 .SS Use cases for PID file descriptors
150 A PID file descriptor returned by
156 flag) can be used for the following purposes:
159 .BR pidfd_send_signal (2)
160 system call can be used to send a signal to the process referred to by
161 a PID file descriptor.
163 A PID file descriptor can be monitored using
168 When the process that it refers to terminates,
169 these interfaces indicate the file descriptor as readable.
170 Note, however, that in the current implementation,
171 nothing can be read from the file descriptor
173 on the file descriptor fails with the error
176 If the PID file descriptor refers to a child of the calling process,
177 then it can be waited on using
182 system call can be used to obtain a duplicate of a file descriptor
183 of another process referred to by a PID file descriptor.
185 A PID file descriptor can be used as the argument of
187 in order to move into one or more of the same namespaces as the process
188 referred to by the file descriptor.
190 A PID file descriptor can be used as the argument of
191 .BR process_madvise (2)
192 in order to provide advice on the memory usage patterns of the process
193 referred to by the file descriptor.
197 system call is the preferred way of obtaining a PID file descriptor
198 for an already existing process.
199 The alternative is to obtain a file descriptor by opening a
202 However, the latter technique is possible only if the
204 filesystem is mounted;
205 furthermore, the file descriptor obtained in this way is
207 pollable and can't be waited on with
210 The program below opens a PID file descriptor for the
211 process whose PID is specified as its command-line argument.
214 to monitor the file descriptor for process exit, as indicated by an
222 #include <sys/types.h>
223 #include <sys/syscall.h>
229 #ifndef __NR_pidfd_open
230 #define __NR_pidfd_open 434 /* System call # on most architectures */
234 pidfd_open(pid_t pid, unsigned int flags)
236 return syscall(__NR_pidfd_open, pid, flags);
240 main(int argc, char *argv[])
242 struct pollfd pollfd;
246 fprintf(stderr, "Usage: %s <pid>\en", argv[0]);
250 pidfd = pidfd_open(atoi(argv[1]), 0);
252 perror("pidfd_open");
257 pollfd.events = POLLIN;
259 ready = poll(&pollfd, 1, \-1);
265 printf("Events (%#x): POLLIN is %sset\en", pollfd.revents,
266 (pollfd.revents & POLLIN) ? "" : "not ");
276 .BR pidfd_send_signal (2),
278 .BR process_madvise (2),