Backend crate for signal-hook
  • Rust 98.7%
  • Shell 0.7%
  • C 0.6%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Michal 'vorner' Vaner f37a7b5918
Some checks failed
Security audit / security_audit (push) Has been cancelled
test / Build & test-5 (push) Has been cancelled
test / Build & test-6 (push) Has been cancelled
test / Build & test-7 (push) Has been cancelled
test / Build & test-8 (push) Has been cancelled
test / Build & test-9 (push) Has been cancelled
test / Build & test-10 (push) Has been cancelled
test / Build & test-11 (push) Has been cancelled
test / Check formatting (push) Has been cancelled
Coverage / Coverage (push) Has been cancelled
test / Build & test (push) Has been cancelled
test / Build & test-1 (push) Has been cancelled
test / Build & test-2 (push) Has been cancelled
test / Build & test-3 (push) Has been cancelled
test / Build & test-4 (push) Has been cancelled
test / Check documentation links (push) Has been cancelled
test / Check compilation of signal-hook-registry on old compilers (push) Has been cancelled
test / Check compilation of signal-hook-registry on old compilers-1 (push) Has been cancelled
test / Check compilation of signal-hook-registry on old compilers-2 (push) Has been cancelled
test / Check compilation of signal-hook-registry on old compilers-3 (push) Has been cancelled
test / Clippy lints (push) Has been cancelled
test / Check weird platforms (push) Has been cancelled
test / Check weird platforms-1 (push) Has been cancelled
test / Check weird platforms-2 (push) Has been cancelled
test / Check weird platforms-3 (push) Has been cancelled
test / Check weird platforms-4 (push) Has been cancelled
test / Check weird platforms-5 (push) Has been cancelled
test / Check weird platforms-6 (push) Has been cancelled
test / Check weird platforms-7 (push) Has been cancelled
test / Check weird platforms-8 (push) Has been cancelled
test / Check weird platforms-9 (push) Has been cancelled
Document SIGBUS
2026-04-04 10:08:49 +02:00
.github Fix some links & link tests 2025-12-26 12:33:26 +01:00
examples defuse the print example on windows 2025-11-29 17:14:32 +01:00
serial_test replace serial_test with a plain lock 2025-11-23 17:25:21 +01:00
signal-hook-async-std Exclude development script from published package 2026-01-24 14:15:14 +01:00
signal-hook-mio Exclude development script from published package 2026-01-24 14:15:14 +01:00
signal-hook-registry Exclude development script from published package 2026-01-24 14:15:14 +01:00
signal-hook-tokio Exclude development script from published package 2026-01-24 14:15:14 +01:00
src Document SIGBUS 2026-04-04 10:08:49 +02:00
tests I/O safety 2025-12-26 12:33:00 +01:00
.gitignore Add the signal-hook-sys crate 2020-12-15 11:49:40 +01:00
build.rs siginfo module doesn't work on windows 2025-11-29 16:50:32 +01:00
Cargo.lock Document SIGBUS 2026-04-04 10:08:49 +02:00
Cargo.lock.old Adjust tests for higher minimal rust version 2025-12-26 12:33:26 +01:00
Cargo.toml Document SIGBUS 2026-04-04 10:08:49 +02:00
CHANGELOG.md Document SIGBUS 2026-04-04 10:08:49 +02:00
ci-check.sh Adjust tests for higher minimal rust version 2025-12-26 12:33:26 +01:00
LICENSE-APACHE Formalities 2018-06-22 19:32:56 +02:00
LICENSE-MIT Formalities 2018-06-22 19:32:56 +02:00
README.md Update Readme 2025-08-25 20:44:42 -07: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.