Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/uu/install/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
6 changes: 3 additions & 3 deletions src/uu/install/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")))]
Expand Down Expand Up @@ -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;
}

Expand All @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions src/uu/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
12 changes: 6 additions & 6 deletions src/uu/test/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ 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)]
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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down
3 changes: 3 additions & 0 deletions src/uu/whoami/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 2 additions & 2 deletions src/uu/whoami/src/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OsString> {
// uid2usr should arguably return an OsString but currently doesn't
uid2usr(geteuid()).map(Into::into)
uid2usr(geteuid().as_raw()).map(Into::into)
}
2 changes: 1 addition & 1 deletion src/uucore/src/lib/features/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ unsafe extern "C" {
pub fn get_groups_gnu(arg_id: Option<u32>) -> IOResult<Vec<rustix::process::RawGid>> {
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))
}

Expand Down
14 changes: 10 additions & 4 deletions src/uucore/src/lib/features/proc_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
85 changes: 1 addition & 84 deletions src/uucore/src/lib/features/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<pid_t, Errno> {
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.
Expand Down Expand Up @@ -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());
}
}
18 changes: 9 additions & 9 deletions tests/by-util/test_chgrp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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();
}
Expand All @@ -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())
Expand All @@ -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");
Expand Down
Loading
Loading