[][src]Module signal_hook::iterator

An iterator over incoming signals.

This provides a higher abstraction over the signals, providing a structure (Signals) able to iterate over the incoming signals.

In case the tokio-support feature is turned on, the Async is also available, making it possible to integrate with the tokio runtime.

Examples

extern crate libc;
extern crate signal_hook;

use std::io::Error;

use signal_hook::iterator::Signals;

fn main() -> Result<(), Error> {
    let signals = Signals::new(&[
        signal_hook::SIGHUP,
        signal_hook::SIGTERM,
        signal_hook::SIGINT,
        signal_hook::SIGQUIT,
    ])?;
    'outer: loop {
        // Pick up signals that arrived since last time
        for signal in signals.pending() {
            match signal as libc::c_int {
                signal_hook::SIGHUP => {
                    // Reload configuration
                    // Reopen the log file
                }
                signal_hook::SIGTERM | signal_hook::SIGINT | signal_hook::SIGQUIT => {
                    break 'outer;
                },
                _ => unreachable!(),
            }
        }
        // Do some bit of work ‒ something with upper limit on waiting, so we don't block
        // forever with a SIGTERM already waiting.
    }
    println!("Terminating. Bye bye");
    Ok(())
}

Structs

Async

An asynchronous stream of registered signals.

Forever

The infinite iterator of signals.

Pending

The iterator of one batch of signals.

Signals

The main structure of the module, representing interest in some signals.