diff --git a/Cargo.toml b/Cargo.toml index cdb04d6d..e92bb337 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,14 +5,15 @@ authors = ["Jeremy Soller "] edition = "2018" [dependencies] -dbus = "=0.6.2" +dbus = "0.6" libc = "0.2" -clap = "2.32.0" +clap = "2.32" log = "0.4.3" fern = "0.5.6" intel-pstate = "0.2.0" sysfs-class = { git = "https://github.com/pop-os/sysfs-class" } err-derive = "0.1.5" + [build-dependencies] vergen = "0.1.1" diff --git a/src/daemon/mod.rs b/src/daemon/mod.rs index 91724f1a..a4a5db80 100644 --- a/src/daemon/mod.rs +++ b/src/daemon/mod.rs @@ -26,6 +26,24 @@ mod profiles; use self::profiles::*; +static CONTINUE: AtomicBool = AtomicBool::new(true); + +fn signal_handling() { + extern "C" fn handler(signal: libc::c_int) { + info!("caught signal: {}", signal); + CONTINUE.store(false, Ordering::SeqCst); + } + + unsafe fn signal(signal: libc::c_int) { libc::signal(signal, handler as libc::sighandler_t); } + + unsafe { + signal(libc::SIGINT); + signal(libc::SIGHUP); + signal(libc::SIGTERM); + signal(libc::SIGKILL); + } +} + // Disabled by default because some systems have quirky ACPI tables that fail to resume from // suspension. static PCI_RUNTIME_PM: AtomicBool = AtomicBool::new(false); @@ -124,6 +142,7 @@ impl Power for PowerDaemon { } pub fn daemon() -> Result<(), String> { + signal_handling(); let pci_runtime_pm = std::env::var("S76_POWER_PCI_RUNTIME_PM").ok().map_or(false, |v| v == "1"); info!( @@ -262,7 +281,7 @@ pub fn daemon() -> Result<(), String> { let mut last = hpd(); info!("Handling dbus requests"); - loop { + while CONTINUE.load(Ordering::SeqCst) { c.incoming(1000).next(); if let Ok(ref fan_daemon) = fan_daemon_res { @@ -286,4 +305,7 @@ pub fn daemon() -> Result<(), String> { } } } + + info!("daemon exited from loop"); + Ok(()) }