sysinfo/unix/linux/
product.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3pub(crate) struct ProductInner;
4
5impl ProductInner {
6    pub(crate) fn family() -> Option<String> {
7        std::fs::read_to_string("/sys/devices/virtual/dmi/id/product_family")
8            .ok()
9            .map(|s| s.trim().to_owned())
10    }
11
12    pub(crate) fn name() -> Option<String> {
13        std::fs::read_to_string("/sys/devices/virtual/dmi/id/product_name")
14            .ok()
15            .or_else(|| {
16                std::fs::read_to_string("/sys/firmware/devicetree/base/model")
17                    .ok()
18                    .or_else(|| {
19                        std::fs::read_to_string("/sys/firmware/devicetree/base/banner-name").ok()
20                    })
21                    .or_else(|| std::fs::read_to_string("/tmp/sysinfo/model").ok())
22                    .map(|s| s.trim_end_matches('\0').to_owned())
23            })
24            .map(|s| s.trim().to_owned())
25    }
26
27    pub(crate) fn serial_number() -> Option<String> {
28        std::fs::read_to_string("/sys/devices/virtual/dmi/id/product_serial")
29            .ok()
30            .or_else(|| {
31                std::fs::read_to_string("/sys/firmware/devicetree/base/serial-number")
32                    .ok()
33                    .map(|s| s.trim_end_matches('\0').to_owned())
34            })
35            .map(|s| s.trim().to_owned())
36    }
37
38    pub(crate) fn stock_keeping_unit() -> Option<String> {
39        std::fs::read_to_string("/sys/devices/virtual/dmi/id/product_sku")
40            .ok()
41            .map(|s| s.trim().to_owned())
42    }
43
44    pub(crate) fn uuid() -> Option<String> {
45        std::fs::read_to_string("/sys/devices/virtual/dmi/id/product_uuid")
46            .ok()
47            .map(|s| s.trim().to_owned())
48    }
49
50    pub(crate) fn version() -> Option<String> {
51        std::fs::read_to_string("/sys/devices/virtual/dmi/id/product_version")
52            .ok()
53            .map(|s| s.trim().to_owned())
54    }
55
56    pub(crate) fn vendor_name() -> Option<String> {
57        std::fs::read_to_string("/sys/devices/virtual/dmi/id/sys_vendor")
58            .ok()
59            .map(|s| s.trim().to_owned())
60    }
61}