man-pages/man2/pidfd_open.2.html
2021-03-31 01:06:50 +01:00

381 lines
8.9 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of PIDFD_OPEN</TITLE>
</HEAD><BODY>
<H1>PIDFD_OPEN</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-11-19<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
pidfd_open - obtain a file descriptor that refers to a process
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>&gt;</B>
<B>int pidfd_open(pid_t </B><I>pid</I><B>, unsigned int </B><I>flags</I><B>);</B>
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>pidfd_open</B>()
system call creates a file descriptor that refers to
the process whose PID is specified in
<I>pid</I>.
The file descriptor is returned as the function result;
the close-on-exec flag is set on the file descriptor.
<P>
The
<I>flags</I>
argument is reserved for future use;
currently, this argument must be specified as 0.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success,
<B>pidfd_open</B>()
returns a nonnegative file descriptor.
On error, -1 is returned and
<I>errno</I>
is set to indicate the cause of the error.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="1"><B>EINVAL</B>
<DD>
<I>flags</I>
is not 0.
<DT id="2"><B>EINVAL</B>
<DD>
<I>pid</I>
is not valid.
<DT id="3"><B>EMFILE</B>
<DD>
The per-process limit on the number of open file descriptors has been reached
(see the description of
<B>RLIMIT_NOFILE</B>
in
<B><A HREF="/cgi-bin/man/man2html?2+getrlimit">getrlimit</A></B>(2)).
<DT id="4"><B>ENFILE</B>
<DD>
The system-wide limit on the total number of open files has been reached.
<DT id="5"><B>ENODEV</B>
<DD>
The anonymous inode filesystem is not available in this kernel.
<DT id="6"><B>ENOMEM</B>
<DD>
Insufficient kernel memory was available.
<DT id="7"><B>ESRCH</B>
<DD>
The process specified by
<I>pid</I>
does not exist.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
<B>pidfd_open</B>()
first appeared in Linux 5.3.
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
<B>pidfd_open</B>()
is Linux specific.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
Currently, there is no glibc wrapper for this system call; call it using
<B><A HREF="/cgi-bin/man/man2html?2+syscall">syscall</A></B>(2).
<P>
The following code sequence can be used to obtain a file descriptor
for the child of
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2):
<P>
pid = fork();
if (pid &gt; 0) { /* If parent */
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pidfd&nbsp;=&nbsp;pidfd_open(pid,&nbsp;0);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;...
}
<P>
Even if the child has already terminated by the time of the
<B>pidfd_open</B>()
call, its PID will not have been recycled and the returned
file descriptor will refer to the resulting zombie process.
Note, however, that this is guaranteed only if the following
conditions hold true:
<DL COMPACT>
<DT id="8">*<DD>
the disposition of
<B>SIGCHLD</B>
has not been explicitly set to
<B>SIG_IGN</B>
(see
<B><A HREF="/cgi-bin/man/man2html?2+sigaction">sigaction</A></B>(2));
<DT id="9">*<DD>
the
<B>SA_NOCLDWAIT</B>
flag was not specified while establishing a handler for
<B>SIGCHLD</B>
or while setting the disposition of that signal to
<B>SIG_DFL</B>
(see
<B><A HREF="/cgi-bin/man/man2html?2+sigaction">sigaction</A></B>(2));
and
<DT id="10">*<DD>
the zombie process was not reaped elsewhere in the program
(e.g., either by an asynchronously executed signal handler or by
<B><A HREF="/cgi-bin/man/man2html?2+wait">wait</A></B>(2)
or similar in another thread).
</DL>
<P>
If any of these conditions does not hold,
then the child process (along with a PID file descriptor that refers to it)
should instead be created using
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2)
with the
<B>CLONE_PIDFD</B>
flag.
<A NAME="lbAJ">&nbsp;</A>
<H3>Use cases for PID file descriptors</H3>
<P>
A PID file descriptor returned by
<B>pidfd_open</B>()
(of by
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2)
with the
<B>CLONE_PID</B>
flag) can be used for the following purposes:
<DL COMPACT>
<DT id="11">*<DD>
The
<B><A HREF="/cgi-bin/man/man2html?2+pidfd_send_signal">pidfd_send_signal</A></B>(2)
system call can be used to send a signal to the process referred to by
a PID file descriptor.
<DT id="12">*<DD>
A PID file descriptor can be monitored using
<B><A HREF="/cgi-bin/man/man2html?2+poll">poll</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+select">select</A></B>(2),
and
<B><A HREF="/cgi-bin/man/man2html?7+epoll">epoll</A></B>(7).
When the process that it refers to terminates,
these interfaces indicate the file descriptor as readable.
Note, however, that in the current implementation,
nothing can be read from the file descriptor
(<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)
on the file descriptor fails with the error
<B>EINVAL</B>).
<DT id="13">*<DD>
If the PID file descriptor refers to a child of the calling process,
then it can be waited on using
<B><A HREF="/cgi-bin/man/man2html?2+waitid">waitid</A></B>(2).
</DL>
<P>
The
<B>pidfd_open</B>()
system call is the preferred way of obtaining a PID file descriptor
for an already existing process.
The alternative is to obtain a file descriptor by opening a
<I>/proc/[pid]</I>
directory.
However, the latter technique is possible only if the
<B><A HREF="/cgi-bin/man/man2html?5+proc">proc</A></B>(5)
filesystem is mounted;
furthermore, the file descriptor obtained in this way is
<I>not</I>
pollable and can't be waited on with
<B><A HREF="/cgi-bin/man/man2html?2+waitid">waitid</A></B>(2).
<A NAME="lbAK">&nbsp;</A>
<H2>EXAMPLE</H2>
The program below opens a PID file descriptor for the
process whose PID is specified as its command-line argument.
It then uses
<B><A HREF="/cgi-bin/man/man2html?2+poll">poll</A></B>(2)
to monitor the file descriptor for process exit, as indicated by an
<B>EPOLLIN</B>
event.
<A NAME="lbAL">&nbsp;</A>
<H3>Program source</H3>
<PRE>
#define _GNU_SOURCE
#include &lt;<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/sys/syscall.h">sys/syscall.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/poll.h">poll.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#ifndef __NR_pidfd_open
#define __NR_pidfd_open 434 /* System call # on most architectures */
#endif
static int
pidfd_open(pid_t pid, unsigned int flags)
{
return syscall(__NR_pidfd_open, pid, flags);
}
int
main(int argc, char *argv[])
{
struct pollfd pollfd;
int pidfd, ready;
if (argc != 2) {
fprintf(stderr, &quot;Usage: %s &lt;pid&gt;\n&quot;, argv[0]);
exit(EXIT_SUCCESS);
}
pidfd = pidfd_open(atoi(argv[1]), 0);
if (pidfd == -1) {
perror(&quot;pidfd_open&quot;);
exit(EXIT_FAILURE);
}
pollfd.fd = pidfd;
pollfd.events = POLLIN;
ready = poll(&amp;pollfd, 1, -1);
if (ready == -1) {
perror(&quot;poll&quot;);
exit(EXIT_FAILURE);
}
printf(&quot;Events (0x%x): POLLIN is %sset\n&quot;, pollfd.revents,
(pollfd.revents &amp; POLLIN) ? &quot;&quot; : &quot;not &quot;);
exit(EXIT_SUCCESS);
}
</PRE>
<A NAME="lbAM">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+kill">kill</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+pidfd_send_signal">pidfd_send_signal</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+poll">poll</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+select">select</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+waitid">waitid</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?7+epoll">epoll</A></B>(7)
<A NAME="lbAN">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="14"><A HREF="#lbAB">NAME</A><DD>
<DT id="15"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="16"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="17"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="18"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="19"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="20"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="21"><A HREF="#lbAI">NOTES</A><DD>
<DL>
<DT id="22"><A HREF="#lbAJ">Use cases for PID file descriptors</A><DD>
</DL>
<DT id="23"><A HREF="#lbAK">EXAMPLE</A><DD>
<DL>
<DT id="24"><A HREF="#lbAL">Program source</A><DD>
</DL>
<DT id="25"><A HREF="#lbAM">SEE ALSO</A><DD>
<DT id="26"><A HREF="#lbAN">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:33 GMT, March 31, 2021
</BODY>
</HTML>