refactor(main): Relocate and enhance signal handler with improved logging

This commit is contained in:
Nils Andreas Svee committed 2026-04-22 21:45:27 +07:00
1 parent 72fe0230ee
commit e22c100e57
1 file changed
+17 -10
+17 -10
View File
@@ -20,19 +20,13 @@ use std::collections::HashMap;
use std::net::IpAddr; use std::net::IpAddr;
use std::str::FromStr; use std::str::FromStr;
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::mpsc::{channel, sync_channel, RecvTimeoutError}; use std::sync::mpsc::{RecvTimeoutError, channel, sync_channel};
use std::sync::{Arc, Mutex, RwLock}; use std::sync::{Arc, Mutex, RwLock};
use std::thread::sleep; use std::thread::sleep;
use std::time::Duration; use std::time::Duration;
use std::time::Instant; use std::time::Instant;
use std::{process, thread}; use std::{process, thread};
pub static SHUTDOWN: AtomicBool = AtomicBool::new(false);
extern "C" fn signal_handler(_: libc::c_int) {
SHUTDOWN.store(true, Ordering::Relaxed);
}
use crate::config::{Config, MeasurementType}; use crate::config::{Config, MeasurementType};
use crate::netlink::Netlink; use crate::netlink::Netlink;
use crate::pinger::{PingListener, PingSender}; use crate::pinger::{PingListener, PingSender};
@@ -41,14 +35,27 @@ use crate::pinger_icmp_ts::{PingerICMPTimestampListener, PingerICMPTimestampSend
use crate::ratecontroller::{Ratecontroller, StatsDirection}; use crate::ratecontroller::{Ratecontroller, StatsDirection};
use crate::reflector_selector::ReflectorSelector; use crate::reflector_selector::ReflectorSelector;
pub static SHUTDOWN: AtomicBool = AtomicBool::new(false);
extern "C" fn signal_handler(_: libc::c_int) {
println!("Signal received, shutting down sqm-autorate-rust");
SHUTDOWN.store(true, Ordering::Relaxed);
}
const VERSION: &str = env!("CARGO_PKG_VERSION"); const VERSION: &str = env!("CARGO_PKG_VERSION");
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
println!("Starting sqm-autorate version {}", VERSION); println!("Starting sqm-autorate-rust version {}", VERSION);
unsafe { unsafe {
libc::signal(libc::SIGINT, signal_handler as *const () as libc::sighandler_t); libc::signal(
libc::signal(libc::SIGTERM, signal_handler as *const () as libc::sighandler_t); libc::SIGINT,
signal_handler as *const () as libc::sighandler_t,
);
libc::signal(
libc::SIGTERM,
signal_handler as *const () as libc::sighandler_t,
);
} }
let config = Config::new()?; let config = Config::new()?;