1 #![doc( 2 html_root_url = "https://docs.rs/signal-hook/0.1.13/signal-hook/", 3 test(attr(deny(warnings))), 4 test(attr(allow(bare_trait_objects, unknown_lints))) 5 )] 6 #![deny(missing_docs, warnings)] 7 // Don't fail on links to things not enabled in features 8 #![allow(unknown_lints, intra_doc_link_resolution_failure)] 9 //! Library for easier and safe Unix signal handling 10 //! 11 //! Unix signals are inherently hard to handle correctly, for several reasons: 12 //! 13 //! * They are a global resource. If a library wants to set its own signal handlers, it risks 14 //! disturbing some other library. It is possible to chain the previous signal handler, but then 15 //! it is impossible to remove the old signal handlers from the chains in any practical manner. 16 //! * They can be called from whatever thread, requiring synchronization. Also, as they can 17 //! interrupt a thread at any time, making most handling race-prone. 18 //! * According to the POSIX standard, the set of functions one may call inside a signal handler is 19 //! limited to very few of them. To highlight, mutexes (or other locking mechanisms) and memory 20 //! allocation and deallocation is *not* allowed. 21 //! 22 //! This library aims to solve some of the problems. It provides a global registry of actions 23 //! performed on arrival of signals. It is possible to register multiple actions for the same 24 //! signal and it is possible to remove the actions later on. If there was a previous signal 25 //! handler when the first action for a signal is registered, it is chained (but the original one 26 //! can't be removed). 27 //! 28 //! The main function of the library is [`register`](fn.register.html). 29 //! 30 //! It also offers several common actions one might want to register, implemented in the correct 31 //! way. They are scattered through submodules and have the same limitations and characteristics as 32 //! the [`register`](fn.register.html) function. Generally, they work to postpone the action taken 33 //! outside of the signal handler, where the full freedom and power of rust is available. 34 //! 35 //! Unlike other Rust libraries for signal handling, this should be flexible enough to handle all 36 //! the common and useful patterns. 37 //! 38 //! The library avoids all the newer fancy signal-handling routines. These generally have two 39 //! downsides: 40 //! 41 //! * They are not fully portable, therefore the library would have to contain *both* the 42 //! implementation using the basic routines and the fancy ones. As signal handling is not on the 43 //! hot path of most programs, this would not bring any actual benefit. 44 //! * The other routines require that the given signal is masked in all application's threads. As 45 //! the signals are not masked by default and a new thread inherits the signal mask of its 46 //! parent, it is possible to guarantee such global mask by masking them before any threads 47 //! start. While this is possible for an application developer to do, it is not possible for a 48 //! a library. 49 //! 50 //! # Warning 51 //! 52 //! Even with this library, you should thread with care. It does not eliminate all the problems 53 //! mentioned above. 54 //! 55 //! Also, note that the OS may collate multiple instances of the same signal into just one call of 56 //! the signal handler. Furthermore, some abstractions implemented here also naturally collate 57 //! multiple instances of the same signal. The general guarantee is, if there was at least one 58 //! signal of the given number delivered, an action will be taken, but it is not specified how many 59 //! times ‒ signals work mostly as kind of „wake up now“ nudge, if the application is slow to wake 60 //! up, it may be nudged multiple times before it does so. 61 //! 62 //! # Signal limitations 63 //! 64 //! OS limits still apply ‒ it is not possible to redefine certain signals (eg. `SIGKILL` or 65 //! `SIGSTOP`) and it is probably a *very* stupid idea to touch certain other ones (`SIGSEGV`, 66 //! `SIGFPE`, `SIGILL`). Therefore, this library will panic if any attempt at manipulating these is 67 //! made. There are some use cases for redefining the latter ones, but these are not well served by 68 //! this library and you really *really* have to know what you're doing and are generally on your 69 //! own doing that. 70 //! 71 //! # Signal masks 72 //! 73 //! As the library uses `sigaction` under the hood, signal masking works as expected (eg. with 74 //! `pthread_sigmask`). This means, signals will *not* be delivered if the signal is masked in all 75 //! program's threads. 76 //! 77 //! By the way, if you do want to modify the signal mask (or do other Unix-specific magic), the 78 //! [nix](https://crates.io/crates/nix) crate offers safe interface to many low-level functions, 79 //! including 80 //! [`pthread_sigmask`](https://docs.rs/nix/0.11.0/nix/sys/signal/fn.pthread_sigmask.html). 81 //! 82 //! # Portability 83 //! 84 //! It should work on any POSIX.1-2001 system, which are all the major big OSes with the notable 85 //! exception of Windows. 86 //! 87 //! Non-standard signals are also supported. Pass the signal value directly from `libc` or use 88 //! the numeric value directly. 89 //! 90 //! ```rust 91 //! use std::sync::Arc; 92 //! use std::sync::atomic::{AtomicBool}; 93 //! let term = Arc::new(AtomicBool::new(false)); 94 //! let _ = signal_hook::flag::register(libc::SIGINT, Arc::clone(&term)); 95 //! ``` 96 //! 97 //! This crate includes a limited support for Windows, based on `signal`/`raise` in the CRT. 98 //! There are differences in both API and behavior: 99 //! 100 //! - `iterator` and `pipe` are not yet implemented. 101 //! - We have only a few signals: `SIGABRT`, `SIGABRT_COMPAT`, `SIGBREAK`, 102 //! `SIGFPE`, `SIGILL`, `SIGINT`, `SIGSEGV` and `SIGTERM`. 103 //! - Due to lack of signal blocking, there's a race condition. 104 //! After the call to `signal`, there's a moment where we miss a signal. 105 //! That means when you register a handler, there may be a signal which invokes 106 //! neither the default handler or the handler you register. 107 //! - Handlers registered by `signal` in Windows are cleared on first signal. 108 //! To match behavior in other platforms, we re-register the handler each time the handler is 109 //! called, but there's a moment where we miss a handler. 110 //! That means when you receive two signals in a row, there may be a signal which invokes 111 //! the default handler, nevertheless you certainly have registered the handler. 112 //! 113 //! Moreover, signals won't work as you expected. `SIGTERM` isn't actually used and 114 //! not all `Ctrl-C`s are turned into `SIGINT`. 115 //! 116 //! Patches to improve Windows support in this library are welcome. 117 //! 118 //! # Examples 119 //! 120 //! ```rust 121 //! extern crate signal_hook; 122 //! 123 //! use std::io::Error; 124 //! use std::sync::Arc; 125 //! use std::sync::atomic::{AtomicBool, Ordering}; 126 //! 127 //! fn main() -> Result<(), Error> { 128 //! let term = Arc::new(AtomicBool::new(false)); 129 //! signal_hook::flag::register(signal_hook::SIGTERM, Arc::clone(&term))?; 130 //! while !term.load(Ordering::Relaxed) { 131 //! // Do some time-limited stuff here 132 //! // (if this could block forever, then there's no guarantee the signal will have any 133 //! // effect). 134 //! # 135 //! # // Hack to terminate the example, not part of the real code. 136 //! # term.store(true, Ordering::Relaxed); 137 //! } 138 //! Ok(()) 139 //! } 140 //! ``` 141 //! 142 //! # Features 143 //! 144 //! * `mio-support`: The [`Signals` iterator](iterator/struct.Signals.html) becomes pluggable into 145 //! mio. 146 //! * `tokio-support`: The [`Signals`](iterator/struct.Signals.html) can be turned into 147 //! [`Async`](iterator/struct.Async.html), which provides a `Stream` interface for integration in 148 //! the asynchronous world. 149 150 #[cfg(feature = "tokio-support")] 151 extern crate futures; 152 extern crate libc; 153 #[cfg(feature = "mio-support")] 154 extern crate mio; 155 extern crate signal_hook_registry; 156 #[cfg(feature = "tokio-support")] 157 extern crate tokio_reactor; 158 159 pub mod cleanup; 160 pub mod flag; 161 #[cfg(not(windows))] 162 pub mod iterator; 163 #[cfg(not(windows))] 164 pub mod pipe; 165 166 #[cfg(not(windows))] 167 pub use libc::{ 168 SIGABRT, SIGALRM, SIGBUS, SIGCHLD, SIGCONT, SIGFPE, SIGHUP, SIGILL, SIGINT, SIGIO, SIGKILL, 169 SIGPIPE, SIGPROF, SIGQUIT, SIGSEGV, SIGSTOP, SIGSYS, SIGTERM, SIGTRAP, SIGUSR1, SIGUSR2, 170 SIGWINCH, 171 }; 172 173 #[cfg(windows)] 174 pub use libc::{SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM}; 175 176 // NOTE: they perhaps deserve backport to libc. 177 #[cfg(windows)] 178 /// Same as `SIGABRT`, but the number is compatible to other platforms. 179 pub const SIGABRT_COMPAT: libc::c_int = 6; 180 #[cfg(windows)] 181 /// Ctrl-Break is pressed for Windows Console processes. 182 pub const SIGBREAK: libc::c_int = 21; 183 184 pub use signal_hook_registry::{register, unregister, SigId, FORBIDDEN}; 185