unshare/
status.rs

1use std::fmt;
2use crate::{Signal};
3
4
5/// The exit status of a process
6///
7/// Returned either by `reap_zombies()` or by `child_events()`
8/// or by `Child::wait()`
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum ExitStatus {
11    /// Process exited normally with some exit code
12    Exited(i8),
13    /// Process was killed by a signal (bool flag is true when core is dumped)
14    Signaled(Signal, /* dore dumped */bool)
15}
16
17impl ExitStatus {
18    /// Returns `true` if this exit status means successful exit
19    pub fn success(&self) -> bool {
20        self == &ExitStatus::Exited(0)
21    }
22    /// Returns exit code if the process has exited normally
23    pub fn code(&self) -> Option<i32> {
24        match self {
25            &ExitStatus::Exited(e) => Some(e as i32),
26            &ExitStatus::Signaled(_, _) => None,
27        }
28    }
29    /// Returns signal number if he process was killed by signal
30    pub fn signal(&self) -> Option<i32> {
31        match self {
32            &ExitStatus::Exited(_) => None,
33            &ExitStatus::Signaled(sig, _) => Some(sig as i32),
34        }
35    }
36}
37
38impl fmt::Display for ExitStatus {
39    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
40        use self::ExitStatus::*;
41        match self {
42            &Exited(c) => write!(fmt, "exited with code {}", c),
43            &Signaled(sig, false) => {
44                write!(fmt, "killed by signal {:?}[{}]",
45                    sig, sig as i32)
46            }
47            &Signaled(sig, true) => {
48                write!(fmt, "killed by signal {:?}[{}] (core dumped)",
49                    sig, sig as i32)
50            }
51        }
52    }
53}