Skip to content

Commit 6334f55

Browse files
committed
Merge #722
722: Add a convenience method .pid() for WaitStatus. r=asomers
2 parents be7acc1 + 0370de6 commit 6334f55

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1212
([#672](https://github.com/nix-rust/nix/pull/672))
1313
- Added protocol families in `AddressFamily` enum.
1414
([#647](https://github.com/nix-rust/nix/pull/647))
15+
- Added the `pid()` method to `WaitStatus` for extracting the PID.
16+
([#722](https://github.com/nix-rust/nix/pull/722))
1517

1618
### Changed
1719
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))

src/sys/wait.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,25 @@ pub enum WaitStatus {
9393
StillAlive
9494
}
9595

96+
impl WaitStatus {
97+
/// Extracts the PID from the WaitStatus unless it equals StillAlive.
98+
pub fn pid(&self) -> Option<Pid> {
99+
use self::WaitStatus::*;
100+
match *self {
101+
Exited(p, _) => Some(p),
102+
Signaled(p, _, _) => Some(p),
103+
Stopped(p, _) => Some(p),
104+
Continued(p) => Some(p),
105+
StillAlive => None,
106+
107+
#[cfg(any(target_os = "linux", target_os = "android"))]
108+
PtraceEvent(p, _, _) => Some(p),
109+
#[cfg(any(target_os = "linux", target_os = "android"))]
110+
PtraceSyscall(p) => Some(p),
111+
}
112+
}
113+
}
114+
96115
#[cfg(any(target_os = "linux",
97116
target_os = "android"))]
98117
mod status {

test/sys/test_wait.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ fn test_wait_exit() {
3737
}
3838
}
3939

40+
#[test]
41+
fn test_waitstatus_pid() {
42+
let _m = ::FORK_MTX.lock().expect("Mutex got poisoned by another test");
43+
44+
match fork().unwrap() {
45+
Child => unsafe { _exit(0) },
46+
Parent { child } => {
47+
let status = waitpid(child, None).unwrap();
48+
assert_eq!(status.pid(), Some(child));
49+
}
50+
}
51+
}
52+
4053
#[cfg(any(target_os = "linux", target_os = "android"))]
4154
// FIXME: qemu-user doesn't implement ptrace on most arches
4255
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
@@ -47,7 +60,7 @@ mod ptrace {
4760
use nix::sys::wait::*;
4861
use nix::unistd::*;
4962
use nix::unistd::ForkResult::*;
50-
use std::{ptr, process};
63+
use std::ptr;
5164
use libc::_exit;
5265

5366
fn ptrace_child() -> ! {

0 commit comments

Comments
 (0)