Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ crc32fast = "1.1.2"
byteorder = "1.3.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
sysctl = "0.4.0"
33 changes: 27 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ use std::io::prelude::*;
use std::process;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
#[cfg(target_os = "macos")]
use sysctl::{Sysctl, CtlValue, SysctlError};

const ID_LEN: usize = 12;

Expand Down Expand Up @@ -152,7 +154,7 @@ impl Generator {
pub fn new_id_with_time(&self, t: SystemTime) -> Result<ID, IDGenerationError> {
match t.duration_since(UNIX_EPOCH) {
Ok(n) => Ok(self.generate(n.as_secs())),
Err(e) => Err(IDGenerationError(e.description().to_string())),
Err(e) => Err(IDGenerationError(e.to_string())),
}
}

Expand Down Expand Up @@ -450,16 +452,35 @@ fn read_machine_id() -> [u8; 3] {
[hash[0], hash[1], hash[2]]
}

#[cfg(target_os = "macos")]
fn platform_machine_id() -> Result<String, SysctlError> {
let ctl = sysctl::Ctl::new("kern.uuid")?;

Ok(ctl.value()?.to_string())
}

#[cfg(target_os = "linux")]
fn platform_machine_id() -> Result<String, io::Error> {
// XXX: unlikely to work if read with an unpriviledged user
let mut file = File::open("/sys/class/dmi/id/product_uuid")?;

let mut contents = String::new();

file.read_to_string(&mut contents)?;
// Try reading machine id first
let result = File::open("/etc/machine-id");

match result {
Ok(mut file) => {
file.read_to_string(&mut contents)?;

Ok(contents)
Ok(contents)
}
_ => {
// XXX: unlikely to work if read with an unprivileged user
let mut file = File::open("/sys/class/dmi/id/product_uuid")?;

file.read_to_string(&mut contents)?;

Ok(contents)
}
}
}

fn hostname() -> String {
Expand Down