watchexec/src/process.rs

285 lines
8.1 KiB
Rust
Raw Normal View History

use std::path::PathBuf;
pub use self::imp::*;
#[cfg(target_family = "unix")]
mod imp {
use std::io::Result;
use std::path::PathBuf;
use std::process::Command;
pub struct Process {
pid: i32,
}
impl Process {
pub fn new(cmd: &str, updated_paths: Vec<PathBuf>) -> Result<Process> {
use std::io;
use std::os::unix::process::CommandExt;
2016-11-15 23:04:15 +01:00
use libc::exit;
use nix::unistd::*;
let mut command = Command::new("sh");
command.arg("-c").arg(cmd);
if let Some(single_path) = super::get_single_updated_path(&updated_paths) {
command.env("WATCHEXEC_UPDATED_PATH", single_path);
}
if let Some(common_path) = super::get_longest_common_path(&updated_paths) {
command.env("WATCHEXEC_COMMON_PATH", common_path);
}
// Until process_exec lands in stable, handle fork/exec ourselves
//command.before_exec(|| setpgid(0, 0).map_err(io::Error::from))
//.spawn()
//.and_then(|p| Ok(Process { pid: p.id() as i32 }))
match fork() {
Ok(ForkResult::Parent {child, .. }) => {
Ok(Process {
pid: child as i32
})
},
Ok(ForkResult::Child) => {
let _ = setpgid(0, 0);
2016-11-15 23:04:15 +01:00
let _ = command.exec();
2016-11-15 23:04:15 +01:00
// If we get here, there isn't much we can do
unsafe {
exit(1);
}
}
Err(e) => { Err(io::Error::from(e)) }
}
}
2016-11-09 15:44:00 +01:00
pub fn kill(&self) {
use libc;
extern "C" {
fn killpg(pgrp: libc::pid_t, sig: libc::c_int) -> libc::c_int;
}
unsafe {
killpg(self.pid, libc::SIGTERM);
}
}
2016-11-09 15:44:00 +01:00
pub fn wait(&self) {
use nix::sys::wait::waitpid;
let _ = waitpid(self.pid, None);
}
}
}
#[cfg(target_family = "windows")]
mod imp {
use std::io;
use std::io::Result;
use std::mem;
use std::path::PathBuf;
use std::process::Command;
use kernel32::*;
use winapi::*;
pub struct Process {
job: HANDLE,
}
impl Process {
pub fn new(cmd: &str, updated_paths: Vec<PathBuf>) -> Result<Process> {
use std::os::windows::io::IntoRawHandle;
fn last_err() -> io::Error {
io::Error::last_os_error()
}
let job = unsafe { CreateJobObjectW(0 as *mut _, 0 as *const _) };
if job.is_null() {
panic!("failed to create job object: {}", last_err());
}
let mut info: JOBOBJECT_EXTENDED_LIMIT_INFORMATION = unsafe { mem::zeroed() };
info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
let r = unsafe {
SetInformationJobObject(job,
JobObjectExtendedLimitInformation,
&mut info as *mut _ as LPVOID,
mem::size_of_val(&info) as DWORD)
};
if r == 0 {
panic!("failed to set job info: {}", last_err());
}
let mut command = Command::new("cmd.exe");
command.arg("/C").arg(cmd);
if let Some(single_path) = super::get_single_updated_path(&updated_paths) {
command.env("WATCHEXEC_UPDATED_PATH", single_path);
}
if let Some(common_path) = super::get_longest_common_path(&updated_paths) {
command.env("WATCHEXEC_COMMON_PATH", common_path);
}
command.spawn()
.and_then(|p| {
let r = unsafe { AssignProcessToJobObject(job, p.into_raw_handle()) };
if r == 0 {
panic!("failed to add to job object: {}", last_err());
}
2016-11-09 23:25:52 +01:00
Ok(Process { job: job })
})
}
2016-11-09 15:44:00 +01:00
pub fn kill(&self) {
unsafe {
let _ = TerminateJobObject(self.job, 1);
}
}
2016-11-09 15:44:00 +01:00
pub fn wait(&self) {
unsafe {
let _ = WaitForSingleObject(self.job, INFINITE);
}
}
}
impl Drop for Process {
2016-11-09 23:20:13 +01:00
fn drop(&mut self) {
unsafe {
let _ = CloseHandle(self.job);
}
}
}
unsafe impl Send for Process {}
2016-11-09 15:44:00 +01:00
unsafe impl Sync for Process {}
}
2016-10-30 17:37:34 +01:00
fn get_single_updated_path(paths: &[PathBuf]) -> Option<&str> {
paths.get(0).and_then(|p| p.to_str())
}
fn get_longest_common_path(paths: &[PathBuf]) -> Option<String> {
match paths.len() {
0 => return None,
1 => return paths[0].to_str().map(|ref_val| ref_val.to_string()),
_ => {}
};
let mut longest_path: Vec<_> = paths[0].components().collect();
for path in &paths[1..] {
let mut greatest_distance = 0;
for component_pair in path.components().zip(longest_path.iter()) {
if component_pair.0 != *component_pair.1 {
break;
}
greatest_distance += 1;
}
if greatest_distance != longest_path.len() {
longest_path.truncate(greatest_distance);
}
}
let mut result = PathBuf::new();
for component in longest_path {
result.push(component.as_os_str());
}
result.to_str().map(|ref_val| ref_val.to_string())
}
#[cfg(test)]
#[cfg(target_family = "unix")]
mod tests {
use std::path::{Path, PathBuf};
use std::thread;
use std::time::Duration;
use mktemp::Temp;
use super::imp::Process;
use super::get_longest_common_path;
fn file_contents(path: &Path) -> String {
use std::fs::File;
use std::io::Read;
let mut f = File::open(path).unwrap();
let mut s = String::new();
f.read_to_string(&mut s).unwrap();
s
}
#[test]
fn test_start() {
let process = Process::new("echo hi", vec![]);
2016-10-26 22:29:34 +02:00
assert!(process.is_ok());
}
#[test]
fn test_wait() {
let file = Temp::new_file().unwrap();
let path = file.to_path_buf();
2016-11-09 23:37:35 +01:00
let process = Process::new(&format!("echo hi > {}", path.to_str().unwrap()), vec![])
.unwrap();
process.wait();
assert!(file_contents(&path).starts_with("hi"));
}
#[test]
fn test_kill() {
let file = Temp::new_file().unwrap();
let path = file.to_path_buf();
2016-11-09 23:37:35 +01:00
let process = Process::new(&format!("sleep 20; echo hi > {}", path.to_str().unwrap()),
vec![])
.unwrap();
thread::sleep(Duration::from_millis(250));
process.kill();
process.wait();
assert!(file_contents(&path) == "");
}
#[test]
fn longest_common_path_should_return_correct_value() {
let single_path = vec![PathBuf::from("/tmp/random/")];
let single_result = get_longest_common_path(&single_path).unwrap();
assert_eq!(single_result, "/tmp/random/");
2016-10-30 17:28:54 +01:00
let common_paths = vec![PathBuf::from("/tmp/logs/hi"),
PathBuf::from("/tmp/logs/bye"),
PathBuf::from("/tmp/logs/bye"),
PathBuf::from("/tmp/logs/fly")];
let common_result = get_longest_common_path(&common_paths).unwrap();
assert_eq!(common_result, "/tmp/logs");
2016-10-30 17:28:54 +01:00
let diverging_paths = vec![PathBuf::from("/tmp/logs/hi"), PathBuf::from("/var/logs/hi")];
let diverging_result = get_longest_common_path(&diverging_paths).unwrap();
assert_eq!(diverging_result, "/");
2016-10-30 17:28:54 +01:00
let uneven_paths = vec![PathBuf::from("/tmp/logs/hi"),
PathBuf::from("/tmp/logs/"),
PathBuf::from("/tmp/logs/bye")];
let uneven_result = get_longest_common_path(&uneven_paths).unwrap();
assert_eq!(uneven_result, "/tmp/logs");
}
}