1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Try to read BOOT_TIME from utmpx if /proc/uptime fails.

This commit is contained in:
Kevin Butler 2014-03-25 17:28:31 +00:00
parent 25ee699756
commit cf136e1ff4
2 changed files with 32 additions and 13 deletions

View file

@ -1,7 +1,7 @@
#[allow(non_camel_case_types)]; #[allow(non_camel_case_types)];
#[allow(dead_code)]; #[allow(dead_code)];
pub use self::utmpx::{DEFAULT_FILE,USER_PROCESS,c_utmp}; pub use self::utmpx::{DEFAULT_FILE,USER_PROCESS,BOOT_TIME,c_utmp};
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
mod utmpx { mod utmpx {
use std::libc; use std::libc;

View file

@ -72,8 +72,10 @@ fn main() {
} }
print_time(); print_time();
print_uptime(); let (boot_time, user_count) = process_utmpx();
print_nusers(); let upsecs = get_uptime(boot_time) / 100;
print_uptime(upsecs);
print_nusers(user_count);
print_loadavg(); print_loadavg();
} }
@ -93,7 +95,7 @@ fn print_loadavg() {
} }
} }
fn print_nusers() { fn process_utmpx() -> (Option<time_t>, uint) {
DEFAULT_FILE.with_c_str(|filename| { DEFAULT_FILE.with_c_str(|filename| {
unsafe { unsafe {
utmpxname(filename); utmpxname(filename);
@ -101,6 +103,7 @@ fn print_nusers() {
}); });
let mut nusers = 0; let mut nusers = 0;
let mut boot_time = None;
unsafe { unsafe {
setutxent(); setutxent();
@ -112,14 +115,25 @@ fn print_nusers() {
break; break;
} }
if (*line).ut_type == USER_PROCESS { match (*line).ut_type {
nusers += 1; USER_PROCESS => nusers += 1,
BOOT_TIME => {
let t = (*line).ut_tv;
if t.tv_sec > 0 {
boot_time = Some(t.tv_sec);
}
},
_ => continue
} }
} }
endutxent(); endutxent();
} }
(boot_time, nusers)
}
fn print_nusers(nusers: uint) {
if nusers == 1 { if nusers == 1 {
print!("1 user, "); print!("1 user, ");
} else if nusers > 1 { } else if nusers > 1 {
@ -137,13 +151,19 @@ fn print_time() {
} }
} }
fn get_uptime() -> int { fn get_uptime(boot_time: Option<time_t>) -> i64 {
let proc_uptime = File::open(&Path::new("/proc/uptime")) let proc_uptime = File::open(&Path::new("/proc/uptime"))
.read_to_str(); .read_to_str();
let uptime_text = match proc_uptime { let uptime_text = match proc_uptime {
Ok(s) => s, Ok(s) => s,
_ => return -1 _ => return match boot_time {
Some(t) => {
let now = unsafe { time(null()) };
(now - t) * 100 // Return in ms
},
_ => -1
}
}; };
match uptime_text.words().next() { match uptime_text.words().next() {
@ -155,11 +175,10 @@ fn get_uptime() -> int {
} }
} }
fn print_uptime() { fn print_uptime(upsecs: i64) {
let uptime = get_uptime() / 100; let updays = upsecs / 86400;
let updays = uptime / 86400; let uphours = (upsecs - (updays * 86400)) / 3600;
let uphours = (uptime - (updays * 86400)) / 3600; let upmins = (upsecs - (updays * 86400) - (uphours * 3600)) / 60;
let upmins = (uptime - (updays * 86400) - (uphours * 3600)) / 60;
if updays == 1 { if updays == 1 {
print!("up {:1d} day, {:2d}:{:02d}, ", updays, uphours, upmins); print!("up {:1d} day, {:2d}:{:02d}, ", updays, uphours, upmins);
} }