1#![doc = include_str!("../README.md")]
4#![cfg_attr(feature = "serde", doc = include_str!("../md_doc/serde.md"))]
5#![allow(unknown_lints)]
6#![deny(missing_docs)]
7#![deny(rustdoc::broken_intra_doc_links)]
8#![allow(clippy::upper_case_acronyms)]
9#![allow(clippy::non_send_fields_in_send_ty)]
10#![allow(renamed_and_removed_lints)]
11#![allow(clippy::assertions_on_constants)]
12#![allow(unknown_lints)]
13
14#[macro_use]
15mod macros;
16
17cfg_if::cfg_if! {
18 if #[cfg(feature = "unknown-ci")] {
19 mod unknown;
21 use unknown as sys;
22
23 #[cfg(test)]
24 pub(crate) const MIN_USERS: usize = 0;
25 } else if #[cfg(any(target_os = "macos", target_os = "ios"))] {
26 mod apple;
27 use apple as sys;
28 pub(crate) mod users;
29 mod network_helper_nix;
30 use network_helper_nix as network_helper;
31 mod network;
32
33 pub(crate) type GroupId = libc::c_int;
35 pub(crate) use libc::__error as libc_errno;
36
37 #[cfg(test)]
38 pub(crate) const MIN_USERS: usize = 1;
39 } else if #[cfg(windows)] {
40 mod windows;
41 use windows as sys;
42 mod network_helper_win;
43 use network_helper_win as network_helper;
44 mod network;
45
46 #[cfg(test)]
47 pub(crate) const MIN_USERS: usize = 1;
48 } else if #[cfg(any(target_os = "linux", target_os = "android"))] {
49 mod linux;
50 use linux as sys;
51 pub(crate) mod users;
52 mod network_helper_nix;
53 use network_helper_nix as network_helper;
54 mod network;
55
56 pub(crate) type GroupId = libc::gid_t;
58 #[cfg(target_os = "linux")]
59 pub(crate) use libc::__errno_location as libc_errno;
60 #[cfg(target_os = "android")]
61 pub(crate) use libc::__errno as libc_errno;
62
63 #[cfg(test)]
64 pub(crate) const MIN_USERS: usize = 1;
65 } else if #[cfg(target_os = "freebsd")] {
66 mod freebsd;
67 use freebsd as sys;
68 pub(crate) mod users;
69 mod network_helper_nix;
70 use network_helper_nix as network_helper;
71 mod network;
72
73 pub(crate) type GroupId = libc::gid_t;
75 pub(crate) use libc::__error as libc_errno;
76
77 #[cfg(test)]
78 pub(crate) const MIN_USERS: usize = 1;
79 } else {
80 mod unknown;
81 use unknown as sys;
82
83 #[cfg(test)]
84 pub(crate) const MIN_USERS: usize = 0;
85 }
86}
87
88pub use common::{
89 get_current_pid, CpuRefreshKind, DiskKind, DiskUsage, Gid, LoadAvg, MacAddr, NetworksIter, Pid,
90 PidExt, ProcessRefreshKind, ProcessStatus, RefreshKind, Signal, Uid, User,
91};
92pub use sys::{Component, Cpu, Disk, NetworkData, Networks, Process, System};
93pub use traits::{
94 ComponentExt, CpuExt, DiskExt, NetworkExt, NetworksExt, ProcessExt, SystemExt, UserExt,
95};
96
97#[cfg(feature = "c-interface")]
98pub use c_interface::*;
99
100#[cfg(feature = "c-interface")]
101mod c_interface;
102mod common;
103mod debug;
104#[cfg(feature = "serde")]
105mod serde;
106mod system;
107mod traits;
108mod utils;
109
110pub fn set_open_files_limit(mut _new_limit: isize) -> bool {
134 cfg_if::cfg_if! {
135 if #[cfg(all(not(feature = "unknown-ci"), any(target_os = "linux", target_os = "android")))]
136 {
137 if _new_limit < 0 {
138 _new_limit = 0;
139 }
140 let max = sys::system::get_max_nb_fds();
141 if _new_limit > max {
142 _new_limit = max;
143 }
144 unsafe {
145 if let Ok(ref mut x) = sys::system::REMAINING_FILES.lock() {
146 let diff = max.saturating_sub(**x);
150 **x = _new_limit.saturating_sub(diff);
151 true
152 } else {
153 false
154 }
155 }
156 } else {
157 false
158 }
159 }
160}
161
162#[cfg(doctest)]
164mod doctest {
165 mod process_clone {}
186
187 mod system_clone {}
206}
207
208#[cfg(test)]
209mod test {
210 use crate::*;
211
212 #[cfg(feature = "unknown-ci")]
213 #[test]
214 fn check_unknown_ci_feature() {
215 assert!(!System::IS_SUPPORTED);
216 }
217
218 #[test]
219 fn check_process_memory_usage() {
220 let mut s = System::new();
221 s.refresh_all();
222
223 if System::IS_SUPPORTED {
224 #[cfg(not(feature = "apple-sandbox"))]
226 assert!(!s.processes().iter().all(|(_, proc_)| proc_.memory() == 0));
227 } else {
228 assert!(s.processes().iter().all(|(_, proc_)| proc_.memory() == 0));
230 }
231 }
232
233 #[test]
234 fn check_memory_usage() {
235 let mut s = System::new();
236
237 assert_eq!(s.total_memory(), 0);
238 assert_eq!(s.free_memory(), 0);
239 assert_eq!(s.available_memory(), 0);
240 assert_eq!(s.used_memory(), 0);
241 assert_eq!(s.total_swap(), 0);
242 assert_eq!(s.free_swap(), 0);
243 assert_eq!(s.used_swap(), 0);
244
245 s.refresh_memory();
246 if System::IS_SUPPORTED {
247 assert!(s.total_memory() > 0);
248 assert!(s.used_memory() > 0);
249 if s.total_swap() > 0 {
250 assert!(s.free_swap() > 0);
252 }
253 } else {
254 assert_eq!(s.total_memory(), 0);
255 assert_eq!(s.used_memory(), 0);
256 assert_eq!(s.total_swap(), 0);
257 assert_eq!(s.free_swap(), 0);
258 }
259 }
260
261 #[cfg(target_os = "linux")]
262 #[test]
263 fn check_processes_cpu_usage() {
264 if !System::IS_SUPPORTED {
265 return;
266 }
267 let mut s = System::new();
268
269 s.refresh_processes();
270 assert!(s
272 .processes()
273 .iter()
274 .all(|(_, proc_)| proc_.cpu_usage() == 0.0));
275
276 std::thread::sleep(System::MINIMUM_CPU_UPDATE_INTERVAL);
278 s.refresh_processes();
279 assert!(s
280 .processes()
281 .iter()
282 .all(|(_, proc_)| proc_.cpu_usage() >= 0.0
283 && proc_.cpu_usage() <= (s.cpus().len() as f32) * 100.0));
284 assert!(s
285 .processes()
286 .iter()
287 .any(|(_, proc_)| proc_.cpu_usage() > 0.0));
288 }
289
290 #[test]
291 fn check_cpu_usage() {
292 if !System::IS_SUPPORTED {
293 return;
294 }
295 let mut s = System::new();
296 for _ in 0..10 {
297 s.refresh_cpu();
298 std::thread::sleep(System::MINIMUM_CPU_UPDATE_INTERVAL);
300 if s.cpus().iter().any(|c| c.cpu_usage() > 0.0) {
301 return;
303 }
304 }
305 panic!("CPU usage is always zero...");
306 }
307
308 #[test]
309 fn check_users() {
310 let mut s = System::new();
311 assert!(s.users().is_empty());
312 s.refresh_users_list();
313 assert!(s.users().len() >= MIN_USERS);
314
315 let mut s = System::new();
316 assert!(s.users().is_empty());
317 s.refresh_all();
318 assert!(s.users().is_empty());
319
320 let s = System::new_all();
321 assert!(s.users().len() >= MIN_USERS);
322 }
323
324 #[test]
325 fn check_uid_gid() {
326 let mut s = System::new();
327 assert!(s.users().is_empty());
328 s.refresh_users_list();
329 let users = s.users();
330 assert!(users.len() >= MIN_USERS);
331
332 if System::IS_SUPPORTED {
333 #[cfg(not(target_os = "windows"))]
334 {
335 let user = users
336 .iter()
337 .find(|u| u.name() == "root")
338 .expect("no root user");
339 assert_eq!(**user.id(), 0);
340 assert_eq!(*user.group_id(), 0);
341 if let Some(user) = users.iter().find(|u| *u.group_id() > 0) {
342 assert!(**user.id() > 0);
343 assert!(*user.group_id() > 0);
344 }
345 assert!(users.iter().filter(|u| **u.id() > 0).count() > 0);
346 }
347
348 s.refresh_processes();
350 assert!(s
351 .processes()
352 .iter()
353 .filter_map(|(_, p)| p.user_id())
354 .any(|uid| s.get_user_by_id(uid).is_some()));
355 }
356 }
357
358 #[test]
359 fn check_all_process_uids_resolvable() {
360 if System::IS_SUPPORTED {
361 let s = System::new_with_specifics(
362 RefreshKind::new()
363 .with_processes(ProcessRefreshKind::new().with_user())
364 .with_users_list(),
365 );
366
367 for process in s.processes().values() {
370 if let Some(uid) = process.user_id() {
371 assert!(s.get_user_by_id(uid).is_some(), "No UID {:?} found", uid);
372 }
373 }
374 }
375 }
376
377 #[test]
378 fn check_system_info() {
379 let s = System::new();
380
381 if System::IS_SUPPORTED {
383 assert!(!s.name().expect("Failed to get system name").is_empty());
384
385 assert!(!s
386 .kernel_version()
387 .expect("Failed to get kernel version")
388 .is_empty());
389
390 assert!(!s.os_version().expect("Failed to get os version").is_empty());
391
392 assert!(!s
393 .long_os_version()
394 .expect("Failed to get long OS version")
395 .is_empty());
396 }
397
398 assert!(!s.distribution_id().is_empty());
399 }
400
401 #[test]
402 fn check_host_name() {
403 if System::IS_SUPPORTED {
405 let s = System::new();
406 assert!(s.host_name().is_some());
407 }
408 }
409
410 #[test]
411 fn check_refresh_process_return_value() {
412 if System::IS_SUPPORTED {
414 let _pid = get_current_pid().expect("Failed to get current PID");
415
416 #[cfg(not(feature = "apple-sandbox"))]
417 {
418 let mut s = System::new();
419 assert!(s.refresh_process(_pid));
421 assert!(s.refresh_process(_pid));
423 }
424 }
425 }
426
427 #[test]
428 fn ensure_is_supported_is_set_correctly() {
429 if MIN_USERS > 0 {
430 assert!(System::IS_SUPPORTED);
431 } else {
432 assert!(!System::IS_SUPPORTED);
433 }
434 }
435
436 #[test]
437 fn check_cpus_number() {
438 let mut s = System::new();
439
440 assert!(s.cpus().is_empty());
442 if System::IS_SUPPORTED {
443 let physical_cores_count = s
446 .physical_core_count()
447 .expect("failed to get number of physical cores");
448
449 s.refresh_cpu();
450 assert!(!s.cpus().is_empty());
452
453 let physical_cores_count2 = s
456 .physical_core_count()
457 .expect("failed to get number of physical cores");
458 assert!(physical_cores_count2 <= s.cpus().len());
459 assert_eq!(physical_cores_count, physical_cores_count2);
460 } else {
461 assert_eq!(s.physical_core_count(), None);
462 }
463 assert!(s.physical_core_count().unwrap_or(0) <= s.cpus().len());
464 }
465
466 #[test]
467 fn check_nb_supported_signals() {
468 if System::IS_SUPPORTED {
469 assert!(
470 !System::SUPPORTED_SIGNALS.is_empty(),
471 "SUPPORTED_SIGNALS shoudn't be empty on supported systems!"
472 );
473 } else {
474 assert!(
475 System::SUPPORTED_SIGNALS.is_empty(),
476 "SUPPORTED_SIGNALS should be empty on not support systems!"
477 );
478 }
479 }
480
481 #[test]
483 fn check_cpu_frequency() {
484 if !System::IS_SUPPORTED {
485 return;
486 }
487 let mut s = System::new();
488 s.refresh_processes();
489 for proc_ in s.cpus() {
490 assert_eq!(proc_.frequency(), 0);
491 }
492 s.refresh_cpu();
493 for proc_ in s.cpus() {
494 assert_eq!(proc_.frequency(), 0);
495 }
496 if std::env::var("APPLE_CI").is_err() && std::env::var("FREEBSD_CI").is_err() {
498 s.refresh_cpu_specifics(CpuRefreshKind::everything());
499 for proc_ in s.cpus() {
500 assert_ne!(proc_.frequency(), 0);
501 }
502 }
503 }
504
505 #[test]
508 fn check_refresh_process_update() {
509 if !System::IS_SUPPORTED {
510 return;
511 }
512 let mut s = System::new_all();
513 let total = s.processes().len() as isize;
514 s.refresh_processes();
515 let new_total = s.processes().len() as isize;
516 assert!(
518 (new_total - total).abs() <= 5,
519 "{} <= 5",
520 (new_total - total).abs()
521 );
522 }
523
524 #[test]
526 fn check_cmd_line() {
527 if !System::IS_SUPPORTED {
528 return;
529 }
530 let mut sys = System::new();
531 sys.refresh_processes_specifics(ProcessRefreshKind::new());
532
533 assert!(sys
534 .processes()
535 .iter()
536 .any(|(_, process)| !process.cmd().is_empty()));
537 }
538}