Trait ExitStatusExt
pub trait ExitStatusExt
Unix-specific extensions to ExitStatus and ExitStatusError.
On Unix, ExitStatus does not necessarily represent an exit status, as
passed to the _exit system call or returned by
ExitStatus::code(). It represents any wait status
as returned by one of the wait family of system
calls.
A Unix wait status (a Rust ExitStatus) can represent a Unix exit status, but can also
represent other kinds of process event.
Required Methods
fn from_raw(raw: i32) -> SelfCreates a new
ExitStatusorExitStatusErrorfrom the raw underlying integer status value fromwait.The value should be a wait status, not an exit status.
Example
A signal-terminated
waitstatus carries the signal number, whichExitStatus::signalrecovers using the platform'sWTERMSIGmacro. Note that the bit layout of a wait status is not specified by POSIX and is platform-specific. By convention on most Unix platforms, the signal number occupies the low 7 bits with the exit-code byte left zero, so a bare signal number between 1 and 126 is treated as a signal-terminated wait status. The following example relies on that convention and is therefore not guaranteed to hold on every target:# if cfg!(target_os = "fuchsia") { return; } use std::os::unix::process::ExitStatusExt; use std::process::ExitStatus; let signal = 15; // SIGTERM assert!(signal > 0 && signal < 0x7f, "not a valid Unix termination signal: {signal}"); let status = ExitStatus::from_raw(signal); assert!(!status.success()); assert_eq!(status.code(), None); assert_eq!(status.signal(), Some(15));Generating an
ExitStatuswith a given exit code (0-255) is system-dependent. The value returned byExitStatus::codeis specified to come from applying theWEXITSTATUSmacro, but there is no POSIX-specified constructor and the bit layout is left unspecified. By near-universal convention every Unix libc stores the 8-bit exit code in bits 8..16, so a status built with(code & 0xff) << 8will usually round-trip back to the original exit code:# if cfg!(target_os = "fuchsia") { return; } use std::os::unix::process::ExitStatusExt; use std::process::ExitStatus; let code = 41; let status = ExitStatus::from_raw((code & 0xff) << 8); assert_eq!(status.code(), Some(41)); assert!(!status.success());Panics
ExitStatusError::from_rawpanics on an attempt to make anExitStatusErrorfrom awaitstatus of0.ExitStatus::from_rawalways succeeds and never panics.
fn signal(&self) -> Option<i32>If the process was terminated by a signal, returns that signal.
In other words, if
WIFSIGNALED, this returnsWTERMSIG. For such a status,ExitStatus::codereturnsNone:# if cfg!(target_os = "fuchsia") { return; } use std::os::unix::process::ExitStatusExt; use std::process::ExitStatus; let sigterm = 15; let status = ExitStatus::from_raw(sigterm); assert_eq!(status.code(), None); assert_eq!(status.signal(), Some(sigterm));A process that receives a signal may catch and handle it, then exit normally with an exit code. When that happens,
signalreturnsNone.Rust does not pass commands through a shell, such as
bashandsh, but it is possible to do so manually. When invoking a shell, the signal value indicates whether the top-level shell itself received a terminating signal. If instead a command within an invoked shell receives a terminating signal, many shells convert the signal number into an exit code by adding 128. For example, a command run undershthat receives aSIGTERMcanonically causes the shell to report an exit code of15 + 128, i.e.143.fn core_dumped(&self) -> boolIf the process was terminated by a signal, says whether it dumped core.
fn stopped_signal(&self) -> Option<i32>If the process was stopped by a signal, returns that signal.
In other words, if
WIFSTOPPED, this returnsWSTOPSIG. This is only possible if the status came from awaitsystem call which was passedWUNTRACED, and was then converted into anExitStatus.fn continued(&self) -> boolWhether the process was continued from a stopped status.
I.e.
WIFCONTINUED. This is only possible if the status came from awaitsystem call which was passedWCONTINUED, and was then converted into anExitStatus.fn into_raw(self) -> i32Returns the underlying raw
waitstatus.The returned integer is a wait status, not an exit status.
Implementors
impl ExitStatusExt for ExitStatusimpl ExitStatusExt for ExitStatusError