process: Split out from manager

This commit is contained in:
Vicki Pfau 2024-03-27 15:53:36 -07:00
parent 128c5ee36a
commit 0d90859841
3 changed files with 40 additions and 27 deletions

View file

@ -23,6 +23,7 @@ use crate::sls::{LogLayer, LogReceiver};
mod ds_inhibit;
mod hardware;
mod manager;
mod process;
mod sls;
#[cfg(test)]

View file

@ -6,12 +6,13 @@
*/
use anyhow::{ensure, Result};
use std::{ffi::OsStr, fmt};
use tokio::{fs, fs::File, io::AsyncWriteExt, process::Command};
use std::fmt;
use tokio::{fs, fs::File, io::AsyncWriteExt};
use tracing::{error, warn};
use zbus::{interface, zvariant::Fd};
use crate::hardware::{variant, HardwareVariant};
use crate::process::{run_script, script_output};
#[derive(PartialEq, Debug, Copy, Clone)]
#[repr(u32)]
@ -78,31 +79,6 @@ const GPU_CLOCKS_PATH: &str = "/sys/class/drm/card0/device/pp_od_clk_voltage";
const SYSTEMCTL_PATH: &str = "/usr/bin/systemctl";
const TRACE_CMD_PATH: &str = "/usr/bin/trace-cmd";
async fn script_exit_code(executable: &str, args: &[impl AsRef<OsStr>]) -> Result<bool> {
// Run given script and return true on success
let mut child = Command::new(executable).args(args).spawn()?;
let status = child.wait().await?;
Ok(status.success())
}
async fn run_script(name: &str, executable: &str, args: &[impl AsRef<OsStr>]) -> Result<bool> {
// Run given script to get exit code and return true on success.
// Return false on failure, but also print an error if needed
script_exit_code(executable, args)
.await
.inspect_err(|message| warn!("Error running {name} {message}"))
}
async fn script_output(executable: &str, args: &[impl AsRef<OsStr>]) -> Result<String> {
// Run given command and return the output given
let output = Command::new(executable).args(args).output();
let output = output.await?;
let s = std::str::from_utf8(&output.stdout)?;
Ok(s.to_string())
}
async fn setup_iwd_config(want_override: bool) -> std::io::Result<()> {
// Copy override.conf file into place or out of place depending
// on install value

36
src/process.rs Normal file
View file

@ -0,0 +1,36 @@
/*
* Copyright © 2023 Collabora Ltd.
* Copyright © 2024 Valve Software
*
* SPDX-License-Identifier: MIT
*/
use anyhow::Result;
use std::ffi::OsStr;
use tokio::process::Command;
use tracing::warn;
pub async fn script_exit_code(executable: &str, args: &[impl AsRef<OsStr>]) -> Result<bool> {
// Run given script and return true on success
let mut child = Command::new(executable).args(args).spawn()?;
let status = child.wait().await?;
Ok(status.success())
}
pub async fn run_script(name: &str, executable: &str, args: &[impl AsRef<OsStr>]) -> Result<bool> {
// Run given script to get exit code and return true on success.
// Return false on failure, but also print an error if needed
script_exit_code(executable, args)
.await
.inspect_err(|message| warn!("Error running {name} {message}"))
}
pub async fn script_output(executable: &str, args: &[impl AsRef<OsStr>]) -> Result<String> {
// Run given command and return the output given
let output = Command::new(executable).args(args).output();
let output = output.await?;
let s = std::str::from_utf8(&output.stdout)?;
Ok(s.to_string())
}