hyprshell_core_lib/util/
boot.rs

1use anyhow::Context;
2use std::fs::File;
3use std::io::Read;
4use std::sync::OnceLock;
5use tracing::{instrument, warn};
6
7pub fn get_boot_id() -> &'static Option<String> {
8    static BOOT_ID: OnceLock<Option<String>> = OnceLock::new();
9    BOOT_ID.get_or_init(|| {
10        load_boot_id().map_or_else(
11            |e| {
12                warn!("Failed to load boot ID: {e}");
13                None
14            },
15            Some,
16        )
17    })
18}
19
20#[instrument(level = "debug", ret(level = "trace"))]
21fn load_boot_id() -> anyhow::Result<String> {
22    let mut file = File::open("/proc/sys/kernel/random/boot_id")
23        .context("Failed to open /proc/sys/kernel/random/boot_id")?;
24    let mut contents = String::new();
25    file.read_to_string(&mut contents)
26        .context("Failed to read boot_id")?;
27    Ok(contents.trim().to_string())
28}