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 authored and sharkdp committed Nov 15, 2021
1 parent 2b1bf47 commit fe99270
Show file tree
Hide file tree
Showing 3 changed files with 15 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
13 changes: 13 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::{raise, signal, SigHandler, Signal};

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

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

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

0 comments on commit fe99270

Please sign in to comment.