mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 03:57:44 +00:00
Merge pull request #1200 from Arcterus/uptime-fix
uptime: error when uptime cannot be found
This commit is contained in:
commit
e04911e938
3 changed files with 32 additions and 11 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1695,6 +1695,7 @@ name = "uptime"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"getopts 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
|
"getopts 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"uucore 0.0.1",
|
"uucore 0.0.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ path = "uptime.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
getopts = "0.2.14"
|
getopts = "0.2.14"
|
||||||
|
time = "0.1.38"
|
||||||
|
|
||||||
[dependencies.uucore]
|
[dependencies.uucore]
|
||||||
path = "../uucore"
|
path = "../uucore"
|
||||||
|
|
|
@ -13,18 +13,15 @@
|
||||||
/* last synced with: cat (GNU coreutils) 8.13 */
|
/* last synced with: cat (GNU coreutils) 8.13 */
|
||||||
|
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
|
extern crate time;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
// import crate time from utmpx
|
// import crate time from utmpx
|
||||||
use uucore::utmpx::*;
|
use uucore::libc::time_t;
|
||||||
use uucore::libc::{c_double, time_t};
|
|
||||||
pub use uucore::libc;
|
pub use uucore::libc;
|
||||||
|
|
||||||
use getopts::Options;
|
use getopts::Options;
|
||||||
use std::fs::File;
|
|
||||||
use std::io::Read;
|
|
||||||
use std::mem::transmute;
|
|
||||||
|
|
||||||
static NAME: &'static str = "uptime";
|
static NAME: &'static str = "uptime";
|
||||||
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
||||||
|
@ -70,15 +67,26 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
|
|
||||||
print_time();
|
print_time();
|
||||||
let (boot_time, user_count) = process_utmpx();
|
let (boot_time, user_count) = process_utmpx();
|
||||||
let upsecs = get_uptime(boot_time) / 100;
|
let uptime = get_uptime(boot_time);
|
||||||
print_uptime(upsecs);
|
if uptime < 0 {
|
||||||
print_nusers(user_count);
|
show_error!("could not retrieve system uptime");
|
||||||
print_loadavg();
|
|
||||||
|
|
||||||
0
|
1
|
||||||
|
} else {
|
||||||
|
let upsecs = uptime / 100;
|
||||||
|
print_uptime(upsecs);
|
||||||
|
print_nusers(user_count);
|
||||||
|
print_loadavg();
|
||||||
|
|
||||||
|
0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
fn print_loadavg() {
|
fn print_loadavg() {
|
||||||
|
use libc::c_double;
|
||||||
|
use std::mem::transmute;
|
||||||
|
|
||||||
let mut avg: [c_double; 3] = [0.0; 3];
|
let mut avg: [c_double; 3] = [0.0; 3];
|
||||||
let loads: i32 = unsafe { transmute(getloadavg(avg.as_mut_ptr(), 3)) };
|
let loads: i32 = unsafe { transmute(getloadavg(avg.as_mut_ptr(), 3)) };
|
||||||
|
|
||||||
|
@ -96,8 +104,16 @@ fn print_loadavg() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
fn print_loadavg() {
|
||||||
|
// XXX: currently this is a noop as Windows does not seem to have anything comparable to
|
||||||
|
// getloadavg()
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn process_utmpx() -> (Option<time_t>, usize) {
|
fn process_utmpx() -> (Option<time_t>, usize) {
|
||||||
|
use uucore::utmpx::*;
|
||||||
|
|
||||||
let mut nusers = 0;
|
let mut nusers = 0;
|
||||||
let mut boot_time = None;
|
let mut boot_time = None;
|
||||||
|
|
||||||
|
@ -140,6 +156,9 @@ fn print_time() {
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn get_uptime(boot_time: Option<time_t>) -> i64 {
|
fn get_uptime(boot_time: Option<time_t>) -> i64 {
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
let mut proc_uptime = String::new();
|
let mut proc_uptime = String::new();
|
||||||
|
|
||||||
if let Some(n) = File::open("/proc/uptime")
|
if let Some(n) = File::open("/proc/uptime")
|
||||||
|
@ -162,7 +181,7 @@ fn get_uptime(boot_time: Option<time_t>) -> i64 {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
fn get_uptime(boot_time: Option<time_t>) -> i64 {
|
fn get_uptime(_boot_time: Option<time_t>) -> i64 {
|
||||||
unsafe { GetTickCount() as i64 }
|
unsafe { GetTickCount() as i64 }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue