sysinfo 0.38.4

Library to get system information such as processes, CPUs, disks, components and networks
Documentation
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// Take a look at the license at the top of the repository in the LICENSE file.

use std::collections::HashMap;
use std::ffi::{OsStr, OsString};
use std::marker::PhantomData;
use std::os::unix::ffi::OsStringExt;
use std::path::{Path, PathBuf};
use std::ptr::{NonNull, null_mut};
use std::sync::OnceLock;

use libc::{c_void, devstat, devstat_getversion};

use super::ffi::{
    DEVSTAT_READ, DEVSTAT_WRITE, geom_stats_open, geom_stats_snapshot_free,
    geom_stats_snapshot_get, geom_stats_snapshot_next, geom_stats_snapshot_reset,
};
use super::utils::{c_buf_to_utf8_str, get_sys_value_str_by_name};
use crate::{Disk, DiskKind, DiskRefreshKind, DiskUsage};

#[derive(Debug)]
pub(crate) struct DiskInner {
    name: OsString,
    c_mount_point: Vec<libc::c_char>,
    dev_id: Option<String>,
    mount_point: PathBuf,
    total_space: u64,
    available_space: u64,
    pub(crate) file_system: OsString,
    is_removable: bool,
    is_read_only: bool,
    read_bytes: u64,
    old_read_bytes: u64,
    written_bytes: u64,
    old_written_bytes: u64,
    updated: bool,
}

#[cfg(test)]
impl Default for DiskInner {
    fn default() -> Self {
        Self {
            name: OsString::new(),
            c_mount_point: Vec::new(),
            dev_id: None,
            mount_point: PathBuf::new(),
            total_space: 0,
            available_space: 0,
            file_system: OsString::new(),
            is_removable: false,
            is_read_only: false,
            read_bytes: 0,
            old_read_bytes: 0,
            written_bytes: 0,
            old_written_bytes: 0,
            updated: false,
        }
    }
}

impl DiskInner {
    pub(crate) fn kind(&self) -> DiskKind {
        // Currently don't know how to retrieve this information on FreeBSD.
        DiskKind::Unknown(-1)
    }

    pub(crate) fn name(&self) -> &OsStr {
        &self.name
    }

    pub(crate) fn file_system(&self) -> &OsStr {
        &self.file_system
    }

    pub(crate) fn mount_point(&self) -> &Path {
        &self.mount_point
    }

    pub(crate) fn total_space(&self) -> u64 {
        self.total_space
    }

    pub(crate) fn available_space(&self) -> u64 {
        self.available_space
    }

    pub(crate) fn is_removable(&self) -> bool {
        self.is_removable
    }

    pub(crate) fn is_read_only(&self) -> bool {
        self.is_read_only
    }

    pub(crate) fn refresh_specifics(&mut self, refresh_kind: DiskRefreshKind) -> bool {
        refresh_disk(self, refresh_kind)
    }

    pub(crate) fn usage(&self) -> DiskUsage {
        DiskUsage {
            read_bytes: self.read_bytes.saturating_sub(self.old_read_bytes),
            total_read_bytes: self.read_bytes,
            written_bytes: self.written_bytes.saturating_sub(self.old_written_bytes),
            total_written_bytes: self.written_bytes,
        }
    }
}

impl crate::DisksInner {
    pub(crate) fn new() -> Self {
        Self {
            disks: Vec::with_capacity(2),
        }
    }

    pub(crate) fn refresh_specifics(
        &mut self,
        remove_not_listed_disks: bool,
        refresh_kind: DiskRefreshKind,
    ) {
        unsafe { get_all_list(&mut self.disks, remove_not_listed_disks, refresh_kind) }
    }

    pub(crate) fn list(&self) -> &[Disk] {
        &self.disks
    }

    pub(crate) fn list_mut(&mut self) -> &mut [Disk] {
        &mut self.disks
    }
}

trait GetValues {
    fn update_old(&mut self);
    fn get_read(&mut self) -> &mut u64;
    fn get_written(&mut self) -> &mut u64;
    fn dev_id(&self) -> Option<&String>;
}

impl GetValues for crate::Disk {
    fn update_old(&mut self) {
        self.inner.update_old()
    }
    fn get_read(&mut self) -> &mut u64 {
        self.inner.get_read()
    }
    fn get_written(&mut self) -> &mut u64 {
        self.inner.get_written()
    }
    fn dev_id(&self) -> Option<&String> {
        self.inner.dev_id()
    }
}

impl GetValues for &mut DiskInner {
    fn update_old(&mut self) {
        self.old_read_bytes = self.read_bytes;
        self.old_written_bytes = self.written_bytes;
    }
    fn get_read(&mut self) -> &mut u64 {
        &mut self.read_bytes
    }
    fn get_written(&mut self) -> &mut u64 {
        &mut self.written_bytes
    }
    fn dev_id(&self) -> Option<&String> {
        self.dev_id.as_ref()
    }
}
impl GetValues for DiskInner {
    fn update_old(&mut self) {
        self.old_read_bytes = self.read_bytes;
        self.old_written_bytes = self.written_bytes;
    }
    fn get_read(&mut self) -> &mut u64 {
        &mut self.read_bytes
    }
    fn get_written(&mut self) -> &mut u64 {
        &mut self.written_bytes
    }
    fn dev_id(&self) -> Option<&String> {
        self.dev_id.as_ref()
    }
}

/// Returns `(total_space, available_space, is_read_only)`.
unsafe fn get_statvfs(
    c_mount_point: &[libc::c_char],
    vfs: &mut libc::statvfs,
) -> Option<(u64, u64, bool)> {
    if unsafe { libc::statvfs(c_mount_point.as_ptr() as *const _, vfs as *mut _) < 0 } {
        sysinfo_debug!("statvfs failed");
        None
    } else {
        let block_size: u64 = vfs.f_frsize as _;
        Some((
            vfs.f_blocks.saturating_mul(block_size),
            vfs.f_favail.saturating_mul(block_size),
            (vfs.f_flag & libc::ST_RDONLY) != 0,
        ))
    }
}

fn refresh_disk(disk: &mut DiskInner, refresh_kind: DiskRefreshKind) -> bool {
    if refresh_kind.storage() {
        unsafe {
            let mut vfs: libc::statvfs = std::mem::zeroed();
            if let Some((total_space, available_space, is_read_only)) =
                get_statvfs(&disk.c_mount_point, &mut vfs)
            {
                disk.total_space = total_space;
                disk.available_space = available_space;
                disk.is_read_only = is_read_only;
            }
        }
    }

    if refresh_kind.io_usage() {
        unsafe {
            refresh_disk_io(&mut [disk]);
        }
    }

    true
}

unsafe fn initialize_geom() -> Result<(), ()> {
    let version = unsafe { devstat_getversion(null_mut()) };
    if version != 6 {
        // For now we only handle the devstat 6 version.
        sysinfo_debug!("version {version} of devstat is not supported");
        return Err(());
    }
    let r = unsafe { geom_stats_open() };
    if r != 0 {
        sysinfo_debug!("`geom_stats_open` failed: {r}");
        Err(())
    } else {
        Ok(())
    }
}

unsafe fn refresh_disk_io<T: GetValues>(disks: &mut [T]) {
    static GEOM_STATS: OnceLock<Result<(), ()>> = OnceLock::new();

    if GEOM_STATS
        .get_or_init(|| unsafe { initialize_geom() })
        .is_err()
    {
        return;
    }
    let snap = unsafe { GeomSnapshot::new() };
    let Some(mut snap) = snap else {
        return;
    };
    for device in snap.iter() {
        let device = unsafe { device.devstat.as_ref() };
        let Some(device_name) = c_buf_to_utf8_str(&device.device_name) else {
            continue;
        };
        let dev_stat_name = format!("{device_name}{}", device.unit_number);

        for disk in disks
            .iter_mut()
            .filter(|d| d.dev_id().is_some_and(|id| *id == dev_stat_name))
        {
            disk.update_old();
            *disk.get_read() = device.bytes[DEVSTAT_READ];
            *disk.get_written() = device.bytes[DEVSTAT_WRITE];
        }
    }

    // thread_local! {
    //     static DEV_INFO: RefCell<DevInfoWrapper> = RefCell::new(DevInfoWrapper::new());
    // }

    // DEV_INFO.with_borrow_mut(|dev_info| {
    //     let Some(stat_info) = dev_info.get_devs() else { return };
    //     let dinfo = (*stat_info).dinfo;

    //     let numdevs = (*dinfo).numdevs;
    //     if numdevs < 0 {
    //         return;
    //     }
    //     let devices: &mut [devstat] = std::slice::from_raw_parts_mut((*dinfo).devices, numdevs as _);
    //     for device in devices {
    //         let Some(device_name) = c_buf_to_utf8_str(&device.device_name) else { continue };
    //         let dev_stat_name = format!("{device_name}{}", device.unit_number);

    //         for disk in disks.iter_mut().filter(|d| d.dev_id().is_some_and(|id| *id == dev_stat_name)) {
    //             disk.update_old();
    //             let mut read = 0u64;
    //             // This code cannot work because `devstat_compute_statistics` expects a
    //             // `long double` as 3rd argument, making it impossible for rust to call it...
    //             devstat_compute_statistics(
    //                 device,
    //                 null_mut(),
    //                 0,
    //                 DSM_TOTAL_BYTES_READ,
    //                 &mut read,
    //                 DSM_TOTAL_BYTES_WRITE,
    //                 disk.get_written(),
    //                 DSM_NONE,
    //             );
    //             *disk.get_read() = read;
    //         }
    //     }
    // });
}

fn get_disks_mapping() -> HashMap<String, String> {
    let mut disk_mapping = HashMap::new();
    let Some(mapping) = get_sys_value_str_by_name(b"kern.geom.conftxt\0") else {
        return disk_mapping;
    };

    let mut last_id = String::new();

    for line in mapping.lines() {
        let mut parts = line.split_whitespace();
        let Some(kind) = parts.next() else { continue };

        #[allow(clippy::collapsible_if)]
        if kind == "0" {
            if let Some("DISK") = parts.next()
                && let Some(id) = parts.next()
            {
                last_id.clear();
                last_id.push_str(id);
            }
        } else if kind == "2" && !last_id.is_empty() {
            if let Some("LABEL") = parts.next()
                && let Some(path) = parts.next()
            {
                disk_mapping.insert(format!("/dev/{path}"), last_id.clone());
            }
        }
    }
    disk_mapping
}

pub unsafe fn get_all_list(
    container: &mut Vec<Disk>,
    remove_not_listed_disks: bool,
    refresh_kind: DiskRefreshKind,
) {
    let mut fs_infos: *mut libc::statfs = null_mut();

    let count = unsafe { libc::getmntinfo(&mut fs_infos, libc::MNT_WAIT) };

    if count < 1 {
        return;
    }
    let disk_mapping = get_disks_mapping();

    let fs_infos: &[libc::statfs] =
        unsafe { std::slice::from_raw_parts(fs_infos as _, count as _) };

    for fs_info in fs_infos {
        if fs_info.f_mntfromname[0] == 0 || fs_info.f_mntonname[0] == 0 {
            // If we have missing information, no need to look any further...
            continue;
        }
        let fs_type: Vec<u8> = {
            let len = fs_info
                .f_fstypename
                .iter()
                .position(|x| *x == 0)
                .unwrap_or(fs_info.f_fstypename.len());
            fs_info.f_fstypename[..len]
                .iter()
                .map(|c| *c as u8)
                .collect()
        };
        match &fs_type[..] {
            b"autofs" | b"devfs" | b"linprocfs" | b"procfs" | b"fdesckfs" | b"tmpfs"
            | b"linsysfs" => {
                sysinfo_debug!(
                    "Memory filesystem `{:?}`, ignoring it.",
                    c_buf_to_utf8_str(&fs_info.f_fstypename),
                );
                continue;
            }
            _ => {}
        }

        let mount_point = match c_buf_to_utf8_str(&fs_info.f_mntonname) {
            Some(m) => m,
            None => {
                sysinfo_debug!("Cannot get disk mount point, ignoring it.");
                continue;
            }
        };

        if mount_point == "/boot/efi" {
            continue;
        }
        let name = if mount_point == "/" {
            OsString::from("root")
        } else {
            OsString::from(mount_point)
        };

        if let Some(disk) = container.iter_mut().find(|d| {
            d.inner.name == name
                && d.inner
                    .file_system
                    .as_encoded_bytes()
                    .iter()
                    .zip(fs_type.iter())
                    .all(|(a, b)| a == b)
        }) {
            // I/O usage is updated for all disks at once at the end.
            refresh_disk(&mut disk.inner, refresh_kind.without_io_usage());
            disk.inner.updated = true;
        } else {
            let dev_mount_point = c_buf_to_utf8_str(&fs_info.f_mntfromname).unwrap_or("");

            // USB keys and CDs are removable.
            let is_removable = if refresh_kind.storage() {
                [b"USB", b"usb"].iter().any(|b| *b == &fs_type[..])
                    || fs_type.starts_with(b"/dev/cd")
            } else {
                false
            };

            let mut disk = DiskInner {
                name,
                c_mount_point: fs_info.f_mntonname.to_vec(),
                mount_point: PathBuf::from(mount_point),
                dev_id: disk_mapping.get(dev_mount_point).map(ToString::to_string),
                total_space: 0,
                available_space: 0,
                file_system: OsString::from_vec(fs_type),
                is_removable,
                is_read_only: false,
                read_bytes: 0,
                old_read_bytes: 0,
                written_bytes: 0,
                old_written_bytes: 0,
                updated: true,
            };
            // I/O usage is updated for all disks at once at the end.
            refresh_disk(&mut disk, refresh_kind.without_io_usage());
            container.push(Disk { inner: disk });
        }
    }

    if remove_not_listed_disks {
        container.retain_mut(|disk| {
            if !disk.inner.updated {
                return false;
            }
            disk.inner.updated = false;
            true
        });
    } else {
        for c in container.iter_mut() {
            c.inner.updated = false;
        }
    }
    if refresh_kind.io_usage() {
        unsafe {
            refresh_disk_io(container.as_mut_slice());
        }
    }
}

// struct DevInfoWrapper {
//     info: statinfo,
// }

// impl DevInfoWrapper {
//     fn new() -> Self {
//         Self {
//             info: unsafe { std::mem::zeroed() },
//         }
//     }

//     unsafe fn get_devs(&mut self) -> Option<&statinfo> {
//         let version = devstat_getversion(null_mut());
//         if version != 6 {
//             // For now we only handle the devstat 6 version.
//             sysinfo_debug!("version {version} of devstat is not supported");
//             return None;
//         }
//         if self.info.dinfo.is_null() {
//             self.info.dinfo = libc::calloc(1, std::mem::size_of::<devinfo>()) as *mut _;
//             if self.info.dinfo.is_null() {
//                 return None;
//             }
//         }
//         if devstat_getdevs(null_mut(), &mut self.info as *mut _) != -1 {
//             Some(&self.info)
//         } else {
//             None
//         }
//     }
// }

// impl Drop for DevInfoWrapper {
//     fn drop(&mut self) {
//         if !self.info.dinfo.is_null() {
//             unsafe { libc::free(self.info.dinfo as *mut _); }
//         }
//     }
// }

// Most of this code was adapted from `gstat-rs` (https://github.com/asomers/gstat-rs).
struct GeomSnapshot(NonNull<c_void>);

impl GeomSnapshot {
    unsafe fn new() -> Option<Self> {
        match NonNull::new(unsafe { geom_stats_snapshot_get() }) {
            Some(n) => Some(Self(n)),
            None => {
                sysinfo_debug!("geom_stats_snapshot_get failed");
                None
            }
        }
    }

    fn iter(&mut self) -> GeomSnapshotIter<'_> {
        GeomSnapshotIter(self)
    }

    fn reset(&mut self) {
        unsafe { geom_stats_snapshot_reset(self.0.as_mut()) }
    }
}

impl Drop for GeomSnapshot {
    fn drop(&mut self) {
        unsafe { geom_stats_snapshot_free(self.0.as_mut()) };
    }
}

#[repr(transparent)]
struct Devstat<'a> {
    devstat: NonNull<devstat>,
    phantom: PhantomData<&'a devstat>,
}

struct GeomSnapshotIter<'a>(&'a mut GeomSnapshot);

impl<'a> Iterator for GeomSnapshotIter<'a> {
    type Item = Devstat<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        let raw = unsafe { geom_stats_snapshot_next(self.0.0.as_mut()) };
        NonNull::new(raw).map(|devstat| Devstat {
            devstat,
            phantom: PhantomData,
        })
    }
}

impl Drop for GeomSnapshotIter<'_> {
    fn drop(&mut self) {
        self.0.reset();
    }
}