diff options
author | Marcin Mielniczuk <marmistrz.dev@zoho.eu> | 2017-08-07 15:10:41 +0200 |
---|---|---|
committer | Marcin Mielniczuk <marmistrz.dev@zoho.eu> | 2017-08-08 10:05:44 +0200 |
commit | 0370de6cb99a320b67afebe048e9b1d9a7474b13 (patch) | |
tree | c1f94d4336d1f0a8d614283adcc374517785b8f4 | |
parent | 607ab97ac64f597e78ab321aedd3063f8e040074 (diff) | |
download | nix-0370de6cb99a320b67afebe048e9b1d9a7474b13.zip |
Add a convenience method .pid() to WaitStatus.
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | src/sys/wait.rs | 19 | ||||
-rw-r--r-- | test/sys/test_wait.rs | 15 |
3 files changed, 35 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index f1d278d6..247e9b36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ([#672](https://github.com/nix-rust/nix/pull/672)) - Added protocol families in `AddressFamily` enum. ([#647](https://github.com/nix-rust/nix/pull/647)) +- Added the `pid()` method to `WaitStatus` for extracting the PID. + ([#722](https://github.com/nix-rust/nix/pull/722)) ### Changed - Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692)) diff --git a/src/sys/wait.rs b/src/sys/wait.rs index f31f666d..b2ca3bd6 100644 --- a/src/sys/wait.rs +++ b/src/sys/wait.rs @@ -93,6 +93,25 @@ pub enum WaitStatus { StillAlive } +impl WaitStatus { + /// Extracts the PID from the WaitStatus unless it equals StillAlive. + pub fn pid(&self) -> Option<Pid> { + use self::WaitStatus::*; + match *self { + Exited(p, _) => Some(p), + Signaled(p, _, _) => Some(p), + Stopped(p, _) => Some(p), + Continued(p) => Some(p), + StillAlive => None, + + #[cfg(any(target_os = "linux", target_os = "android"))] + PtraceEvent(p, _, _) => Some(p), + #[cfg(any(target_os = "linux", target_os = "android"))] + PtraceSyscall(p) => Some(p), + } + } +} + #[cfg(any(target_os = "linux", target_os = "android"))] mod status { diff --git a/test/sys/test_wait.rs b/test/sys/test_wait.rs index 8d2ca85f..620a4e33 100644 --- a/test/sys/test_wait.rs +++ b/test/sys/test_wait.rs @@ -37,6 +37,19 @@ fn test_wait_exit() { } } +#[test] +fn test_waitstatus_pid() { + let _m = ::FORK_MTX.lock().expect("Mutex got poisoned by another test"); + + match fork().unwrap() { + Child => unsafe { _exit(0) }, + Parent { child } => { + let status = waitpid(child, None).unwrap(); + assert_eq!(status.pid(), Some(child)); + } + } +} + #[cfg(any(target_os = "linux", target_os = "android"))] // FIXME: qemu-user doesn't implement ptrace on most arches #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] @@ -47,7 +60,7 @@ mod ptrace { use nix::sys::wait::*; use nix::unistd::*; use nix::unistd::ForkResult::*; - use std::{ptr, process}; + use std::ptr; use libc::_exit; fn ptrace_child() -> ! { |