Migrate to nix::sys::signal::Signal

This commit is contained in:
Chris Aumann 2017-03-13 19:00:45 +01:00
parent 68caf04269
commit 24e9b0c1c5
3 changed files with 19 additions and 45 deletions

View File

@ -34,8 +34,8 @@ use std::time::Duration;
use notification_filter::NotificationFilter;
use process::Process;
use signal::Signal;
use watcher::{Event, Watcher};
use nix::sys::signal::Signal;
fn init_logger(debug: bool) {
let mut log_builder = env_logger::LogBuilder::new();
@ -65,13 +65,14 @@ fn main() {
match sig {
// TODO: This should be generalized to use new --signal flag
// TODO: Not sure what this is doing tbh :(
Signal::Terminate => {
Signal::SIGTERM => {
// TODO: Removed kill variable for now
child.terminate();
}
Signal::Stop => child.pause(),
Signal::Continue => child.resume(),
Signal::ChildExit => child.reap(),
Signal::SIGSTOP => child.pause(),
Signal::SIGCONT => child.resume(),
Signal::SIGCHLD => child.reap(),
_ => debug!("Unhandled signal: {:?}", sig),
}
}
}
@ -191,12 +192,12 @@ fn wait_fs(rx: &Receiver<Event>, filter: &NotificationFilter) -> Vec<PathBuf> {
paths
}
fn wait_process(process: &RwLock<Option<Process>>, signal: signal::Signal, restart: bool) {
fn wait_process(process: &RwLock<Option<Process>>, signal: Signal, restart: bool) {
let guard = process.read().unwrap();
if let Some(ref child) = *guard {
if restart {
debug!("Stopping child process with {} signal", signal);
debug!("Stopping child process with {:?} signal", signal);
child.signal(signal);
}

View File

@ -13,7 +13,7 @@ mod imp {
use std::path::PathBuf;
use std::process::Command;
use std::sync::*;
use signal::Signal;
use nix::sys::signal::Signal;
pub struct Process {
pgid: pid_t,
@ -92,7 +92,6 @@ mod imp {
pub fn signal(&self, signal: Signal) {
// TODO: Sending dummy signal for now
println!("DEBUG: {}", signal);
self.c_signal(SIGCONT);
}

View File

@ -1,31 +1,13 @@
use std::sync::Mutex;
use std::fmt;
use nix::sys::signal::Signal;
lazy_static! {
static ref CLEANUP: Mutex<Option<Box<Fn(self::Signal) + Send>>> = Mutex::new(None);
}
// TODO: Probably a good idea to use
// https://nix-rust.github.io/nix/nix/sys/signal/enum.Signal.html
#[derive(Debug, Clone, Copy)]
pub enum Signal {
// 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
Signal::SIGTERM
}
#[cfg(unix)]
@ -64,36 +46,28 @@ pub fn install_handler<F>(handler: F)
// Spawn a thread to catch these signals
thread::spawn(move || {
loop {
let raw_signal = mask.wait().expect("unable to sigwait");
debug!("Received {:?}", raw_signal);
let sig = match raw_signal {
SIGTERM | SIGINT => self::Signal::Terminate,
SIGTSTP => self::Signal::Stop,
SIGCONT => self::Signal::Continue,
SIGCHLD => self::Signal::ChildExit,
_ => unreachable!(),
};
let signal = mask.wait().expect("Unable to sigwait");
debug!("Received {:?}", signal);
// Invoke closure
invoke(sig);
invoke(signal);
// Restore default behavior for received signal and unmask it
if raw_signal != SIGCHLD {
if signal != SIGCHLD {
let default_action =
SigAction::new(SigHandler::SigDfl, SaFlags::empty(), SigSet::empty());
unsafe {
let _ = sigaction(raw_signal, &default_action);
let _ = sigaction(signal, &default_action);
}
}
let mut new_mask = SigSet::empty();
new_mask.add(raw_signal);
new_mask.add(signal);
// Re-raise with signal unmasked
let _ = new_mask.thread_unblock();
let _ = raise(raw_signal);
let _ = raise(signal);
let _ = new_mask.thread_block();
}
});
@ -107,7 +81,7 @@ pub fn install_handler<F>(handler: F)
use winapi::{BOOL, DWORD, FALSE, TRUE};
pub unsafe extern "system" fn ctrl_handler(_: DWORD) -> BOOL {
invoke(self::Signal::Terminate);
invoke(self::Signal::SIGTERM);
FALSE
}