mirror of
https://github.com/watchexec/watchexec.git
synced 2024-09-29 22:51:33 +02:00
Use before_exec as process_exec landed in stable
This commit is contained in:
parent
4fffb534b6
commit
f62c3724b2
@ -25,10 +25,7 @@ mod imp {
|
|||||||
impl Process {
|
impl Process {
|
||||||
pub fn new(cmd: &str, updated_paths: Vec<PathBuf>) -> Result<Process> {
|
pub fn new(cmd: &str, updated_paths: Vec<PathBuf>) -> Result<Process> {
|
||||||
use nix::unistd::*;
|
use nix::unistd::*;
|
||||||
use std::fs::File;
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::{Read, Write};
|
|
||||||
use std::os::unix::io::FromRawFd;
|
|
||||||
use std::os::unix::process::CommandExt;
|
use std::os::unix::process::CommandExt;
|
||||||
|
|
||||||
let mut command = Command::new("sh");
|
let mut command = Command::new("sh");
|
||||||
@ -43,47 +40,15 @@ mod imp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Until process_exec lands in stable, handle fork/exec ourselves
|
// Until process_exec lands in stable, handle fork/exec ourselves
|
||||||
// command.before_exec(|| setpgid(0, 0).map_err(io::Error::from))
|
command.before_exec(|| setpgid(0, 0).map_err(io::Error::from))
|
||||||
// .spawn()
|
.spawn()
|
||||||
// .and_then(|p| Ok(Process { pid: p.id() as i32 }))
|
.and_then(|p| {
|
||||||
|
|
||||||
// Wait for child to call setpgid()
|
|
||||||
// Else, we risk racing waitpid/killpg (mostly just in tests, but hey)
|
|
||||||
let (r, w) = try!(pipe());
|
|
||||||
|
|
||||||
match fork() {
|
|
||||||
Ok(ForkResult::Parent { child, .. }) => {
|
|
||||||
{
|
|
||||||
let mut reader = unsafe { File::from_raw_fd(r) };
|
|
||||||
let mut buffer = vec![0];
|
|
||||||
let _ = reader.read_exact(&mut buffer);
|
|
||||||
}
|
|
||||||
let _ = close(w);
|
|
||||||
|
|
||||||
Ok(Process {
|
Ok(Process {
|
||||||
pgid: child,
|
pgid: p.id() as i32,
|
||||||
lock: Mutex::new(false),
|
lock: Mutex::new(false),
|
||||||
cvar: Condvar::new(),
|
cvar: Condvar::new(),
|
||||||
})
|
})
|
||||||
}
|
})
|
||||||
Ok(ForkResult::Child) => {
|
|
||||||
let _ = setpgid(0, 0);
|
|
||||||
|
|
||||||
{
|
|
||||||
let mut writer = unsafe { File::from_raw_fd(w) };
|
|
||||||
let _ = writer.write_all(&[42]);
|
|
||||||
}
|
|
||||||
let _ = close(r);
|
|
||||||
|
|
||||||
let _ = command.exec();
|
|
||||||
|
|
||||||
// If we get here, there isn't much we can do
|
|
||||||
unsafe {
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => Err(io::Error::from(e)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn kill(&self) {
|
pub fn kill(&self) {
|
||||||
@ -195,8 +160,7 @@ mod imp {
|
|||||||
command.env("WATCHEXEC_COMMON_PATH", common_path);
|
command.env("WATCHEXEC_COMMON_PATH", common_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
command.spawn()
|
command.spawn().and_then(|p| {
|
||||||
.and_then(|p| {
|
|
||||||
let r = unsafe { AssignProcessToJobObject(job, p.into_raw_handle()) };
|
let r = unsafe { AssignProcessToJobObject(job, p.into_raw_handle()) };
|
||||||
if r == 0 {
|
if r == 0 {
|
||||||
panic!("failed to add to job object: {}", last_err());
|
panic!("failed to add to job object: {}", last_err());
|
||||||
|
Loading…
Reference in New Issue
Block a user