Skip to content

Commit

Permalink
Merge pull request #12 from flokli/machine-id
Browse files Browse the repository at this point in the history
src/system: use /etc/machine-id as machine identifier
  • Loading branch information
flokli authored Jul 12, 2024
2 parents 3212fdc + e82a5d0 commit 082b83e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 31 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,11 @@ rec {
packageId = "tracing-subscriber";
features = [ "env-filter" ];
}
{
name = "uuid";
packageId = "uuid";
features = [ "v4" ];
}
{
name = "wry";
packageId = "wry";
Expand Down Expand Up @@ -7363,6 +7368,47 @@ rec {
features = { };
resolvedDefaultFeatures = [ "default" ];
};
"uuid" = rec {
crateName = "uuid";
version = "1.10.0";
edition = "2018";
sha256 = "0503gvp08dh5mnm3f0ffqgisj6x3mbs53dmnn1lm19pga43a1pw1";
authors = [
"Ashley Mannix<ashleymannix@live.com.au>"
"Dylan DPC<dylan.dpc@gmail.com>"
"Hunar Roop Kahlon<hunar.roop@gmail.com>"
];
dependencies = [
{
name = "getrandom";
packageId = "getrandom 0.2.15";
optional = true;
}
];
features = {
"arbitrary" = [ "dep:arbitrary" ];
"atomic" = [ "dep:atomic" ];
"borsh" = [ "dep:borsh" "dep:borsh-derive" ];
"bytemuck" = [ "dep:bytemuck" ];
"default" = [ "std" ];
"fast-rng" = [ "rng" "dep:rand" ];
"js" = [ "dep:wasm-bindgen" "getrandom?/js" ];
"macro-diagnostics" = [ "dep:uuid-macro-internal" ];
"md5" = [ "dep:md-5" ];
"rng" = [ "dep:getrandom" ];
"serde" = [ "dep:serde" ];
"sha1" = [ "dep:sha1_smol" ];
"slog" = [ "dep:slog" ];
"v1" = [ "atomic" ];
"v3" = [ "md5" ];
"v4" = [ "rng" ];
"v5" = [ "sha1" ];
"v6" = [ "atomic" ];
"v7" = [ "rng" ];
"zerocopy" = [ "dep:zerocopy" ];
};
resolvedDefaultFeatures = [ "default" "rng" "std" "v4" ];
};
"valuable" = rec {
crateName = "valuable";
version = "0.1.0";
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ tao = "0.28.1"
thiserror = "1.0.62"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
uuid = { version = "1.10.0", features = ["v4"] }
wry = "0.41.0"

# FUTUREWORK: Somehow rustc wants to link this against zlib.
Expand Down
6 changes: 2 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ fn main() -> color_eyre::eyre::Result<()> {

let cli = Cli::parse();

debug!("Acquiring the CPU serial number...");
let serial = system::get_cpu_serial().context("getting cpu serial")?;
debug!(%serial, "got CPU serial number");
let machine_id = system::get_machine_id().context("getting machine id")?;

debug!("Loading the config");
let config = Config::load(cli.default_config_path.map(|p| PathBuf::from(p)))
Expand All @@ -44,7 +42,7 @@ fn main() -> color_eyre::eyre::Result<()> {
let (sender, receiver) = channel();

let listener = mqtt::Listener {
id: config.id.unwrap_or(serial),
id: config.id.unwrap_or(machine_id),
host: config.host,
port: config.port,
sender,
Expand Down
40 changes: 13 additions & 27 deletions src/system.rs
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()))
}

0 comments on commit 082b83e

Please sign in to comment.