[][src]Function signal_hook::cleanup::register

pub fn register(signal: c_int, cleanup: Vec<c_int>) -> Result<SigId, Error>

Register a cleanup after receiving a signal.

Registers an action that, after receiving signal, will reset all signals specified in cleanup to their OS defaults. The reset is done as part of the signal handler.

The intended use case is that at CTRL+C (or something similar), the application starts shutting down. This might take some time so by resetting all terminal signals to the defaults at that time makes sure a second CTRL+C results in immediate (hard) termination of the application.

The hooks are still left inside and any following hooks after the reset are still run. Only the next signal will be affected (and the hooks will be inert).

Warning

The reset as part of the action is global and irreversible. All signal hooks and all signals registered outside of signal-hook are affected and won't be run any more. Registering more hooks for the same signals as cleaned will have no effect.

The latter part of having no effect may be changed in the future, do not rely on it. Preferably, don't manipulate the signal any longer.

Examples

use signal_hook::{cleanup, flag, SIGINT, SIGTERM};

fn main() -> Result<(), Error> {
    let terminated = Arc::new(AtomicBool::new(false));
    flag::register(SIGTERM, Arc::clone(&terminated))?;
    cleanup::register(SIGTERM, vec![SIGTERM, SIGINT])?;

    while !terminated.load(Ordering::Relaxed) {
        keep_processing();
    }

    app_cleanup();
    Ok(())
}