Merge pull request #7430 from Bluemangoo/fix/uptime

fix uptime on windows
This commit is contained in:
Daniel Hofstetter 2025-03-10 15:55:02 +01:00 committed by GitHub
commit 11c20b7686
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 9 additions and 16 deletions

1
Cargo.lock generated
View file

@ -3439,7 +3439,6 @@ dependencies = [
"thiserror 2.0.12",
"utmp-classic",
"uucore",
"windows-sys 0.59.0",
]
[[package]]

View file

@ -25,12 +25,6 @@ uucore = { workspace = true, features = ["libc", "utmpx", "uptime"] }
[target.'cfg(target_os = "openbsd")'.dependencies]
utmp-classic = { workspace = true }
[target.'cfg(target_os="windows")'.dependencies]
windows-sys = { workspace = true, features = [
"Win32_System_RemoteDesktop",
"Wdk_System_SystemInformation",
] }
[[bin]]
name = "uptime"
path = "src/main.rs"

View file

@ -30,11 +30,6 @@ pub mod options {
pub static PATH: &str = "path";
}
#[cfg(windows)]
extern "C" {
fn GetTickCount() -> u32;
}
#[derive(Debug, Error)]
pub enum UptimeError {
// io::Error wrapper

View file

@ -75,6 +75,7 @@ windows-sys = { workspace = true, optional = true, default-features = false, fea
"Win32_Storage_FileSystem",
"Win32_Foundation",
"Win32_System_RemoteDesktop",
"Win32_System_SystemInformation",
"Win32_System_WindowsProgramming",
] }

View file

@ -140,17 +140,19 @@ pub fn get_uptime(boot_time: Option<time_t>) -> UResult<i64> {
/// Get the system uptime
///
/// # Arguments
///
/// boot_time will be ignored, pass None.
///
/// # Returns
///
/// Returns a UResult with the uptime in seconds if successful, otherwise an UptimeError.
#[cfg(windows)]
pub fn get_uptime(_boot_time: Option<time_t>) -> UResult<i64> {
use windows_sys::Win32::System::SystemInformation::GetTickCount;
// SAFETY: always return u32
let uptime = unsafe { GetTickCount() };
if uptime < 0 {
Err(UptimeError::SystemUptime)?;
}
Ok(uptime as i64)
Ok(uptime as i64 / 1000)
}
/// Get the system uptime in a human-readable format
@ -244,6 +246,7 @@ pub fn get_nusers() -> usize {
let mut num_user = 0;
// SAFETY: WTS_CURRENT_SERVER_HANDLE is a valid handle
unsafe {
let mut session_info_ptr = ptr::null_mut();
let mut session_count = 0;
@ -335,6 +338,7 @@ pub fn get_loadavg() -> UResult<(f64, f64, f64)> {
use libc::getloadavg;
let mut avg: [c_double; 3] = [0.0; 3];
// SAFETY: checked whether it returns -1
let loads: i32 = unsafe { getloadavg(avg.as_mut_ptr(), 3) };
if loads == -1 {