mirror of
https://gitlab.steamos.cloud/holo/steamos-manager.git
synced 2025-07-12 17:32:21 -04:00
Replace sysbase function with passing the path to a function
This commit is contained in:
parent
194a3f2c01
commit
fab64e1ff7
3 changed files with 27 additions and 28 deletions
|
@ -10,7 +10,7 @@ use tokio::fs;
|
|||
use tokio_stream::StreamExt;
|
||||
use tracing::{debug, error, info, warn};
|
||||
|
||||
use crate::{sysbase, Service};
|
||||
use crate::{path, Service};
|
||||
|
||||
struct HidNode {
|
||||
id: u32,
|
||||
|
@ -28,11 +28,11 @@ impl HidNode {
|
|||
}
|
||||
|
||||
fn sys_base(&self) -> PathBuf {
|
||||
PathBuf::from(format!("{}/sys/class/hidraw/hidraw{}/device", sysbase(), self.id).as_str())
|
||||
path(format!("/sys/class/hidraw/hidraw{}/device", self.id))
|
||||
}
|
||||
|
||||
fn hidraw(&self) -> PathBuf {
|
||||
PathBuf::from(format!("{}/dev/hidraw{}", sysbase(), self.id).as_str())
|
||||
path(format!("/dev/hidraw{}", self.id))
|
||||
}
|
||||
|
||||
async fn get_nodes(&self) -> Result<Vec<PathBuf>> {
|
||||
|
@ -92,7 +92,7 @@ impl HidNode {
|
|||
|
||||
async fn check(&self) -> Result<()> {
|
||||
let hidraw = self.hidraw();
|
||||
let mut dir = fs::read_dir(sysbase() + "/proc").await?;
|
||||
let mut dir = fs::read_dir(path("/proc")).await?;
|
||||
while let Some(entry) = dir.next_entry().await? {
|
||||
let path = entry.path();
|
||||
let proc = match path.file_name().map(|p| p.to_str()) {
|
||||
|
@ -119,7 +119,7 @@ impl HidNode {
|
|||
}
|
||||
};
|
||||
if path == hidraw {
|
||||
let comm = match fs::read(format!("{}/proc/{proc}/comm", sysbase())).await {
|
||||
let comm = match fs::read(crate::path(format!("/proc/{proc}/comm"))).await {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
debug!("Process {proc} disappeared while scanning: {e}");
|
||||
|
@ -165,9 +165,7 @@ impl HidNode {
|
|||
impl Inhibitor {
|
||||
pub fn new() -> Result<Inhibitor> {
|
||||
let inotify = Inotify::init()?.into_event_stream([0; 512])?;
|
||||
let dev_watch = inotify
|
||||
.watches()
|
||||
.add(sysbase() + "/dev", WatchMask::CREATE)?;
|
||||
let dev_watch = inotify.watches().add(path("/dev"), WatchMask::CREATE)?;
|
||||
|
||||
Ok(Inhibitor {
|
||||
inotify,
|
||||
|
@ -185,7 +183,7 @@ impl Inhibitor {
|
|||
}
|
||||
};
|
||||
|
||||
let mut dir = fs::read_dir(sysbase() + "/dev").await?;
|
||||
let mut dir = fs::read_dir(path("/dev")).await?;
|
||||
while let Some(entry) = dir.next_entry().await? {
|
||||
if let Err(e) = inhibitor.watch(entry.path().as_path()).await {
|
||||
error!("Encountered error attempting to watch: {e}");
|
||||
|
@ -244,7 +242,7 @@ impl Inhibitor {
|
|||
}
|
||||
};
|
||||
debug!("New device {} found", path.display());
|
||||
let path = PathBuf::from(sysbase() + "/dev").join(path);
|
||||
let path = crate::path("/dev").join(path);
|
||||
sleep(QSEC); // Wait a quarter second for nodes to enumerate
|
||||
if let Err(e) = self.watch(path.as_path()).await {
|
||||
error!("Encountered error attempting to watch: {e}");
|
||||
|
|
18
src/main.rs
18
src/main.rs
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
|
||||
use anyhow::{bail, Error, Result};
|
||||
use std::path::PathBuf;
|
||||
use tokio::signal::unix::{signal, SignalKind};
|
||||
use tokio::task::JoinSet;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
@ -58,24 +59,25 @@ where
|
|||
}
|
||||
|
||||
#[cfg(not(test))]
|
||||
pub fn sysbase() -> String {
|
||||
String::new()
|
||||
pub fn path<S: AsRef<str>>(path: S) -> PathBuf {
|
||||
PathBuf::from(path.as_ref())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn sysbase() -> String {
|
||||
pub fn path<S: AsRef<str>>(path: S) -> PathBuf {
|
||||
let current_test = crate::testing::current();
|
||||
let path = current_test.path();
|
||||
String::from(path.as_os_str().to_str().unwrap())
|
||||
let test_path = current_test.path();
|
||||
PathBuf::from(test_path.as_os_str().to_str().unwrap())
|
||||
.join(path.as_ref().trim_start_matches('/'))
|
||||
}
|
||||
|
||||
pub fn read_comm(pid: u32) -> Result<String> {
|
||||
let comm = std::fs::read_to_string(format!("{}/proc/{}/comm", sysbase(), pid))?;
|
||||
let comm = std::fs::read_to_string(path(format!("/proc/{}/comm", pid)))?;
|
||||
Ok(comm.trim_end().to_string())
|
||||
}
|
||||
|
||||
pub fn get_appid(pid: u32) -> Result<Option<u64>> {
|
||||
let environ = std::fs::read_to_string(format!("{}/proc/{}/environ", sysbase(), pid))?;
|
||||
let environ = std::fs::read_to_string(path(format!("/proc/{}/environ", pid)))?;
|
||||
for env_var in environ.split('\0') {
|
||||
let (key, value) = match env_var.split_once('=') {
|
||||
Some((k, v)) => (k, v),
|
||||
|
@ -90,7 +92,7 @@ pub fn get_appid(pid: u32) -> Result<Option<u64>> {
|
|||
};
|
||||
}
|
||||
|
||||
let stat = std::fs::read_to_string(format!("{}/proc/{}/stat", sysbase(), pid))?;
|
||||
let stat = std::fs::read_to_string(path(format!("/proc/{}/stat", pid)))?;
|
||||
let stat = match stat.rsplit_once(") ") {
|
||||
Some((_, v)) => v,
|
||||
None => return Ok(None),
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
use anyhow::{Error, Result};
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::Debug;
|
||||
use std::path::Path;
|
||||
use std::path::{Path, PathBuf};
|
||||
use tokio::fs;
|
||||
use tokio::io::{AsyncBufReadExt, BufReader};
|
||||
use tokio::net::unix::pipe;
|
||||
|
@ -10,7 +10,7 @@ use tracing::{error, info};
|
|||
use zbus::connection::Connection;
|
||||
use zbus::zvariant;
|
||||
|
||||
use crate::{get_appid, read_comm, sysbase, Service};
|
||||
use crate::{get_appid, path, read_comm, Service};
|
||||
|
||||
#[zbus::proxy(
|
||||
interface = "com.steampowered.SteamOSLogSubmitter.Trace",
|
||||
|
@ -42,10 +42,9 @@ async fn setup_traces(path: &Path) -> Result<()> {
|
|||
|
||||
impl Ftrace {
|
||||
pub async fn init(connection: Connection) -> Result<Ftrace> {
|
||||
let base = Self::base();
|
||||
let path = Path::new(base.as_str());
|
||||
fs::create_dir_all(path).await?;
|
||||
setup_traces(path).await?;
|
||||
let path = Self::base();
|
||||
fs::create_dir_all(&path).await?;
|
||||
setup_traces(&path.as_path()).await?;
|
||||
let file = pipe::OpenOptions::new()
|
||||
.unchecked(true) // Thanks tracefs for making trace_pipe a "regular" file
|
||||
.open_receiver(path.join("trace_pipe"))?;
|
||||
|
@ -55,8 +54,8 @@ impl Ftrace {
|
|||
})
|
||||
}
|
||||
|
||||
fn base() -> String {
|
||||
sysbase() + "/sys/kernel/tracing/instances/steamos-log-submitter"
|
||||
fn base() -> PathBuf {
|
||||
path("/sys/kernel/tracing/instances/steamos-log-submitter")
|
||||
}
|
||||
|
||||
async fn handle_pid(data: &mut HashMap<&str, zvariant::Value<'_>>, pid: u32) -> Result<()> {
|
||||
|
@ -193,7 +192,7 @@ mod test {
|
|||
let h = testing::start();
|
||||
let path = h.test.path();
|
||||
|
||||
let tracefs = PathBuf::from(Ftrace::base());
|
||||
let tracefs = Ftrace::base();
|
||||
|
||||
fs::create_dir_all(tracefs.join("events/oom/mark_victim")).expect("create_dir_all");
|
||||
unistd::mkfifo(
|
||||
|
@ -215,7 +214,7 @@ mod test {
|
|||
let h = testing::start();
|
||||
let path = h.test.path();
|
||||
|
||||
let tracefs = PathBuf::from(Ftrace::base());
|
||||
let tracefs = Ftrace::base();
|
||||
|
||||
fs::create_dir_all(tracefs.join("events/oom/mark_victim")).expect("create_dir_all");
|
||||
unistd::mkfifo(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue