Function set_handler

Source
pub fn set_handler(
    signal_type: SignalType,
    handler: SignalHandler,
) -> Result<(), SetHandlerError>
Available on Unix and crate feature signals only.
Expand description

Installs the specified handler for the specified standard signal, using the default values for the flags.

See HandlerOptions builder if you’d like to customize the flags.

§Example

use interprocess::os::unix::signal::{self, SignalType, SignalHandler};

let handler = unsafe {
    // Since signal handlers are restricted to a specific set of C functions, creating a
    // handler from an arbitrary function is unsafe because it might call a function
    // outside the list, and there's no real way to know that at compile time with the
    // current version of Rust. Since we're only using the write() system call here, this
    // is safe.
    SignalHandler::from_fn(|| {
        println!("You pressed Ctrl-C!");
    })
};

// Install our handler for the KeyboardInterrupt signal type.
signal::set_handler(SignalType::KeyboardInterrupt, handler)?;