syd 3.52.0

rock-solid application kernel
Documentation
use std::fmt;

use crate::Signal;

/// The exit status of a process
///
/// Returned either by `reap_zombies()` or by `child_events()`
/// or by `Child::wait()`
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExitStatus {
    /// Process exited normally with some exit code
    Exited(i8),
    /// Process was killed by a signal (bool flag is true when core is dumped)
    Signaled(Signal, /* dore dumped */ bool),
}

impl ExitStatus {
    /// Returns `true` if this exit status means successful exit
    pub fn success(&self) -> bool {
        *self == ExitStatus::Exited(0)
    }
    /// Returns exit code if the process has exited normally
    pub fn code(&self) -> Option<i32> {
        match *self {
            ExitStatus::Exited(e) => Some(i32::from(e)),
            ExitStatus::Signaled(_, _) => None,
        }
    }
    /// Returns signal number if he process was killed by signal
    pub fn signal(&self) -> Option<i32> {
        match *self {
            ExitStatus::Exited(_) => None,
            ExitStatus::Signaled(sig, _) => Some(sig as i32),
        }
    }
}

impl fmt::Display for ExitStatus {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        use self::ExitStatus::*;
        match *self {
            Exited(c) => write!(fmt, "exited with code {c}"),
            Signaled(sig, false) => {
                write!(fmt, "killed by signal {:?}[{}]", sig, sig as i32)
            }
            Signaled(sig, true) => {
                write!(
                    fmt,
                    "killed by signal {:?}[{}] (core dumped)",
                    sig, sig as i32
                )
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use nix::sys::signal::Signal;

    use super::*;

    #[test]
    fn test_exit_status_1() {
        assert!(ExitStatus::Exited(0).success());
    }

    #[test]
    fn test_exit_status_2() {
        assert!(!ExitStatus::Exited(1).success());
    }

    #[test]
    fn test_exit_status_3() {
        assert!(!ExitStatus::Signaled(Signal::SIGKILL, false).success());
    }

    #[test]
    fn test_exit_status_4() {
        assert_eq!(ExitStatus::Exited(0).code(), Some(0));
    }

    #[test]
    fn test_exit_status_5() {
        assert_eq!(ExitStatus::Exited(42).code(), Some(42));
    }

    #[test]
    fn test_exit_status_6() {
        assert_eq!(ExitStatus::Signaled(Signal::SIGKILL, false).code(), None);
    }

    #[test]
    fn test_exit_status_7() {
        assert_eq!(ExitStatus::Exited(0).signal(), None);
    }

    #[test]
    fn test_exit_status_8() {
        let status = ExitStatus::Signaled(Signal::SIGTERM, false);
        assert_eq!(status.signal(), Some(Signal::SIGTERM as i32));
    }

    #[test]
    fn test_exit_status_9() {
        let s = ExitStatus::Exited(0).to_string();
        assert!(s.contains("exited"));
    }

    #[test]
    fn test_exit_status_10() {
        let s = ExitStatus::Signaled(Signal::SIGKILL, false).to_string();
        assert!(s.contains("killed"));
        assert!(!s.contains("core"));
    }

    #[test]
    fn test_exit_status_11() {
        let s = ExitStatus::Signaled(Signal::SIGSEGV, true).to_string();
        assert!(s.contains("core"));
    }
}