From f86dc36c3f76c5d716cd939147133d9a28ef6d37 Mon Sep 17 00:00:00 2001 From: Tony Bajan Date: Thu, 12 Feb 2026 18:13:50 +0000 Subject: [PATCH] top: Switch some uses of libc to rustix Use rustix instead libc to provide getpriority, setpriorty and page size calls. Part of #599 --- Cargo.lock | 1 + Cargo.toml | 2 +- src/uu/top/Cargo.toml | 1 + src/uu/top/src/action.rs | 10 +++------- src/uu/top/src/picker.rs | 17 ++++------------- 5 files changed, 10 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e879c657..adbd98da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2479,6 +2479,7 @@ dependencies = [ "nix 0.30.1", "pkg-config", "ratatui", + "rustix", "sysinfo", "uu_vmstat", "uu_w", diff --git a/Cargo.toml b/Cargo.toml index ee3aace5..7aeb8027 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,7 +68,7 @@ prettytable-rs = "0.10.0" rand = { version = "0.10.0" } ratatui = "0.30.0" regex = "1.10.4" -rustix = { version = "1.1.2", features = ["fs", "process"] } +rustix = { version = "1.1.2", features = ["fs", "process", "param"] } sysinfo = "0.38.0" tempfile = "3.10.1" terminal_size = "0.4.2" diff --git a/src/uu/top/Cargo.toml b/src/uu/top/Cargo.toml index 05ac82fe..7ecb22f9 100644 --- a/src/uu/top/Cargo.toml +++ b/src/uu/top/Cargo.toml @@ -22,6 +22,7 @@ ratatui = { workspace = true, features = ["crossterm"] } sysinfo = { workspace = true } chrono = { workspace = true } bytesize = { workspace = true } +rustix = { workspace = true } uu_vmstat = { path = "../vmstat" } uu_w = { path = "../w" } diff --git a/src/uu/top/src/action.rs b/src/uu/top/src/action.rs index d40e3d95..0cd1c495 100644 --- a/src/uu/top/src/action.rs +++ b/src/uu/top/src/action.rs @@ -5,15 +5,11 @@ #[cfg(target_os = "linux")] pub(crate) fn renice(pid: u32, nice_value: i32) -> uucore::error::UResult<()> { + use rustix::process::{setpriority_process, Pid}; use uucore::error::USimpleError; - use uucore::libc::{setpriority, PRIO_PROCESS}; - let result = unsafe { setpriority(PRIO_PROCESS, pid, nice_value) }; - if result == -1 { - Err(USimpleError::new(0, "Permission Denied")) - } else { - Ok(()) - } + let pid = Pid::from_raw(pid as i32); + setpriority_process(pid, nice_value).map_err(|_| USimpleError::new(0, "Permission Denied")) } #[cfg(unix)] diff --git a/src/uu/top/src/picker.rs b/src/uu/top/src/picker.rs index bc3db487..fd1c6736 100644 --- a/src/uu/top/src/picker.rs +++ b/src/uu/top/src/picker.rs @@ -317,20 +317,11 @@ fn pr(_pid: u32, _stat: Stat) -> Box { #[cfg(not(target_os = "windows"))] fn get_nice(pid: u32) -> i32 { - use nix::errno::Errno; - use uucore::libc::{getpriority, PRIO_PROCESS}; + use rustix::process::{getpriority_process, Pid}; // this is nice value, not priority value - let result = unsafe { getpriority(PRIO_PROCESS, pid) }; - - let result = if Errno::last() == Errno::UnknownErrno { - result - } else { - Errno::clear(); - 0 - }; - - result as i32 + let pid = Pid::from_raw(pid as i32); + getpriority_process(pid).unwrap_or(0) } #[cfg(not(target_os = "windows"))] @@ -381,7 +372,7 @@ fn shr(pid: u32, _stat: Stat) -> Box { let content = read_to_string(file).unwrap(); let values = content.split_whitespace(); if let Some(shared) = values.collect::>().get(2) { - let page_size = unsafe { uucore::libc::sysconf(uucore::libc::_SC_PAGESIZE) }; + let page_size = rustix::param::page_size(); MemValue::new_boxed(shared.parse::().unwrap() * page_size as u64) } else { MemValue::new_boxed(0)