mirror of
https://github.com/watchexec/watchexec.git
synced 2024-09-29 14:41:35 +02:00
Launch child processes in new process group
This commit is contained in:
parent
f0650274a0
commit
f3c6df8845
3
Cargo.lock
generated
3
Cargo.lock
generated
@ -1,10 +1,11 @@
|
|||||||
[root]
|
[root]
|
||||||
name = "watchexec"
|
name = "watchexec"
|
||||||
version = "1.1.0"
|
version = "1.1.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap 2.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"clap 2.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
"env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"notify 2.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
"notify 2.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
@ -15,6 +15,7 @@ lto = true
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
glob = "0.2.11"
|
glob = "0.2.11"
|
||||||
|
libc = "0.2.16"
|
||||||
log = "0.3.6"
|
log = "0.3.6"
|
||||||
notify = "2.6.3"
|
notify = "2.6.3"
|
||||||
|
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
|
#![feature(process_exec)]
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate clap;
|
extern crate clap;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
|
extern crate libc;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
extern crate notify;
|
extern crate notify;
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use libc;
|
||||||
use std::process::{Child, Command};
|
use std::process::{Child, Command};
|
||||||
|
|
||||||
pub struct Runner {
|
pub struct Runner {
|
||||||
@ -11,7 +12,7 @@ impl Runner {
|
|||||||
Runner {
|
Runner {
|
||||||
process: None,
|
process: None,
|
||||||
restart: restart,
|
restart: restart,
|
||||||
cls: clear,
|
cls: clear
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,6 +26,22 @@ impl Runner {
|
|||||||
let _ = Command::new("clear").status();
|
let _ = Command::new("clear").status();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(target_family = "windows")]
|
||||||
|
fn kill(child: &mut Child) {
|
||||||
|
let _ = child.kill();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_family = "unix")]
|
||||||
|
fn kill(child: &mut Child) {
|
||||||
|
extern {
|
||||||
|
fn killpg(pgrp: libc::pid_t, sig: libc::c_int) -> libc::c_int;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
killpg(child.id() as i32, libc::SIGTERM);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(target_family = "windows")]
|
#[cfg(target_family = "windows")]
|
||||||
fn invoke(&self, cmd: &str, updated_paths: Vec<&str>) -> Option<Child> {
|
fn invoke(&self, cmd: &str, updated_paths: Vec<&str>) -> Option<Child> {
|
||||||
let mut command = Command::new("cmd.exe");
|
let mut command = Command::new("cmd.exe");
|
||||||
@ -39,6 +56,8 @@ impl Runner {
|
|||||||
|
|
||||||
#[cfg(target_family = "unix")]
|
#[cfg(target_family = "unix")]
|
||||||
fn invoke(&self, cmd: &str, updated_paths: Vec<&str>) -> Option<Child> {
|
fn invoke(&self, cmd: &str, updated_paths: Vec<&str>) -> Option<Child> {
|
||||||
|
use std::os::unix::process::CommandExt;
|
||||||
|
|
||||||
let mut command = Command::new("sh");
|
let mut command = Command::new("sh");
|
||||||
command.arg("-c").arg(cmd);
|
command.arg("-c").arg(cmd);
|
||||||
|
|
||||||
@ -46,18 +65,21 @@ impl Runner {
|
|||||||
command.env("WATCHEXEC_UPDATED_PATH", updated_paths[0]);
|
command.env("WATCHEXEC_UPDATED_PATH", updated_paths[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
command.spawn().ok()
|
command
|
||||||
|
.before_exec(|| unsafe { libc::setpgid(0, 0); Ok(()) })
|
||||||
|
.spawn()
|
||||||
|
.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_command(&mut self, cmd: &str, updated_paths: Vec<&str>) {
|
pub fn run_command(&mut self, cmd: &str, updated_paths: Vec<&str>) {
|
||||||
if let Some(ref mut child) = self.process {
|
if let Some(ref mut child) = self.process {
|
||||||
if self.restart {
|
if self.restart {
|
||||||
debug!("Killing child process (pid: {})", child.id());
|
debug!("Killing child process (pid: {})", child.id());
|
||||||
let _ = child.kill();
|
Runner::kill(child);
|
||||||
}
|
}
|
||||||
|
|
||||||
debug!("Waiting for child process (pid: {})", child.id());
|
debug!("Waiting for child process (pid: {})", child.id());
|
||||||
let _ = child.wait();
|
Runner::wait(child);
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.cls {
|
if self.cls {
|
||||||
@ -68,4 +90,19 @@ impl Runner {
|
|||||||
|
|
||||||
self.process = self.invoke(cmd, updated_paths);
|
self.process = self.invoke(cmd, updated_paths);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(target_family = "windows")]
|
||||||
|
fn wait(child: &mut Child) {
|
||||||
|
let _ = child.wait();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_family = "unix")]
|
||||||
|
fn wait(child: &mut Child) {
|
||||||
|
unsafe {
|
||||||
|
let pid = child.id() as i32;
|
||||||
|
let status: Box<i32> = Box::new(0);
|
||||||
|
libc::waitpid(-pid, Box::into_raw(status), 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user