Skip to content

Commit 7d5d764

Browse files
pidfd_send_signal
1 parent d655abc commit 7d5d764

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
@@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
66
## [Unreleased] - ReleaseDate
77
### Added
88

9+
- Added `pidfd_send_signal`.
10+
([#1868](https://github.com/nix-rust/nix/pull/1868))
911
- Added `pid_open`.
1012
([#1868](https://github.com/nix-rust/nix/pull/1868))
1113
- Added `pidfd_getfd`.

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

0 commit comments

Comments
 (0)