Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add signal_ignore() and signal_default() #2191

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2191.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `signal_ignore()` and `signal_default()` functions
16 changes: 16 additions & 0 deletions src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,22 @@ pub unsafe fn signal(signal: Signal, handler: SigHandler) -> Result<SigHandler>
})
}

/// Reset the specified signal to its default action.
///
/// `signal` can be any signal except `SIGKILL` or `SIGSTOP`.
pub fn signal_default(signal: Signal) -> Result<()> {
// SAFETY: restoring a default handler is safe if the old handler isn't called
unsafe { self::signal(signal, SigHandler::SigDfl) }.map(drop)
}

/// Ignore the specified signal.
///
/// `signal` can be any signal except `SIGKILL` or `SIGSTOP`.
pub fn signal_ignore(signal: Signal) -> Result<()> {
// SAFETY: ignoring a signal is safe if the old handler isn't called
unsafe { self::signal(signal, SigHandler::SigIgn) }.map(drop)
}

fn do_pthread_sigmask(how: SigmaskHow,
set: Option<&SigSet>,
oldset: Option<*mut libc::sigset_t>) -> Result<()> {
Expand Down
16 changes: 16 additions & 0 deletions test/sys/test_signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,19 @@ fn test_signal() {
// Restore default signal handler
unsafe { signal(Signal::SIGINT, SigHandler::SigDfl) }.unwrap();
}

#[test]
fn test_safe_signal() {
let _m = crate::SIGNAL_MTX.lock();

signal_ignore(SIGINT).unwrap();
raise(Signal::SIGINT).unwrap();

signal_default(SIGINT).unwrap();

// Ensure default was restored
assert_eq!(
unsafe { signal(Signal::SIGINT, SigHandler::SigDfl) }.unwrap(),
SigHandler::SigDfl
);
}