Skip to content

Commit afd0d9d

Browse files
committed
Add a convenience method .pid() for WaitStatus.
1 parent 607ab97 commit afd0d9d

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

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 if the status is not 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 => {},
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)