From c38a43210cb0ac7d58cb7732e14ac516fb7d19f1 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Wed, 27 Dec 2023 23:51:49 +0100 Subject: [PATCH] use our getegid & geteuid wrappers function instead of libc calls --- src/uu/install/src/install.rs | 9 ++++----- src/uu/test/Cargo.toml | 2 +- src/uu/test/src/test.rs | 6 ++++-- src/uu/whoami/src/platform/unix.rs | 5 ++--- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/uu/install/src/install.rs b/src/uu/install/src/install.rs index fb80b2f0ec..92910650da 100644 --- a/src/uu/install/src/install.rs +++ b/src/uu/install/src/install.rs @@ -19,7 +19,6 @@ use uucore::mode::get_umask; use uucore::perms::{wrap_chown, Verbosity, VerbosityLevel}; use uucore::{format_usage, help_about, help_usage, show, show_error, show_if_err, uio_error}; -use libc::{getegid, geteuid}; use std::error::Error; use std::fmt::{Debug, Display}; use std::fs; @@ -29,6 +28,8 @@ use std::os::unix::fs::MetadataExt; use std::os::unix::prelude::OsStrExt; use std::path::{Path, PathBuf, MAIN_SEPARATOR}; use std::process; +#[cfg(not(target_os = "windows"))] +use uucore::process::{getegid, geteuid}; const DEFAULT_MODE: u32 = 0o755; const DEFAULT_STRIP_PROGRAM: &str = "strip"; @@ -959,10 +960,8 @@ fn need_copy(from: &Path, to: &Path, b: &Behavior) -> UResult { } } else { #[cfg(not(target_os = "windows"))] - unsafe { - if to_meta.uid() != geteuid() || to_meta.gid() != getegid() { - return Ok(true); - } + if to_meta.uid() != geteuid() || to_meta.gid() != getegid() { + return Ok(true); } } diff --git a/src/uu/test/Cargo.toml b/src/uu/test/Cargo.toml index bc27511867..93a77b11d4 100644 --- a/src/uu/test/Cargo.toml +++ b/src/uu/test/Cargo.toml @@ -17,7 +17,7 @@ path = "src/test.rs" [dependencies] clap = { workspace = true } libc = { workspace = true } -uucore = { workspace = true } +uucore = { workspace = true, features = ["process"] } [target.'cfg(target_os = "redox")'.dependencies] redox_syscall = { workspace = true } diff --git a/src/uu/test/src/test.rs b/src/uu/test/src/test.rs index 4f230a590d..a0d0f33959 100644 --- a/src/uu/test/src/test.rs +++ b/src/uu/test/src/test.rs @@ -17,6 +17,8 @@ use std::fs; use std::os::unix::fs::MetadataExt; use uucore::display::Quotable; use uucore::error::{UResult, USimpleError}; +#[cfg(not(windows))] +use uucore::process::{getegid, geteuid}; use uucore::{format_usage, help_about, help_section}; const ABOUT: &str = help_about!("test.md"); @@ -276,7 +278,7 @@ fn path(path: &OsStr, condition: &PathCondition) -> bool { let geteuid = || { #[cfg(not(target_os = "redox"))] - let euid = unsafe { libc::geteuid() }; + let euid = geteuid(); #[cfg(target_os = "redox")] let euid = syscall::geteuid().unwrap() as u32; @@ -285,7 +287,7 @@ fn path(path: &OsStr, condition: &PathCondition) -> bool { let getegid = || { #[cfg(not(target_os = "redox"))] - let egid = unsafe { libc::getegid() }; + let egid = getegid(); #[cfg(target_os = "redox")] let egid = syscall::getegid().unwrap() as u32; diff --git a/src/uu/whoami/src/platform/unix.rs b/src/uu/whoami/src/platform/unix.rs index 31ab16fba4..9310a6de0a 100644 --- a/src/uu/whoami/src/platform/unix.rs +++ b/src/uu/whoami/src/platform/unix.rs @@ -7,10 +7,9 @@ use std::ffi::OsString; use std::io; use uucore::entries::uid2usr; +use uucore::process::geteuid; pub fn get_username() -> io::Result { - // SAFETY: getuid() does nothing with memory and is always successful. - let uid = unsafe { libc::geteuid() }; // uid2usr should arguably return an OsString but currently doesn't - uid2usr(uid).map(Into::into) + uid2usr(geteuid()).map(Into::into) }