Skip to content

Commit e82740f

Browse files
feat: pidfd_send_signal
1 parent 9b34bd2 commit e82740f

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1515
([#1868](https://github.com/nix-rust/nix/pull/1868))
1616
- Added `pid_open` on Linux.
1717
([#1868](https://github.com/nix-rust/nix/pull/1868))
18+
- Added `pidfd_send_signal` on Linux.
19+
([#1868](https://github.com/nix-rust/nix/pull/1868))
1820

1921
### Changed
2022

src/sys/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,6 @@ feature! {
227227
pub mod timer;
228228
}
229229

230-
#[cfg(all(target_os = "linux", feature = "process"))]
230+
#[cfg(all(target_os = "linux", feature = "signal", feature = "process"))]
231231
/// pidfd related functionality
232232
pub mod pidfd;

src/sys/pidfd.rs

+42
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::errno::Errno;
2+
use crate::sys::signal::Signal;
23
use crate::unistd::Pid;
34
use crate::Result;
45
use std::convert::TryFrom;
@@ -74,3 +75,44 @@ pub fn pid_open(pid: Pid, nonblock: bool) -> Result<OwnedFd> {
7475
_ => unreachable!(),
7576
}
7677
}
78+
79+
/// Sends the signal `sig` to the target process referred to by `pid`, a PID file descriptor that
80+
/// refers to a process.
81+
///
82+
/// If the info argument is some [`libc::siginfo_t`] buffer, that buffer should be populated as
83+
/// described in [rt_sigqueueinfo(2)](https://man7.org/linux/man-pages/man2/rt_sigqueueinfo.2.html).
84+
///
85+
/// If the info argument is `None`, this is equivalent to specifying a pointer to a `siginfo_t`
86+
/// buffer whose fields match the values that are implicitly supplied when a signal is sent using
87+
/// [`crate::sys::signal::kill`]:
88+
///
89+
/// - `si_signo` is set to the signal number;
90+
/// - `si_errno` is set to 0;
91+
/// - `si_code` is set to SI_USER;
92+
/// - `si_pid` is set to the caller's PID; and
93+
/// - `si_uid` is set to the caller's real user ID.
94+
///
95+
/// The calling process must either be in the same PID namespace as the process referred to by
96+
/// pidfd, or be in an ancestor of that namespace.
97+
pub fn pidfd_send_signal<Fd: AsRawFd>(
98+
pid: Fd,
99+
sig: Signal,
100+
info: Option<libc::siginfo_t>,
101+
) -> Result<()> {
102+
let info = match info {
103+
Some(i) => &i,
104+
None => std::ptr::null(),
105+
};
106+
match unsafe {
107+
libc::syscall(
108+
libc::SYS_pidfd_send_signal,
109+
pid.as_raw_fd(),
110+
sig as i32,
111+
info,
112+
)
113+
} {
114+
-1 => Err(Errno::last()),
115+
0 => Ok(()),
116+
_ => unreachable!(),
117+
}
118+
}

0 commit comments

Comments
 (0)