-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from flokli/machine-id
src/system: use /etc/machine-id as machine identifier
- Loading branch information
Showing
5 changed files
with
72 additions
and
31 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
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 |
---|---|---|
@@ -1,31 +1,17 @@ | ||
use std::{ | ||
fs::File, | ||
io::{BufRead, BufReader}, | ||
}; | ||
use tracing::{instrument, warn}; | ||
|
||
use crate::common::Error; | ||
const MACHINE_ID_PATH: &'static str = "/etc/machine-id"; | ||
|
||
#[cfg(debug_assertions)] | ||
const CPU_INFO_PATH: &'static str = "./cpuinfo"; | ||
|
||
#[cfg(not(debug_assertions))] | ||
const CPU_INFO_PATH: &'static str = "/proc/cpuinfo"; | ||
|
||
pub(crate) fn get_cpu_serial() -> Result<String, Error> { | ||
let file = | ||
File::open(CPU_INFO_PATH).map_err(|e| Error::FileIoError(CPU_INFO_PATH.into(), e))?; | ||
let reader = BufReader::new(file); | ||
let mut lines = reader.lines(); | ||
|
||
while let Some(Ok(line)) = lines.next() { | ||
if line.starts_with("Serial") { | ||
let (_, serial) = line | ||
.split_once(':') | ||
.ok_or(Error::ParsingError(CPU_INFO_PATH.into()))?; | ||
|
||
return Ok(serial.trim().to_string()); | ||
} | ||
/// Returns the system machine id | ||
/// (https://www.freedesktop.org/software/systemd/man/latest/machine-id.html) | ||
/// Falls back to a random uuid if the file doesn't exist, or another error | ||
/// occurs during retrieval. It logs a warning in this case. | ||
#[instrument(ret, err)] | ||
pub(crate) fn get_machine_id() -> std::io::Result<String> { | ||
if let Ok(machine_id) = std::fs::read_to_string(MACHINE_ID_PATH) { | ||
Ok(machine_id.trim().to_string()) | ||
} else { | ||
warn!("unable to read machine id, setting a random one"); | ||
Ok(uuid::Uuid::new_v4().to_string()) | ||
} | ||
|
||
Err(Error::ParsingError(CPU_INFO_PATH.into())) | ||
} |