[][src]Module signal_hook::iterator

An iterator over incoming signals.

This provides a higher abstraction over the signals, providing the SignalsInfo structure which is able to iterate over the incoming signals. The structure is parametrized by an Exfiltrator, which specifies what information is returned for each delivered signal. Note that some exfiltrators are behind a feature flag.

The Signals is a type alias for the common case when it is enough to get the signal number.

This module (and everything in it) is turned by the iterator feature. It is on by default, the possibility to turn off is mostly possible for very special purposes (compiling on <rustc-1.36, minimizing the amount of code compiled, …). In a sense, this is the highest level abstraction of the crate and the API expected to be used by most of the people.

Examples

extern crate libc;
extern crate signal_hook;

use std::io::Error;

use signal_hook::consts::signal::*;
use signal_hook::iterator::Signals;

fn main() -> Result<(), Error> {
    let mut signals = Signals::new(&[
        SIGHUP,
        SIGTERM,
        SIGINT,
        SIGQUIT,
    ])?;
    'outer: loop {
        // Pick up signals that arrived since last time
        for signal in signals.pending() {
            match signal as libc::c_int {
                SIGHUP => {
                    // Reload configuration
                    // Reopen the log file
                }
                SIGTERM | SIGINT | 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(())
}

Re-exports

pub use self::backend::Handle;
pub use self::backend::Pending;

Modules

backend

A backend module for implementing the iterator like iterator module and the asynchronous adapter crates.

exfiltrator

An abstraction over exfiltrating information out of signal handlers.

Structs

Forever

An infinit iterator of arriving signals.

SignalsInfo

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

Type Definitions

Signals

A type alias for an iterator returning just the signal numbers.