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 2020-04-11 "Linux" "Linux Programmer's Manual"
27 pidfd_open \- obtain a file descriptor that refers to a process
30 .B #include <sys/types.h>
32 .BI "int pidfd_open(pid_t " pid ", unsigned int " flags );
37 system call creates a file descriptor that refers to
38 the process whose PID is specified in
40 The file descriptor is returned as the function result;
41 the close-on-exec flag is set on the file descriptor.
45 argument is reserved for future use;
46 currently, this argument must be specified as 0.
50 returns a file descriptor (a nonnegative integer).
51 On error, \-1 is returned and
53 is set to indicate the cause of the error.
65 The per-process limit on the number of open file descriptors has been reached
66 (see the description of
72 The system-wide limit on the total number of open files has been reached.
75 The anonymous inode filesystem is not available in this kernel.
78 Insufficient kernel memory was available.
81 The process specified by
86 first appeared in Linux 5.3.
91 Currently, there is no glibc wrapper for this system call; call it using
94 The following code sequence can be used to obtain a file descriptor
101 if (pid > 0) { /* If parent */
102 pidfd = pidfd_open(pid, 0);
108 Even if the child has already terminated by the time of the
110 call, its PID will not have been recycled and the returned
111 file descriptor will refer to the resulting zombie process.
112 Note, however, that this is guaranteed only if the following
113 conditions hold true:
117 has not been explicitly set to
124 flag was not specified while establishing a handler for
126 or while setting the disposition of that signal to
132 the zombie process was not reaped elsewhere in the program
133 (e.g., either by an asynchronously executed signal handler or by
135 or similar in another thread).
137 If any of these conditions does not hold,
138 then the child process (along with a PID file descriptor that refers to it)
139 should instead be created using
145 .SS Use cases for PID file descriptors
147 A PID file descriptor returned by
153 flag) can be used for the following purposes:
156 .BR pidfd_send_signal (2)
157 system call can be used to send a signal to the process referred to by
158 a PID file descriptor.
160 A PID file descriptor can be monitored using
165 When the process that it refers to terminates,
166 these interfaces indicate the file descriptor as readable.
167 Note, however, that in the current implementation,
168 nothing can be read from the file descriptor
170 on the file descriptor fails with the error
173 If the PID file descriptor refers to a child of the calling process,
174 then it can be waited on using
179 system call can be used to obtain a duplicate of a file descriptor
180 of another process referred to by a PID file descriptor.
184 system call is the preferred way of obtaining a PID file descriptor
185 for an already existing process.
186 The alternative is to obtain a file descriptor by opening a
189 However, the latter technique is possible only if the
191 filesystem is mounted;
192 furthermore, the file descriptor obtained in this way is
194 pollable and can't be waited on with
197 The program below opens a PID file descriptor for the
198 process whose PID is specified as its command-line argument.
201 to monitor the file descriptor for process exit, as indicated by an
209 #include <sys/types.h>
210 #include <sys/syscall.h>
216 #ifndef __NR_pidfd_open
217 #define __NR_pidfd_open 434 /* System call # on most architectures */
221 pidfd_open(pid_t pid, unsigned int flags)
223 return syscall(__NR_pidfd_open, pid, flags);
227 main(int argc, char *argv[])
229 struct pollfd pollfd;
233 fprintf(stderr, "Usage: %s <pid>\en", argv[0]);
237 pidfd = pidfd_open(atoi(argv[1]), 0);
239 perror("pidfd_open");
244 pollfd.events = POLLIN;
246 ready = poll(&pollfd, 1, \-1);
252 printf("Events (0x%x): POLLIN is %sset\en", pollfd.revents,
253 (pollfd.revents & POLLIN) ? "" : "not ");
262 .BR pidfd_send_signal (2),