Skip to content

Commit 013d2d2

Browse files
committed
std: unix process_unsupported: Provide a wait status emulation
Fixes #114593 Needs FCP due to behavioural changes.
1 parent d8c69df commit 013d2d2

File tree

3 files changed

+120
-50
lines changed

3 files changed

+120
-50
lines changed

Diff for: library/std/src/sys/unix/process/process_unix.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1061,3 +1061,8 @@ impl crate::os::linux::process::ChildExt for crate::process::Child {
10611061
#[cfg(test)]
10621062
#[path = "process_unix/tests.rs"]
10631063
mod tests;
1064+
1065+
// See [`process_unsupported_wait_status::compare_with_linux`];
1066+
#[cfg(all(test, target_os = "linux"))]
1067+
#[path = "process_unsupported/wait_status.rs"]
1068+
mod process_unsupported_wait_status;

Diff for: library/std/src/sys/unix/process/process_unsupported.rs

+2-50
Original file line numberDiff line numberDiff line change
@@ -55,56 +55,8 @@ impl Process {
5555
}
5656
}
5757

58-
#[derive(PartialEq, Eq, Clone, Copy, Debug, Default)]
59-
pub struct ExitStatus(c_int);
60-
61-
impl ExitStatus {
62-
#[cfg_attr(target_os = "horizon", allow(unused))]
63-
pub fn success(&self) -> bool {
64-
self.code() == Some(0)
65-
}
66-
67-
pub fn exit_ok(&self) -> Result<(), ExitStatusError> {
68-
Err(ExitStatusError(1.try_into().unwrap()))
69-
}
70-
71-
pub fn code(&self) -> Option<i32> {
72-
None
73-
}
74-
75-
pub fn signal(&self) -> Option<i32> {
76-
None
77-
}
78-
79-
pub fn core_dumped(&self) -> bool {
80-
false
81-
}
82-
83-
pub fn stopped_signal(&self) -> Option<i32> {
84-
None
85-
}
86-
87-
pub fn continued(&self) -> bool {
88-
false
89-
}
90-
91-
pub fn into_raw(&self) -> c_int {
92-
0
93-
}
94-
}
95-
96-
/// Converts a raw `c_int` to a type-safe `ExitStatus` by wrapping it without copying.
97-
impl From<c_int> for ExitStatus {
98-
fn from(a: c_int) -> ExitStatus {
99-
ExitStatus(a as i32)
100-
}
101-
}
102-
103-
impl fmt::Display for ExitStatus {
104-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105-
write!(f, "exit code: {}", self.0)
106-
}
107-
}
58+
mod wait_status;
59+
pub use wait_status::ExitStatus;
10860

10961
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
11062
pub struct ExitStatusError(NonZero_c_int);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
//! Emulated wait status for non-Unix #[cfg(unix) platforms
2+
//!
3+
//! Separate module to facilitate testing against a real Unix implementation.
4+
5+
use crate::ffi::c_int;
6+
use crate::fmt;
7+
8+
/// Emulated wait status for use by `process_unsupported.rs`
9+
///
10+
/// Uses the "traditional unix" encoding. For use on platfors which are `#[cfg(unix)]`
11+
/// but do not actually support subprocesses at all.
12+
///
13+
/// These platforms aren't Unix, but are simply pretending to be for porting convenience.
14+
/// So, we provide a faithful pretence here.
15+
#[derive(PartialEq, Eq, Clone, Copy, Debug, Default)]
16+
pub struct ExitStatus {
17+
wait_status: c_int,
18+
}
19+
20+
/// Converts a raw `c_int` to a type-safe `ExitStatus` by wrapping it
21+
impl From<c_int> for ExitStatus {
22+
fn from(wait_status: c_int) -> ExitStatus {
23+
ExitStatus { wait_status }
24+
}
25+
}
26+
27+
impl fmt::Display for ExitStatus {
28+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29+
write!(f, "emulated wait status: {}", self.wait_status)
30+
}
31+
}
32+
33+
impl ExitStatus {
34+
pub fn code(&self) -> Option<i32> {
35+
// Linux and FreeBSD both agree that values linux 0x80
36+
// count as "WIFEXITED" even though this is quite mad.
37+
// Likewise the macros disregard all the high bits, so are happy to declare
38+
// out-of-range values to be WIFEXITED, WIFSTOPPED, etc.
39+
let w = self.wait_status;
40+
if (w & 0x7f) == 0 {
41+
Some((w & 0xff00) >> 8)
42+
} else {
43+
None
44+
}
45+
}
46+
47+
pub fn signal(&self) -> Option<i32> {
48+
let signal = self.wait_status & 0x007f;
49+
if signal > 0 && signal < 0x7f {
50+
Some(signal)
51+
} else {
52+
None
53+
}
54+
}
55+
56+
pub fn core_dumped(&self) -> bool {
57+
self.signal().is_some() && (self.wait_status & 0x80) != 0
58+
}
59+
60+
pub fn stopped_signal(&self) -> Option<i32> {
61+
let w = self.wait_status;
62+
if (w & 0xff) == 0x7f {
63+
Some((w & 0xff00) >> 8)
64+
} else {
65+
None
66+
}
67+
}
68+
69+
pub fn continued(&self) -> bool {
70+
self.wait_status == 0xffff
71+
}
72+
73+
pub fn into_raw(&self) -> c_int {
74+
self.wait_status
75+
}
76+
}
77+
78+
// Test that our emulation exactly matches Linux
79+
//
80+
// This test runs *on Linux* but it tests
81+
// the implementation used on non-Unix `#[cfg(unix)]` platforms.
82+
//
83+
// I.e. we're using Linux as a proxy for "trad unix".
84+
#[cfg(all(test, target_os = "linux"))]
85+
mod compare_with_linux {
86+
use super::ExitStatus as Emulated;
87+
use crate::process::ExitStatus as Real;
88+
use crate::os::unix::process::ExitStatusExt as _;
89+
90+
#[test]
91+
fn compare() {
92+
// Check that we handle out-of-range values similarly, too.
93+
for wstatus in -0xf_ffff..0xf_ffff {
94+
let emulated = Emulated::from(wstatus);
95+
let real = Real::from_raw(wstatus);
96+
97+
macro_rules! compare { { $method:ident } => {
98+
assert_eq!(
99+
emulated.$method(),
100+
real.$method(),
101+
"{wstatus:#x}.{}()",
102+
stringify!($method),
103+
);
104+
} }
105+
compare!(code);
106+
compare!(signal);
107+
compare!(core_dumped);
108+
compare!(stopped_signal);
109+
compare!(continued);
110+
compare!(into_raw);
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)