Skip to content

Commit

Permalink
Add pidfd_send_signal syscall
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
  • Loading branch information
SUPERCILEX committed Feb 16, 2024
1 parent 394dd7d commit 052d7fa
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/backend/libc/process/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,26 @@ pub(crate) fn pidfd_open(pid: Pid, flags: PidfdFlags) -> io::Result<OwnedFd> {
}
}

#[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<'_>,
Expand Down
13 changes: 13 additions & 0 deletions src/backend/linux_raw/process/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,19 @@ pub(crate) fn pidfd_open(pid: Pid, flags: PidfdFlags) -> io::Result<OwnedFd> {
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<usize> {
Expand Down
15 changes: 14 additions & 1 deletion src/process/pidfd.rs
Original file line number Diff line number Diff line change
@@ -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`].
Expand Down Expand Up @@ -28,3 +29,15 @@ bitflags::bitflags! {
pub fn pidfd_open(pid: Pid, flags: PidfdFlags) -> io::Result<OwnedFd> {
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<Fd: AsFd>(pidfd: Fd, sig: Signal) -> io::Result<()> {
backend::process::syscalls::pidfd_send_signal(pidfd.as_fd(), sig)
}

0 comments on commit 052d7fa

Please sign in to comment.