mirror of
https://github.com/watchexec/watchexec.git
synced 2024-10-01 07:31:35 +02:00
Migrate to nix::sys::signal::Signal
This commit is contained in:
parent
68caf04269
commit
24e9b0c1c5
15
src/main.rs
15
src/main.rs
@ -34,8 +34,8 @@ use std::time::Duration;
|
|||||||
|
|
||||||
use notification_filter::NotificationFilter;
|
use notification_filter::NotificationFilter;
|
||||||
use process::Process;
|
use process::Process;
|
||||||
use signal::Signal;
|
|
||||||
use watcher::{Event, Watcher};
|
use watcher::{Event, Watcher};
|
||||||
|
use nix::sys::signal::Signal;
|
||||||
|
|
||||||
fn init_logger(debug: bool) {
|
fn init_logger(debug: bool) {
|
||||||
let mut log_builder = env_logger::LogBuilder::new();
|
let mut log_builder = env_logger::LogBuilder::new();
|
||||||
@ -65,13 +65,14 @@ fn main() {
|
|||||||
match sig {
|
match sig {
|
||||||
// TODO: This should be generalized to use new --signal flag
|
// TODO: This should be generalized to use new --signal flag
|
||||||
// TODO: Not sure what this is doing tbh :(
|
// TODO: Not sure what this is doing tbh :(
|
||||||
Signal::Terminate => {
|
Signal::SIGTERM => {
|
||||||
// TODO: Removed kill variable for now
|
// TODO: Removed kill variable for now
|
||||||
child.terminate();
|
child.terminate();
|
||||||
}
|
}
|
||||||
Signal::Stop => child.pause(),
|
Signal::SIGSTOP => child.pause(),
|
||||||
Signal::Continue => child.resume(),
|
Signal::SIGCONT => child.resume(),
|
||||||
Signal::ChildExit => child.reap(),
|
Signal::SIGCHLD => child.reap(),
|
||||||
|
_ => debug!("Unhandled signal: {:?}", sig),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -191,12 +192,12 @@ fn wait_fs(rx: &Receiver<Event>, filter: &NotificationFilter) -> Vec<PathBuf> {
|
|||||||
paths
|
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();
|
let guard = process.read().unwrap();
|
||||||
|
|
||||||
if let Some(ref child) = *guard {
|
if let Some(ref child) = *guard {
|
||||||
if restart {
|
if restart {
|
||||||
debug!("Stopping child process with {} signal", signal);
|
debug!("Stopping child process with {:?} signal", signal);
|
||||||
child.signal(signal);
|
child.signal(signal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ mod imp {
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use std::sync::*;
|
use std::sync::*;
|
||||||
use signal::Signal;
|
use nix::sys::signal::Signal;
|
||||||
|
|
||||||
pub struct Process {
|
pub struct Process {
|
||||||
pgid: pid_t,
|
pgid: pid_t,
|
||||||
@ -92,7 +92,6 @@ mod imp {
|
|||||||
|
|
||||||
pub fn signal(&self, signal: Signal) {
|
pub fn signal(&self, signal: Signal) {
|
||||||
// TODO: Sending dummy signal for now
|
// TODO: Sending dummy signal for now
|
||||||
println!("DEBUG: {}", signal);
|
|
||||||
self.c_signal(SIGCONT);
|
self.c_signal(SIGCONT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,31 +1,13 @@
|
|||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use std::fmt;
|
use nix::sys::signal::Signal;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref CLEANUP: Mutex<Option<Box<Fn(self::Signal) + Send>>> = Mutex::new(None);
|
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 {
|
pub fn new(signal_name: &str) -> Signal {
|
||||||
println!("Using signal {}", signal_name);
|
println!("Using signal {}", signal_name);
|
||||||
Signal::Terminate
|
Signal::SIGTERM
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
@ -64,36 +46,28 @@ pub fn install_handler<F>(handler: F)
|
|||||||
// Spawn a thread to catch these signals
|
// Spawn a thread to catch these signals
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
loop {
|
loop {
|
||||||
let raw_signal = mask.wait().expect("unable to sigwait");
|
let signal = mask.wait().expect("Unable to sigwait");
|
||||||
debug!("Received {:?}", raw_signal);
|
debug!("Received {:?}", signal);
|
||||||
|
|
||||||
let sig = match raw_signal {
|
|
||||||
SIGTERM | SIGINT => self::Signal::Terminate,
|
|
||||||
SIGTSTP => self::Signal::Stop,
|
|
||||||
SIGCONT => self::Signal::Continue,
|
|
||||||
SIGCHLD => self::Signal::ChildExit,
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Invoke closure
|
// Invoke closure
|
||||||
invoke(sig);
|
invoke(signal);
|
||||||
|
|
||||||
// Restore default behavior for received signal and unmask it
|
// Restore default behavior for received signal and unmask it
|
||||||
if raw_signal != SIGCHLD {
|
if signal != SIGCHLD {
|
||||||
let default_action =
|
let default_action =
|
||||||
SigAction::new(SigHandler::SigDfl, SaFlags::empty(), SigSet::empty());
|
SigAction::new(SigHandler::SigDfl, SaFlags::empty(), SigSet::empty());
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let _ = sigaction(raw_signal, &default_action);
|
let _ = sigaction(signal, &default_action);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut new_mask = SigSet::empty();
|
let mut new_mask = SigSet::empty();
|
||||||
new_mask.add(raw_signal);
|
new_mask.add(signal);
|
||||||
|
|
||||||
// Re-raise with signal unmasked
|
// Re-raise with signal unmasked
|
||||||
let _ = new_mask.thread_unblock();
|
let _ = new_mask.thread_unblock();
|
||||||
let _ = raise(raw_signal);
|
let _ = raise(signal);
|
||||||
let _ = new_mask.thread_block();
|
let _ = new_mask.thread_block();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -107,7 +81,7 @@ pub fn install_handler<F>(handler: F)
|
|||||||
use winapi::{BOOL, DWORD, FALSE, TRUE};
|
use winapi::{BOOL, DWORD, FALSE, TRUE};
|
||||||
|
|
||||||
pub unsafe extern "system" fn ctrl_handler(_: DWORD) -> BOOL {
|
pub unsafe extern "system" fn ctrl_handler(_: DWORD) -> BOOL {
|
||||||
invoke(self::Signal::Terminate);
|
invoke(self::Signal::SIGTERM);
|
||||||
|
|
||||||
FALSE
|
FALSE
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user