1PIDFD_OPEN(2)              Linux Programmer's Manual             PIDFD_OPEN(2)
2
3
4

NAME

6       pidfd_open - obtain a file descriptor that refers to a process
7

SYNOPSIS

9       #include <sys/types.h>
10
11       int pidfd_open(pid_t pid, unsigned int flags);
12

DESCRIPTION

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

RETURN VALUE

23       On  success,  pidfd_open()  returns  a nonnegative file descriptor.  On
24       error, -1 is returned and errno is set to indicate  the  cause  of  the
25       error.
26

ERRORS

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

VERSIONS

46       pidfd_open() first appeared in Linux 5.3.
47

CONFORMING TO

49       pidfd_open() is Linux specific.
50

NOTES

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() (of 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_open()  system  call is the preferred way of obtaining a PID
103       file descriptor for an already existing process.  The alternative is to
104       obtain  a file descriptor by opening a /proc/[pid] directory.  However,
105       the latter technique is possible only  if  the  proc(5)  filesystem  is
106       mounted;  furthermore,  the file descriptor obtained in this way is not
107       pollable and can't be waited on with waitid(2).
108

EXAMPLE

110       The program below opens a PID file descriptor for the process whose PID
111       is  specified  as  its  command-line argument.  It then uses poll(2) to
112       monitor the file descriptor  for  process  exit,  as  indicated  by  an
113       EPOLLIN event.
114
115   Program source
116
117       #define _GNU_SOURCE
118       #include <sys/types.h>
119       #include <sys/syscall.h>
120       #include <unistd.h>
121       #include <poll.h>
122       #include <stdlib.h>
123       #include <stdio.h>
124
125       #ifndef __NR_pidfd_open
126       #define __NR_pidfd_open 434   /* System call # on most architectures */
127       #endif
128
129       static int
130       pidfd_open(pid_t pid, unsigned int flags)
131       {
132           return syscall(__NR_pidfd_open, pid, flags);
133       }
134
135       int
136       main(int argc, char *argv[])
137       {
138           struct pollfd pollfd;
139           int pidfd, ready;
140
141           if (argc != 2) {
142               fprintf(stderr, "Usage: %s <pid>\n", argv[0]);
143               exit(EXIT_SUCCESS);
144           }
145
146           pidfd = pidfd_open(atoi(argv[1]), 0);
147           if (pidfd == -1) {
148               perror("pidfd_open");
149               exit(EXIT_FAILURE);
150           }
151
152           pollfd.fd = pidfd;
153           pollfd.events = POLLIN;
154
155           ready = poll(&pollfd, 1, -1);
156           if (ready == -1) {
157               perror("poll");
158               exit(EXIT_FAILURE);
159           }
160
161           printf("Events (0x%x): POLLIN is %sset\n", pollfd.revents,
162                   (pollfd.revents & POLLIN) ? "" : "not ");
163
164           exit(EXIT_SUCCESS);
165       }
166

SEE ALSO

168       clone(2), kill(2), pidfd_send_signal(2), poll(2), select(2), waitid(2),
169       epoll(7)
170

COLOPHON

172       This page is part of release 5.04 of the Linux  man-pages  project.   A
173       description  of  the project, information about reporting bugs, and the
174       latest    version    of    this    page,    can     be     found     at
175       https://www.kernel.org/doc/man-pages/.
176
177
178
179Linux                             2019-11-19                     PIDFD_OPEN(2)
Impressum