Skip to content

Commit 2c77483

Browse files
committed
uucore: remove process functions
1 parent c90f1ea commit 2c77483

13 files changed

Lines changed: 41 additions & 113 deletions

File tree

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fuzz/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uu/install/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ path = "src/install.rs"
2121
clap = { workspace = true }
2222
filetime = { workspace = true }
2323
file_diff = { workspace = true }
24+
rustix = { workspace = true }
2425
selinux = { workspace = true, optional = true }
2526
thiserror = { workspace = true }
2627
uucore = { workspace = true, default-features = true, features = [

src/uu/install/src/install.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mod mode;
1010
use clap::{Arg, ArgAction, ArgMatches, Command};
1111
use file_diff::diff;
1212
use filetime::{FileTime, set_file_times};
13+
use rustix::process::{getegid, geteuid};
1314
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
1415
use selinux::SecurityContext;
1516
use std::ffi::OsString;
@@ -27,7 +28,6 @@ use uucore::entries::{grp2gid, usr2uid};
2728
use uucore::error::{FromIo, UError, UResult, UUsageError};
2829
use uucore::fs::dir_strip_dot_for_creation;
2930
use uucore::perms::{Verbosity, VerbosityLevel, wrap_chown};
30-
use uucore::process::{getegid, geteuid};
3131
#[cfg(unix)]
3232
use uucore::safe_traversal::{DirFd, SymlinkBehavior, create_dir_all_safe};
3333
#[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 {
11651165
use std::os::unix::fs::MetadataExt;
11661166

11671167
// Check if the destination file's owner differs from the effective user ID
1168-
if to_meta.uid() != geteuid() {
1168+
if to_meta.uid() != geteuid().as_raw() {
11691169
return true;
11701170
}
11711171

@@ -1177,7 +1177,7 @@ fn needs_copy_for_ownership(to: &Path, to_meta: &fs::Metadata) -> bool {
11771177
.parent()
11781178
.and_then(|parent| metadata(parent).ok())
11791179
.filter(|parent_meta| parent_meta.mode() & 0o2000 != 0)
1180-
.map_or(getegid(), |parent_meta| parent_meta.gid());
1180+
.map_or(getegid().as_raw(), |parent_meta| parent_meta.gid());
11811181

11821182
to_meta.gid() != expected_gid
11831183
}

src/uu/test/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ libc = { workspace = true }
2525
thiserror = { workspace = true }
2626
uucore = { workspace = true, features = ["process"] }
2727

28+
[target.'cfg(not(windows))'.dependencies]
29+
rustix = { workspace = true }
30+
2831
[dev-dependencies]
2932
tempfile = { workspace = true }
3033

src/uu/test/src/test.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ mod parser;
1111
use clap::Command;
1212
use error::{ParseError, ParseResult};
1313
use parser::{Operator, Symbol, UnaryOperator, parse};
14+
#[cfg(not(windows))]
15+
use rustix::process::{getegid, geteuid};
1416
use std::ffi::{OsStr, OsString};
1517
use std::fs;
1618
#[cfg(unix)]
1719
use std::os::unix::fs::MetadataExt;
1820
use uucore::display::Quotable;
1921
use uucore::error::{UResult, USimpleError};
2022
use uucore::format_usage;
21-
#[cfg(not(windows))]
22-
use uucore::process::{getegid, geteuid};
2323

2424
use uucore::translate;
2525

@@ -278,9 +278,9 @@ fn path(path: &OsStr, condition: &PathCondition) -> bool {
278278
}
279279

280280
let perm = |metadata: Metadata, p: Permission| {
281-
if geteuid() == metadata.uid() {
281+
if geteuid().as_raw() == metadata.uid() {
282282
metadata.mode() & ((p as u32) << 6) != 0
283-
} else if getegid() == metadata.gid() {
283+
} else if getegid().as_raw() == metadata.gid() {
284284
metadata.mode() & ((p as u32) << 3) != 0
285285
} else {
286286
metadata.mode() & (p as u32) != 0
@@ -309,10 +309,10 @@ fn path(path: &OsStr, condition: &PathCondition) -> bool {
309309
}
310310
PathCondition::Regular => file_type.is_file(),
311311
PathCondition::GroupIdFlag => metadata.mode() & S_ISGID != 0,
312-
PathCondition::GroupOwns => metadata.gid() == getegid(),
312+
PathCondition::GroupOwns => metadata.gid() == getegid().as_raw(),
313313
PathCondition::SymLink => metadata.file_type().is_symlink(),
314314
PathCondition::Sticky => metadata.mode() & S_ISVTX != 0,
315-
PathCondition::UserOwns => metadata.uid() == geteuid(),
315+
PathCondition::UserOwns => metadata.uid() == geteuid().as_raw(),
316316
PathCondition::Fifo => file_type.is_fifo(),
317317
PathCondition::Readable => perm(metadata, Permission::Read),
318318
PathCondition::Socket => file_type.is_socket(),

src/uu/whoami/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ windows-sys = { workspace = true, features = [
3131
"Win32_Foundation",
3232
] }
3333

34+
[target.'cfg(unix)'.dependencies]
35+
rustix = { workspace = true }
36+
3437
[[bin]]
3538
name = "whoami"
3639
path = "src/main.rs"

src/uu/whoami/src/platform/unix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
use std::ffi::OsString;
77
use std::io;
88

9+
use rustix::process::geteuid;
910
use uucore::entries::uid2usr;
10-
use uucore::process::geteuid;
1111

1212
pub fn get_username() -> io::Result<OsString> {
1313
// uid2usr should arguably return an OsString but currently doesn't
14-
uid2usr(geteuid()).map(Into::into)
14+
uid2usr(geteuid().as_raw()).map(Into::into)
1515
}

src/uucore/src/lib/features/entries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ unsafe extern "C" {
8383
pub fn get_groups_gnu(arg_id: Option<u32>) -> IOResult<Vec<rustix::process::RawGid>> {
8484
let groups = rustix::process::getgroups()
8585
.map(|g| g.into_iter().map(rustix::fs::Gid::as_raw).collect())?;
86-
let egid = arg_id.unwrap_or_else(crate::features::process::getegid);
86+
let egid = arg_id.unwrap_or_else(|| rustix::process::getegid().as_raw());
8787
Ok(sort_groups(groups, egid))
8888
}
8989

src/uucore/src/lib/features/process.rs

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
// spell-checker:ignore (vars) cvar exitstatus cmdline kworker getsid getpid
7-
// spell-checker:ignore (sys/unix) WIFSIGNALED ESRCH
8-
// spell-checker:ignore pgrep pwait snice getpgrp
9-
10-
use libc::{gid_t, pid_t, uid_t};
11-
#[cfg(not(target_os = "redox"))]
12-
use nix::errno::Errno;
6+
use libc::pid_t;
137
use nix::sys::signal::{self as nix_signal, SigHandler, Signal};
148
use nix::unistd::Pid;
159
use std::io;
@@ -20,61 +14,6 @@ use std::sync::atomic::AtomicBool;
2014
use std::thread;
2115
use std::time::{Duration, Instant};
2216

23-
/// `geteuid()` returns the effective user ID of the calling process.
24-
pub fn geteuid() -> uid_t {
25-
nix::unistd::geteuid().as_raw()
26-
}
27-
28-
/// `getpgrp()` returns the process group ID of the calling process.
29-
/// It is a trivial wrapper over nix::unistd::getpgrp.
30-
pub fn getpgrp() -> pid_t {
31-
nix::unistd::getpgrp().as_raw()
32-
}
33-
34-
/// `getegid()` returns the effective group ID of the calling process.
35-
pub fn getegid() -> gid_t {
36-
nix::unistd::getegid().as_raw()
37-
}
38-
39-
/// `getgid()` returns the real group ID of the calling process.
40-
pub fn getgid() -> gid_t {
41-
nix::unistd::getgid().as_raw()
42-
}
43-
44-
/// `getuid()` returns the real user ID of the calling process.
45-
pub fn getuid() -> uid_t {
46-
rustix::process::getuid().as_raw()
47-
}
48-
49-
/// `getpid()` returns the pid of the calling process.
50-
pub fn getpid() -> pid_t {
51-
nix::unistd::getpid().as_raw()
52-
}
53-
54-
/// `getsid()` returns the session ID of the process with process ID pid.
55-
///
56-
/// If pid is 0, getsid() returns the session ID of the calling process.
57-
///
58-
/// # Error
59-
///
60-
/// - [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.
61-
/// - [Errno::ESRCH] No process with process ID pid was found.
62-
///
63-
///
64-
/// # Platform
65-
///
66-
/// This function only support standard POSIX implementation platform,
67-
/// so some system such as redox doesn't supported.
68-
#[cfg(not(target_os = "redox"))]
69-
pub fn getsid(pid: i32) -> Result<pid_t, Errno> {
70-
let pid = if pid == 0 {
71-
None
72-
} else {
73-
Some(Pid::from_raw(pid))
74-
};
75-
nix::unistd::getsid(pid).map(Pid::as_raw)
76-
}
77-
7817
/// Missing methods for Child objects
7918
pub trait ChildExt {
8019
/// Send a signal to a Child process.
@@ -166,25 +105,3 @@ impl ChildExt for Child {
166105
Ok(None)
167106
}
168107
}
169-
170-
#[cfg(test)]
171-
mod tests {
172-
use super::*;
173-
174-
#[test]
175-
#[cfg(not(target_os = "redox"))]
176-
fn test_getsid() {
177-
assert_eq!(
178-
getsid(getpid()).expect("getsid(getpid)"),
179-
// zero is a special value for SID.
180-
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsid.html
181-
getsid(0).expect("getsid(0)")
182-
);
183-
184-
// SID never be 0.
185-
assert!(getsid(getpid()).expect("getsid(getpid)") > 0);
186-
187-
// This might caused tests failure but the probability is low.
188-
assert!(getsid(999_999).is_err());
189-
}
190-
}

0 commit comments

Comments
 (0)