heim_process/os/unix/
signal.rs

1use std::convert::TryFrom;
2use std::io;
3
4/// POSIX signals.
5///
6/// Signals list is based on the [POSIX.1-2017] specification.
7///
8/// [POSIX.1-2017]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html
9#[derive(Debug, Copy, Clone, PartialEq, Eq)]
10pub enum Signal {
11    /// Process abort signal.
12    Abrt,
13    /// Alarm clock.
14    Alrm,
15    /// Access to an undefined portion of a memory object.
16    Bus,
17    /// Child process terminated, stopped, or continued.
18    Chld,
19    /// Continue executing, if stopped.
20    Cont,
21    /// Erroneous arithmetic operation.
22    Fpe,
23    /// Hangup.
24    Hup,
25    /// Illegal instruction.
26    Ill,
27    /// Terminal interrupt signal.
28    Int,
29    /// Kill (cannot be caught or ignored).
30    Kill,
31    /// Write on a pipe with no one to read it.
32    Pipe,
33    /// Terminal quit signal.
34    Quit,
35    /// Invalid memory reference.
36    Segv,
37    /// Stop executing (cannot be caught or ignored).
38    Stop,
39    /// Termination signal.
40    Term,
41    /// Terminal stop signal.
42    Tstp,
43    /// Background process attempting read.
44    Ttin,
45    /// Background process attempting write.
46    Ttou,
47    /// User-defined signal 1.
48    Usr1,
49    /// User-defined signal 2.
50    Usr2,
51    /// Pollable event.
52    #[cfg(not(target_os = "macos"))]
53    Poll,
54    /// Profiling timer expired.
55    Prof,
56    /// Bad system call.
57    Sys,
58    /// Trace/breakpoint trap.
59    Trap,
60    /// High bandwidth data is available at a socket.
61    Urg,
62    /// Virtual timer expired.
63    VtAlrm,
64    /// CPU time limit exceeded.
65    XCpu,
66    /// File size limit exceeded.
67    XFsz,
68}
69
70impl TryFrom<libc::c_int> for Signal {
71    type Error = io::Error;
72
73    fn try_from(value: libc::c_int) -> io::Result<Self> {
74        match value {
75            libc::SIGABRT => Ok(Signal::Abrt),
76            libc::SIGALRM => Ok(Signal::Alrm),
77            libc::SIGBUS => Ok(Signal::Bus),
78            libc::SIGCHLD => Ok(Signal::Chld),
79            libc::SIGCONT => Ok(Signal::Cont),
80            libc::SIGFPE => Ok(Signal::Fpe),
81            libc::SIGHUP => Ok(Signal::Hup),
82            libc::SIGILL => Ok(Signal::Ill),
83            libc::SIGINT => Ok(Signal::Int),
84            libc::SIGKILL => Ok(Signal::Kill),
85            libc::SIGPIPE => Ok(Signal::Pipe),
86            libc::SIGQUIT => Ok(Signal::Quit),
87            libc::SIGSEGV => Ok(Signal::Segv),
88            libc::SIGSTOP => Ok(Signal::Stop),
89            libc::SIGTERM => Ok(Signal::Term),
90            libc::SIGTSTP => Ok(Signal::Tstp),
91            libc::SIGTTIN => Ok(Signal::Ttin),
92            libc::SIGTTOU => Ok(Signal::Ttou),
93            libc::SIGUSR1 => Ok(Signal::Usr1),
94            libc::SIGUSR2 => Ok(Signal::Usr2),
95            #[cfg(not(target_os = "macos"))]
96            libc::SIGPOLL => Ok(Signal::Poll),
97            libc::SIGPROF => Ok(Signal::Prof),
98            libc::SIGSYS => Ok(Signal::Sys),
99            libc::SIGTRAP => Ok(Signal::Trap),
100            libc::SIGURG => Ok(Signal::Urg),
101            libc::SIGVTALRM => Ok(Signal::VtAlrm),
102            libc::SIGXCPU => Ok(Signal::XCpu),
103            libc::SIGXFSZ => Ok(Signal::XFsz),
104            _ => Err(io::Error::from(io::ErrorKind::InvalidInput)),
105        }
106    }
107}
108
109impl From<Signal> for libc::c_int {
110    fn from(signal: Signal) -> Self {
111        match signal {
112            Signal::Abrt => libc::SIGABRT,
113            Signal::Alrm => libc::SIGALRM,
114            Signal::Bus => libc::SIGBUS,
115            Signal::Chld => libc::SIGCHLD,
116            Signal::Cont => libc::SIGCONT,
117            Signal::Fpe => libc::SIGFPE,
118            Signal::Hup => libc::SIGHUP,
119            Signal::Ill => libc::SIGILL,
120            Signal::Int => libc::SIGINT,
121            Signal::Kill => libc::SIGKILL,
122            Signal::Pipe => libc::SIGPIPE,
123            Signal::Quit => libc::SIGQUIT,
124            Signal::Segv => libc::SIGSEGV,
125            Signal::Stop => libc::SIGSTOP,
126            Signal::Term => libc::SIGTERM,
127            Signal::Tstp => libc::SIGTSTP,
128            Signal::Ttin => libc::SIGTTIN,
129            Signal::Ttou => libc::SIGTTOU,
130            Signal::Usr1 => libc::SIGUSR1,
131            Signal::Usr2 => libc::SIGUSR2,
132            #[cfg(not(target_os = "macos"))]
133            Signal::Poll => libc::SIGPOLL,
134            Signal::Prof => libc::SIGPROF,
135            Signal::Sys => libc::SIGSYS,
136            Signal::Trap => libc::SIGTRAP,
137            Signal::Urg => libc::SIGURG,
138            Signal::VtAlrm => libc::SIGVTALRM,
139            Signal::XCpu => libc::SIGXCPU,
140            Signal::XFsz => libc::SIGXFSZ,
141        }
142    }
143}