Skip to content

Commit 3fb1d16

Browse files
authored
Add pidfd_send_signal syscall (#1019)
Signed-off-by: Alex Saveau <[email protected]>
1 parent 966fde1 commit 3fb1d16

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

src/backend/libc/process/syscalls.rs

+20
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,26 @@ pub(crate) fn pidfd_open(pid: Pid, flags: PidfdFlags) -> io::Result<OwnedFd> {
683683
}
684684
}
685685

686+
#[cfg(target_os = "linux")]
687+
pub(crate) fn pidfd_send_signal(pidfd: BorrowedFd<'_>, sig: Signal) -> io::Result<()> {
688+
syscall! {
689+
fn pidfd_send_signal(
690+
pid: c::pid_t,
691+
sig: c::c_int,
692+
info: *const c::siginfo_t,
693+
flags: c::c_int
694+
) via SYS_pidfd_send_signal -> c::c_int
695+
}
696+
unsafe {
697+
ret(pidfd_send_signal(
698+
borrowed_fd(pidfd),
699+
sig as c::c_int,
700+
core::ptr::null(),
701+
0,
702+
))
703+
}
704+
}
705+
686706
#[cfg(target_os = "linux")]
687707
pub(crate) fn pidfd_getfd(
688708
pidfd: BorrowedFd<'_>,

src/backend/linux_raw/process/syscalls.rs

+13
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,19 @@ pub(crate) fn pidfd_open(pid: Pid, flags: PidfdFlags) -> io::Result<OwnedFd> {
588588
unsafe { ret_owned_fd(syscall_readonly!(__NR_pidfd_open, pid, flags)) }
589589
}
590590

591+
#[inline]
592+
pub(crate) fn pidfd_send_signal(fd: BorrowedFd<'_>, sig: Signal) -> io::Result<()> {
593+
unsafe {
594+
ret(syscall_readonly!(
595+
__NR_pidfd_send_signal,
596+
fd,
597+
sig,
598+
pass_usize(0),
599+
pass_usize(0)
600+
))
601+
}
602+
}
603+
591604
#[cfg(feature = "alloc")]
592605
#[inline]
593606
pub(crate) fn getgroups(buf: &mut [Gid]) -> io::Result<usize> {

src/process/pidfd.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::fd::OwnedFd;
2-
use crate::process::Pid;
2+
use crate::process::{Pid, Signal};
33
use crate::{backend, io};
4+
use backend::fd::AsFd;
45

56
bitflags::bitflags! {
67
/// `PIDFD_*` flags for use with [`pidfd_open`].
@@ -28,3 +29,15 @@ bitflags::bitflags! {
2829
pub fn pidfd_open(pid: Pid, flags: PidfdFlags) -> io::Result<OwnedFd> {
2930
backend::process::syscalls::pidfd_open(pid, flags)
3031
}
32+
33+
/// `syscall(SYS_pidfd_send_signal, pidfd, sig, NULL, 0)`—Send a signal to a process
34+
/// specified by a file descriptor.
35+
///
36+
/// # References
37+
/// - [Linux]
38+
///
39+
/// [Linux]: https://man7.org/linux/man-pages/man2/pidfd_send_signal.2.html
40+
#[inline]
41+
pub fn pidfd_send_signal<Fd: AsFd>(pidfd: Fd, sig: Signal) -> io::Result<()> {
42+
backend::process::syscalls::pidfd_send_signal(pidfd.as_fd(), sig)
43+
}

0 commit comments

Comments
 (0)