1 | //! Some low level utilities |
2 | //! |
3 | //! More often to build other abstractions than used directly. |
4 | |
5 | use std::io::Error; |
6 | |
7 | use libc::c_int; |
8 | |
9 | #[cfg (feature = "channel" )] |
10 | #[cfg_attr (docsrs, doc(cfg(feature = "channel" )))] |
11 | pub mod channel; |
12 | #[cfg (not(windows))] |
13 | #[cfg_attr (docsrs, doc(cfg(not(windows))))] |
14 | pub mod pipe; |
15 | #[cfg (feature = "extended-siginfo-raw" )] |
16 | #[cfg_attr (docsrs, doc(cfg(feature = "extended-siginfo-raw" )))] |
17 | pub mod siginfo; |
18 | mod signal_details; |
19 | |
20 | pub use signal_hook_registry::{register, unregister}; |
21 | |
22 | pub use self::signal_details::{emulate_default_handler, signal_name}; |
23 | |
24 | /// The usual raise, just the safe wrapper around it. |
25 | /// |
26 | /// This is async-signal-safe. |
27 | pub fn raise(sig: c_int) -> Result<(), Error> { |
28 | let result: i32 = unsafe { libc::raise(signum:sig) }; |
29 | if result == -1 { |
30 | Err(Error::last_os_error()) |
31 | } else { |
32 | Ok(()) |
33 | } |
34 | } |
35 | |
36 | /// A bare libc abort. |
37 | /// |
38 | /// Unlike the [std::process::abort], this one is guaranteed to contain no additions or wrappers |
39 | /// and therefore is async-signal-safe. You can use this to terminate the application from within a |
40 | /// signal handler. |
41 | pub fn abort() -> ! { |
42 | unsafe { |
43 | libc::abort(); |
44 | } |
45 | } |
46 | |
47 | /// A bare libc exit. |
48 | /// |
49 | /// Unlike the [std::process::exit], this one is guaranteed to contain no additions or wrappers and |
50 | /// therefore is async-signal-safe. You can use this to terminate the application from within a |
51 | /// signal handler. |
52 | /// |
53 | /// Also, see [`register_conditional_shutdown`][crate::flag::register_conditional_shutdown]. |
54 | pub fn exit(status: c_int) -> ! { |
55 | unsafe { |
56 | // Yes, the one with underscore. That one doesn't call the at-exit hooks. |
57 | libc::_exit(status); |
58 | } |
59 | } |
60 | |