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

Migrate Unix implementation to signal-hook-registry #98

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ readme = "README.md"

[target.'cfg(unix)'.dependencies]
nix = { version = "0.26", default-features = false, features = ["fs", "signal"]}
signal-hook-registry = { version = "1.4.0", default-features = false }

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.45", features = ["Win32_Foundation", "Win32_System_Threading", "Win32_Security", "Win32_System_WindowsProgramming", "Win32_System_Console"] }
Expand All @@ -29,3 +30,5 @@ termination = []
harness = false
name = "tests"
path = "src/tests.rs"

[dependencies]
40 changes: 18 additions & 22 deletions src/platform/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub type Error = nix::Error;
/// Platform specific signal type
pub type Signal = nix::sys::signal::Signal;

extern "C" fn os_handler(_: nix::libc::c_int) {
fn os_handler() {
// Assuming this always succeeds. Can't really handle errors in any meaningful way.
unsafe {
let _ = unistd::write(PIPE.1, &[0u8]);
Expand Down Expand Up @@ -85,7 +85,6 @@ fn pipe2(flags: nix::fcntl::OFlag) -> nix::Result<(RawFd, RawFd)> {
#[inline]
pub unsafe fn init_os_handler() -> Result<(), Error> {
use nix::fcntl;
use nix::sys::signal;

PIPE = pipe2(fcntl::OFlag::O_CLOEXEC)?;

Expand All @@ -102,34 +101,31 @@ pub unsafe fn init_os_handler() -> Result<(), Error> {
return Err(close_pipe(e));
}

let handler = signal::SigHandler::Handler(os_handler);
let new_action = signal::SigAction::new(
handler,
signal::SaFlags::SA_RESTART,
signal::SigSet::empty(),
);
// Register the handler.
let res = unsafe { signal_hook_registry::register(nix::libc::SIGINT, os_handler) };

#[allow(unused_variables)]
let sigint_old = match signal::sigaction(signal::Signal::SIGINT, &new_action) {
let sigint_id = match res {
Ok(old) => old,
Err(e) => return Err(close_pipe(e)),
Err(_e) => return Err(close_pipe(nix::Error::EINVAL)),
};

#[cfg(feature = "termination")]
{
let sigterm_old = match signal::sigaction(signal::Signal::SIGTERM, &new_action) {
Ok(old) => old,
Err(e) => {
signal::sigaction(signal::Signal::SIGINT, &sigint_old).unwrap();
return Err(close_pipe(e));
}
};
match signal::sigaction(signal::Signal::SIGHUP, &new_action) {
let sigterm_id =
match unsafe { signal_hook_registry::register(nix::libc::SIGTERM, os_handler) } {
Ok(old) => old,
Err(_e) => {
signal_hook_registry::unregister(sigint_id);
return Err(close_pipe(nix::Error::EINVAL));
}
};
match unsafe { signal_hook_registry::register(nix::libc::SIGHUP, os_handler) } {
Ok(_) => {}
Err(e) => {
signal::sigaction(signal::Signal::SIGINT, &sigint_old).unwrap();
signal::sigaction(signal::Signal::SIGTERM, &sigterm_old).unwrap();
return Err(close_pipe(e));
Err(_e) => {
signal_hook_registry::unregister(sigint_id);
signal_hook_registry::unregister(sigterm_id);
return Err(close_pipe(nix::Error::EINVAL));
}
}
}
Expand Down