#![crate_name = "sysinfo"]
#![crate_type = "lib"]
#![crate_type = "rlib"]
#![allow(unknown_lints)]
#![deny(missing_docs)]
#![deny(broken_intra_doc_links)]
#![allow(clippy::upper_case_acronyms)]
#![allow(renamed_and_removed_lints)]
#[cfg(doctest)]
doc_comment::doctest!("../README.md");
#[cfg(feature = "debug")]
#[doc(hidden)]
#[allow(unused)]
macro_rules! sysinfo_debug {
($($x:tt)*) => {{
eprintln!($($x)*);
}}
}
#[cfg(not(feature = "debug"))]
#[doc(hidden)]
#[allow(unused)]
macro_rules! sysinfo_debug {
($($x:tt)*) => {{}};
}
cfg_if::cfg_if! {
if #[cfg(any(target_os = "macos", target_os = "ios"))] {
mod apple;
use apple as sys;
extern crate core_foundation_sys;
#[cfg(test)]
pub(crate) const MIN_USERS: usize = 1;
} else if #[cfg(windows)] {
mod windows;
use windows as sys;
extern crate winapi;
extern crate ntapi;
#[cfg(test)]
pub(crate) const MIN_USERS: usize = 1;
} else if #[cfg(any(target_os = "linux", target_os = "android"))] {
mod linux;
use linux as sys;
#[cfg(test)]
pub(crate) const MIN_USERS: usize = 1;
} else {
mod unknown;
use unknown as sys;
#[cfg(test)]
pub(crate) const MIN_USERS: usize = 0;
}
}
pub use common::{
AsU32, DiskType, DiskUsage, Gid, LoadAvg, NetworksIter, Pid, RefreshKind, Signal, Uid, User,
};
pub use sys::{Component, Disk, NetworkData, Networks, Process, ProcessStatus, Processor, System};
pub use traits::{
ComponentExt, DiskExt, NetworkExt, NetworksExt, ProcessExt, ProcessorExt, SystemExt, UserExt,
};
#[cfg(feature = "c-interface")]
pub use c_interface::*;
pub use utils::get_current_pid;
#[cfg(feature = "c-interface")]
mod c_interface;
mod common;
mod debug;
mod system;
mod traits;
mod utils;
pub fn set_open_files_limit(mut _new_limit: isize) -> bool {
#[cfg(any(target_os = "linux", target_os = "android"))]
{
if _new_limit < 0 {
_new_limit = 0;
}
let max = sys::system::get_max_nb_fds();
if _new_limit > max {
_new_limit = max;
}
if let Ok(ref mut x) = unsafe { sys::system::REMAINING_FILES.lock() } {
let diff = max - **x;
**x = _new_limit - diff;
true
} else {
false
}
}
#[cfg(all(not(target_os = "linux"), not(target_os = "android")))]
{
false
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn check_memory_usage() {
if System::IS_SUPPORTED {
let mut s = System::new();
s.refresh_all();
assert_eq!(
s.processes().iter().all(|(_, proc_)| proc_.memory() == 0),
false
);
}
}
#[cfg(target_os = "linux")]
#[test]
fn check_cpu_usage() {
let mut s = System::new();
s.refresh_all();
assert_eq!(
s.processes()
.iter()
.all(|(_, proc_)| proc_.cpu_usage() == 0.0),
true
);
std::thread::sleep(std::time::Duration::from_millis(100));
s.refresh_all();
assert_eq!(
s.processes()
.iter()
.all(|(_, proc_)| proc_.cpu_usage() >= 0.0
&& proc_.cpu_usage() <= (s.processors().len() as f32) * 100.0),
true
);
}
#[test]
fn check_users() {
let mut s = System::new();
assert!(s.users().is_empty());
s.refresh_users_list();
assert!(s.users().len() >= MIN_USERS);
let mut s = System::new();
assert!(s.users().is_empty());
s.refresh_all();
assert!(s.users().is_empty());
let s = System::new_all();
assert!(s.users().len() >= MIN_USERS);
}
#[test]
fn check_uid_gid() {
let mut s = System::new();
assert!(s.users().is_empty());
s.refresh_users_list();
let users = s.users();
assert!(users.len() >= MIN_USERS);
if System::IS_SUPPORTED {
#[cfg(not(target_os = "windows"))]
{
let user = users
.iter()
.find(|u| u.name() == "root")
.expect("no root user");
assert_eq!(*user.uid(), 0);
assert_eq!(*user.gid(), 0);
if let Some(user) = users.iter().find(|u| *u.gid() > 0) {
assert!(*user.uid() > 0);
assert!(*user.gid() > 0);
}
}
#[cfg(not(target_os = "windows"))]
{
assert!(users.iter().filter(|u| *u.uid() > 0).count() > 0);
}
}
}
#[test]
fn check_system_info() {
if System::IS_SUPPORTED {
let s = System::new();
assert!(!s.name().expect("Failed to get system name").is_empty());
assert!(!s
.kernel_version()
.expect("Failed to get kernel version")
.is_empty());
assert!(!s.os_version().expect("Failed to get os version").is_empty());
assert!(!s
.long_os_version()
.expect("Failed to get long OS version")
.is_empty());
}
}
#[test]
fn check_host_name() {
if System::IS_SUPPORTED {
let s = System::new();
assert!(!s.host_name().expect("Failed to get host name").is_empty());
}
}
#[test]
fn check_refresh_process_return_value() {
if System::IS_SUPPORTED {
let pid = get_current_pid().expect("Failed to get current PID");
let mut s = System::new();
assert!(s.refresh_process(pid));
assert!(s.refresh_process(pid));
}
}
#[test]
fn ensure_is_supported_is_set_correctly() {
if MIN_USERS > 0 {
assert!(System::IS_SUPPORTED);
} else {
assert!(!System::IS_SUPPORTED);
}
}
#[test]
fn check_processors_number() {
let s = System::new();
if System::IS_SUPPORTED {
assert!(!s.processors().is_empty());
let physical_cores_count = s
.physical_core_count()
.expect("failed to get number of physical cores");
assert!(physical_cores_count <= s.processors().len());
} else {
assert!(s.processors().is_empty());
assert_eq!(s.physical_core_count(), None);
}
}
}
#[cfg(doctest)]
doc_comment::doc_comment!(
"
```
fn is_send<T: Send>() {}
is_send::<sysinfo::System>();
```
```
fn is_sync<T: Sync>() {}
is_sync::<sysinfo::System>();
```"
);