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

366 lines
8.2 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of PIDFD_SEND_SIGNAL</TITLE>
</HEAD><BODY>
<H1>PIDFD_SEND_SIGNAL</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-09-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_send_signal - send a signal to a process specified by a file descriptor
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/signal.h">signal.h</A>&gt;</B>
<B>int pidfd_send_signal(int </B><I>pidfd</I><B>, int </B><I>sig</I><B>, siginfo_t *</B><I>info</I><B>,</B>
<B> unsigned int </B><I>flags</I><B>);</B>
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>pidfd_send_signal</B>()
system call sends the signal
<I>sig</I>
to the target process referred to by
<I>pidfd</I>,
a PID file descriptor that refers to a process.
<P>
If the
<I>info</I>
argument points to a
<I>siginfo_t</I>
buffer, that buffer should be populated as described in
<B><A HREF="/cgi-bin/man/man2html?2+rt_sigqueueinfo">rt_sigqueueinfo</A></B>(2).
<P>
If the
<I>info</I>
argument is a NULL pointer,
this is equivalent to specifying a pointer to a
<I>siginfo_t</I>
buffer whose fields match the values that are
implicitly supplied when a signal is sent using
<B><A HREF="/cgi-bin/man/man2html?2+kill">kill</A></B>(2):
<P>
<DL COMPACT>
<DT id="1">*<DD>
<I>si_signo</I>
is set to the signal number;
<DT id="2">*<DD>
<I>si_errno</I>
is set to 0;
<DT id="3">*<DD>
<I>si_code</I>
is set to
<B>SI_USER;</B>
<DT id="4">*<DD>
<I>si_pid</I>
is set to the caller's PID; and
<DT id="5">*<DD>
<I>si_uid</I>
is set to the caller's real user ID.
</DL>
<P>
The calling process must either be in the same PID namespace as the
process referred to by
<I>pidfd</I>,
or be in an ancestor of that namespace.
<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_send_signal</B>()
returns 0.
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="6"><B>EBADF</B>
<DD>
<I>pidfd</I>
is not a valid PID file descriptor.
<DT id="7"><B>EINVAL</B>
<DD>
<I>sig</I>
is not a valid signal.
<DT id="8"><B>EINVAL</B>
<DD>
The calling process is not in a PID namespace from which it can
send a signal to the target process.
<DT id="9"><B>EINVAL</B>
<DD>
<I>flags</I>
is not 0.
<DT id="10"><B>EPERM</B>
<DD>
The calling process does not have permission to send the signal
to the target process.
<DT id="11"><B>EPERM</B>
<DD>
<I>pidfd</I>
doesn't refer to the calling process, and
<I>info.si_code</I>
is invalid (see
<B><A HREF="/cgi-bin/man/man2html?2+rt_sigqueueinfo">rt_sigqueueinfo</A></B>(2)).
<DT id="12"><B>ESRCH</B>
<DD>
The target process does not exist
(i.e., it has terminated and been waited on).
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
<B>pidfd_send_signal</B>()
first appeared in Linux 5.1.
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
<B>pidfd_send_signal</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).
<A NAME="lbAJ">&nbsp;</A>
<H3>PID file descriptors</H3>
The
<I>pidfd</I>
argument is a PID file descriptor,
a file descriptor that refers to process.
Such a file descriptor can be obtained in any of the following ways:
<DL COMPACT>
<DT id="13">*<DD>
by opening a
<I>/proc/[pid]</I>
directory;
<DT id="14">*<DD>
using
<B><A HREF="/cgi-bin/man/man2html?2+pidfd_open">pidfd_open</A></B>(2);
or
<DT id="15">*<DD>
via the PID file descriptor that is returned by a call to
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2)
or
<B><A HREF="/cgi-bin/man/man2html?2+clone3">clone3</A></B>(2)
that specifies the
<B>CLONE_PIDFD</B>
flag.
</DL>
<P>
The
<B>pidfd_send_signal</B>()
system call allows the avoidance of race conditions that occur
when using traditional interfaces (such as
<B><A HREF="/cgi-bin/man/man2html?2+kill">kill</A></B>(2))
to signal a process.
The problem is that the traditional interfaces specify the target process
via a process ID (PID),
with the result that the sender may accidentally send a signal to
the wrong process if the originally intended target process
has terminated and its PID has been recycled for another process.
By contrast,
a PID file descriptor is a stable reference to a specific process;
if that process terminates,
<B>pidfd_send_signal</B>()
fails with the error
<B>ESRCH</B>.
<A NAME="lbAK">&nbsp;</A>
<H2>EXAMPLE</H2>
<PRE>
#define _GNU_SOURCE
#include &lt;<A HREF="file:///usr/include/limits.h">limits.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/signal.h">signal.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/string.h">string.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/sys/syscall.h">sys/syscall.h</A>&gt;
#ifndef __NR_pidfd_send_signal
#define __NR_pidfd_send_signal 424
#endif
static int
pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
unsigned int flags)
{
return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
}
int
main(int argc, char *argv[])
{
siginfo_t info;
char path[PATH_MAX];
int pidfd, sig;
if (argc != 3) {
fprintf(stderr, &quot;Usage: %s &lt;pid&gt; &lt;signal&gt;\n&quot;, argv[0]);
exit(EXIT_FAILURE);
}
sig = atoi(argv[2]);
/* Obtain a PID file descriptor by opening the /proc/PID directory
of the target process */
snprintf(path, sizeof(path), &quot;/proc/%s&quot;, argv[1]);
pidfd = open(path, O_RDONLY);
if (pidfd == -1) {
perror(&quot;open&quot;);
exit(EXIT_FAILURE);
}
/* Populate a 'siginfo_t' structure for use with
pidfd_send_signal() */
memset(&amp;info, 0, sizeof(info));
info.si_code = SI_QUEUE;
info.si_signo = sig;
info.si_errno = 0;
info.si_uid = getuid();
info.si_pid = getpid();
info.si_value.sival_int = 1234;
/* Send the signal */
if (pidfd_send_signal(pidfd, sig, &amp;info, 0) == -1) {
perror(&quot;pidfd_send_signal&quot;);
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
</PRE>
<A NAME="lbAL">&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_open">pidfd_open</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+rt_sigqueueinfo">rt_sigqueueinfo</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+sigaction">sigaction</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?7+pid_namespaces">pid_namespaces</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+signal">signal</A></B>(7)
<A NAME="lbAM">&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="16"><A HREF="#lbAB">NAME</A><DD>
<DT id="17"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="18"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="19"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="20"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="21"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="22"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="23"><A HREF="#lbAI">NOTES</A><DD>
<DL>
<DT id="24"><A HREF="#lbAJ">PID file descriptors</A><DD>
</DL>
<DT id="25"><A HREF="#lbAK">EXAMPLE</A><DD>
<DT id="26"><A HREF="#lbAL">SEE ALSO</A><DD>
<DT id="27"><A HREF="#lbAM">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>