asusd: check if nvidia-powerd enabled before toggling

This commit is contained in:
Luke D. Jones
2022-12-10 21:05:27 +13:00
parent 8feacf863a
commit a318fbceec
3 changed files with 42 additions and 25 deletions

View File

@@ -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

View File

@@ -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}");
}
}
}

View File

@@ -24,6 +24,9 @@ impl From<SystemdUnitAction> for &str {
pub enum SystemdUnitState {
Active,
Inactive,
Masked,
Disabled,
Enabled,
}
impl From<SystemdUnitState> for &str {
@@ -31,6 +34,9 @@ impl From<SystemdUnitState> 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<bool
Ok(false)
}
/// Get systemd unit state. Blocks while command is run.
pub fn is_systemd_unit_enabled(state: SystemdUnitState, unit: &str) -> Result<bool, RogError> {
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");