63 lines
1.9 KiB
Rust
63 lines
1.9 KiB
Rust
use std::path::{Path, PathBuf};
|
|
|
|
pub const VALID_ACTIONS: &[&str] = &["add", "remove", "change"];
|
|
|
|
pub async fn trigger(set: &str, action: &str) {
|
|
assert!(VALID_ACTIONS.contains(&action));
|
|
for i in set_to_dirs(set) {
|
|
let path = &sysfs_root().join(i);
|
|
if trigger_dir(path, action).await.is_err() {
|
|
tracing::warn!(
|
|
"\"{}\" is inaccessible. Check your system configuration.",
|
|
path.display()
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
async fn trigger_dir(path: &Path, action: &str) -> std::io::Result<()> {
|
|
let mut read_dir = tokio::fs::read_dir(path).await?;
|
|
while let Ok(Some(dirent)) = read_dir.next_entry().await {
|
|
if dirent.file_name().as_encoded_bytes() == b"uevent" {
|
|
match tokio::fs::write(dirent.path(), action.as_bytes()).await {
|
|
Ok(()) => {
|
|
tracing::debug!(
|
|
"Triggering uevent \"{action}\" for \"{}\".",
|
|
dirent.path().display()
|
|
);
|
|
}
|
|
Err(err) => {
|
|
tracing::warn!(
|
|
"Failed to trigger uevent \"{action}\" for \"{}\": {err}",
|
|
dirent.path().display()
|
|
);
|
|
}
|
|
};
|
|
} else if dirent
|
|
.file_type()
|
|
.await
|
|
.map(|x| x.is_dir())
|
|
.unwrap_or_default()
|
|
{
|
|
_ = Box::pin(trigger_dir(&dirent.path(), action)).await;
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn set_to_dirs(set: &str) -> Vec<PathBuf> {
|
|
if set == "all" {
|
|
vec![sysfs_root().into()]
|
|
} else if set == "devices" {
|
|
vec![sysfs_root().join("devices")]
|
|
} else {
|
|
vec![]
|
|
}
|
|
}
|
|
|
|
fn sysfs_root() -> PathBuf {
|
|
std::env::var("LXDEVICED_SYSFS_ROOT")
|
|
.map(PathBuf::from)
|
|
.unwrap_or_else(|_| "/sys".into())
|
|
}
|