Skip to content

Instantly share code, notes, and snippets.

@tilpner
Created September 19, 2016 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tilpner/f3a56cf60e392377c4d34887fb2cbe3d to your computer and use it in GitHub Desktop.
Save tilpner/f3a56cf60e392377c4d34887fb2cbe3d to your computer and use it in GitHub Desktop.
extern crate libc;
use std::{thread, process};
use std::time::Duration;
fn sigint_handler(sig: libc::c_int) {
if sig != libc::SIGINT {
println!("Oops, not for me. Ignoring {}.", sig);
return;
}
println!("Just give me three seconds to clean up...");
thread::sleep(Duration::from_secs(3));
println!("Alright, done here.");
process::exit(0);
}
fn register_sighandler(sig: libc::c_int, handler: fn(libc::c_int)) -> Result<(), ()> {
unsafe {
match libc::signal(sig, handler as usize) {
libc::SIG_ERR => Err(()),
_ => Ok(()),
}
}
}
fn main() {
register_sighandler(libc::SIGINT, sigint_handler).expect("Unable to register SIGINT handler");
loop {
thread::sleep(Duration::from_secs(1));
println!("Running just fine.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment