• Stars
    star
    580
  • Rank 74,527 (Top 2 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created almost 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Rust library allowing to register multiple handlers for the same signal

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.

More Repositories

1

arc-swap

Support atomic operations on Arc itself
Rust
504
star
2

spirit

Rust
150
star
3

corona

Coroutine and async/await support for tokio-based futures
Rust
120
star
4

tuntap

Rust's wrapper for creation of TUN/TAP devices
Rust
111
star
5

vorner.github.io

81
star
6

slipstream

Nudging the compiler to auto-vectorize things
Rust
55
star
7

contrie

Concurrent hash trie
Rust
50
star
8

pyo3-log

Logging bridge from pyo3 native extension to python
Rust
45
star
9

fastmatmult

Some experiments… and maybe a surprise coming.
Rust
28
star
10

bumpalo-herd

Trying to create Sync bump allocator
Rust
21
star
11

tokio-jsonrpc

Some experiments with JSON RPC in rust and tokio
Rust
15
star
12

bytes-utils

Additional utilities around the bytes crate
Rust
11
star
13

playlist_mgr

Playlist manager
Rust
11
star
14

tspam

Measuring a network link by creating new connections
Rust
10
star
15

tokio-serde-cbor

Tokio codec for serde-cbor
Rust
9
star
16

rust-presentations

Slides for various (public) presentations, mostly about Rust language, but others too
SCSS
9
star
17

reopen

Rust
7
star
18

pgz

Parallel gzip
Rust
6
star
19

squash

Saving space on slices and small strings
Rust
5
star
20

tracing-dipstick

(Experimental) bridge from tracing to dipstick, extracting metrics
Rust
4
star
21

structdoc

Rust
4
star
22

slog-retry

Slog drain that reconnects on errors
Rust
3
star
23

minimal-fixtures

Minimal test fixtures as proc-macro decorators (experimental)
Rust
3
star
24

adaptive-barrier

Similar to std::sync::Barrier, but automatically adjusts to the number of subscribers
Rust
3
star
25

bigbuffer

A buffer to balance commands in pipelines
Rust
2
star
26

numstream

Generates a stream of binary unsigned 64 numbers
Rust
2
star
27

thrust

Little peaceful spacecraft parking game
Rust
2
star
28

eveboros

EveBoros ‒ experiment with event loops in Rust (not production quality)
Rust
2
star
29

compile-commands-post

Manager of clang compilation database
Rust
1
star
30

jsonrpc-param-ord

Rust
1
star
31

rats

A toy game project
Rust
1
star
32

sls

A testing language server that does mostly nothing but sleeps
Rust
1
star
33

duprm

BTRFS deduplicator tailored for my own needs… some README will appear soon
Rust
1
star
34

just-get-it-done

A board game with corporate theme
1
star