Skip to content

Commit

Permalink
Add root check
Browse files Browse the repository at this point in the history
  • Loading branch information
nbdd0121 committed Apr 22, 2024
1 parent c98e2b4 commit 54d429a
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ use hotplug::{AttachedDevice, HotPlug};
use std::fmt::Display;
use std::mem::ManuallyDrop;
use std::pin::pin;
use std::process::ExitCode;
use std::sync::Arc;

use anyhow::{Context, Result};
use anyhow::{bail, Context, Result};
use clap::Parser;
use clap_verbosity_flag::{InfoLevel, Verbosity};
use log::info;
Expand Down Expand Up @@ -114,8 +115,7 @@ async fn run(param: cli::Run, verbosity: Verbosity<InfoLevel>) -> Result<u8> {
Ok(status)
}

#[tokio::main]
async fn main() {
fn do_main() -> Result<u8> {
let args = cli::Args::parse();

let log_env = env_logger::Env::default()
Expand All @@ -134,17 +134,27 @@ async fn main() {
.format_level(args.log_format.level)
.init();

let result = match args.action {
Action::Run(param) => run(param, args.verbosity).await,
// Check that we're not running rootless. We need to control device access and must be root.
if !rustix::process::geteuid().is_root() {
bail!("This program must be run as root");
}

let param = match args.action {
Action::Run(param) => param,
};
let code = match result {
Ok(code) => code,

let rt = tokio::runtime::Runtime::new()?;
let result = rt.block_on(run(param, args.verbosity));
rt.shutdown_background();
result
}

fn main() -> ExitCode {
match do_main() {
Ok(code) => code.into(),
Err(err) => {
log::error!("{err:?}");
125
log::error!("{:?}", err);
ExitCode::from(125)
}
};
// Upon returning from `main`, tokio will attempt to shutdown, but if there're any blocking
// operation (e.g. fs operations), then the shutdown will hang.
std::process::exit(code.into());
}
}

0 comments on commit 54d429a

Please sign in to comment.