devela::all

Function linux_sig_handler_no_return

Source
pub fn linux_sig_handler_no_return(handler: fn(_: i32) -> !, signals: &[i32])
Available on crate features unsafe_syscall and linux only.
Expand description

Registers multiple signals using a handler function that never returns.

§Examples

use std::{process::exit, time::Duration, thread::sleep};
use devela::{linux_sig_handler_no_return, LINUX_SIGNAL as LS};

fn handler(sig: i32) -> ! {
   println!("\nsignal `{sig}` received! exiting. . .");
   exit(1);
}

fn main() {
    // handle all the signals used to quit
    linux_sig_handler_no_return(handler, &[LS::SIGINT, LS::SIGQUIT, LS::SIGSEGV, LS::SIGABRT]);
    // press Ctrl+C before the time expires to catch the SIGINT signal
    sleep(Duration::from_secs(2));
    println!("bye");
}

§Rationale

It would be very nice to be able to register a signal handler that can return, unfortunately I’ve been unable to make it work.

Apparently the handler needs the SA_RESTORER flag to run, but doing so without providing a restorer function produces a segmentation fault. The only way to simply avoid that is to not return from the handler function.

The libc library sets it up correctly but doing so manually seems a too complex too low level task.