Function nix::sys::signal::signal[][src]

pub unsafe fn signal(signal: Signal, handler: SigHandler) -> Result<SigHandler>
Expand description

Signal management (see signal(3p))

Installs handler for the given signal, returning the previous signal handler. signal should only be used following another call to signal or if the current handler is the default. The return value of signal is undefined after setting the handler with sigaction.

Safety

If the pointer to the previous signal handler is invalid, undefined behavior could be invoked when casting it back to a SigAction.

Examples

Ignore SIGINT:

unsafe { signal::signal(Signal::SIGINT, SigHandler::SigIgn) }.unwrap();

Use a signal handler to set a flag variable:

lazy_static! {
   static ref SIGNALED: AtomicBool = AtomicBool::new(false);
}

extern fn handle_sigint(signal: libc::c_int) {
    let signal = Signal::try_from(signal).unwrap();
    SIGNALED.store(signal == Signal::SIGINT, Ordering::Relaxed);
}

fn main() {
    let handler = SigHandler::Handler(handle_sigint);
    unsafe { signal::signal(Signal::SIGINT, handler) }.unwrap();
}

Errors

Returns Error::UnsupportedOperation if handler is SigAction. Use sigaction instead.

signal also returns any error from libc::signal, such as when an attempt is made to catch a signal that cannot be caught or to ignore a signal that cannot be ignored.