Skip to content

Commit b9e9ea4

Browse files
committed
use rustix process functions
1 parent c0b9017 commit b9e9ea4

7 files changed

Lines changed: 28 additions & 14 deletions

File tree

Cargo.lock

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

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ xattr = { workspace = true }
136136
[target.'cfg(any(target_os = "linux", target_os = "android"))'.dev-dependencies]
137137
rlimit = "0.11.0"
138138

139+
[target.'cfg(target_os = "linux")'.dependencies]
140+
rustix = { workspace = true }
141+
139142
[build-dependencies]
140143
phf_codegen = { workspace = true }
141144

src/uu/pgrep/src/process.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ impl ProcessInformation {
491491

492492
pub fn current_process_info() -> Result<ProcessInformation, io::Error> {
493493
#[cfg(target_os = "linux")]
494-
let pid = uucore::process::getpid();
494+
let pid = rustix::process::getpid().as_raw_pid();
495495
#[cfg(not(target_os = "linux"))]
496496
let pid = 0; // dummy
497497

@@ -798,9 +798,9 @@ pub fn walk_threads() -> impl Iterator<Item = ProcessInformation> {
798798
mod tests {
799799
use super::*;
800800
#[cfg(target_os = "linux")]
801-
use std::collections::HashSet;
801+
use rustix::process::getpid;
802802
#[cfg(target_os = "linux")]
803-
use uucore::process::getpid;
803+
use std::collections::HashSet;
804804

805805
#[test]
806806
#[cfg(target_os = "linux")]
@@ -870,7 +870,7 @@ unknown /dev/tty 4 1-63 console"#;
870870
#[test]
871871
#[cfg(target_os = "linux")]
872872
fn test_walk_pid() {
873-
let find = walk_process().find(|it| it.pid == getpid() as usize);
873+
let find = walk_process().find(|it| it.pid == getpid().as_raw_pid() as usize);
874874

875875
assert!(find.is_some());
876876
}
@@ -963,10 +963,16 @@ unknown /dev/tty 4 1-63 console"#;
963963
#[cfg(target_os = "linux")]
964964
fn test_uid_gid() {
965965
let mut pid_entry = ProcessInformation::current_process_info().unwrap();
966-
assert_eq!(pid_entry.uid().unwrap(), uucore::process::getuid());
967-
assert_eq!(pid_entry.euid().unwrap(), uucore::process::geteuid());
968-
assert_eq!(pid_entry.gid().unwrap(), uucore::process::getgid());
969-
assert_eq!(pid_entry.egid().unwrap(), uucore::process::getegid());
966+
assert_eq!(pid_entry.uid().unwrap(), rustix::process::getuid().as_raw());
967+
assert_eq!(
968+
pid_entry.euid().unwrap(),
969+
rustix::process::geteuid().as_raw()
970+
);
971+
assert_eq!(pid_entry.gid().unwrap(), rustix::process::getgid().as_raw());
972+
assert_eq!(
973+
pid_entry.egid().unwrap(),
974+
rustix::process::getegid().as_raw()
975+
);
970976
}
971977

972978
#[test]

src/uu/pidof/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ uucore = { workspace = true, features = ["process"] }
1818
clap = { workspace = true }
1919
uu_pgrep = { path = "../pgrep" }
2020

21+
[target.'cfg(unix)'.dependencies]
22+
rustix = { workspace = true }
23+
2124
[lib]
2225
path = "src/pidof.rs"
2326

src/uu/pidof/src/pidof.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
use std::path::PathBuf;
77

88
use clap::{crate_version, Arg, ArgAction, ArgMatches, Command};
9+
#[cfg(unix)]
10+
use rustix::process::geteuid;
911
use uu_pgrep::process::{walk_process, ProcessInformation};
1012
use uucore::error::UResult;
11-
#[cfg(unix)]
12-
use uucore::process::geteuid;
1313

1414
#[uucore::main]
1515
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
@@ -95,7 +95,7 @@ fn collect_matched_pids(matches: &ArgMatches) -> Vec<usize> {
9595

9696
// Original pidof silently ignores the check-root option if the user is not root.
9797
#[cfg(unix)]
98-
let check_root = matches.get_flag("check-root") && geteuid() == 0;
98+
let check_root = matches.get_flag("check-root") && geteuid().as_raw() == 0;
9999
#[cfg(not(unix))]
100100
let check_root = false;
101101
let our_root = ProcessInformation::current_process_info()

tests/by-util/test_pgrep.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ fn test_invalid_group_name() {
437437
fn test_current_user() {
438438
new_ucmd!()
439439
.arg("-U")
440-
.arg(uucore::process::getuid().to_string())
440+
.arg(rustix::process::getuid().as_raw().to_string())
441441
.succeeds();
442442
}
443443

tests/by-util/test_ps.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use regex::Regex;
88
use uutests::new_ucmd;
99

1010
#[cfg(target_os = "linux")]
11-
use uucore::process::geteuid;
11+
use rustix::process::geteuid;
1212

1313
#[test]
1414
#[cfg(target_os = "linux")]
@@ -402,7 +402,7 @@ fn test_combined_selection_criteria() {
402402
"--pid",
403403
"1",
404404
"--user",
405-
&geteuid().to_string(),
405+
&geteuid().as_raw().to_string(),
406406
"--no-headers",
407407
"-o",
408408
"pid",

0 commit comments

Comments
 (0)