2018-12-31 14:07:55 +02:00
|
|
|
//! SIGINT handling in Unix systems.
|
2020-08-26 22:30:19 +03:00
|
|
|
use crate::ctrlc::interrupted::set_interrupted;
|
2023-11-24 07:50:41 +08:00
|
|
|
use nix::sys::signal::{sigaction, SaFlags, SigAction, SigHandler, SigSet, Signal};
|
2018-10-17 14:07:58 +03:00
|
|
|
|
2018-12-31 14:07:55 +02:00
|
|
|
/// Handle SIGINT. Set the interruption flag.
|
2018-10-17 14:07:58 +03:00
|
|
|
extern "C" fn handle_sigint(_: i32) {
|
2025-02-02 19:24:57 -08:00
|
|
|
set_interrupted();
|
2018-10-17 14:07:58 +03:00
|
|
|
}
|
|
|
|
|
|
2018-12-31 14:07:55 +02:00
|
|
|
/// Set the necessary signal handlers.
|
|
|
|
|
/// The function panics on failure.
|
2018-10-17 14:07:58 +03:00
|
|
|
pub fn set_handler() {
|
2023-11-24 07:50:41 +08:00
|
|
|
let sig_action = SigAction::new(SigHandler::Handler(handle_sigint), SaFlags::empty(), SigSet::empty());
|
2018-10-17 14:07:58 +03:00
|
|
|
unsafe {
|
2023-11-24 07:50:41 +08:00
|
|
|
sigaction(Signal::SIGINT, &sig_action).unwrap();
|
2018-10-17 14:07:58 +03:00
|
|
|
}
|
|
|
|
|
}
|