heim_process/os/unix/
signal.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use std::convert::TryFrom;
use std::io;

/// POSIX signals.
///
/// Signals list is based on the [POSIX.1-2017] specification.
///
/// [POSIX.1-2017]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Signal {
    /// Process abort signal.
    Abrt,
    /// Alarm clock.
    Alrm,
    /// Access to an undefined portion of a memory object.
    Bus,
    /// Child process terminated, stopped, or continued.
    Chld,
    /// Continue executing, if stopped.
    Cont,
    /// Erroneous arithmetic operation.
    Fpe,
    /// Hangup.
    Hup,
    /// Illegal instruction.
    Ill,
    /// Terminal interrupt signal.
    Int,
    /// Kill (cannot be caught or ignored).
    Kill,
    /// Write on a pipe with no one to read it.
    Pipe,
    /// Terminal quit signal.
    Quit,
    /// Invalid memory reference.
    Segv,
    /// Stop executing (cannot be caught or ignored).
    Stop,
    /// Termination signal.
    Term,
    /// Terminal stop signal.
    Tstp,
    /// Background process attempting read.
    Ttin,
    /// Background process attempting write.
    Ttou,
    /// User-defined signal 1.
    Usr1,
    /// User-defined signal 2.
    Usr2,
    /// Pollable event.
    #[cfg(not(target_os = "macos"))]
    Poll,
    /// Profiling timer expired.
    Prof,
    /// Bad system call.
    Sys,
    /// Trace/breakpoint trap.
    Trap,
    /// High bandwidth data is available at a socket.
    Urg,
    /// Virtual timer expired.
    VtAlrm,
    /// CPU time limit exceeded.
    XCpu,
    /// File size limit exceeded.
    XFsz,
}

impl TryFrom<libc::c_int> for Signal {
    type Error = io::Error;

    fn try_from(value: libc::c_int) -> io::Result<Self> {
        match value {
            libc::SIGABRT => Ok(Signal::Abrt),
            libc::SIGALRM => Ok(Signal::Alrm),
            libc::SIGBUS => Ok(Signal::Bus),
            libc::SIGCHLD => Ok(Signal::Chld),
            libc::SIGCONT => Ok(Signal::Cont),
            libc::SIGFPE => Ok(Signal::Fpe),
            libc::SIGHUP => Ok(Signal::Hup),
            libc::SIGILL => Ok(Signal::Ill),
            libc::SIGINT => Ok(Signal::Int),
            libc::SIGKILL => Ok(Signal::Kill),
            libc::SIGPIPE => Ok(Signal::Pipe),
            libc::SIGQUIT => Ok(Signal::Quit),
            libc::SIGSEGV => Ok(Signal::Segv),
            libc::SIGSTOP => Ok(Signal::Stop),
            libc::SIGTERM => Ok(Signal::Term),
            libc::SIGTSTP => Ok(Signal::Tstp),
            libc::SIGTTIN => Ok(Signal::Ttin),
            libc::SIGTTOU => Ok(Signal::Ttou),
            libc::SIGUSR1 => Ok(Signal::Usr1),
            libc::SIGUSR2 => Ok(Signal::Usr2),
            #[cfg(not(target_os = "macos"))]
            libc::SIGPOLL => Ok(Signal::Poll),
            libc::SIGPROF => Ok(Signal::Prof),
            libc::SIGSYS => Ok(Signal::Sys),
            libc::SIGTRAP => Ok(Signal::Trap),
            libc::SIGURG => Ok(Signal::Urg),
            libc::SIGVTALRM => Ok(Signal::VtAlrm),
            libc::SIGXCPU => Ok(Signal::XCpu),
            libc::SIGXFSZ => Ok(Signal::XFsz),
            _ => Err(io::Error::from(io::ErrorKind::InvalidInput)),
        }
    }
}

impl From<Signal> for libc::c_int {
    fn from(signal: Signal) -> Self {
        match signal {
            Signal::Abrt => libc::SIGABRT,
            Signal::Alrm => libc::SIGALRM,
            Signal::Bus => libc::SIGBUS,
            Signal::Chld => libc::SIGCHLD,
            Signal::Cont => libc::SIGCONT,
            Signal::Fpe => libc::SIGFPE,
            Signal::Hup => libc::SIGHUP,
            Signal::Ill => libc::SIGILL,
            Signal::Int => libc::SIGINT,
            Signal::Kill => libc::SIGKILL,
            Signal::Pipe => libc::SIGPIPE,
            Signal::Quit => libc::SIGQUIT,
            Signal::Segv => libc::SIGSEGV,
            Signal::Stop => libc::SIGSTOP,
            Signal::Term => libc::SIGTERM,
            Signal::Tstp => libc::SIGTSTP,
            Signal::Ttin => libc::SIGTTIN,
            Signal::Ttou => libc::SIGTTOU,
            Signal::Usr1 => libc::SIGUSR1,
            Signal::Usr2 => libc::SIGUSR2,
            #[cfg(not(target_os = "macos"))]
            Signal::Poll => libc::SIGPOLL,
            Signal::Prof => libc::SIGPROF,
            Signal::Sys => libc::SIGSYS,
            Signal::Trap => libc::SIGTRAP,
            Signal::Urg => libc::SIGURG,
            Signal::VtAlrm => libc::SIGVTALRM,
            Signal::XCpu => libc::SIGXCPU,
            Signal::XFsz => libc::SIGXFSZ,
        }
    }
}