Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a coldplug utility #27

Merged
merged 2 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/bin/mdev-coldplug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::path::PathBuf;

use clap::Parser;
use mdev::setup_log;
use tracing::{debug, error};
use walkdir::WalkDir;

#[derive(Parser)]
struct Opt {
/// Verbose mode, logs to stderr
#[arg(short, long, action = clap::ArgAction::Count)]
verbose: u8,

/// Path where the sysfs is mounted
#[arg(short, long, default_value = "/sys")]
sysfs_mount: PathBuf,
}

impl Opt {
fn setup_log(&self) -> anyhow::Result<()> {
setup_log(self.verbose)
}
}

fn main() -> anyhow::Result<()> {
let opt = Opt::parse();

opt.setup_log()?;

let classdir = WalkDir::new(opt.sysfs_mount.join("class"))
.follow_links(true)
.max_depth(4)
.into_iter();
let busdir = WalkDir::new(opt.sysfs_mount.join("bus"))
.follow_links(true)
.max_depth(3)
.into_iter();

for entry in classdir
.chain(busdir)
.filter_map(|e| e.ok().filter(|e| e.file_name().eq("uevent")))
{
debug!("{entry:?}");
let p = entry.path();
std::fs::write(p, "add").unwrap_or_else(|e| error!("cannot write to {}: {e}", p.display()));
}

Ok(())
}
24 changes: 2 additions & 22 deletions src/bin/mdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tokio::{fs, join};
use tracing::{debug, info, warn};
use walkdir::WalkDir;

use mdev::{rule, RebroadcastMessage, Rebroadcaster};
use mdev::{rule, setup_log, RebroadcastMessage, Rebroadcaster};
use mdev_parser::Conf;

#[derive(Parser)]
Expand Down Expand Up @@ -254,35 +254,15 @@ impl Opt {
}

fn setup_log(&self) -> anyhow::Result<()> {
use tracing_subscriber::prelude::*;
use tracing_subscriber::{fmt, EnvFilter};

if self.daemon && !self.foreground && !self.syslog {
return Ok(());
}

let fmt_layer = fmt::layer().with_target(false);

if self.syslog {
todo!("Wire in syslog somehow");
}

let filter_layer = EnvFilter::try_from_default_env().unwrap_or_else(|_| {
if self.verbose < 1 {
EnvFilter::new("info")
} else if self.verbose < 2 {
EnvFilter::new("warn")
} else {
EnvFilter::new("debug")
}
});

tracing_subscriber::registry()
.with(filter_layer)
.with(fmt_layer)
.init();

Ok(())
setup_log(self.verbose)
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,27 @@ mod tests {
);
}
}

pub fn setup_log(verbose: u8) -> anyhow::Result<()> {
use tracing_subscriber::prelude::*;
use tracing_subscriber::{fmt, EnvFilter};

let fmt_layer = fmt::layer().with_target(false);

let filter_layer = EnvFilter::try_from_default_env().unwrap_or_else(|_| {
if verbose < 1 {
EnvFilter::new("info")
} else if verbose < 2 {
EnvFilter::new("warn")
} else {
EnvFilter::new("debug")
}
});

tracing_subscriber::registry()
.with(filter_layer)
.with(fmt_layer)
.init();

Ok(())
}
Loading