2016-12-15 02:19:58 +01:00
|
|
|
use std::sync::Mutex;
|
2017-03-13 18:55:12 +01:00
|
|
|
use std::fmt;
|
2016-12-15 02:19:58 +01:00
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref CLEANUP: Mutex<Option<Box<Fn(self::Signal) + Send>>> = Mutex::new(None);
|
|
|
|
}
|
|
|
|
|
2017-03-13 18:55:12 +01:00
|
|
|
// TODO: Probably a good idea to use
|
|
|
|
// https://nix-rust.github.io/nix/nix/sys/signal/enum.Signal.html
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
2016-12-15 02:19:58 +01:00
|
|
|
pub enum Signal {
|
2017-03-13 18:55:12 +01:00
|
|
|
// TODO: Probably a good idea to use original names here:
|
|
|
|
// TODO: Add SIGUSR1+2 SIGHUP here?
|
|
|
|
Terminate, // SIGTERM
|
|
|
|
Stop, // SIGTSTP
|
|
|
|
Continue, // SIGCONT
|
|
|
|
ChildExit, // SIGCHLD
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Signal {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{:?}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(signal_name: &str) -> Signal {
|
|
|
|
println!("Using signal {}", signal_name);
|
|
|
|
Signal::Terminate
|
2016-12-15 02:19:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
pub fn install_handler<F>(handler: F)
|
|
|
|
where F: Fn(self::Signal) + 'static + Send + Sync
|
|
|
|
{
|
|
|
|
use std::thread;
|
2016-12-20 17:44:18 +01:00
|
|
|
use libc::c_int;
|
2016-12-15 02:19:58 +01:00
|
|
|
use nix::sys::signal::*;
|
|
|
|
|
|
|
|
// Mask all signals interesting to us. The mask propagates
|
|
|
|
// to all threads started after this point.
|
|
|
|
let mut mask = SigSet::empty();
|
|
|
|
mask.add(SIGTERM);
|
|
|
|
mask.add(SIGINT);
|
2017-03-13 18:55:12 +01:00
|
|
|
// mask.add(SIGHUP);
|
2016-12-15 02:19:58 +01:00
|
|
|
mask.add(SIGTSTP);
|
|
|
|
mask.add(SIGCONT);
|
2016-12-20 17:44:18 +01:00
|
|
|
mask.add(SIGCHLD);
|
2017-03-13 18:55:12 +01:00
|
|
|
// mask.add(SIGUSR1);
|
|
|
|
// mask.add(SIGUSR2);
|
2016-12-15 02:19:58 +01:00
|
|
|
mask.thread_set_mask().expect("unable to set signal mask");
|
|
|
|
|
|
|
|
set_handler(handler);
|
|
|
|
|
2016-12-20 17:44:18 +01:00
|
|
|
// Indicate interest in SIGCHLD by setting a dummy handler
|
2016-12-20 18:20:21 +01:00
|
|
|
pub extern "C" fn sigchld_handler(_: c_int) {}
|
2016-12-20 17:44:18 +01:00
|
|
|
|
|
|
|
unsafe {
|
2016-12-20 18:20:21 +01:00
|
|
|
let _ = sigaction(SIGCHLD,
|
|
|
|
&SigAction::new(SigHandler::Handler(sigchld_handler),
|
|
|
|
SaFlags::empty(),
|
|
|
|
SigSet::empty()));
|
2016-12-20 17:44:18 +01:00
|
|
|
}
|
|
|
|
|
2016-12-15 02:19:58 +01:00
|
|
|
// Spawn a thread to catch these signals
|
|
|
|
thread::spawn(move || {
|
|
|
|
loop {
|
|
|
|
let raw_signal = mask.wait().expect("unable to sigwait");
|
2016-12-20 17:44:18 +01:00
|
|
|
debug!("Received {:?}", raw_signal);
|
2016-12-15 02:19:58 +01:00
|
|
|
|
|
|
|
let sig = match raw_signal {
|
2016-12-20 18:20:21 +01:00
|
|
|
SIGTERM | SIGINT => self::Signal::Terminate,
|
2016-12-15 02:19:58 +01:00
|
|
|
SIGTSTP => self::Signal::Stop,
|
|
|
|
SIGCONT => self::Signal::Continue,
|
2016-12-20 17:44:18 +01:00
|
|
|
SIGCHLD => self::Signal::ChildExit,
|
2016-12-20 18:20:21 +01:00
|
|
|
_ => unreachable!(),
|
2016-12-15 02:19:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Invoke closure
|
|
|
|
invoke(sig);
|
|
|
|
|
|
|
|
// Restore default behavior for received signal and unmask it
|
2016-12-20 17:44:18 +01:00
|
|
|
if raw_signal != SIGCHLD {
|
2016-12-20 18:20:21 +01:00
|
|
|
let default_action =
|
|
|
|
SigAction::new(SigHandler::SigDfl, SaFlags::empty(), SigSet::empty());
|
2016-12-15 02:19:58 +01:00
|
|
|
|
2016-12-20 17:44:18 +01:00
|
|
|
unsafe {
|
|
|
|
let _ = sigaction(raw_signal, &default_action);
|
|
|
|
}
|
2016-12-15 02:19:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut new_mask = SigSet::empty();
|
|
|
|
new_mask.add(raw_signal);
|
|
|
|
|
2016-12-15 15:41:10 +01:00
|
|
|
// Re-raise with signal unmasked
|
|
|
|
let _ = new_mask.thread_unblock();
|
2016-12-15 02:19:58 +01:00
|
|
|
let _ = raise(raw_signal);
|
2016-12-15 15:41:10 +01:00
|
|
|
let _ = new_mask.thread_block();
|
2016-12-15 02:19:58 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
pub fn install_handler<F>(handler: F)
|
|
|
|
where F: Fn(self::Signal) + 'static + Send + Sync
|
|
|
|
{
|
|
|
|
use kernel32::SetConsoleCtrlHandler;
|
|
|
|
use winapi::{BOOL, DWORD, FALSE, TRUE};
|
|
|
|
|
|
|
|
pub unsafe extern "system" fn ctrl_handler(_: DWORD) -> BOOL {
|
|
|
|
invoke(self::Signal::Terminate);
|
|
|
|
|
|
|
|
FALSE
|
|
|
|
}
|
|
|
|
|
|
|
|
set_handler(handler);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
SetConsoleCtrlHandler(Some(ctrl_handler), TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn invoke(sig: self::Signal) {
|
|
|
|
if let Some(ref handler) = *CLEANUP.lock().unwrap() {
|
|
|
|
handler(sig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_handler<F>(handler: F)
|
|
|
|
where F: Fn(self::Signal) + 'static + Send + Sync
|
|
|
|
{
|
|
|
|
*CLEANUP.lock().unwrap() = Some(Box::new(handler));
|
|
|
|
}
|