-
Notifications
You must be signed in to change notification settings - Fork 0
/
waitpid.rs
77 lines (67 loc) · 1.83 KB
/
waitpid.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#[allow(unstable)]
extern crate libc;
extern crate ptrace;
extern crate "posix-ipc" as ipc;
use self::ipc::signals;
use std::os;
use std::num::FromPrimitive;
#[derive(Copy, Show)]
pub enum WaitState {
Stopped(signals::Signal),
Continued,
Exited(isize),
Signaled(signals::Signal),
PTrace(ptrace::Event)
}
impl WaitState {
pub fn from_i32(v: i32) -> Self {
if v & 0xff == 0x7f {
let sig = FromPrimitive::from_i32((v & 0xff00) >> 8).expect("Unknown signal");
let evt = ptrace::Event::from_wait_status(v);
match evt {
Option::Some(s) => WaitState::PTrace(s),
Option::None => WaitState::Stopped(sig)
}
} else if v == 0xffff {
WaitState::Continued
} else if (v & 0xff00) >> 8 == 0 {
WaitState::Exited((v & 0xff) as isize)
} else if (((v & 0x7f) + 1) >> 1) > 0 {
WaitState::Signaled(FromPrimitive::from_i32(v & 0x7f).expect("Unknown signal"))
} else {
panic! ("Unknown wait state: {:?}", v);
}
}
}
#[derive(Show, Copy)]
pub struct WaitResult {
pub pid: libc::pid_t,
pub status: i32,
pub state: WaitState
}
bitflags! {
flags Options: i32 {
const None = 0,
const NoWait = 1,
const All = 0x40000000
}
}
#[allow(unstable)]
pub fn wait(pid: libc::pid_t, opts: Options) -> Result<WaitResult, usize> {
let mut st: libc::c_int = 0;
let r;
unsafe {
r = ext::waitpid(pid, &mut st, opts.bits);
}
if r >= 0 {
Result::Ok(WaitResult {pid: r, status: st, state: WaitState::from_i32(st)})
} else {
Result::Err(os::errno())
}
}
mod ext {
use super::libc;
extern "C" {
pub fn waitpid(pid: libc::pid_t, st: *mut libc::c_int, options: libc::c_int) -> libc::pid_t;
}
}