From 673d322faa41abb5fc644c2edb0357f176bc3e48 Mon Sep 17 00:00:00 2001 From: yushuoqi Date: Mon, 9 Feb 2026 17:22:39 +0800 Subject: [PATCH] ps: remove libc in ps Handling unsafe uucore::libc::getsid functions with Rustix --- Cargo.lock | 2 +- src/uu/ps/Cargo.toml | 4 ++-- src/uu/ps/src/process_selection.rs | 15 +++++---------- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 25c2f3dc..b2182faf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2277,8 +2277,8 @@ version = "0.0.1" dependencies = [ "chrono", "clap", - "nix 0.30.1", "prettytable-rs", + "rustix", "uu_pgrep", "uucore", ] diff --git a/src/uu/ps/Cargo.toml b/src/uu/ps/Cargo.toml index e940e8a4..6e46f83f 100644 --- a/src/uu/ps/Cargo.toml +++ b/src/uu/ps/Cargo.toml @@ -14,11 +14,11 @@ version.workspace = true workspace = true [dependencies] -uucore = { workspace = true, features = ["utmpx", "libc"] } +uucore = { workspace = true, features = ["utmpx"] } clap = { workspace = true } chrono = { workspace = true, default-features = false, features = ["clock"] } prettytable-rs = { workspace = true } -nix = { workspace = true } +rustix = { workspace = true, features = ["fs", "process", "std", "termios"] } uu_pgrep = { path = "../pgrep" } diff --git a/src/uu/ps/src/process_selection.rs b/src/uu/ps/src/process_selection.rs index ed5ffe19..c2a0a5af 100644 --- a/src/uu/ps/src/process_selection.rs +++ b/src/uu/ps/src/process_selection.rs @@ -8,20 +8,15 @@ use std::collections::HashSet; use uu_pgrep::process::{walk_process, ProcessInformation, RunState, Teletype}; use uucore::error::UResult; -#[cfg(target_family = "unix")] -use nix::errno::Errno; - // TODO: Temporary add to this file, this function will add to uucore. #[cfg(not(target_os = "redox"))] #[cfg(target_family = "unix")] fn getsid(pid: i32) -> Option { - unsafe { - let result = uucore::libc::getsid(pid); - if Errno::last() == Errno::UnknownErrno { - Some(result) - } else { - None - } + use rustix::process::Pid; + + match rustix::process::getsid(Pid::from_raw(pid)) { + Ok(sid) => Some(sid.as_raw_nonzero().get()), + Err(_) => None, } }