Skip to content

Commit

Permalink
use our getegid & geteuid wrappers function instead of libc calls
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvestre committed Dec 27, 2023
1 parent 04a7f9c commit c38a432
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
9 changes: 4 additions & 5 deletions src/uu/install/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -959,10 +960,8 @@ fn need_copy(from: &Path, to: &Path, b: &Behavior) -> UResult<bool> {
}
} 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);

Check warning on line 964 in src/uu/install/src/install.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/install/src/install.rs#L964

Added line #L964 was not covered by tests
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/uu/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
6 changes: 4 additions & 2 deletions src/uu/test/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand Down
5 changes: 2 additions & 3 deletions src/uu/whoami/src/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OsString> {
// 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)
}

0 comments on commit c38a432

Please sign in to comment.