diff --git a/Cargo.lock b/Cargo.lock index 1cb8811..02be16c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,6 +30,12 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "libc" +version = "0.2.186" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" + [[package]] name = "proc-macro2" version = "1.0.106" @@ -160,6 +166,7 @@ name = "zfs_backup" version = "0.1.0" dependencies = [ "anyhow", + "libc", "serde", "toml", ] diff --git a/Cargo.toml b/Cargo.toml index aed3913..46888ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,5 +5,6 @@ edition = "2024" [dependencies] anyhow = "1.0.98" +libc = "0.2.186" serde = { version = "1.0", features = ["derive"] } toml = "0.9.2" diff --git a/src/main.rs b/src/main.rs index b534054..6c51fc9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,8 @@ -use std::env; use std::io::stdin; use anyhow::{Context, Result, bail}; +use libc::geteuid; use zfs_backup::cmd::zfs::zpool_import; use zfs_backup::config; use zfs_backup::encryption; @@ -119,8 +119,8 @@ fn ask_user_confirmation(todos: &Vec) -> Result { } fn verify_running_as_root() -> Result<()> { - let uid = env::var("USER")?; - if uid != "root" { + let uid = unsafe { geteuid() }; + if uid != 0 { bail!("must be run as root") } Ok(())