From dad997e6a02005bd403848fb92d06fdbbbf6e54c Mon Sep 17 00:00:00 2001 From: Devel Date: Mon, 6 Jul 2026 17:42:28 +0300 Subject: [PATCH] uucore, uptime: replace unsafe libc::clock_gettime by rustix (#13291) * replace libc with rustix for openbsd * cargo: add time feature to rustix * ignore clippy unnecessary_wraps warning --- Cargo.toml | 2 +- src/uucore/src/lib/features/uptime.rs | 26 ++++---------------------- 2 files changed, 5 insertions(+), 23 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 92c61de963..8f2bede135 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -466,7 +466,7 @@ rstest_reuse = "0.7.0" rustc-hash = "2.1.1" rust-ini = "0.21.0" # raw-backend's linux_execfn is not supported on linux < 6.4 if /proc is not mounted. So use-libc-auxv is needed -rustix = { version = "1.1.4", features = ["param", "use-libc-auxv"] } +rustix = { version = "1.1.4", features = ["param", "use-libc-auxv", "time"] } same-file = "1.0.6" self_cell = "1.0.4" selinux = "0.6" diff --git a/src/uucore/src/lib/features/uptime.rs b/src/uucore/src/lib/features/uptime.rs index d36d11ebd8..29015098ae 100644 --- a/src/uucore/src/lib/features/uptime.rs +++ b/src/uucore/src/lib/features/uptime.rs @@ -97,31 +97,13 @@ fn get_macos_boot_time_sysctl() -> Option { /// /// Returns a UResult with the uptime in seconds if successful, otherwise an UptimeError. #[cfg(target_os = "openbsd")] +#[allow(clippy::unnecessary_wraps, reason = "needed on some platforms")] pub fn get_uptime(_boot_time: Option) -> UResult { - use libc::CLOCK_BOOTTIME; - use libc::clock_gettime; + use rustix::time::{ClockId, clock_gettime}; - use libc::c_int; - use libc::timespec; + let tp = clock_gettime(ClockId::Boottime); - let mut tp: timespec = timespec { - tv_sec: 0, - tv_nsec: 0, - }; - - // OpenBSD prototype: clock_gettime(clk_id: ::clockid_t, tp: *mut ::timespec) -> ::c_int; - let ret: c_int = unsafe { clock_gettime(CLOCK_BOOTTIME, &raw mut tp) }; - - if ret == 0 { - #[cfg(target_pointer_width = "64")] - let uptime: i64 = tp.tv_sec; - #[cfg(not(target_pointer_width = "64"))] - let uptime: i64 = tp.tv_sec.into(); - - Ok(uptime) - } else { - Err(UptimeError::SystemUptime)? - } + Ok(tp.tv_sec as i64) } /// Get the system uptime