diff --git a/Cargo.toml b/Cargo.toml index f734454..2ae9c52 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index ea221b3..122ad4c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -152,7 +154,7 @@ impl Generator { pub fn new_id_with_time(&self, t: SystemTime) -> Result { 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())), } } @@ -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 { + let ctl = sysctl::Ctl::new("kern.uuid")?; + + Ok(ctl.value()?.to_string()) +} + #[cfg(target_os = "linux")] fn platform_machine_id() -> Result { - // 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 {