From b4d02e8ab142f476a8111dd9e655bdf76d0e2cdb Mon Sep 17 00:00:00 2001 From: Fries Date: Fri, 28 Oct 2022 23:22:58 -0700 Subject: [PATCH] amethyst: wrap root check in a safe wrapper --- src/internal/utils.rs | 5 +++++ src/main.rs | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/internal/utils.rs b/src/internal/utils.rs index 252a3d2..9651225 100644 --- a/src/internal/utils.rs +++ b/src/internal/utils.rs @@ -93,3 +93,8 @@ pub fn wrap_text>(s: S, padding: usize) -> Vec { .map(String::from) .collect() } + +pub fn is_run_with_root() -> bool { + let uid = unsafe { libc::geteuid() }; + uid == 0 +} diff --git a/src/main.rs b/src/main.rs index 7ab4c0a..dd8d99f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,8 @@ use builder::pacman::{PacmanColor, PacmanQueryBuilder}; use clap::Parser; use internal::commands::ShellCommand; -use internal::detect; use internal::error::SilentUnwrap; +use internal::{detect, utils}; use crate::args::{InstallArgs, Operation, QueryArgs, RemoveArgs}; use crate::interact::page_string; @@ -32,7 +32,7 @@ use logging::init_logger; #[tokio::main] async fn main() { color_eyre::install().unwrap(); - if unsafe { libc::geteuid() } == 0 { + if utils::is_run_with_root() { fl_crash!(AppExitCode::RunAsRoot, "run-as-root"); }