Crates.io | signal-hook |
lib.rs | signal-hook |
version | 0.3.17 |
source | src |
created_at | 2018-06-22 17:38:56.065914 |
updated_at | 2023-07-18 15:21:46.884143 |
description | Unix signal handling |
homepage | |
repository | https://github.com/vorner/signal-hook |
max_upload_size | |
id | 71297 |
size | 162,713 |
Library for safe and correct Unix signal handling in Rust.
Unix signals are inherently hard to handle correctly, for several reasons:
This library aims to solve some of the problems. It provides a global registry of actions performed on arrival of signals. It is possible to register multiple actions for the same signal and it is possible to remove the actions later on. If there was a previous signal handler when the first action for a signal is registered, it is chained (but the original one can't be removed).
Besides the basic registration of an arbitrary action, several helper actions are provided to cover the needs of the most common use cases, available from safe Rust.
For further details, see the documentation.
(This likely does a lot more than you need in each individual application, it's more of a show-case of what everything is possible, not of what you need to do each time).
use std::io::Error;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
fn main() -> Result<(), Error> {
let term = Arc::new(AtomicBool::new(false));
signal_hook::flag::register(signal_hook::consts::SIGTERM, Arc::clone(&term))?;
while !term.load(Ordering::Relaxed) {
// Do some time-limited stuff here
// (if this could block forever, then there's no guarantee the signal will have any
// effect).
}
Ok(())
}
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.