diff --git a/src/backend/libc/process/syscalls.rs b/src/backend/libc/process/syscalls.rs index 46fd18211..fc00071de 100644 --- a/src/backend/libc/process/syscalls.rs +++ b/src/backend/libc/process/syscalls.rs @@ -683,6 +683,26 @@ pub(crate) fn pidfd_open(pid: Pid, flags: PidfdFlags) -> io::Result { } } +#[cfg(target_os = "linux")] +pub(crate) fn pidfd_send_signal(pidfd: BorrowedFd<'_>, sig: Signal) -> io::Result<()> { + syscall! { + fn pidfd_send_signal( + pid: c::pid_t, + sig: c::c_int, + info: *const c::siginfo_t, + flags: c::c_int + ) via SYS_pidfd_send_signal -> c::c_int + } + unsafe { + ret(pidfd_send_signal( + borrowed_fd(pidfd), + sig as c::c_int, + core::ptr::null(), + 0, + )) + } +} + #[cfg(target_os = "linux")] pub(crate) fn pidfd_getfd( pidfd: BorrowedFd<'_>, diff --git a/src/backend/linux_raw/process/syscalls.rs b/src/backend/linux_raw/process/syscalls.rs index bc00af9b1..fb900a4d3 100644 --- a/src/backend/linux_raw/process/syscalls.rs +++ b/src/backend/linux_raw/process/syscalls.rs @@ -588,6 +588,19 @@ pub(crate) fn pidfd_open(pid: Pid, flags: PidfdFlags) -> io::Result { unsafe { ret_owned_fd(syscall_readonly!(__NR_pidfd_open, pid, flags)) } } +#[inline] +pub(crate) fn pidfd_send_signal(fd: BorrowedFd<'_>, sig: Signal) -> io::Result<()> { + unsafe { + ret(syscall_readonly!( + __NR_pidfd_send_signal, + fd, + sig, + pass_usize(0), + pass_usize(0) + )) + } +} + #[cfg(feature = "alloc")] #[inline] pub(crate) fn getgroups(buf: &mut [Gid]) -> io::Result { diff --git a/src/process/pidfd.rs b/src/process/pidfd.rs index abebaf21f..41706d743 100644 --- a/src/process/pidfd.rs +++ b/src/process/pidfd.rs @@ -1,6 +1,7 @@ use crate::fd::OwnedFd; -use crate::process::Pid; +use crate::process::{Pid, Signal}; use crate::{backend, io}; +use backend::fd::AsFd; bitflags::bitflags! { /// `PIDFD_*` flags for use with [`pidfd_open`]. @@ -28,3 +29,15 @@ bitflags::bitflags! { pub fn pidfd_open(pid: Pid, flags: PidfdFlags) -> io::Result { backend::process::syscalls::pidfd_open(pid, flags) } + +/// `syscall(SYS_pidfd_send_signal, pidfd, sig, NULL, 0)`—Send a signal to a process +/// specified by a file descriptor. +/// +/// # References +/// - [Linux] +/// +/// [Linux]: https://man7.org/linux/man-pages/man2/pidfd_send_signal.2.html +#[inline] +pub fn pidfd_send_signal(pidfd: Fd, sig: Signal) -> io::Result<()> { + backend::process::syscalls::pidfd_send_signal(pidfd.as_fd(), sig) +}