sysinfo_rs 0.1.9

A library for retrieving system information.
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
use anyhow::{bail, Result};
use pnet::datalink;
use serde::{Deserialize, Serialize};
use std::fs::{self, File};
use std::io::{BufRead, BufReader, Read};
use std::path::Path;
use std::process::Command;
use udev;

const BIOS_INFO_PATH: &str = "/sys/firmware/dmi/entries/0-0/raw";
const SYSTEM_INFO_PATH: &str = "/sys/firmware/dmi/entries/1-0/raw";
const ENCLOSURE_INFO_PATH: &str = "/sys/firmware/dmi/entries/3-0/raw";

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HardwareInfo {
    pub cpu_is_virtual: bool,
    pub disk_serial_number: String,
    pub mac_addresses: String,
    pub bios_info: BiosInfo,
    pub system_info: SystemInfo,
    pub enclosure_info: EnclosureInfo,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extra: Option<serde_json::Value>,
}

impl HardwareInfo {
    pub fn new() -> Result<Self> {
        Ok(HardwareInfo {
            cpu_is_virtual: determine_virtual_machine_status(),
            disk_serial_number: get_root_device()
                .and_then(|disk_part_name| get_serial_number(&disk_part_name))
                .unwrap_or_default(),
            mac_addresses: get_mac_addresses()?,
            bios_info: read_bios_info(BIOS_INFO_PATH).unwrap_or_default(),
            system_info: read_system_info(SYSTEM_INFO_PATH).unwrap_or_default(),
            enclosure_info: read_enclosure_info(ENCLOSURE_INFO_PATH).unwrap_or_default(),
            extra: None,
        })
    }

    pub fn with_extra(mut self, extra: serde_json::Value) -> Self {
        self.extra = Some(extra);
        self
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct BiosInfo {
    pub vendor: String,
    pub bios_version: String,
    pub bios_release_date: String,
    pub is_virtual_machine: bool,
    pub system_bios_major_release: String,
    pub system_bios_minor_release: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct SystemInfo {
    pub manufacturer: String,
    pub product_name: String,
    pub serial_number: String,
    pub uuid: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct EnclosureInfo {
    pub manufacturer: String,
    pub enclosure_type: String,
    pub version: String,
    pub serial_number: String,
    pub asset_tag_number: String,
}

#[cfg(target_arch = "x86_64")]
fn is_hypervisor_present() -> bool {
    use std::arch::x86_64::__cpuid;

    // Check CPUID hypervisor bit
    let basic_cpuid = unsafe { __cpuid(1) };
    let is_vm = (basic_cpuid.ecx & (1 << 31)) != 0;

    // Early return if hypervisor bit is set
    if is_vm {
        return true;
    }

    // Check hypervisor name
    if get_hypervisor_name().is_some() {
        return true;
    }

    // Check system indicators
    check_sys_hypervisor() || check_dmesg_hypervisor()
}

#[cfg(target_arch = "x86_64")]
fn get_hypervisor_name() -> Option<&'static str> {
    use std::arch::x86_64::__cpuid;

    // CPUID leaf 0x40000000 returns hypervisor signature
    let hypervisor_cpuid = unsafe { __cpuid(0x40000000) };
    let signature = [
        hypervisor_cpuid.ebx,
        hypervisor_cpuid.ecx,
        hypervisor_cpuid.edx,
    ];

    const VMWARE: [u32; 3] = [0x56_4D_77_61, 0x72_65_56_4D, 0x77_61_72_65];
    const HYPERV: [u32; 3] = [0x4D_69_63_72, 0x6F_73_6F_66, 0x74_20_48_76];
    const KVM: [u32; 3] = [0x4B_56_4D_4B, 0x56_4D_4B_56, 0x4D_4B_56_4D];
    const XEN: [u32; 3] = [0x58_65_6E_56, 0x4D_4D_58_65, 0x6E_56_4D_4D];

    match signature {
        VMWARE => Some("VMware"),
        HYPERV => Some("Microsoft Hyper-V"),
        KVM => Some("KVM"),
        XEN => Some("Xen"),
        _ => None,
    }
}

fn check_sys_hypervisor() -> bool {
    match fs::read_to_string("/sys/hypervisor/type") {
        Ok(content) => content.contains("xen") || content.contains("kvm"),
        Err(_) => false,
    }
}

fn check_dmesg_hypervisor() -> bool {
    Command::new("dmesg")
        .output()
        .map(|output| String::from_utf8_lossy(&output.stdout).contains("hypervisor"))
        .unwrap_or(false)
}

#[cfg(target_arch = "aarch64")]
fn is_hypervisor_present() -> bool {
    // Use lazy evaluation with || to short-circuit checks
    check_cpuinfo_hypervisor()
        || check_sys_hypervisor()
        || check_rdmsr_hypervisor()
        || check_dmesg_hypervisor()
        || check_device_tree_hypervisor()
}

#[cfg(target_arch = "aarch64")]
fn check_cpuinfo_hypervisor() -> bool {
    fs::read_to_string("/proc/cpuinfo")
        .map(|content| content.contains("hypervisor"))
        .unwrap_or(false)
}

#[cfg(target_arch = "aarch64")]
fn check_rdmsr_hypervisor() -> bool {
    Command::new("rdmsr")
        .arg("0xC0C")
        .output()
        .map(|output| String::from_utf8_lossy(&output.stdout).contains("hypervisor"))
        .unwrap_or(false)
}

#[cfg(target_arch = "aarch64")]
fn check_device_tree_hypervisor() -> bool {
    Command::new("cat")
        .arg("/proc/device-tree/hypervisor")
        .output()
        .map(|output| !String::from_utf8_lossy(&output.stdout).is_empty())
        .unwrap_or(false)
}

// TODO: add other arch support, such as riscv64
#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
fn is_hypervisor_present() -> bool {
    // Use const array for paths to check
    const VIRT_PATHS: [&str; 4] = ["/.dockerenv", "/proc/xen", "/proc/vz", "/proc/bc"];

    // Check cpuinfo first as it's most reliable
    if let Ok(content) = fs::read_to_string("/proc/cpuinfo") {
        if content.contains("hypervisor")
            || content.contains("virtualization")
            || content.contains("paravirtualized")
        {
            return true;
        }
    }

    if check_sys_hypervisor() {
        return true;
    }

    if check_dmesg_hypervisor() {
        return true;
    }

    if fs::metadata("/proc/device-tree/hypervisor").is_ok() {
        return true;
    }

    VIRT_PATHS.iter().any(|path| fs::metadata(path).is_ok())
}

/// Detects if the system is running in a virtual machine environment.
/// Uses various detection techniques inspired by Al-khaser and Pafish projects.
/// - Al-khaser: https://github.com/LordNoteworthy/al-khaser
/// - Pafish: https://github.com/a0rtega/pafish
fn determine_virtual_machine_status() -> bool {
    const CONTAINER_PATHS: [&str; 2] = ["/.dockerenv", "/.dockerinit"];

    if is_hypervisor_present() {
        return true;
    }

    if CONTAINER_PATHS
        .iter()
        .any(|path| fs::metadata(path).is_ok())
    {
        return true;
    }

    Command::new("systemctl")
        .arg("is-system-running")
        .output()
        .map(|output| String::from_utf8_lossy(&output.stdout).contains("running in container"))
        .unwrap_or(false)
}

fn get_root_device() -> Result<String> {
    let file = File::open("/proc/mounts")
        .map_err(|e| anyhow::anyhow!("Failed to open /proc/mounts: {}", e))?;
    let reader = BufReader::with_capacity(4096, file);

    for line in reader.lines() {
        let line = line?;
        let mut parts = line.split_whitespace().take(2);
        if let (Some(device), Some("/")) = (parts.next(), parts.next()) {
            if device.starts_with("/dev/") {
                return Ok(device[5..].to_string());
            }
            return Ok(device.to_string());
        }
    }
    bail!("Root device not found in /proc/mounts")
}

fn get_serial_number(disk_part_name: &str) -> Result<String> {
    let udev = udev::Udev::new()?;
    let mut enumerator = udev::Enumerator::with_udev(udev)?;

    enumerator.match_subsystem("block")?;
    enumerator.match_sysname(disk_part_name)?;

    let device = enumerator
        .scan_devices()?
        .next()
        .ok_or_else(|| anyhow::anyhow!("Device not found"))?;

    let parent = device
        .parent()
        .ok_or_else(|| anyhow::anyhow!("Failed to get parent device"))?;

    let serial = parent
        .property_value("ID_SERIAL")
        .ok_or_else(|| anyhow::anyhow!("Serial number not found"))?
        .to_str()
        .ok_or_else(|| anyhow::anyhow!("Invalid serial number encoding"))?
        .to_string();

    Ok(serial)
}

fn get_mac_addresses() -> Result<String> {
    let interfaces = datalink::interfaces();
    let mut mac_addresses = Vec::new();

    for iface in interfaces {
        if let Some(mac) = iface.mac {
            mac_addresses.push(format!("{}", mac));
        }
    }

    Ok(mac_addresses.join(", "))
}

fn read_bios_info<P: AsRef<Path>>(path: P) -> Result<BiosInfo> {
    let mut buffer = Vec::new();
    File::open(&path)?.read_to_end(&mut buffer)?;

    if buffer.len() < 2 {
        bail!("Buffer too small");
    }

    let length = buffer[1] as usize;
    if buffer.len() <= length {
        bail!("Invalid buffer length");
    }

    let unformatted_section = &buffer[length..];

    if buffer.len() <= 0x15 {
        bail!("Buffer too small for BIOS info");
    }

    Ok(BiosInfo {
        vendor: extract_string(unformatted_section, buffer[0x04])?,
        bios_version: extract_string(unformatted_section, buffer[0x05])?,
        bios_release_date: extract_string(unformatted_section, buffer[0x08])?,
        is_virtual_machine: (buffer[0x13] & 0x08) >> 3 == 1 || determine_virtual_machine_status(),
        system_bios_major_release: buffer[0x14].to_string(),
        system_bios_minor_release: buffer[0x15].to_string(),
    })
}

fn read_system_info<P: AsRef<Path>>(path: P) -> Result<SystemInfo> {
    let mut buffer = Vec::new();
    File::open(&path)?.read_to_end(&mut buffer)?;

    if buffer.len() < 2 {
        bail!("Buffer too small");
    }

    let length = buffer[1] as usize;
    if buffer.len() <= length {
        bail!("Invalid buffer length");
    }

    let unformed_section = &buffer[length..];

    if buffer.len() <= 0x17 {
        bail!("Buffer too small for system info");
    }

    Ok(SystemInfo {
        manufacturer: extract_string(unformed_section, buffer[0x04])?,
        product_name: extract_string(unformed_section, buffer[0x05])?,
        serial_number: extract_string(unformed_section, buffer[0x07])?,
        uuid: format!(
            "{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
            buffer[0x08], buffer[0x09], buffer[0x0a], buffer[0x0b],
            buffer[0x0c], buffer[0x0d], buffer[0x0e], buffer[0x0f],
            buffer[0x10], buffer[0x11], buffer[0x12], buffer[0x13],
            buffer[0x14], buffer[0x15], buffer[0x16], buffer[0x17]
        ),
    })
}

fn read_enclosure_info<P: AsRef<Path>>(path: P) -> Result<EnclosureInfo> {
    let mut buffer = Vec::new();
    File::open(&path)?.read_to_end(&mut buffer)?;

    if buffer.len() < 2 {
        bail!("Buffer too small");
    }

    let length = buffer[1] as usize;
    if buffer.len() <= length {
        bail!("Invalid buffer length");
    }

    let unformed_section = &buffer[length..];

    if buffer.len() <= 0x08 {
        bail!("Buffer too small for enclosure info");
    }

    Ok(EnclosureInfo {
        manufacturer: extract_string(unformed_section, buffer[0x04])?,
        enclosure_type: extract_string(unformed_section, buffer[0x05])?,
        version: extract_string(unformed_section, buffer[0x06])?,
        serial_number: extract_string(unformed_section, buffer[0x07])?,
        asset_tag_number: extract_string(unformed_section, buffer[0x08])?,
    })
}

fn extract_string(unformed_section: &[u8], index: u8) -> Result<String> {
    if index == 0 {
        return Ok(String::new());
    }

    let s = unformed_section
        .split(|&b| b == 0)
        .nth(index as usize - 1)
        .ok_or_else(|| anyhow::anyhow!("String not found"))?;

    Ok(String::from_utf8_lossy(s).into_owned())
}

#[cfg(test)]
mod tests {
    use super::*;
    use anyhow::Result;

    #[test]
    fn test_get_root_device() -> Result<()> {
        let root_device = get_root_device()?;
        assert!(!root_device.is_empty());
        Ok(())
    }

    #[test]
    fn test_get_serial_number() -> Result<()> {
        match get_root_device() {
            Ok(disk_part_name) => {
                match get_serial_number(&disk_part_name) {
                    Ok(serial_number) => {
                        assert!(!serial_number.is_empty());
                        Ok(())
                    }
                    Err(_) => Ok(()), // If we can't get the serial number, still pass the test
                }
            }
            Err(_) => Ok(()), // If root device doesn't exist, pass the test
        }
    }

    #[test]
    fn test_get_mac_addresses() -> Result<()> {
        let mac_addresses = get_mac_addresses()?;
        assert!(!mac_addresses.is_empty());
        Ok(())
    }

    #[test]
    fn test_get_bios_info() -> Result<()> {
        match read_bios_info(BIOS_INFO_PATH) {
            Ok(bios_info) => {
                assert!(!bios_info.vendor.is_empty());
                Ok(())
            }
            Err(_) => Ok(()),
        }
    }

    #[test]
    fn test_get_system_info() -> Result<()> {
        match read_system_info(SYSTEM_INFO_PATH) {
            Ok(system_info) => {
                assert!(!system_info.manufacturer.is_empty());
                Ok(())
            }
            Err(_) => Ok(()),
        }
    }

    #[test]
    fn test_get_enclosure_info() -> Result<()> {
        match read_enclosure_info(ENCLOSURE_INFO_PATH) {
            Ok(enclosure_info) => {
                assert!(!enclosure_info.manufacturer.is_empty());
                Ok(())
            }
            Err(_) => Ok(()),
        }
    }

    #[test]
    fn test_hardware_info_with_extra() -> Result<()> {
        let hardware_info =
            HardwareInfo::new()?.with_extra(serde_json::json!({"custom_field": "value"}));

        assert!(hardware_info.extra.is_some());
        assert_eq!(hardware_info.extra.unwrap()["custom_field"], "value");
        Ok(())
    }

    #[test]
    fn test_hardware_info_serialization() -> Result<()> {
        let hardware_info = HardwareInfo::new()?;
        let serialized = serde_json::to_string(&hardware_info)?;
        let deserialized: HardwareInfo = serde_json::from_str(&serialized)?;
        assert_eq!(hardware_info.cpu_is_virtual, deserialized.cpu_is_virtual);
        assert_eq!(
            hardware_info.disk_serial_number,
            deserialized.disk_serial_number
        );
        Ok(())
    }
}