Skip to content

Commit

Permalink
check for root
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmaSd committed Jan 9, 2020
1 parent 183a75b commit 366c1c8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ mod gui;
mod limit;
mod tc;
mod utils;
use utils::check_for_iproute2;
use utils::{check_for_iproute2, is_root};

pub type CatchAll<T> = Result<T, Box<dyn std::error::Error>>;

fn main() {
if !is_root().expect("Error while verifying root permission") {
eprintln!("This program needs sudo privilege");
std::process::exit(1);
}
if let Err(e) = check_for_iproute2() {
eprintln!("Error: {}", e);
std::process::exit(1);
Expand Down
29 changes: 17 additions & 12 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@ use crate::CatchAll;
use std::collections::HashMap;
use std::process::{Command, Output};

pub fn check_for_iproute2() -> Result<(), String> {
const TOOLS: [&str; 4] = ["tc", "ss", "ifstat", "ip"];
for tool in &TOOLS {
if let Err(e) = std::process::Command::new(tool).output() {
if e.kind() == std::io::ErrorKind::NotFound {
return Err(format!("Missing program: {}\nIs iproute2 installed?", tool));
}
}
}
Ok(())
}

// run macro
#[macro_export]
macro_rules! run {
Expand All @@ -40,6 +28,23 @@ pub fn run(v: String) -> CatchAll<Output> {
Ok(output)
}

pub fn is_root() -> CatchAll<bool> {
let output = run!("id -ur")?;
let user_id: usize = String::from_utf8(output.stdout)?.trim().parse()?;
Ok(user_id == 0)
}
pub fn check_for_iproute2() -> Result<(), String> {
const TOOLS: [&str; 4] = ["tc", "ss", "ifstat", "ip"];
for tool in &TOOLS {
if let Err(e) = std::process::Command::new(tool).output() {
if e.kind() == std::io::ErrorKind::NotFound {
return Err(format!("Missing program: {}\nIs iproute2 installed?", tool));
}
}
}
Ok(())
}

#[test]
fn tifstat() {
dbg!(ifstat());
Expand Down

0 comments on commit 366c1c8

Please sign in to comment.