Skip to content

Instantly share code, notes, and snippets.

@philippkeller
Created January 9, 2017 05:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philippkeller/3a2d50bdc80c11f6d0163d0b7f4ad900 to your computer and use it in GitHub Desktop.
Save philippkeller/3a2d50bdc80c11f6d0163d0b7f4ad900 to your computer and use it in GitHub Desktop.
catch SIGINT and SIGTERM and write them into a log file to investigate signal sending
extern crate libc;
use libc::{c_int, SIGTERM, SIGINT, SIG_ERR, signal, pause};
use std::fs::OpenOptions;
use std::io::Write;
fn sig(sig:c_int) {
let mut file = OpenOptions::new().append(true).open("/tmp/signals.txt").expect("cannot open");
match sig {
SIGINT => file.write_all(b"sigint\n"),
SIGTERM => file.write_all(b"sigterm\n"),
_ => file.write_all(b"other sit\n"),
};
}
fn main() {
unsafe {
if signal(SIGINT, sig as usize) == SIG_ERR {
println!("error registering sig_int");
}
if signal(SIGTERM, sig as usize) == SIG_ERR {
println!("error registering sig_term");
}
pause();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment