-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[release/1.2] Move system information gathering to common crate (#353)
*Cf.* #342.
- Loading branch information
1 parent
fb2770e
commit 8acaa7a
Showing
5 changed files
with
163 additions
and
108 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
use std::env::consts::ARCH; | ||
use std::fs; | ||
use std::io::{self, BufRead}; | ||
|
||
use serde::Serialize; | ||
use serde_with::skip_serializing_none; | ||
|
||
#[cfg(target_pointer_width = "32")] | ||
const BITNESS: usize = 32; | ||
#[cfg(target_pointer_width = "64")] | ||
const BITNESS: usize = 64; | ||
#[cfg(target_pointer_width = "128")] | ||
const BITNESS: usize = 128; | ||
|
||
/// A subset of the DMI variables exposed through /sys/devices/virtual/dmi/id. | ||
/// | ||
/// Examples: | ||
/// | ||
/// ```ignore | ||
/// Host | name | vendor | ||
/// ---------+-----------------+----------------------- | ||
/// Hyper-V | Virtual Machine | Microsoft Corporation | ||
/// ``` | ||
/// | ||
/// Ref: <https://www.kernel.org/doc/html/latest/filesystems/sysfs.html> | ||
#[derive(Clone, Debug, Serialize)] | ||
pub struct DmiInfo { | ||
pub product: Option<String>, | ||
pub vendor: Option<String>, | ||
} | ||
|
||
impl Default for DmiInfo { | ||
fn default() -> Self { | ||
Self { | ||
product: try_read_dmi("product_name"), | ||
vendor: try_read_dmi("sys_vendor"), | ||
} | ||
} | ||
} | ||
|
||
/// A subset of the fields from /etc/os-release. | ||
/// | ||
/// Examples: | ||
/// | ||
/// ```ignore | ||
/// OS | id | version_id | ||
/// ---------------------+---------------------+------------ | ||
/// CentOS 7 | centos | 7 | ||
/// Debian 9 | debian | 9 | ||
/// openSUSE Tumbleweed | opensuse-tumbleweed | 20190325 | ||
/// Ubuntu 18.04 | ubuntu | 18.04 | ||
/// ``` | ||
/// | ||
/// Ref: <https://www.freedesktop.org/software/systemd/man/os-release.html> | ||
#[skip_serializing_none] | ||
#[derive(Clone, Debug, Serialize)] | ||
pub struct OsInfo { | ||
pub id: Option<String>, | ||
pub version_id: Option<String>, | ||
pub variant_id: Option<String>, | ||
pub build_id: Option<String>, | ||
pub arch: &'static str, | ||
pub bitness: usize, | ||
} | ||
|
||
impl Default for OsInfo { | ||
fn default() -> Self { | ||
let mut result = Self { | ||
id: None, | ||
version_id: None, | ||
variant_id: None, | ||
build_id: None, | ||
arch: ARCH, | ||
bitness: BITNESS, | ||
}; | ||
|
||
let os_release = | ||
fs::File::open("/etc/os-release").or_else(|_| fs::File::open("/usr/lib/os-release")); | ||
|
||
if let Ok(os_release) = os_release { | ||
let os_release = io::BufReader::new(os_release); | ||
|
||
for line in os_release.lines() { | ||
if let Ok(line) = &line { | ||
match parse_shell_line(line) { | ||
Some(("ID", value)) => { | ||
result.id = Some(value.to_owned()); | ||
} | ||
Some(("VERSION_ID", value)) => { | ||
result.version_id = Some(value.to_owned()); | ||
} | ||
Some(("VARIANT_ID", value)) => { | ||
result.variant_id = Some(value.to_owned()); | ||
} | ||
Some(("BUILD_ID", value)) => { | ||
result.build_id = Some(value.to_owned()); | ||
} | ||
_ => (), | ||
}; | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
pub fn parse_shell_line(line: &str) -> Option<(&str, &str)> { | ||
let line = line.trim(); | ||
|
||
let pos = line.find('=')?; | ||
let (key, value) = (&line[..pos], &line[pos + 1..]); | ||
|
||
// The value is essentially a shell string, so it can be quoted in single or | ||
// double quotes, and can have escaped sequences using backslash. | ||
// For simplicitly, just trim the quotes instead of implementing a full shell | ||
// string grammar. | ||
let value = if (value.starts_with('\'') && value.ends_with('\'')) | ||
|| (value.starts_with('"') && value.ends_with('"')) | ||
{ | ||
&value[1..(value.len() - 1)] | ||
} else { | ||
value | ||
}; | ||
|
||
Some((key, value)) | ||
} | ||
|
||
fn try_read_dmi(entry: &'static str) -> Option<String> { | ||
let path = format!("/sys/devices/virtual/dmi/id/{}", entry); | ||
|
||
let bytes = fs::read(path).ok()?; | ||
|
||
Some(std::str::from_utf8(&bytes).ok()?.trim().to_owned()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters