feat(src/commands): add inhibit command to block sleep using systemd-inhibit
This command prevents system sleep while optionally executing a user‑provided command, offering both foreground and background inhibition modes. - New Features - Implemented inhibit.rs with logic to invoke systemd-inhibit. - Exposed inhibit functionality by adding pub mod inhibit; to src/commands/mod.rs. - Enhancements - (none) - Bug Fixes - (none)
This commit is contained in:
2 files changed
+55
No files matched your search
@@ -0,0 +1,54 @@
|
|||||||
|
use log::{error, info};
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
pub fn execute(command_args: &[String], why: &str, who: &str, daemon: bool) {
|
||||||
|
let mut args = vec![
|
||||||
|
"--why".to_string(),
|
||||||
|
why.to_string(),
|
||||||
|
"--who".to_string(),
|
||||||
|
who.to_string(),
|
||||||
|
"--what".to_string(),
|
||||||
|
"sleep:idle".to_string(),
|
||||||
|
];
|
||||||
|
|
||||||
|
if command_args.is_empty() {
|
||||||
|
if !daemon {
|
||||||
|
info!("No command provided. Inhibiting sleep indefinitely. Press Ctrl+C to stop.");
|
||||||
|
}
|
||||||
|
args.push("sleep".to_string());
|
||||||
|
args.push("infinity".to_string());
|
||||||
|
} else {
|
||||||
|
info!(
|
||||||
|
"Running command while inhibiting sleep: {}",
|
||||||
|
command_args.join(" ")
|
||||||
|
);
|
||||||
|
args.extend(command_args.iter().cloned());
|
||||||
|
}
|
||||||
|
|
||||||
|
if daemon {
|
||||||
|
match Command::new("systemd-inhibit").args(&args).spawn() {
|
||||||
|
Ok(child) => {
|
||||||
|
info!("Inhibitor started in background with PID: {}", child.id());
|
||||||
|
info!("To stop it, use: pkill -P {} systemd-inhibit", child.id());
|
||||||
|
// In background mode, we just let the child run and exit lapctl
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("Failed to spawn systemd-inhibit: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
match Command::new("systemd-inhibit").args(&args).status() {
|
||||||
|
Ok(status) => {
|
||||||
|
if !status.success() {
|
||||||
|
error!("systemd-inhibit exited with status: {}", status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("Failed to execute systemd-inhibit: {}", e);
|
||||||
|
eprintln!(
|
||||||
|
"Error: make sure 'systemd-inhibit' is installed and available in your PATH."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
pub mod battery;
|
pub mod battery;
|
||||||
pub mod cooling;
|
pub mod cooling;
|
||||||
pub mod gpu;
|
pub mod gpu;
|
||||||
|
pub mod inhibit;
|
||||||
pub mod install_rules;
|
pub mod install_rules;
|
||||||
pub mod power;
|
pub mod power;
|
||||||
pub mod status;
|
pub mod status;
|
||||||
Reference in new issue
Block a user