Skip to content

Commit

Permalink
Raise SIGINT when dying by ^C
Browse files Browse the repository at this point in the history
  • Loading branch information
tavianator committed Nov 14, 2021
1 parent 8971fc6 commit 6b5edd7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ features = ["suggestions", "color", "wrap_help"]

[target.'cfg(unix)'.dependencies]
users = "0.11.0"
nix = "0.23.0"

[target.'cfg(all(unix, not(target_os = "redox")))'.dependencies]
libc = "0.2"
Expand Down
16 changes: 16 additions & 0 deletions src/exit_codes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::process;

#[cfg(unix)]
use nix::sys::signal::{SigHandler, Signal, raise, signal};

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ExitCode {
Success,
Expand All @@ -26,6 +29,19 @@ impl ExitCode {

/// Exit the process with the appropriate code.
pub fn exit(self) -> ! {
#[cfg(unix)]
match self {
ExitCode::KilledBySigint => {
// Get rid of the SIGINT handler, if present, and raise SIGINT
unsafe {
if let Ok(_) = signal(Signal::SIGINT, SigHandler::SigDfl) {
let _ = raise(Signal::SIGINT);
}
}
}
_ => {},
}

process::exit(self.into())
}
}
Expand Down

0 comments on commit 6b5edd7

Please sign in to comment.