Skip to content

Commit 11c20b7

Browse files
authored
Merge pull request #7430 from Bluemangoo/fix/uptime
fix uptime on windows
2 parents 3243fba + 150960a commit 11c20b7

File tree

5 files changed

+9
-16
lines changed

5 files changed

+9
-16
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uu/uptime/Cargo.toml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ uucore = { workspace = true, features = ["libc", "utmpx", "uptime"] }
2525
[target.'cfg(target_os = "openbsd")'.dependencies]
2626
utmp-classic = { workspace = true }
2727

28-
[target.'cfg(target_os="windows")'.dependencies]
29-
windows-sys = { workspace = true, features = [
30-
"Win32_System_RemoteDesktop",
31-
"Wdk_System_SystemInformation",
32-
] }
33-
3428
[[bin]]
3529
name = "uptime"
3630
path = "src/main.rs"

src/uu/uptime/src/uptime.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ pub mod options {
3030
pub static PATH: &str = "path";
3131
}
3232

33-
#[cfg(windows)]
34-
extern "C" {
35-
fn GetTickCount() -> u32;
36-
}
37-
3833
#[derive(Debug, Error)]
3934
pub enum UptimeError {
4035
// io::Error wrapper

src/uucore/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ windows-sys = { workspace = true, optional = true, default-features = false, fea
7575
"Win32_Storage_FileSystem",
7676
"Win32_Foundation",
7777
"Win32_System_RemoteDesktop",
78+
"Win32_System_SystemInformation",
7879
"Win32_System_WindowsProgramming",
7980
] }
8081

src/uucore/src/lib/features/uptime.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,19 @@ pub fn get_uptime(boot_time: Option<time_t>) -> UResult<i64> {
140140

141141
/// Get the system uptime
142142
///
143+
/// # Arguments
144+
///
145+
/// boot_time will be ignored, pass None.
146+
///
143147
/// # Returns
144148
///
145149
/// Returns a UResult with the uptime in seconds if successful, otherwise an UptimeError.
146150
#[cfg(windows)]
147151
pub fn get_uptime(_boot_time: Option<time_t>) -> UResult<i64> {
148152
use windows_sys::Win32::System::SystemInformation::GetTickCount;
153+
// SAFETY: always return u32
149154
let uptime = unsafe { GetTickCount() };
150-
if uptime < 0 {
151-
Err(UptimeError::SystemUptime)?;
152-
}
153-
Ok(uptime as i64)
155+
Ok(uptime as i64 / 1000)
154156
}
155157

156158
/// Get the system uptime in a human-readable format
@@ -244,6 +246,7 @@ pub fn get_nusers() -> usize {
244246

245247
let mut num_user = 0;
246248

249+
// SAFETY: WTS_CURRENT_SERVER_HANDLE is a valid handle
247250
unsafe {
248251
let mut session_info_ptr = ptr::null_mut();
249252
let mut session_count = 0;
@@ -335,6 +338,7 @@ pub fn get_loadavg() -> UResult<(f64, f64, f64)> {
335338
use libc::getloadavg;
336339

337340
let mut avg: [c_double; 3] = [0.0; 3];
341+
// SAFETY: checked whether it returns -1
338342
let loads: i32 = unsafe { getloadavg(avg.as_mut_ptr(), 3) };
339343

340344
if loads == -1 {

0 commit comments

Comments
 (0)