Support graceful termination via SIGINT (#23)

* Support graceful termination via SIGINT

* $ cargo fmt

* Explain why we need to ignore the SIGINT
This commit is contained in:
Nikolay Edigaryev
2023-03-16 18:38:03 +04:00
committed by GitHub
parent 535e03c97f
commit d7699e95a9
5 changed files with 405 additions and 309 deletions
+10 -1
View File
@@ -1,5 +1,6 @@
use anyhow::{anyhow, Context};
use clap::Parser;
use nix::sys::signal::{signal, SigHandler, Signal};
use privdrop::PrivDrop;
use softnet::proxy::Proxy;
use std::borrow::Cow;
@@ -55,7 +56,7 @@ fn main() -> ExitCode {
// Initialize Sentry
let _sentry = sentry::init(sentry::ClientOptions {
release: option_env!("CIRRUS_TAG").map(|tag| { Cow::from(format!("softnet@{tag}")) }),
release: option_env!("CIRRUS_TAG").map(|tag| Cow::from(format!("softnet@{tag}"))),
..Default::default()
});
@@ -84,6 +85,14 @@ fn main() -> ExitCode {
}
fn try_main() -> anyhow::Result<()> {
// The default signal(3)[1] action for SIGINT is to interrupt program,
// but we want to handle SIGINT ourselves, so we ignore it. The kqueue(2)'s[2]
// EVFILT_SIGNAL will receive it anyways, because it has lower precedence.
//
// [1]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/signal.3.html
// [2]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/kqueue.2.html
unsafe { signal(Signal::SIGINT, SigHandler::SigIgn) }?;
let args: Args = Args::parse();
// No need to run anything, just return