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_SEND_SIGNAL 2 2021-03-22 "Linux" "Linux Programmer's Manual"
27 pidfd_send_signal \- send a signal to a process specified by a file descriptor
30 .BR "#include <linux/signal.h>" " /* Definition of " SIG* " constants */"
31 .BR "#include <signal.h>" " /* Definition of " SI_* " constants */"
32 .BR "#include <sys/syscall.h>" " /* Definition of " SYS_* " constants */"
33 .B #include <unistd.h>
35 .BI "int syscall(SYS_pidfd_send_signal, int " pidfd ", int " sig \
36 ", siginfo_t *" info ,
37 .BI " unsigned int " flags );
41 glibc provides no wrapper for
42 .BR pidfd_send_signal (),
43 necessitating the use of
47 .BR pidfd_send_signal ()
48 system call sends the signal
50 to the target process referred to by
52 a PID file descriptor that refers to a process.
53 .\" See the very detailed commit message for kernel commit
54 .\" 3eb39f47934f9d5a3027fe00d906a45fe3a15fad
60 buffer, that buffer should be populated as described in
61 .BR rt_sigqueueinfo (2).
65 argument is a NULL pointer,
66 this is equivalent to specifying a pointer to a
68 buffer whose fields match the values that are
69 implicitly supplied when a signal is sent using
75 is set to the signal number;
85 is set to the caller's PID; and
88 is set to the caller's real user ID.
91 The calling process must either be in the same PID namespace as the
92 process referred to by
94 or be in an ancestor of that namespace.
98 argument is reserved for future use;
99 currently, this argument must be specified as 0.
102 .BR pidfd_send_signal ()
104 On error, \-1 is returned and
106 is set to indicate the error.
111 is not a valid PID file descriptor.
115 is not a valid signal.
118 The calling process is not in a PID namespace from which it can
119 send a signal to the target process.
126 The calling process does not have permission to send the signal
127 to the target process.
131 doesn't refer to the calling process, and
134 .BR rt_sigqueueinfo (2)).
137 The target process does not exist
138 (i.e., it has terminated and been waited on).
140 .BR pidfd_send_signal ()
141 first appeared in Linux 5.1.
143 .BR pidfd_send_signal ()
146 .SS PID file descriptors
149 argument is a PID file descriptor,
150 a file descriptor that refers to process.
151 Such a file descriptor can be obtained in any of the following ways:
161 via the PID file descriptor that is returned by a call to
170 .BR pidfd_send_signal ()
171 system call allows the avoidance of race conditions that occur
172 when using traditional interfaces (such as
175 The problem is that the traditional interfaces specify the target process
176 via a process ID (PID),
177 with the result that the sender may accidentally send a signal to
178 the wrong process if the originally intended target process
179 has terminated and its PID has been recycled for another process.
181 a PID file descriptor is a stable reference to a specific process;
182 if that process terminates,
183 .BR pidfd_send_signal ()
196 #include <sys/syscall.h>
198 #ifndef __NR_pidfd_send_signal
199 #define __NR_pidfd_send_signal 424
203 pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
206 return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
210 main(int argc, char *argv[])
217 fprintf(stderr, "Usage: %s <pid> <signal>\en", argv[0]);
223 /* Obtain a PID file descriptor by opening the /proc/PID directory
224 of the target process. */
226 snprintf(path, sizeof(path), "/proc/%s", argv[1]);
228 pidfd = open(path, O_RDONLY);
234 /* Populate a \(aqsiginfo_t\(aq structure for use with
235 pidfd_send_signal(). */
237 memset(&info, 0, sizeof(info));
238 info.si_code = SI_QUEUE;
241 info.si_uid = getuid();
242 info.si_pid = getpid();
243 info.si_value.sival_int = 1234;
245 /* Send the signal. */
247 if (pidfd_send_signal(pidfd, sig, &info, 0) == \-1) {
248 perror("pidfd_send_signal");
259 .BR rt_sigqueueinfo (2),
261 .BR pid_namespaces (7),