Skip to content

Commit 7aa5c45

Browse files
committed
id: replace nix by rustix
1 parent 0ddd6f4 commit 7aa5c45

4 files changed

Lines changed: 19 additions & 19 deletions

File tree

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/id/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ doctest = false
2121

2222
[dependencies]
2323
clap = { workspace = true }
24+
rustix = { workspace = true }
2425
uucore = { workspace = true, features = ["entries", "process"] }
2526
selinux = { workspace = true, optional = true }
2627
fluent = { workspace = true }

src/uu/id/src/id.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ use uucore::entries::{self, Group, Locate, Passwd};
4141
use uucore::error::UResult;
4242
use uucore::error::{USimpleError, set_exit_code};
4343
pub use uucore::libc;
44-
use uucore::libc::{getlogin, uid_t};
44+
use uucore::libc::getlogin;
4545
use uucore::line_ending::LineEnding;
4646
use uucore::translate;
4747

48-
use uucore::process::{getegid, geteuid, getgid, getuid};
48+
use rustix::process::{Uid, getegid, geteuid, getgid, getuid};
4949
use uucore::{format_usage, show_error};
5050

5151
macro_rules! cstr2cow {
@@ -245,7 +245,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
245245
// GNU's `id` does not support the flags: -p/-P/-A.
246246
if matches.get_flag(options::OPT_PASSWORD) {
247247
// BSD's `id` ignores all but the first specified user
248-
pline(possible_pw.as_ref().map(|v| v.uid))?;
248+
pline(possible_pw.as_ref().map(|v| Uid::from_raw(v.uid)))?;
249249
return Ok(());
250250
}
251251
if matches.get_flag(options::OPT_HUMAN_READABLE) {
@@ -263,18 +263,18 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
263263
{
264264
let use_effective = !state.rflag && (state.uflag || state.gflag || state.gsflag);
265265
if use_effective {
266-
(geteuid(), getegid())
266+
(geteuid().as_raw(), getegid().as_raw())
267267
} else {
268-
(getuid(), getgid())
268+
(getuid().as_raw(), getgid().as_raw())
269269
}
270270
},
271271
|p| (p.uid, p.gid),
272272
);
273273
state.ids = Some(Ids {
274274
uid,
275275
gid,
276-
euid: geteuid(),
277-
egid: getegid(),
276+
euid: geteuid().as_raw(),
277+
egid: getegid().as_raw(),
278278
});
279279

280280
if state.gflag {
@@ -499,7 +499,7 @@ fn pretty(possible_pw: Option<Passwd>) -> io::Result<()> {
499499
)?;
500500
} else {
501501
let login = cstr2cow!(getlogin().cast_const());
502-
let uid = getuid();
502+
let uid = getuid().as_raw();
503503
if let Ok(p) = Passwd::locate(uid) {
504504
if let Some(user_name) = login {
505505
writeln!(lock, "{}\t{user_name}", translate!("id-output-login"))?;
@@ -509,7 +509,7 @@ fn pretty(possible_pw: Option<Passwd>) -> io::Result<()> {
509509
writeln!(lock, "{}\t{uid}", translate!("id-output-uid"))?;
510510
}
511511

512-
let euid = geteuid();
512+
let euid = geteuid().as_raw();
513513
if euid != uid {
514514
if let Ok(p) = Passwd::locate(euid) {
515515
writeln!(lock, "{}\t{}", translate!("id-output-euid"), p.name)?;
@@ -518,8 +518,8 @@ fn pretty(possible_pw: Option<Passwd>) -> io::Result<()> {
518518
}
519519
}
520520

521-
let rgid = getgid();
522-
let egid = getegid();
521+
let rgid = getgid().as_raw();
522+
let egid = getegid().as_raw();
523523
if egid != rgid {
524524
if let Ok(g) = Group::locate(rgid) {
525525
writeln!(lock, "{}\t{}", translate!("id-output-rgid"), g.name)?;
@@ -544,9 +544,9 @@ fn pretty(possible_pw: Option<Passwd>) -> io::Result<()> {
544544
}
545545

546546
#[cfg(any(target_vendor = "apple", target_os = "freebsd"))]
547-
fn pline(possible_uid: Option<uid_t>) -> io::Result<()> {
547+
fn pline(possible_uid: Option<Uid>) -> io::Result<()> {
548548
let uid = possible_uid.unwrap_or_else(getuid);
549-
let pw = Passwd::locate(uid)?;
549+
let pw = Passwd::locate(uid.as_raw())?;
550550

551551
writeln!(
552552
io::stdout().lock(),
@@ -571,9 +571,9 @@ fn pline(possible_uid: Option<uid_t>) -> io::Result<()> {
571571
target_os = "cygwin",
572572
target_os = "netbsd"
573573
))]
574-
fn pline(possible_uid: Option<uid_t>) -> io::Result<()> {
574+
fn pline(possible_uid: Option<Uid>) -> io::Result<()> {
575575
let uid = possible_uid.unwrap_or_else(getuid);
576-
let pw = Passwd::locate(uid)?;
576+
let pw = Passwd::locate(uid.as_raw())?;
577577

578578
writeln!(
579579
io::stdout().lock(),

tests/by-util/test_id.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,23 +483,21 @@ fn test_id_pretty_print_password_record() {
483483
#[test]
484484
#[cfg(all(feature = "chmod", feature = "chown"))]
485485
fn test_id_pretty_print_suid_binary() {
486-
use uucore::process::{getgid, getuid};
487-
488486
if let Some(suid_coreutils_path) = create_root_owned_suid_coreutils_binary() {
489487
let result = TestScenario::new(util_name!())
490488
.cmd(suid_coreutils_path.to_str().unwrap())
491489
.args(&[util_name!(), "-p"])
492490
.succeeds();
493491

494492
// The `euid` line should be present only if the real UID does not belong to `root`
495-
if getuid() == 0 {
493+
if rustix::process::getuid().is_root() {
496494
result.stdout_does_not_contain("euid\t");
497495
} else {
498496
result.stdout_contains_line("euid\troot");
499497
}
500498

501499
// The `rgid` line should be present only if the real GID does not belong to `root`
502-
if getgid() == 0 {
500+
if rustix::process::getgid().is_root() {
503501
result.stdout_does_not_contain("rgid\t");
504502
} else {
505503
result.stdout_contains("rgid\t");

0 commit comments

Comments
 (0)