From 3203759701fdc3cc192fafb9741a1adf16e65aa7 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Sun, 17 May 2026 03:17:53 +0100 Subject: [PATCH] uucore: remove process functions --- Cargo.lock | 3 + fuzz/Cargo.lock | 1 + src/uu/install/Cargo.toml | 1 + src/uu/install/src/install.rs | 6 +- src/uu/test/Cargo.toml | 3 + src/uu/test/src/test.rs | 12 ++-- src/uu/whoami/Cargo.toml | 3 + src/uu/whoami/src/platform/unix.rs | 4 +- src/uucore/src/lib/features/entries.rs | 2 +- src/uucore/src/lib/features/proc_info.rs | 14 ++-- src/uucore/src/lib/features/process.rs | 85 +----------------------- tests/by-util/test_chgrp.rs | 18 ++--- tests/by-util/test_install.rs | 14 ++-- tests/by-util/test_ls.rs | 2 +- 14 files changed, 51 insertions(+), 117 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 18d7ece3b73..a3aac0450df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3638,6 +3638,7 @@ dependencies = [ "file_diff", "filetime", "fluent", + "rustix", "selinux", "thiserror 2.0.18", "uucore", @@ -4249,6 +4250,7 @@ dependencies = [ "clap", "fluent", "libc", + "rustix", "tempfile", "thiserror 2.0.18", "uucore", @@ -4441,6 +4443,7 @@ version = "0.8.0" dependencies = [ "clap", "fluent", + "rustix", "uucore", "windows-sys 0.61.2", ] diff --git a/fuzz/Cargo.lock b/fuzz/Cargo.lock index 769daabe9e1..fd9400d04cb 100644 --- a/fuzz/Cargo.lock +++ b/fuzz/Cargo.lock @@ -2040,6 +2040,7 @@ dependencies = [ "clap", "fluent", "libc", + "rustix", "thiserror", "uucore", ] diff --git a/src/uu/install/Cargo.toml b/src/uu/install/Cargo.toml index 229fd06b466..77e92d877ed 100644 --- a/src/uu/install/Cargo.toml +++ b/src/uu/install/Cargo.toml @@ -21,6 +21,7 @@ path = "src/install.rs" clap = { workspace = true } filetime = { workspace = true } file_diff = { workspace = true } +rustix = { workspace = true } selinux = { workspace = true, optional = true } thiserror = { workspace = true } uucore = { workspace = true, default-features = true, features = [ diff --git a/src/uu/install/src/install.rs b/src/uu/install/src/install.rs index f19f8aca249..fbac8d857dd 100644 --- a/src/uu/install/src/install.rs +++ b/src/uu/install/src/install.rs @@ -10,6 +10,7 @@ mod mode; use clap::{Arg, ArgAction, ArgMatches, Command}; use file_diff::diff; use filetime::{FileTime, set_file_times}; +use rustix::process::{getegid, geteuid}; #[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] use selinux::SecurityContext; use std::ffi::OsString; @@ -27,7 +28,6 @@ use uucore::entries::{grp2gid, usr2uid}; use uucore::error::{FromIo, UError, UResult, UUsageError}; use uucore::fs::dir_strip_dot_for_creation; use uucore::perms::{Verbosity, VerbosityLevel, wrap_chown}; -use uucore::process::{getegid, geteuid}; #[cfg(unix)] use uucore::safe_traversal::{DirFd, SymlinkBehavior, create_dir_all_safe}; #[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] @@ -1165,7 +1165,7 @@ fn needs_copy_for_ownership(to: &Path, to_meta: &fs::Metadata) -> bool { use std::os::unix::fs::MetadataExt; // Check if the destination file's owner differs from the effective user ID - if to_meta.uid() != geteuid() { + if to_meta.uid() != geteuid().as_raw() { return true; } @@ -1177,7 +1177,7 @@ fn needs_copy_for_ownership(to: &Path, to_meta: &fs::Metadata) -> bool { .parent() .and_then(|parent| metadata(parent).ok()) .filter(|parent_meta| parent_meta.mode() & 0o2000 != 0) - .map_or(getegid(), |parent_meta| parent_meta.gid()); + .map_or(getegid().as_raw(), |parent_meta| parent_meta.gid()); to_meta.gid() != expected_gid } diff --git a/src/uu/test/Cargo.toml b/src/uu/test/Cargo.toml index 4d9e58ba54f..836b3aa194e 100644 --- a/src/uu/test/Cargo.toml +++ b/src/uu/test/Cargo.toml @@ -25,6 +25,9 @@ libc = { workspace = true } thiserror = { workspace = true } uucore = { workspace = true, features = ["process"] } +[target.'cfg(not(windows))'.dependencies] +rustix = { workspace = true } + [dev-dependencies] tempfile = { workspace = true } diff --git a/src/uu/test/src/test.rs b/src/uu/test/src/test.rs index 38c0072e15e..7a81458e2bc 100644 --- a/src/uu/test/src/test.rs +++ b/src/uu/test/src/test.rs @@ -11,6 +11,8 @@ mod parser; use clap::Command; use error::{ParseError, ParseResult}; use parser::{Operator, Symbol, UnaryOperator, parse}; +#[cfg(not(windows))] +use rustix::process::{getegid, geteuid}; use std::ffi::{OsStr, OsString}; use std::fs; #[cfg(unix)] @@ -18,8 +20,6 @@ use std::os::unix::fs::MetadataExt; use uucore::display::Quotable; use uucore::error::{UResult, USimpleError}; use uucore::format_usage; -#[cfg(not(windows))] -use uucore::process::{getegid, geteuid}; use uucore::translate; @@ -278,9 +278,9 @@ fn path(path: &OsStr, condition: &PathCondition) -> bool { } let perm = |metadata: Metadata, p: Permission| { - if geteuid() == metadata.uid() { + if geteuid().as_raw() == metadata.uid() { metadata.mode() & ((p as u32) << 6) != 0 - } else if getegid() == metadata.gid() { + } else if getegid().as_raw() == metadata.gid() { metadata.mode() & ((p as u32) << 3) != 0 } else { metadata.mode() & (p as u32) != 0 @@ -309,10 +309,10 @@ fn path(path: &OsStr, condition: &PathCondition) -> bool { } PathCondition::Regular => file_type.is_file(), PathCondition::GroupIdFlag => metadata.mode() & S_ISGID != 0, - PathCondition::GroupOwns => metadata.gid() == getegid(), + PathCondition::GroupOwns => metadata.gid() == getegid().as_raw(), PathCondition::SymLink => metadata.file_type().is_symlink(), PathCondition::Sticky => metadata.mode() & S_ISVTX != 0, - PathCondition::UserOwns => metadata.uid() == geteuid(), + PathCondition::UserOwns => metadata.uid() == geteuid().as_raw(), PathCondition::Fifo => file_type.is_fifo(), PathCondition::Readable => perm(metadata, Permission::Read), PathCondition::Socket => file_type.is_socket(), diff --git a/src/uu/whoami/Cargo.toml b/src/uu/whoami/Cargo.toml index 5613c5f6e18..2b03c1ef83c 100644 --- a/src/uu/whoami/Cargo.toml +++ b/src/uu/whoami/Cargo.toml @@ -31,6 +31,9 @@ windows-sys = { workspace = true, features = [ "Win32_Foundation", ] } +[target.'cfg(unix)'.dependencies] +rustix = { workspace = true } + [[bin]] name = "whoami" path = "src/main.rs" diff --git a/src/uu/whoami/src/platform/unix.rs b/src/uu/whoami/src/platform/unix.rs index 9310a6de0a2..6970ddd6e31 100644 --- a/src/uu/whoami/src/platform/unix.rs +++ b/src/uu/whoami/src/platform/unix.rs @@ -6,10 +6,10 @@ use std::ffi::OsString; use std::io; +use rustix::process::geteuid; use uucore::entries::uid2usr; -use uucore::process::geteuid; pub fn get_username() -> io::Result { // uid2usr should arguably return an OsString but currently doesn't - uid2usr(geteuid()).map(Into::into) + uid2usr(geteuid().as_raw()).map(Into::into) } diff --git a/src/uucore/src/lib/features/entries.rs b/src/uucore/src/lib/features/entries.rs index bf2766d9cef..60c44b2d66e 100644 --- a/src/uucore/src/lib/features/entries.rs +++ b/src/uucore/src/lib/features/entries.rs @@ -83,7 +83,7 @@ unsafe extern "C" { pub fn get_groups_gnu(arg_id: Option) -> IOResult> { let groups = rustix::process::getgroups() .map(|g| g.into_iter().map(rustix::fs::Gid::as_raw).collect())?; - let egid = arg_id.unwrap_or_else(crate::features::process::getegid); + let egid = arg_id.unwrap_or_else(|| rustix::process::getegid().as_raw()); Ok(sort_groups(groups, egid)) } diff --git a/src/uucore/src/lib/features/proc_info.rs b/src/uucore/src/lib/features/proc_info.rs index 0d99cb8a8b9..b6c49ac9d5a 100644 --- a/src/uucore/src/lib/features/proc_info.rs +++ b/src/uucore/src/lib/features/proc_info.rs @@ -514,9 +514,15 @@ mod tests { PathBuf::from_str(&format!("/proc/{}", current_pid())).unwrap(), ) .unwrap(); - assert_eq!(pid_entry.uid().unwrap(), crate::process::getuid()); - assert_eq!(pid_entry.euid().unwrap(), crate::process::geteuid()); - assert_eq!(pid_entry.gid().unwrap(), crate::process::getgid()); - assert_eq!(pid_entry.egid().unwrap(), crate::process::getegid()); + assert_eq!(pid_entry.uid().unwrap(), rustix::process::getuid().as_raw()); + assert_eq!( + pid_entry.euid().unwrap(), + rustix::process::geteuid().as_raw() + ); + assert_eq!(pid_entry.gid().unwrap(), rustix::process::getgid().as_raw()); + assert_eq!( + pid_entry.egid().unwrap(), + rustix::process::getegid().as_raw() + ); } } diff --git a/src/uucore/src/lib/features/process.rs b/src/uucore/src/lib/features/process.rs index 06274eb59f9..68f2b50c2e0 100644 --- a/src/uucore/src/lib/features/process.rs +++ b/src/uucore/src/lib/features/process.rs @@ -3,13 +3,7 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -// spell-checker:ignore (vars) cvar exitstatus cmdline kworker getsid getpid -// spell-checker:ignore (sys/unix) WIFSIGNALED ESRCH -// spell-checker:ignore pgrep pwait snice getpgrp - -use libc::{gid_t, pid_t, uid_t}; -#[cfg(not(target_os = "redox"))] -use nix::errno::Errno; +use libc::pid_t; use nix::sys::signal::{self as nix_signal, SigHandler, Signal}; use nix::unistd::Pid; use std::io; @@ -20,61 +14,6 @@ use std::sync::atomic::AtomicBool; use std::thread; use std::time::{Duration, Instant}; -/// `geteuid()` returns the effective user ID of the calling process. -pub fn geteuid() -> uid_t { - nix::unistd::geteuid().as_raw() -} - -/// `getpgrp()` returns the process group ID of the calling process. -/// It is a trivial wrapper over nix::unistd::getpgrp. -pub fn getpgrp() -> pid_t { - nix::unistd::getpgrp().as_raw() -} - -/// `getegid()` returns the effective group ID of the calling process. -pub fn getegid() -> gid_t { - nix::unistd::getegid().as_raw() -} - -/// `getgid()` returns the real group ID of the calling process. -pub fn getgid() -> gid_t { - nix::unistd::getgid().as_raw() -} - -/// `getuid()` returns the real user ID of the calling process. -pub fn getuid() -> uid_t { - rustix::process::getuid().as_raw() -} - -/// `getpid()` returns the pid of the calling process. -pub fn getpid() -> pid_t { - nix::unistd::getpid().as_raw() -} - -/// `getsid()` returns the session ID of the process with process ID pid. -/// -/// If pid is 0, getsid() returns the session ID of the calling process. -/// -/// # Error -/// -/// - [Errno::EPERM] A process with process ID pid exists, but it is not in the same session as the calling process, and the implementation considers this an error. -/// - [Errno::ESRCH] No process with process ID pid was found. -/// -/// -/// # Platform -/// -/// This function only support standard POSIX implementation platform, -/// so some system such as redox doesn't supported. -#[cfg(not(target_os = "redox"))] -pub fn getsid(pid: i32) -> Result { - let pid = if pid == 0 { - None - } else { - Some(Pid::from_raw(pid)) - }; - nix::unistd::getsid(pid).map(Pid::as_raw) -} - /// Missing methods for Child objects pub trait ChildExt { /// Send a signal to a Child process. @@ -166,25 +105,3 @@ impl ChildExt for Child { Ok(None) } } - -#[cfg(test)] -mod tests { - #[test] - #[cfg(not(target_os = "redox"))] - fn test_getsid() { - use super::{getpid, getsid}; - - assert_eq!( - getsid(getpid()).expect("getsid(getpid)"), - // zero is a special value for SID. - // https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsid.html - getsid(0).expect("getsid(0)") - ); - - // SID never be 0. - assert!(getsid(getpid()).expect("getsid(getpid)") > 0); - - // This might caused tests failure but the probability is low. - assert!(getsid(999_999).is_err()); - } -} diff --git a/tests/by-util/test_chgrp.rs b/tests/by-util/test_chgrp.rs index eac0ea048ff..28c550ac6c5 100644 --- a/tests/by-util/test_chgrp.rs +++ b/tests/by-util/test_chgrp.rs @@ -4,11 +4,11 @@ // file that was distributed with this source code. // spell-checker:ignore (words) nosuchgroup groupname +use rustix::process::getegid; #[cfg(not(target_vendor = "apple"))] use rustix::process::getgroups; #[cfg(target_os = "linux")] use std::os::unix::ffi::OsStringExt; -use uucore::process::getegid; use uutests::{at_and_ucmd, new_ucmd}; #[cfg(not(target_vendor = "apple"))] use uutests::{util::TestScenario, util_name}; @@ -63,7 +63,7 @@ fn test_invalid_group() { #[test] fn test_error_1() { - if getegid() != 0 { + if getegid().as_raw() != 0 { new_ucmd!().arg("bin").arg(DIR).fails().stderr_contains( // linux fails with "Operation not permitted (os error 1)" // because of insufficient permissions, @@ -76,7 +76,7 @@ fn test_error_1() { #[test] fn test_fail_silently() { - if getegid() != 0 { + if getegid().as_raw() != 0 { for opt in ["-f", "--silent", "--quiet", "--sil", "--qui"] { new_ucmd!() .arg(opt) @@ -201,7 +201,7 @@ fn test_reference() { // skip for root or MS-WSL // * MS-WSL is bugged (as of 2019-12-25), allowing non-root accounts su-level privileges for `chgrp` // * for MS-WSL, succeeds and stdout == 'group of /etc retained as root' - if !(getegid() == 0 || uucore::os::is_wsl_1()) { + if !(getegid().as_raw() == 0 || uucore::os::is_wsl_1()) { new_ucmd!() .arg("-v") .arg("--reference=/etc/passwd") @@ -267,7 +267,7 @@ fn test_missing_files() { #[test] #[cfg(target_os = "linux")] fn test_big_p() { - if getegid() != 0 { + if getegid().as_raw() != 0 { new_ucmd!() .arg("-RP") .arg("bin") @@ -282,7 +282,7 @@ fn test_big_p() { #[test] #[cfg(any(target_os = "linux", target_os = "android"))] fn test_big_h() { - if getegid() != 0 { + if getegid().as_raw() != 0 { assert!( new_ucmd!() .arg("-RH") @@ -611,7 +611,7 @@ fn test_chgrp_non_utf8_paths() { std::fs::write(at.plus(&filename), b"test content").unwrap(); // Get current user's primary group - let current_gid = getegid(); + let current_gid = getegid().as_raw(); ucmd.arg(current_gid.to_string()).arg(&filename).succeeds(); } @@ -627,7 +627,7 @@ fn test_chgrp_recursive_on_file() { at.touch("regular_file"); - let current_gid = getegid(); + let current_gid = getegid().as_raw(); ucmd.arg("-R") .arg(current_gid.to_string()) @@ -645,7 +645,7 @@ fn test_chgrp_recursive_on_file() { fn test_chgrp_exit_code_not_being_overwritten_by_last_file() { use std::os::unix::prelude::PermissionsExt; - let current_gid = getegid(); + let current_gid = getegid().as_raw(); let (at, mut ucmd) = at_and_ucmd!(); at.mkdir("dir"); at.mkdir("dir/a"); diff --git a/tests/by-util/test_install.rs b/tests/by-util/test_install.rs index afb10eaa250..c7387402bd8 100644 --- a/tests/by-util/test_install.rs +++ b/tests/by-util/test_install.rs @@ -6,6 +6,7 @@ #[cfg(not(target_os = "openbsd"))] use filetime::FileTime; +use rustix::process::{getegid, geteuid}; use std::fs; #[cfg(target_os = "linux")] use std::os::unix::ffi::OsStringExt; @@ -14,7 +15,6 @@ use std::os::unix::fs::{MetadataExt, PermissionsExt}; use std::process; #[cfg(any(target_os = "linux", target_os = "android"))] use std::thread::sleep; -use uucore::process::{getegid, geteuid}; #[cfg(feature = "feat_selinux")] use uucore::selinux::get_getfattr_output; use uutests::at_and_ucmd; @@ -386,7 +386,7 @@ fn test_install_target_new_file_with_group() { let (at, mut ucmd) = at_and_ucmd!(); let file = "file"; let dir = "target_dir"; - let gid = getegid(); + let gid = getegid().as_raw(); at.touch(file); at.mkdir(dir); @@ -413,7 +413,7 @@ fn test_install_target_new_file_with_owner() { let (at, mut ucmd) = at_and_ucmd!(); let file = "file"; let dir = "target_dir"; - let uid = geteuid(); + let uid = geteuid().as_raw(); at.touch(file); at.mkdir(dir); @@ -527,8 +527,8 @@ fn test_multiple_mode_arguments_override_not_error() { let dir = "source_dir"; let file = "source_file"; - let gid = getegid(); - let uid = geteuid(); + let gid = getegid().as_raw(); + let uid = geteuid().as_raw(); at.touch(file); at.mkdir(dir); @@ -2517,7 +2517,7 @@ fn test_install_non_utf8_paths() { #[test] fn test_install_unprivileged_option_u_skips_chown() { // This test only makes sense when not running as root. - if geteuid() == 0 { + if geteuid().as_raw() == 0 { return; } @@ -2543,7 +2543,7 @@ fn test_install_unprivileged_option_u_skips_chown() { .no_stderr(); assert!(at.file_exists(dst_ok)); - assert_eq!(at.metadata(dst_ok).uid(), geteuid()); + assert_eq!(at.metadata(dst_ok).uid(), geteuid().as_raw()); } #[test] diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index 0893c3ffb68..6e01566b4d1 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -6230,7 +6230,7 @@ fn test_acl_padding_not_inflated() { let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; - let uid = uucore::process::getuid(); + let uid = rustix::process::getuid().as_raw(); let names = ["file1", "file2", "file3", "file4", "file5"]; for name in &names { at.touch(name);