1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! Linux specific api
#![cfg(target_os = "linux")]

#[cfg(test)]
pub(crate) mod mocks;
#[cfg(test)]
use mocks::UPTIME;

pub mod cpu;
#[cfg(feature = "display")]
mod display;
pub mod mem;
pub mod misc;
pub mod net;
mod os_impl_ext;
pub mod ps;
pub mod storage;
mod sysproc;

use super::{run, OsImpl};
use crate::{Error, Result};
use std::process::Command;

pub(crate) use {
    cpu::{cpu, cpu_clock, cpu_cores, logical_cores},
    mem::{memory_free, memory_total, swap_free, swap_total},
    net::{default_iface, interfaces, ipv4, ipv6, mac},
    os_impl_ext::OsImplExt,
};

pub(crate) use sysproc::SysPath;

/// Returns a hostname.
pub fn hostname() -> Result<String> {
    Ok(SysPath::ProcHostname.read()?.trim().to_string())
}

/// Internal implementation of parsing uptime from /proc/uptime
fn _uptime(out: &str) -> Result<u64> {
    Ok(out
        .split_ascii_whitespace()
        .take(1)
        .collect::<String>()
        .parse::<f64>()
        .map_err(|e| Error::CommandParseError(e.to_string()))? as u64)
}

/// Returns current uptime.
pub fn uptime() -> Result<u64> {
    _uptime(&SysPath::ProcUptime.read()?)
}

/// Returns the processor architecture
pub fn arch() -> Result<String> {
    run(Command::new("uname").arg("-m"))
}

/// Returns a domainname read from /proc/sys/kernel/domainname
pub fn domainname() -> Result<String> {
    Ok(SysPath::ProcDomainName.read()?.trim().to_string())
}

/// Returns a kernel version of host os.
pub fn kernel_version() -> Result<String> {
    SysPath::ProcKernelRelease.read()
}

#[derive(Default, OsImpl)]
pub(crate) struct Linux {}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn gets_uptime() {
        assert_eq!(_uptime(UPTIME), Ok(5771))
    }
}