Created
January 9, 2017 05:46
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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