Skip to content

Commit 31afa82

Browse files
pidfd_send_signal
1 parent 0343ae2 commit 31afa82

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1313
([#1868](https://github.com/nix-rust/nix/pull/1868))
1414
- Added `pid_open` on Linux.
1515
([#1868](https://github.com/nix-rust/nix/pull/1868))
16+
- Added `pidfd_send_signal` on Linux.
17+
([#1868](https://github.com/nix-rust/nix/pull/1868))
1618

1719
### Changed
1820

src/sys/mod.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 37 additions & 0 deletions
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;
@@ -60,3 +61,39 @@ pub fn pid_open(pid_t: Pid, pidfd_nonblock: bool) -> Result<RawFd> {
6061
_ => unreachable!(),
6162
}
6263
}
64+
65+
/// Sends the signal `sig` to the target process referred to by `pidfd`, a PID file descriptor that
66+
/// refers to a process.
67+
///
68+
/// If the info argument is some [`libc::siginfo_t`] buffer, that buffer should be populated as
69+
/// described in [rt_sigqueueinfo(2)](https://man7.org/linux/man-pages/man2/rt_sigqueueinfo.2.html).
70+
///
71+
/// If the info argument is `None`, this is equivalent to specifying a pointer to a `siginfo_t`
72+
/// buffer whose fields match the values that are implicitly supplied when a signal is sent using
73+
/// [`crate::sys::signal::kill`]:
74+
///
75+
/// - `si_signo` is set to the signal number;
76+
/// - `si_errno` is set to 0;
77+
/// - `si_code` is set to SI_USER;
78+
/// - `si_pid` is set to the caller's PID; and
79+
/// - `si_uid` is set to the caller's real user ID.
80+
///
81+
/// The calling process must either be in the same PID namespace as the process referred to by
82+
/// pidfd, or be in an ancestor of that namespace.
83+
pub fn pidfd_send_signal(
84+
pidfd: RawFd,
85+
sig: Signal,
86+
info: Option<libc::siginfo_t>,
87+
) -> Result<()> {
88+
let info = match info {
89+
Some(i) => &i,
90+
None => std::ptr::null(),
91+
};
92+
match unsafe {
93+
libc::syscall(libc::SYS_pidfd_send_signal, pidfd, sig as i32, info)
94+
} {
95+
-1 => Err(Errno::last()),
96+
0 => Ok(()),
97+
_ => unreachable!(),
98+
}
99+
}

0 commit comments

Comments
 (0)