Skip to content

Commit

Permalink
Add signal::sigaction_is_{default,ignore}()
Browse files Browse the repository at this point in the history
Provide safe mechanisms to determine whether a signal's action is the
default or ignore.
This covers the majority of real-world calls to `sigaction()` with a
NULL action without needing any unsafe code.

Fixes #2172.
  • Loading branch information
Chris Pick committed Nov 14, 2023
1 parent 30a6eee commit 0e88e0e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,22 @@ pub unsafe fn sigaction_current(signal: Signal) -> Result<SigAction> {
sigaction_inner(signal, None)
}

/// Whether the specified signal currently has its default action.
///
/// `signal` can be any signal except `SIGKILL` or `SIGSTOP`.
pub fn sigaction_is_default(signal: Signal) -> Result<bool> {
// SAFETY: fetching the current action is safe if the handler isn't called
unsafe { sigaction_current(signal) }.map(|sigaction| sigaction.handler() == SigHandler::SigDfl)
}

/// Whether the specified signal is currently ignored.
///
/// `signal` can be any signal except `SIGKILL` or `SIGSTOP`.
pub fn sigaction_is_ignore(signal: Signal) -> Result<bool> {
// SAFETY: fetching the current action is safe if the handler isn't called
unsafe { sigaction_current(signal) }.map(|sigaction| sigaction.handler() == SigHandler::SigIgn)
}

/// Signal management (see [signal(3p)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/signal.html))
///
/// Installs `handler` for the given `signal`, returning the previous signal
Expand Down
4 changes: 4 additions & 0 deletions test/sys/test_signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ fn test_current_sigaction() {
unsafe { sigaction_current(SIGINT) }.unwrap().handler(),
SigHandler::SigDfl
);
assert!(sigaction_is_default(SIGINT).unwrap());
assert!(!sigaction_is_ignore(SIGINT).unwrap());

unsafe {
sigaction(
Expand All @@ -72,6 +74,8 @@ fn test_current_sigaction() {
unsafe { sigaction_current(SIGINT) }.unwrap().handler(),
SigHandler::SigIgn
);
assert!(!sigaction_is_default(SIGINT).unwrap());
assert!(sigaction_is_ignore(SIGINT).unwrap());

// restore original
unsafe { sigaction(SIGINT, &oact) }.unwrap();
Expand Down

0 comments on commit 0e88e0e

Please sign in to comment.