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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#![crate_name = "sysinfo"]
#![crate_type = "lib"]
#![crate_type = "rlib"]
#![deny(missing_docs)]
#![deny(broken_intra_doc_links)]
#![allow(unknown_lints)]
#![allow(clippy::upper_case_acronyms)]
#[cfg(doctest)]
doc_comment::doctest!("../README.md");
#[cfg(feature = "debug")]
#[doc(hidden)]
macro_rules! sysinfo_debug {
($($x:tt)*) => {{
eprintln!($($x)*);
}}
}
#[cfg(not(target_os = "ios"))]
#[cfg(not(feature = "debug"))]
#[doc(hidden)]
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)]
const MIN_USERS: usize = 1;
} else if #[cfg(windows)] {
mod windows;
use windows as sys;
extern crate winapi;
extern crate ntapi;
#[cfg(test)]
const MIN_USERS: usize = 1;
} else if #[cfg(any(target_os = "linux", target_os = "android"))] {
mod linux;
use linux as sys;
#[cfg(test)]
const MIN_USERS: usize = 1;
} else {
mod unknown;
use unknown as sys;
#[cfg(test)]
const MIN_USERS: usize = 0;
}
}
pub use common::{
AsU32, DiskType, DiskUsage, LoadAvg, NetworksIter, Pid, RefreshKind, Signal, 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() {
let mut s = System::new();
s.refresh_all();
assert_eq!(
s.get_processes()
.iter()
.all(|(_, proc_)| proc_.memory() == 0),
false
);
}
#[test]
fn check_users() {
let mut s = System::new();
assert!(s.get_users().is_empty());
s.refresh_users_list();
assert!(s.get_users().len() >= MIN_USERS);
let mut s = System::new();
assert!(s.get_users().is_empty());
s.refresh_all();
assert!(s.get_users().is_empty());
let s = System::new_all();
assert!(s.get_users().len() >= MIN_USERS);
}
#[test]
fn check_system_info() {
if MIN_USERS > 0 {
let s = System::new();
assert!(!s.get_name().expect("Failed to get system name").is_empty());
assert!(!s
.get_kernel_version()
.expect("Failed to get kernel version")
.is_empty());
assert!(!s
.get_os_version()
.expect("Failed to get os version")
.is_empty());
assert!(!s
.get_long_os_version()
.expect("Failed to get long OS version")
.is_empty());
}
}
#[test]
fn check_host_name() {
if MIN_USERS > 0 {
let s = System::new();
assert!(!s
.get_host_name()
.expect("Failed to get host name")
.is_empty());
}
}
}
#[cfg(doctest)]
doc_comment::doc_comment!(
"
```
fn is_send<T: Send>() {}
is_send::<sysinfo::System>();
```
```
fn is_sync<T: Sync>() {}
is_sync::<sysinfo::System>();
```"
);