Skip to content

Commit bd9a119

Browse files
committed
Use libc definitions for wait module
1 parent 087aece commit bd9a119

File tree

1 file changed

+23
-136
lines changed

1 file changed

+23
-136
lines changed

src/sys/wait.rs

Lines changed: 23 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,26 @@ use unistd::Pid;
44

55
use sys::signal::Signal;
66

7-
mod ffi {
8-
use libc::{pid_t, c_int};
9-
10-
extern {
11-
pub fn waitpid(pid: pid_t, status: *mut c_int, options: c_int) -> pid_t;
12-
}
13-
}
14-
15-
#[cfg(not(any(target_os = "linux",
16-
target_os = "android")))]
17-
libc_bitflags!(
18-
pub flags WaitPidFlag: c_int {
19-
WNOHANG,
20-
WUNTRACED,
21-
}
22-
);
23-
24-
#[cfg(any(target_os = "linux",
25-
target_os = "android"))]
267
libc_bitflags!(
278
pub flags WaitPidFlag: c_int {
289
WNOHANG,
2910
WUNTRACED,
3011
WEXITED,
3112
WCONTINUED,
32-
WNOWAIT, // Don't reap, just poll status.
33-
__WNOTHREAD, // Don't wait on children of other threads in this group
34-
__WALL, // Wait on all children, regardless of type
13+
WSTOPPED,
14+
/// Don't reap, just poll status.
15+
WNOWAIT,
16+
/// Don't wait on children of other threads in this group
17+
#[cfg(any(target_os = "android", target_os = "linux"))]
18+
__WNOTHREAD,
19+
/// Wait on all children, regardless of type
20+
#[cfg(any(target_os = "android", target_os = "linux"))]
21+
__WALL,
22+
#[cfg(any(target_os = "android", target_os = "linux"))]
3523
__WCLONE,
3624
}
3725
);
3826

39-
#[cfg(any(target_os = "linux",
40-
target_os = "android"))]
41-
const WSTOPPED: WaitPidFlag = WUNTRACED;
42-
4327
/// Possible return values from `wait()` or `waitpid()`.
4428
///
4529
/// Each status (other than `StillAlive`) describes a state transition
@@ -54,7 +38,7 @@ pub enum WaitStatus {
5438
/// The process exited normally (as with `exit()` or returning from
5539
/// `main`) with the given exit code. This case matches the C macro
5640
/// `WIFEXITED(status)`; the second field is `WEXITSTATUS(status)`.
57-
Exited(Pid, i8),
41+
Exited(Pid, i32),
5842
/// The process was killed by the given signal. The third field
5943
/// indicates whether the signal generated a core dump. This case
6044
/// matches the C macro `WIFSIGNALED(status)`; the last two fields
@@ -112,149 +96,52 @@ impl WaitStatus {
11296
}
11397
}
11498

115-
#[cfg(any(target_os = "linux",
116-
target_os = "android"))]
11799
mod status {
118100
use sys::signal::Signal;
119-
use libc::c_int;
120-
use libc::SIGTRAP;
101+
use libc::{self, c_int, SIGTRAP};
121102

122103
pub fn exited(status: i32) -> bool {
123-
(status & 0x7F) == 0
104+
unsafe { libc::WIFEXITED(status) }
124105
}
125106

126-
pub fn exit_status(status: i32) -> i8 {
127-
((status & 0xFF00) >> 8) as i8
107+
pub fn exit_status(status: i32) -> i32 {
108+
unsafe { libc::WEXITSTATUS(status) }
128109
}
129110

130111
pub fn signaled(status: i32) -> bool {
131-
((((status & 0x7f) + 1) as i8) >> 1) > 0
112+
unsafe { libc::WIFSIGNALED(status) }
132113
}
133114

134115
pub fn term_signal(status: i32) -> Signal {
135-
Signal::from_c_int(status & 0x7f).unwrap()
116+
Signal::from_c_int(unsafe { libc::WTERMSIG(status) }).unwrap()
136117
}
137118

138119
pub fn dumped_core(status: i32) -> bool {
139-
(status & 0x80) != 0
120+
unsafe { libc::WCOREDUMP(status) }
140121
}
141122

142123
pub fn stopped(status: i32) -> bool {
143-
(status & 0xff) == 0x7f
124+
unsafe { libc::WIFSTOPPED(status) }
144125
}
145126

146127
pub fn stop_signal(status: i32) -> Signal {
147-
// Keep only 7 bits of the signal: the high bit
148-
// is used to indicate syscall stops, below.
149-
Signal::from_c_int((status & 0x7F00) >> 8).unwrap()
128+
Signal::from_c_int(unsafe { libc::WSTOPSIG(status) }).unwrap()
150129
}
151130

152131
pub fn syscall_stop(status: i32) -> bool {
153132
// From ptrace(2), setting PTRACE_O_TRACESYSGOOD has the effect
154133
// of delivering SIGTRAP | 0x80 as the signal number for syscall
155134
// stops. This allows easily distinguishing syscall stops from
156135
// genuine SIGTRAP signals.
157-
((status & 0xFF00) >> 8) == SIGTRAP | 0x80
136+
unsafe { libc::WSTOPSIG(status) == SIGTRAP | 0x80 }
158137
}
159138

160139
pub fn stop_additional(status: i32) -> c_int {
161140
(status >> 16) as c_int
162141
}
163142

164143
pub fn continued(status: i32) -> bool {
165-
status == 0xFFFF
166-
}
167-
}
168-
169-
#[cfg(any(target_os = "macos",
170-
target_os = "ios"))]
171-
mod status {
172-
use sys::signal::{Signal,SIGCONT};
173-
174-
const WCOREFLAG: i32 = 0x80;
175-
const WSTOPPED: i32 = 0x7f;
176-
177-
fn wstatus(status: i32) -> i32 {
178-
status & 0x7F
179-
}
180-
181-
pub fn exit_status(status: i32) -> i8 {
182-
((status >> 8) & 0xFF) as i8
183-
}
184-
185-
pub fn stop_signal(status: i32) -> Signal {
186-
Signal::from_c_int(status >> 8).unwrap()
187-
}
188-
189-
pub fn continued(status: i32) -> bool {
190-
wstatus(status) == WSTOPPED && stop_signal(status) == SIGCONT
191-
}
192-
193-
pub fn stopped(status: i32) -> bool {
194-
wstatus(status) == WSTOPPED && stop_signal(status) != SIGCONT
195-
}
196-
197-
pub fn exited(status: i32) -> bool {
198-
wstatus(status) == 0
199-
}
200-
201-
pub fn signaled(status: i32) -> bool {
202-
wstatus(status) != WSTOPPED && wstatus(status) != 0
203-
}
204-
205-
pub fn term_signal(status: i32) -> Signal {
206-
Signal::from_c_int(wstatus(status)).unwrap()
207-
}
208-
209-
pub fn dumped_core(status: i32) -> bool {
210-
(status & WCOREFLAG) != 0
211-
}
212-
}
213-
214-
#[cfg(any(target_os = "freebsd",
215-
target_os = "openbsd",
216-
target_os = "dragonfly",
217-
target_os = "netbsd"))]
218-
mod status {
219-
use sys::signal::Signal;
220-
221-
const WCOREFLAG: i32 = 0x80;
222-
const WSTOPPED: i32 = 0x7f;
223-
224-
fn wstatus(status: i32) -> i32 {
225-
status & 0x7F
226-
}
227-
228-
pub fn stopped(status: i32) -> bool {
229-
wstatus(status) == WSTOPPED
230-
}
231-
232-
pub fn stop_signal(status: i32) -> Signal {
233-
Signal::from_c_int(status >> 8).unwrap()
234-
}
235-
236-
pub fn signaled(status: i32) -> bool {
237-
wstatus(status) != WSTOPPED && wstatus(status) != 0 && status != 0x13
238-
}
239-
240-
pub fn term_signal(status: i32) -> Signal {
241-
Signal::from_c_int(wstatus(status)).unwrap()
242-
}
243-
244-
pub fn exited(status: i32) -> bool {
245-
wstatus(status) == 0
246-
}
247-
248-
pub fn exit_status(status: i32) -> i8 {
249-
(status >> 8) as i8
250-
}
251-
252-
pub fn continued(status: i32) -> bool {
253-
status == 0x13
254-
}
255-
256-
pub fn dumped_core(status: i32) -> bool {
257-
(status & WCOREFLAG) != 0
144+
unsafe { libc::WIFCONTINUED(status) }
258145
}
259146
}
260147

@@ -299,7 +186,7 @@ pub fn waitpid<P: Into<Option<Pid>>>(pid: P, options: Option<WaitPidFlag>) -> Re
299186
None => 0
300187
};
301188

302-
let res = unsafe { ffi::waitpid(pid.into().unwrap_or(Pid::from_raw(-1)).into(), &mut status as *mut c_int, option_bits) };
189+
let res = unsafe { libc::waitpid(pid.into().unwrap_or(Pid::from_raw(-1)).into(), &mut status as *mut c_int, option_bits) };
303190

304191
Ok(match try!(Errno::result(res)) {
305192
0 => StillAlive,

0 commit comments

Comments
 (0)