Go to file
openharmony_ci f4aeb56789
!2 OAT告警屏蔽配置更新
Merge pull request !2 from peizhe/master
2023-05-16 09:33:36 +00:00
.github Update codecov 2021-09-12 09:27:44 +02:00
examples Emulating default actions 2021-01-10 10:39:28 +01:00
signal-hook-async-std Release 2022-01-08 12:30:13 +01:00
signal-hook-mio Release mio 0.2.3 2022-03-27 10:05:21 +02:00
signal-hook-registry 告警与遗留问题文件补充 2023-05-09 09:34:14 +08:00
signal-hook-tokio Release 2022-01-08 12:30:13 +01:00
src Version 0.3.15 / registry 1.4.1 2023-02-11 15:18:16 +01:00
tests Fix versions 2021-05-28 21:07:17 +02:00
.gitignore Add the signal-hook-sys crate 2020-12-15 11:49:40 +01:00
BUILD.gn 告警与遗留问题文件补充 2023-05-09 09:34:14 +08:00
build.rs Fold the signal-hook-sys inside the main signal-hook 2021-01-02 18:43:25 +01:00
Cargo.lock Version 0.3.15 / registry 1.4.1 2023-02-11 15:18:16 +01:00
Cargo.toml Version 0.3.15 / registry 1.4.1 2023-02-11 15:18:16 +01:00
CHANGELOG.md Version 0.3.15 / registry 1.4.1 2023-02-11 15:18:16 +01:00
ci-check.sh Exclude tokio from old compilers 2021-01-24 13:38:10 +01:00
LICENSE-APACHE Formalities 2018-06-22 19:32:56 +02:00
LICENSE-MIT Formalities 2018-06-22 19:32:56 +02:00
OAT.xml oat告警屏蔽配置 2023-05-16 16:00:03 +08:00
README.md Use simpler example in readme 2021-05-16 17:20:59 +02:00
README.OpenSource 告警与遗留问题文件补充 2023-05-09 09:34:14 +08:00
rustfmt.toml Initial implementation 2018-06-13 21:12:21 +02:00

Signal-hook

Actions Status codecov docs

Library for safe and correct Unix signal handling in Rust.

Unix signals are inherently hard to handle correctly, for several reasons:

  • They are a global resource. If a library wants to set its own signal handlers, it risks disturbing some other library. It is possible to chain the previous signal handler, but then it is impossible to remove the old signal handlers from the chains in any practical manner.
  • They can be called from whatever thread, requiring synchronization. Also, as they can interrupt a thread at any time, making most handling race-prone.
  • According to the POSIX standard, the set of functions one may call inside a signal handler is limited to very few of them. To highlight, mutexes (or other locking mechanisms) and memory allocation and deallocation are not allowed.

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.

Example

(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(())
}

License

Licensed under either of

at your option.

Contribution

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.