1#![allow(unsafe_code)]
13
14use crate::backend::c;
15use core::fmt;
16use core::num::NonZeroI32;
17
18#[doc(alias = "SIGRTMIN")]
45#[doc(alias = "SIGRTMAX")]
46#[derive(Copy, Clone, Eq, PartialEq)]
47#[repr(transparent)]
48pub struct Signal(NonZeroI32);
49
50#[rustfmt::skip]
52impl Signal {
53 pub const HUP: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGHUP) });
55 pub const INT: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGINT) });
57 pub const QUIT: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGQUIT) });
59 pub const ILL: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGILL) });
61 pub const TRAP: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGTRAP) });
63 #[doc(alias = "IOT")]
65 #[doc(alias = "ABRT")]
66 pub const ABORT: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGABRT) });
67 pub const BUS: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGBUS) });
69 pub const FPE: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGFPE) });
71 pub const KILL: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGKILL) });
73 #[cfg(not(target_os = "vita"))]
75 pub const USR1: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGUSR1) });
76 pub const SEGV: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGSEGV) });
78 #[cfg(not(target_os = "vita"))]
80 pub const USR2: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGUSR2) });
81 pub const PIPE: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGPIPE) });
83 #[doc(alias = "ALRM")]
85 pub const ALARM: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGALRM) });
86 pub const TERM: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGTERM) });
88 #[cfg(not(any(
90 bsd,
91 solarish,
92 target_os = "aix",
93 target_os = "haiku",
94 target_os = "horizon",
95 target_os = "hurd",
96 target_os = "nto",
97 target_os = "vita",
98 all(
99 linux_kernel,
100 any(
101 target_arch = "mips",
102 target_arch = "mips32r6",
103 target_arch = "mips64",
104 target_arch = "mips64r6",
105 target_arch = "sparc",
106 target_arch = "sparc64"
107 ),
108 )
109 )))]
110 pub const STKFLT: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGSTKFLT) });
111 #[cfg(not(target_os = "vita"))]
113 #[doc(alias = "CHLD")]
114 pub const CHILD: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGCHLD) });
115 #[cfg(not(target_os = "vita"))]
117 pub const CONT: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGCONT) });
118 #[cfg(not(target_os = "vita"))]
120 pub const STOP: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGSTOP) });
121 #[cfg(not(target_os = "vita"))]
123 pub const TSTP: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGTSTP) });
124 #[cfg(not(target_os = "vita"))]
126 pub const TTIN: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGTTIN) });
127 #[cfg(not(target_os = "vita"))]
129 pub const TTOU: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGTTOU) });
130 #[cfg(not(target_os = "vita"))]
132 pub const URG: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGURG) });
133 #[cfg(not(target_os = "vita"))]
135 pub const XCPU: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGXCPU) });
136 #[cfg(not(target_os = "vita"))]
138 pub const XFSZ: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGXFSZ) });
139 #[cfg(not(target_os = "vita"))]
141 #[doc(alias = "VTALRM")]
142 pub const VTALARM: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGVTALRM) });
143 #[cfg(not(target_os = "vita"))]
145 pub const PROF: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGPROF) });
146 #[cfg(not(target_os = "vita"))]
148 pub const WINCH: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGWINCH) });
149 #[doc(alias = "POLL")]
151 #[cfg(not(any(target_os = "haiku", target_os = "vita")))]
152 pub const IO: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGIO) });
153 #[cfg(not(any(
155 bsd,
156 target_os = "haiku",
157 target_os = "horizon",
158 target_os = "hurd",
159 target_os = "vita"
160 )))]
161 #[doc(alias = "PWR")]
162 pub const POWER: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGPWR) });
163 #[doc(alias = "UNUSED")]
165 pub const SYS: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGSYS) });
166 #[cfg(any(
168 bsd,
169 solarish,
170 target_os = "aix",
171 target_os = "hermit",
172 all(
173 linux_kernel,
174 any(
175 target_arch = "mips",
176 target_arch = "mips32r6",
177 target_arch = "mips64",
178 target_arch = "mips64r6",
179 target_arch = "sparc",
180 target_arch = "sparc64"
181 )
182 )
183 ))]
184 pub const EMT: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGEMT) });
185 #[cfg(bsd)]
187 pub const INFO: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGINFO) });
188 #[cfg(target_os = "freebsd")]
190 #[doc(alias = "LWP")]
191 pub const THR: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGTHR) });
192 #[cfg(target_os = "freebsd")]
194 pub const LIBRT: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGLIBRT) });
195}
196
197impl Signal {
198 #[inline]
202 pub const fn as_raw(self) -> i32 {
203 self.0.get()
204 }
205
206 #[inline]
208 pub const fn as_raw_nonzero(self) -> NonZeroI32 {
209 self.0
210 }
211
212 #[inline]
231 pub const unsafe fn from_raw_unchecked(sig: i32) -> Self {
232 Self::from_raw_nonzero_unchecked(NonZeroI32::new_unchecked(sig))
233 }
234
235 #[inline]
254 pub const unsafe fn from_raw_nonzero_unchecked(sig: NonZeroI32) -> Self {
255 Self(sig)
256 }
257}
258
259impl fmt::Debug for Signal {
260 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
261 match *self {
262 Self::HUP => "Signal::HUP".fmt(f),
263 Self::INT => "Signal::INT".fmt(f),
264 Self::QUIT => "Signal::QUIT".fmt(f),
265 Self::ILL => "Signal::ILL".fmt(f),
266 Self::TRAP => "Signal::TRAP".fmt(f),
267 Self::ABORT => "Signal::ABORT".fmt(f),
268 Self::BUS => "Signal::BUS".fmt(f),
269 Self::FPE => "Signal::FPE".fmt(f),
270 Self::KILL => "Signal::KILL".fmt(f),
271 #[cfg(not(target_os = "vita"))]
272 Self::USR1 => "Signal::USR1".fmt(f),
273 Self::SEGV => "Signal::SEGV".fmt(f),
274 #[cfg(not(target_os = "vita"))]
275 Self::USR2 => "Signal::USR2".fmt(f),
276 Self::PIPE => "Signal::PIPE".fmt(f),
277 Self::ALARM => "Signal::ALARM".fmt(f),
278 Self::TERM => "Signal::TERM".fmt(f),
279 #[cfg(not(any(
280 bsd,
281 solarish,
282 target_os = "aix",
283 target_os = "haiku",
284 target_os = "horizon",
285 target_os = "hurd",
286 target_os = "nto",
287 target_os = "vita",
288 all(
289 linux_kernel,
290 any(
291 target_arch = "mips",
292 target_arch = "mips32r6",
293 target_arch = "mips64",
294 target_arch = "mips64r6",
295 target_arch = "sparc",
296 target_arch = "sparc64"
297 ),
298 )
299 )))]
300 Self::STKFLT => "Signal::STKFLT".fmt(f),
301 #[cfg(not(target_os = "vita"))]
302 Self::CHILD => "Signal::CHILD".fmt(f),
303 #[cfg(not(target_os = "vita"))]
304 Self::CONT => "Signal::CONT".fmt(f),
305 #[cfg(not(target_os = "vita"))]
306 Self::STOP => "Signal::STOP".fmt(f),
307 #[cfg(not(target_os = "vita"))]
308 Self::TSTP => "Signal::TSTP".fmt(f),
309 #[cfg(not(target_os = "vita"))]
310 Self::TTIN => "Signal::TTIN".fmt(f),
311 #[cfg(not(target_os = "vita"))]
312 Self::TTOU => "Signal::TTOU".fmt(f),
313 #[cfg(not(target_os = "vita"))]
314 Self::URG => "Signal::URG".fmt(f),
315 #[cfg(not(target_os = "vita"))]
316 Self::XCPU => "Signal::XCPU".fmt(f),
317 #[cfg(not(target_os = "vita"))]
318 Self::XFSZ => "Signal::XFSZ".fmt(f),
319 #[cfg(not(target_os = "vita"))]
320 Self::VTALARM => "Signal::VTALARM".fmt(f),
321 #[cfg(not(target_os = "vita"))]
322 Self::PROF => "Signal::PROF".fmt(f),
323 #[cfg(not(target_os = "vita"))]
324 Self::WINCH => "Signal::WINCH".fmt(f),
325 #[cfg(not(any(target_os = "haiku", target_os = "vita")))]
326 Self::IO => "Signal::IO".fmt(f),
327 #[cfg(not(any(
328 bsd,
329 target_os = "haiku",
330 target_os = "horizon",
331 target_os = "hurd",
332 target_os = "vita"
333 )))]
334 Self::POWER => "Signal::POWER".fmt(f),
335 Self::SYS => "Signal::SYS".fmt(f),
336 #[cfg(any(
337 bsd,
338 solarish,
339 target_os = "aix",
340 target_os = "hermit",
341 all(
342 linux_kernel,
343 any(
344 target_arch = "mips",
345 target_arch = "mips32r6",
346 target_arch = "mips64",
347 target_arch = "mips64r6",
348 target_arch = "sparc",
349 target_arch = "sparc64"
350 )
351 )
352 ))]
353 Self::EMT => "Signal::EMT".fmt(f),
354 #[cfg(bsd)]
355 Self::INFO => "Signal::INFO".fmt(f),
356 #[cfg(target_os = "freebsd")]
357 Self::THR => "Signal::THR".fmt(f),
358 #[cfg(target_os = "freebsd")]
359 Self::LIBRT => "Signal::LIBRT".fmt(f),
360
361 n => {
362 "Signal::from_raw(".fmt(f)?;
363 n.as_raw().fmt(f)?;
364 ")".fmt(f)
365 }
366 }
367 }
368}
369
370#[cfg(test)]
371mod tests {
372 use super::*;
373
374 #[test]
375 fn test_basics() {
376 assert_eq!(Signal::HUP.as_raw(), libc::SIGHUP);
377 unsafe {
378 assert_eq!(Signal::from_raw_unchecked(libc::SIGHUP), Signal::HUP);
379 assert_eq!(
380 Signal::from_raw_nonzero_unchecked(NonZeroI32::new(libc::SIGHUP).unwrap()),
381 Signal::HUP
382 );
383 }
384 }
385}