From 24e9b0c1c5c25e726bc8e7791538f952aa8ff552 Mon Sep 17 00:00:00 2001 From: Chris Aumann Date: Mon, 13 Mar 2017 19:00:45 +0100 Subject: [PATCH] Migrate to nix::sys::signal::Signal --- src/main.rs | 15 ++++++++------- src/process.rs | 3 +-- src/signal.rs | 46 ++++++++++------------------------------------ 3 files changed, 19 insertions(+), 45 deletions(-) diff --git a/src/main.rs b/src/main.rs index ac87551..b548130 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, filter: &NotificationFilter) -> Vec { paths } -fn wait_process(process: &RwLock>, signal: signal::Signal, restart: bool) { +fn wait_process(process: &RwLock>, 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); } diff --git a/src/process.rs b/src/process.rs index 5efba17..739cd46 100644 --- a/src/process.rs +++ b/src/process.rs @@ -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); } diff --git a/src/signal.rs b/src/signal.rs index 88468ea..5308fba 100644 --- a/src/signal.rs +++ b/src/signal.rs @@ -1,31 +1,13 @@ use std::sync::Mutex; -use std::fmt; +use nix::sys::signal::Signal; lazy_static! { static ref CLEANUP: Mutex>> = 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(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(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 }