-
Notifications
You must be signed in to change notification settings - Fork 533
/
Copy pathsignal.rs
155 lines (142 loc) · 3.4 KB
/
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
144
145
146
147
148
149
150
151
152
153
154
155
use super::*;
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(i32)]
pub(crate) enum Signal {
Hangup = 1,
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
Info = 29,
Interrupt = 2,
Quit = 3,
Terminate = 15,
}
impl Signal {
#[cfg(not(windows))]
pub(crate) const ALL: &'static [Signal] = &[
Signal::Hangup,
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
Signal::Info,
Signal::Interrupt,
Signal::Quit,
Signal::Terminate,
];
pub(crate) fn code(self) -> Option<i32> {
128i32.checked_add(self.number())
}
pub(crate) fn number(self) -> i32 {
self as libc::c_int
}
}
impl Display for Signal {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(
f,
"{}",
match self {
Signal::Hangup => "SIGHUP",
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
Signal::Info => "SIGINFO",
Signal::Interrupt => "SIGINT",
Signal::Quit => "SIGQUIT",
Signal::Terminate => "SIGTERM",
}
)
}
}
#[cfg(not(windows))]
impl From<Signal> for nix::sys::signal::Signal {
fn from(signal: Signal) -> Self {
match signal {
Signal::Hangup => Self::SIGHUP,
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
Signal::Info => Self::SIGINFO,
Signal::Interrupt => Self::SIGINT,
Signal::Quit => Self::SIGQUIT,
Signal::Terminate => Self::SIGTERM,
}
}
}
impl TryFrom<u8> for Signal {
type Error = io::Error;
fn try_from(n: u8) -> Result<Signal, Self::Error> {
match n {
1 => Ok(Signal::Hangup),
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
29 => Ok(Signal::Info),
2 => Ok(Signal::Interrupt),
3 => Ok(Signal::Quit),
15 => Ok(Signal::Terminate),
_ => Err(io::Error::new(
io::ErrorKind::Other,
format!("unexpected signal: {n}"),
)),
}
}
}
#[cfg(test)]
#[cfg(not(windows))]
mod tests {
use super::*;
#[test]
fn signals_fit_in_u8() {
for signal in Signal::ALL {
assert!(signal.number() <= i32::from(u8::MAX));
}
}
#[test]
fn signal_numbers_are_correct() {
for &signal in Signal::ALL {
let n = match signal {
Signal::Hangup => libc::SIGHUP,
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
Signal::Info => libc::SIGINFO,
Signal::Interrupt => libc::SIGINT,
Signal::Quit => libc::SIGQUIT,
Signal::Terminate => libc::SIGTERM,
};
assert_eq!(signal as i32, n);
assert_eq!(Signal::try_from(u8::try_from(n).unwrap()).unwrap(), signal);
}
}
}