From a318fbceece675c42c2b0553a44e78c401e07e6b Mon Sep 17 00:00:00 2001 From: "Luke D. Jones" Date: Sat, 10 Dec 2022 21:05:27 +1300 Subject: [PATCH] asusd: check if nvidia-powerd enabled before toggling --- CHANGELOG.md | 1 + daemon/src/ctrl_power.rs | 45 ++++++++++++++++++---------------------- daemon/src/systemd.rs | 21 +++++++++++++++++++ 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f99113d5..ea4e4893 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - asusd: Very basic support for running a command on AC/Battery switching, this is in config at `/etc/asusd/asusd.conf`. A restart of asusd is not required if edited. + This is ideal for tasks that require root access (BE SAFE!) - The above AC/Battery commands are probably best set to run a script for more complex tasks +- asusd: check if nvidia-powerd enabled before toggling ## [v4.5.5] ### Changed diff --git a/daemon/src/ctrl_power.rs b/daemon/src/ctrl_power.rs index 26c83d14..41822856 100644 --- a/daemon/src/ctrl_power.rs +++ b/daemon/src/ctrl_power.rs @@ -1,4 +1,6 @@ -use crate::systemd::{do_systemd_unit_action, SystemdUnitAction}; +use crate::systemd::{ + do_systemd_unit_action, is_systemd_unit_enabled, SystemdUnitAction, SystemdUnitState, +}; use crate::{config::Config, error::RogError, GetSupported}; use crate::{task_watch_item, CtrlTask}; use async_trait::async_trait; @@ -171,14 +173,7 @@ impl CtrlTask for CtrlPower { .ok(); if let Ok(value) = power1.power.get_online() { - let action = if value == 1 { - SystemdUnitAction::Restart - } else { - SystemdUnitAction::Stop - }; - if do_systemd_unit_action(action, NVIDIA_POWERD).is_ok() { - info!("CtrlPower task: did {action:?} on {NVIDIA_POWERD}"); - } + do_nvidia_powerd_action(value == 1); } } }, @@ -197,14 +192,7 @@ impl CtrlTask for CtrlPower { .ok(); if let Ok(value) = power2.power.get_online() { - let action = if value == 1 { - SystemdUnitAction::Restart - } else { - SystemdUnitAction::Stop - }; - if do_systemd_unit_action(action, NVIDIA_POWERD).is_ok() { - info!("CtrlPower task: did {action:?} on {NVIDIA_POWERD}"); - } + do_nvidia_powerd_action(value == 1); } } }, @@ -222,14 +210,7 @@ impl CtrlTask for CtrlPower { if let Ok(value) = ctrl.power.get_online() { if online != value { online = value; - let action = if value == 1 { - SystemdUnitAction::Restart - } else { - SystemdUnitAction::Stop - }; - if do_systemd_unit_action(action, NVIDIA_POWERD).is_ok() { - info!("CtrlPower task: did {action:?} on {NVIDIA_POWERD}"); - } + do_nvidia_powerd_action(value == 1); Self::notify_mains_online(&signal_ctxt, value == 1) .await @@ -270,3 +251,17 @@ impl CtrlTask for CtrlPower { Ok(()) } } + +fn do_nvidia_powerd_action(ac_on: bool) { + let action = if ac_on { + SystemdUnitAction::Restart + } else { + SystemdUnitAction::Stop + }; + + if let Ok(res) = is_systemd_unit_enabled(SystemdUnitState::Enabled, NVIDIA_POWERD) { + if res && do_systemd_unit_action(action, NVIDIA_POWERD).is_ok() { + info!("CtrlPower task: did {action:?} on {NVIDIA_POWERD}"); + } + } +} diff --git a/daemon/src/systemd.rs b/daemon/src/systemd.rs index d733265d..e806bb9d 100644 --- a/daemon/src/systemd.rs +++ b/daemon/src/systemd.rs @@ -24,6 +24,9 @@ impl From for &str { pub enum SystemdUnitState { Active, Inactive, + Masked, + Disabled, + Enabled, } impl From for &str { @@ -31,6 +34,9 @@ impl From for &str { match s { SystemdUnitState::Active => "active", SystemdUnitState::Inactive => "inactive", + SystemdUnitState::Masked => "masked", + SystemdUnitState::Disabled => "disabled", + SystemdUnitState::Enabled => "enabled", } } } @@ -66,6 +72,21 @@ pub fn is_systemd_unit_state(state: SystemdUnitState, unit: &str) -> Result Result { + let mut cmd = Command::new("systemctl"); + cmd.arg("is-enabled"); + cmd.arg(unit); + + let output = cmd + .output() + .map_err(|err| RogError::Command(format!("{:?}", cmd), err))?; + if output.stdout.starts_with(<&str>::from(state).as_bytes()) { + return Ok(true); + } + Ok(false) +} + /// Wait for a systemd unit to change to `state`. Checks state every 250ms for 3 seconds. Blocks while running wait. pub fn wait_systemd_unit_state(state: SystemdUnitState, unit: &str) -> Result<(), RogError> { let mut cmd = Command::new("systemctl");