diff options
Diffstat (limited to 'src/wait')
-rw-r--r-- | src/wait/error.rs | 36 | ||||
-rw-r--r-- | src/wait/mod.rs | 81 | ||||
-rw-r--r-- | src/wait/rusage_ffi.rs | 5 |
3 files changed, 62 insertions, 60 deletions
diff --git a/src/wait/error.rs b/src/wait/error.rs index 169097d..fb489f5 100644 --- a/src/wait/error.rs +++ b/src/wait/error.rs @@ -5,7 +5,7 @@ use std::time::Duration; pub struct ProcessSignalInfo { pub pid: nix::unistd::Pid, pub signal: nix::sys::signal::Signal, - pub coredump: bool + pub coredump: bool, } #[derive(Debug)] @@ -14,10 +14,9 @@ pub enum WaitError { ReturnNonZero(i32, nix::unistd::Pid), Signaled(ProcessSignalInfo), OsError(nix::Error), - NotExited + NotExited, } - impl From<nix::Error> for WaitError { fn from(err: nix::Error) -> Self { WaitError::OsError(err) @@ -28,13 +27,20 @@ impl std::fmt::Display for WaitError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match *self { WaitError::TimedOut(dur) => write!(f, "process timed out in {:?}", dur), - WaitError::ReturnNonZero(ret, pid) => - write!(f, "process exited with non-zero exit code ({}). was {}", ret, pid), - WaitError::Signaled(ref info) => - write!(f, "process killed by {} {}. was {}", - info.signal, if info.coredump {"(core dumped)"} else {""}, info.pid), + WaitError::ReturnNonZero(ret, pid) => write!( + f, + "process exited with non-zero exit code ({}). was {}", + ret, pid + ), + WaitError::Signaled(ref info) => write!( + f, + "process killed by {} {}. was {}", + info.signal, + if info.coredump { "(core dumped)" } else { "" }, + info.pid + ), WaitError::OsError(err) => err.fmt(f), - WaitError::NotExited => write!(f, "process signaled, but not exited") + WaitError::NotExited => write!(f, "process signaled, but not exited"), } } } @@ -42,11 +48,11 @@ impl std::fmt::Display for WaitError { impl std::error::Error for WaitError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match *self { - WaitError::TimedOut(_) | - WaitError::ReturnNonZero(_, _) | - WaitError::Signaled(_) | - WaitError::NotExited => None, - WaitError::OsError(ref e) => Some(e) + WaitError::TimedOut(_) + | WaitError::ReturnNonZero(_, _) + | WaitError::Signaled(_) + | WaitError::NotExited => None, + WaitError::OsError(ref e) => Some(e), } } -}
\ No newline at end of file +} diff --git a/src/wait/mod.rs b/src/wait/mod.rs index 1a699a0..f87b954 100644 --- a/src/wait/mod.rs +++ b/src/wait/mod.rs @@ -1,33 +1,29 @@ use nix::libc::{self, c_int}; -use nix::{ - errno::Errno, - sys::wait::WaitStatus, - unistd::Pid -}; -use std::time::{ Instant, Duration }; +use nix::{errno::Errno, sys::wait::WaitStatus, unistd::Pid}; +use std::convert::{TryFrom, TryInto}; use std::process::Child; -use std::thread; use std::sync::mpsc; -use std::convert::{ TryFrom, TryInto }; +use std::thread; +use std::time::{Duration, Instant}; mod error; mod rusage_ffi; pub use rusage_ffi::Rusage; -pub use error::WaitError; use error::ProcessSignalInfo; +pub use error::WaitError; #[derive(Debug)] struct WaitInfo { pub status: WaitStatus, pub usage: Rusage, - pub wall_time: Duration + pub wall_time: Duration, } #[derive(Debug)] pub struct ChildExitStatus { pub usage: Rusage, - pub wall_time: Duration + pub wall_time: Duration, } impl TryFrom<WaitInfo> for ChildExitStatus { @@ -35,38 +31,43 @@ impl TryFrom<WaitInfo> for ChildExitStatus { fn try_from(info: WaitInfo) -> Result<Self, Self::Error> { match info.status { - WaitStatus::Exited(pid, ret) => - match ret { - 0 => Ok(ChildExitStatus { usage: info.usage, wall_time: info.wall_time }), - _ => Err(WaitError::ReturnNonZero(ret, pid)) - }, - WaitStatus::Signaled(pid, signal, coredump) => - Err(WaitError::Signaled(ProcessSignalInfo { pid, signal, coredump })), - _ => Err(WaitError::NotExited) + WaitStatus::Exited(pid, ret) => match ret { + 0 => Ok(ChildExitStatus { + usage: info.usage, + wall_time: info.wall_time, + }), + _ => Err(WaitError::ReturnNonZero(ret, pid)), + }, + WaitStatus::Signaled(pid, signal, coredump) => { + Err(WaitError::Signaled(ProcessSignalInfo { + pid, + signal, + coredump, + })) + } + _ => Err(WaitError::NotExited), } } } - #[cfg(target_os = "macos")] #[link(name = "c")] -extern { +extern "C" { fn wait4( - pid: libc::pid_t, - status: *mut c_int, - options: c_int, - rusage: *mut libc::rusage + pid: libc::pid_t, + status: *mut c_int, + options: c_int, + rusage: *mut libc::rusage, ) -> libc::pid_t; } #[cfg(target_os = "linux")] use libc::wait4; - fn wait4_pid( pid: Pid, chan: mpsc::Sender<std::result::Result<WaitInfo, nix::Error>>, - timer: Instant + timer: Instant, ) { let mut status: c_int = 0; let mut usg: libc::rusage; @@ -77,26 +78,23 @@ fn wait4_pid( wait_ret = wait4(pid.as_raw(), &mut status, 0 as c_int, &mut usg); } - #[allow(unused_must_use)] { + #[allow(unused_must_use)] + { chan.send(match wait_ret { -1 => Err(nix::Error::Sys(Errno::last())), - _ => WaitStatus::from_raw(pid, status).map(|nix_status| { - WaitInfo { - status: nix_status, - usage: usg.into(), - wall_time: timer.elapsed() - } - } - ) + _ => WaitStatus::from_raw(pid, status).map(|nix_status| WaitInfo { + status: nix_status, + usage: usg.into(), + wall_time: timer.elapsed(), + }), }); }; } - pub fn wait_child( mut child: Child, timeout: Duration, - timer: Instant + timer: Instant, ) -> Result<ChildExitStatus, WaitError> { let pid = Pid::from_raw(child.id() as i32); let (send, recv) = mpsc::channel(); @@ -110,12 +108,13 @@ pub fn wait_child( drop(recv); drop(thr); - #[allow(unused_must_use)] { + #[allow(unused_must_use)] + { child.kill(); } Err(WaitError::TimedOut(timeout)) - }, - Err(mpsc::RecvTimeoutError::Disconnected) => unreachable!() + } + Err(mpsc::RecvTimeoutError::Disconnected) => unreachable!(), } } diff --git a/src/wait/rusage_ffi.rs b/src/wait/rusage_ffi.rs index d77884e..4ac68ec 100644 --- a/src/wait/rusage_ffi.rs +++ b/src/wait/rusage_ffi.rs @@ -25,10 +25,7 @@ impl From<libc::rusage> for Rusage { fn from(usg: libc::rusage) -> Rusage { const MICROS_IN_SEC: u64 = 1_000_000; let convert_timeval = |tv: libc::timeval| { - Duration::from_micros( - tv.tv_sec as u64 * MICROS_IN_SEC + - tv.tv_usec as u64 - ) + Duration::from_micros(tv.tv_sec as u64 * MICROS_IN_SEC + tv.tv_usec as u64) }; Rusage { |