|
| 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