From 86cbef83b6b2476e387b6418b40a438d6ccbd5c0 Mon Sep 17 00:00:00 2001 From: "Luke D. Jones" Date: Mon, 22 Jan 2024 21:54:19 +1300 Subject: [PATCH] Reload asusd.ron if changed --- CHANGELOG.md | 1 + Cargo.lock | 29 ++++++++++---------- Cargo.toml | 2 +- asusd/Cargo.toml | 1 + asusd/src/config.rs | 2 +- asusd/src/ctrl_platform.rs | 55 +++++++++++++++++++++++++++++++++++--- asusd/src/daemon.rs | 3 ++- asusd/src/lib.rs | 8 ++++++ config-traits/src/lib.rs | 14 ++++++++++ 9 files changed, 94 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c1a9736..120b14b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Don't change EPP or thermal profile if the battery/ac state hasn't actually changed on resume - Re-implement the `asusctl -s` command (not fully) - Add more docs to some parts of code, and dbus interfaces +- Reload asusd.ron if changed. Does not notify any dbus listeners (yet) ## [v5.0.7] ### Changed diff --git a/Cargo.lock b/Cargo.lock index cc8f121e..9c74b68f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -199,7 +199,7 @@ checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" [[package]] name = "asusctl" -version = "5.0.7" +version = "5.0.8-RC1" dependencies = [ "asusd", "cargo-husky", @@ -219,14 +219,15 @@ dependencies = [ [[package]] name = "asusd" -version = "5.0.7" +version = "5.0.8-RC1" dependencies = [ "cargo-husky", "concat-idents", "config-traits", "dmi_id", "env_logger", - "futures-lite 2.2.0", + "futures-lite 1.13.0", + "inotify", "log", "logind-zbus", "rog_anime", @@ -243,7 +244,7 @@ dependencies = [ [[package]] name = "asusd-user" -version = "5.0.7" +version = "5.0.8-RC1" dependencies = [ "cargo-husky", "config-traits", @@ -846,7 +847,7 @@ dependencies = [ [[package]] name = "config-traits" -version = "5.0.7" +version = "5.0.8-RC1" dependencies = [ "cargo-husky", "log", @@ -899,7 +900,7 @@ dependencies = [ [[package]] name = "cpuctl" -version = "5.0.7" +version = "5.0.8-RC1" [[package]] name = "cpufeatures" @@ -1023,7 +1024,7 @@ dependencies = [ [[package]] name = "dmi_id" -version = "5.0.7" +version = "5.0.8-RC1" dependencies = [ "log", "udev", @@ -2833,7 +2834,7 @@ checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" [[package]] name = "rog-control-center" -version = "5.0.7" +version = "5.0.8-RC1" dependencies = [ "asusd", "cargo-husky", @@ -2866,7 +2867,7 @@ dependencies = [ [[package]] name = "rog_anime" -version = "5.0.7" +version = "5.0.8-RC1" dependencies = [ "cargo-husky", "dmi_id", @@ -2883,7 +2884,7 @@ dependencies = [ [[package]] name = "rog_aura" -version = "5.0.7" +version = "5.0.8-RC1" dependencies = [ "cargo-husky", "dmi_id", @@ -2897,7 +2898,7 @@ dependencies = [ [[package]] name = "rog_dbus" -version = "5.0.7" +version = "5.0.8-RC1" dependencies = [ "asusd", "cargo-husky", @@ -2910,7 +2911,7 @@ dependencies = [ [[package]] name = "rog_platform" -version = "5.0.7" +version = "5.0.8-RC1" dependencies = [ "cargo-husky", "concat-idents", @@ -2927,7 +2928,7 @@ dependencies = [ [[package]] name = "rog_profiles" -version = "5.0.7" +version = "5.0.8-RC1" dependencies = [ "cargo-husky", "log", @@ -2941,7 +2942,7 @@ dependencies = [ [[package]] name = "rog_simulators" -version = "5.0.7" +version = "5.0.8-RC1" dependencies = [ "glam", "log", diff --git a/Cargo.toml b/Cargo.toml index c3fb35c1..3ef3b581 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ default-members = ["asusctl", "asusd", "asusd-user", "cpuctl", "rog-control-cent resolver = "2" [workspace.package] -version = "5.0.7" +version = "5.0.8-RC1" rust-version = "1.75" [workspace.dependencies] diff --git a/asusd/Cargo.toml b/asusd/Cargo.toml index 92a670f9..41e9d71a 100644 --- a/asusd/Cargo.toml +++ b/asusd/Cargo.toml @@ -22,6 +22,7 @@ rog_profiles = { path = "../rog-profiles" } dmi_id = { path = "../dmi-id" } futures-lite = "*" udev.workspace = true +inotify.workspace = true tokio.workspace = true diff --git a/asusd/src/config.rs b/asusd/src/config.rs index aca1a4ab..830d1ed4 100644 --- a/asusd/src/config.rs +++ b/asusd/src/config.rs @@ -5,7 +5,7 @@ use serde_derive::{Deserialize, Serialize}; const CONFIG_FILE: &str = "asusd.ron"; -#[derive(Deserialize, Serialize, Default, Debug)] +#[derive(Deserialize, Serialize, Default, Debug, PartialEq, PartialOrd)] pub struct Config { /// Save charge limit for restoring on boot/resume pub charge_control_end_threshold: u8, diff --git a/asusd/src/ctrl_platform.rs b/asusd/src/ctrl_platform.rs index 284d0159..d089421a 100644 --- a/asusd/src/ctrl_platform.rs +++ b/asusd/src/ctrl_platform.rs @@ -1,3 +1,4 @@ +use std::path::Path; use std::process::Command; use std::sync::Arc; @@ -15,7 +16,7 @@ use crate::ctrl_anime::trait_impls::{CtrlAnimeZbus, ANIME_ZBUS_NAME, ANIME_ZBUS_ use crate::ctrl_aura::trait_impls::{CtrlAuraZbus, AURA_ZBUS_NAME, AURA_ZBUS_PATH}; use crate::ctrl_fancurves::{CtrlFanCurveZbus, FAN_CURVE_ZBUS_NAME, FAN_CURVE_ZBUS_PATH}; use crate::error::RogError; -use crate::{task_watch_item, task_watch_item_notify, CtrlTask}; +use crate::{task_watch_item, task_watch_item_notify, CtrlTask, Reloadable}; const PLATFORM_ZBUS_NAME: &str = "Platform"; const PLATFORM_ZBUS_PATH: &str = "/org/asuslinux/Platform"; @@ -115,7 +116,7 @@ pub struct CtrlPlatform { } impl CtrlPlatform { - pub fn new(config: Arc>) -> Result { + pub fn new(config: Arc>, config_path: &Path) -> Result { let platform = RogPlatform::new()?; let power = AsusPower::new()?; @@ -124,14 +125,60 @@ impl CtrlPlatform { info!("Standard graphics switching will still work."); } - Ok(CtrlPlatform { + let config1 = config.clone(); + let inotify = inotify::Inotify::init()?; + inotify + .watches() + .add(config_path, inotify::WatchMask::MODIFY) + .map_err(|e| { + if e.kind() == std::io::ErrorKind::NotFound { + error!("Not found: {:?}", config_path); + } else { + error!("Could not set asusd config inotify: {:?}", config_path); + } + e + }) + .ok(); + + let ret_self = CtrlPlatform { power, platform, config, cpu_control: CPUControl::new() .map_err(|e| error!("Couldn't get CPU control sysfs: {e}")) .ok(), - }) + }; + let inotify_self = ret_self.clone(); + + tokio::spawn(async move { + use zbus::export::futures_util::StreamExt; + info!("Starting inotify watch for asusd config file"); + + let mut buffer = [0; 32]; + inotify + .into_event_stream(&mut buffer) + .unwrap() + .for_each(|_| async { + let res = config1.lock().await.read_new(); + if let Some(new_cfg) = res { + let mut old_cfg = config1.lock().await; + if *old_cfg != new_cfg { + info!( + "asusd.ron updated externally, updating internal copy and \ + reloading" + ); + *old_cfg = new_cfg; + // shitty way to handle this but it works. Only require the reload() + let mut inotify_self = inotify_self.clone(); + // TODO: better reload with ReloadAndNotify + inotify_self.reload().await.unwrap(); + } + } + }) + .await; + }); + + Ok(ret_self) } fn set_gfx_mode(&self, mode: GpuMode) -> Result<(), RogError> { diff --git a/asusd/src/daemon.rs b/asusd/src/daemon.rs index 45a893f9..34234340 100644 --- a/asusd/src/daemon.rs +++ b/asusd/src/daemon.rs @@ -64,11 +64,12 @@ async fn start_daemon() -> Result<(), Box> { let mut connection = Connection::system().await?; let config = Config::new().load(); + let cfg_path = config.file_path(); let config = Arc::new(Mutex::new(config)); // supported.add_to_server(&mut connection).await; - match CtrlPlatform::new(config.clone()) { + match CtrlPlatform::new(config.clone(), &cfg_path) { Ok(ctrl) => { let sig_ctx = CtrlPlatform::signal_context(&connection)?; start_tasks(ctrl, &mut connection, sig_ctx).await?; diff --git a/asusd/src/lib.rs b/asusd/src/lib.rs index 476cd99b..205444bd 100644 --- a/asusd/src/lib.rs +++ b/asusd/src/lib.rs @@ -132,6 +132,14 @@ pub fn print_board_info() { pub trait Reloadable { fn reload(&mut self) -> impl std::future::Future> + Send; } + +pub trait ReloadAndNotify { + fn reload_and_notify( + &mut self, + signal_context: SignalContext<'static>, + ) -> impl std::future::Future> + Send; +} + pub trait ZbusRun { fn add_to_server(self, server: &mut Connection) -> impl std::future::Future + Send; diff --git a/config-traits/src/lib.rs b/config-traits/src/lib.rs index 982a4a55..f34d1ab9 100644 --- a/config-traits/src/lib.rs +++ b/config-traits/src/lib.rs @@ -109,6 +109,20 @@ where } } + /// Open and parse the config file to self from ron format + fn read_new(&self) -> Option { + if let Ok(data) = fs::read_to_string(self.file_path()) { + if data.is_empty() { + warn!("File is empty {:?}", self.file_path()); + } else if let Ok(data) = ron::from_str(&data) { + return Some(data); + } else { + warn!("Could not deserialise {:?}", self.file_path()); + } + } + None + } + /// Write the config file data to pretty ron format fn write(&self) { let mut file = match File::create(self.file_path()) {