Skip to content

Commit 74e74e9

Browse files
authored
Rollup merge of #82411 - ijackson:fix-exitstatus, r=dtolnay
Fixes to ExitStatus and its docs * On Unix, properly display every possible wait status (and don't panic on weird values) * In the documentation, be clear and consistent about "exit status" vs "wait status".
2 parents c46f948 + 11ca644 commit 74e74e9

File tree

4 files changed

+75
-11
lines changed

4 files changed

+75
-11
lines changed

library/std/src/process.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ impl Command {
885885
}
886886

887887
/// Executes a command as a child process, waiting for it to finish and
888-
/// collecting its exit status.
888+
/// collecting its status.
889889
///
890890
/// By default, stdin, stdout and stderr are inherited from the parent.
891891
///
@@ -899,7 +899,7 @@ impl Command {
899899
/// .status()
900900
/// .expect("failed to execute process");
901901
///
902-
/// println!("process exited with: {}", status);
902+
/// println!("process finished with: {}", status);
903903
///
904904
/// assert!(status.success());
905905
/// ```
@@ -1368,11 +1368,17 @@ impl From<fs::File> for Stdio {
13681368

13691369
/// Describes the result of a process after it has terminated.
13701370
///
1371-
/// This `struct` is used to represent the exit status of a child process.
1371+
/// This `struct` is used to represent the exit status or other termination of a child process.
13721372
/// Child processes are created via the [`Command`] struct and their exit
13731373
/// status is exposed through the [`status`] method, or the [`wait`] method
13741374
/// of a [`Child`] process.
13751375
///
1376+
/// An `ExitStatus` represents every possible disposition of a process. On Unix this
1377+
/// is the **wait status**. It is *not* simply an *exit status* (a value passed to `exit`).
1378+
///
1379+
/// For proper error reporting of failed processes, print the value of `ExitStatus` using its
1380+
/// implementation of [`Display`](crate::fmt::Display).
1381+
///
13761382
/// [`status`]: Command::status
13771383
/// [`wait`]: Child::wait
13781384
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
@@ -1400,7 +1406,7 @@ impl ExitStatus {
14001406
/// if status.success() {
14011407
/// println!("'projects/' directory created");
14021408
/// } else {
1403-
/// println!("failed to create 'projects/' directory");
1409+
/// println!("failed to create 'projects/' directory: {}", status);
14041410
/// }
14051411
/// ```
14061412
#[stable(feature = "process", since = "1.0.0")]
@@ -1410,9 +1416,14 @@ impl ExitStatus {
14101416

14111417
/// Returns the exit code of the process, if any.
14121418
///
1413-
/// On Unix, this will return `None` if the process was terminated
1414-
/// by a signal; `std::os::unix` provides an extension trait for
1415-
/// extracting the signal and other details from the `ExitStatus`.
1419+
/// In Unix terms the return value is the **exit status**: the value passed to `exit`, if the
1420+
/// process finished by calling `exit`. Note that on Unix the exit status is truncated to 8
1421+
/// bits, and that values that didn't come from a program's call to `exit` may be invented the
1422+
/// runtime system (often, for example, 255, 254, 127 or 126).
1423+
///
1424+
/// On Unix, this will return `None` if the process was terminated by a signal.
1425+
/// [`ExitStatusExt`](crate::os::unix::process::ExitStatusExt) is an
1426+
/// extension trait for extracting any such signal, and other details, from the `ExitStatus`.
14161427
///
14171428
/// # Examples
14181429
///

library/std/src/sys/unix/ext/process.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,20 @@ impl CommandExt for process::Command {
188188

189189
/// Unix-specific extensions to [`process::ExitStatus`].
190190
///
191+
/// On Unix, `ExitStatus` **does not necessarily represent an exit status**, as passed to the
192+
/// `exit` system call or returned by [`ExitStatus::code()`](crate::process::ExitStatus::code).
193+
/// It represents **any wait status**, as returned by one of the `wait` family of system calls.
194+
///
195+
/// This is because a Unix wait status (a Rust `ExitStatus`) can represent a Unix exit status, but
196+
/// can also represent other kinds of process event.
197+
///
191198
/// This trait is sealed: it cannot be implemented outside the standard library.
192199
/// This is so that future additional methods are not breaking changes.
193200
#[stable(feature = "rust1", since = "1.0.0")]
194201
pub trait ExitStatusExt: Sealed {
195-
/// Creates a new `ExitStatus` from the raw underlying `i32` return value of
196-
/// a process.
202+
/// Creates a new `ExitStatus` from the raw underlying integer status value from `wait`
203+
///
204+
/// The value should be a **wait status, not an exit status**.
197205
#[stable(feature = "exit_status_from", since = "1.12.0")]
198206
fn from_raw(raw: i32) -> Self;
199207

@@ -222,6 +230,8 @@ pub trait ExitStatusExt: Sealed {
222230
fn continued(&self) -> bool;
223231

224232
/// Returns the underlying raw `wait` status.
233+
///
234+
/// The returned integer is a **wait status, not an exit status**.
225235
#[unstable(feature = "unix_process_wait_more", issue = "80695")]
226236
fn into_raw(self) -> i32;
227237
}

library/std/src/sys/unix/process/process_unix.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,22 @@ impl fmt::Display for ExitStatus {
527527
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
528528
if let Some(code) = self.code() {
529529
write!(f, "exit code: {}", code)
530+
} else if let Some(signal) = self.signal() {
531+
if self.core_dumped() {
532+
write!(f, "signal: {} (core dumped)", signal)
533+
} else {
534+
write!(f, "signal: {}", signal)
535+
}
536+
} else if let Some(signal) = self.stopped_signal() {
537+
write!(f, "stopped (not terminated) by signal: {}", signal)
538+
} else if self.continued() {
539+
write!(f, "continued (WIFCONTINUED)")
530540
} else {
531-
let signal = self.signal().unwrap();
532-
write!(f, "signal: {}", signal)
541+
write!(f, "unrecognised wait status: {} {:#x}", self.0, self.0)
533542
}
534543
}
535544
}
545+
546+
#[cfg(test)]
547+
#[path = "process_unix/tests.rs"]
548+
mod tests;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#[test]
2+
fn exitstatus_display_tests() {
3+
// In practice this is the same on every Unix.
4+
// If some weird platform turns out to be different, and this test fails, use #[cfg].
5+
use crate::os::unix::process::ExitStatusExt;
6+
use crate::process::ExitStatus;
7+
8+
let t = |v, s| assert_eq!(s, format!("{}", <ExitStatus as ExitStatusExt>::from_raw(v)));
9+
10+
t(0x0000f, "signal: 15");
11+
t(0x0008b, "signal: 11 (core dumped)");
12+
t(0x00000, "exit code: 0");
13+
t(0x0ff00, "exit code: 255");
14+
15+
// On MacOS, 0x0137f is WIFCONTINUED, not WIFSTOPPED. Probably *BSD is similar.
16+
// https://github.com/rust-lang/rust/pull/82749#issuecomment-790525956
17+
// The purpose of this test is to test our string formatting, not our understanding of the wait
18+
// status magic numbers. So restrict these to Linux.
19+
if cfg!(target_os = "linux") {
20+
t(0x0137f, "stopped (not terminated) by signal: 19");
21+
t(0x0ffff, "continued (WIFCONTINUED)");
22+
}
23+
24+
// Testing "unrecognised wait status" is hard because the wait.h macros typically
25+
// assume that the value came from wait and isn't mad. With the glibc I have here
26+
// this works:
27+
if cfg!(all(target_os = "linux", target_env = "gnu")) {
28+
t(0x000ff, "unrecognised wait status: 255 0xff");
29+
}
30+
}

0 commit comments

Comments
 (0)