feat(linux): integrate real civil time, boot id, next_wakeup timer, and dbus notifications

This commit is contained in:
2026-10-02 17:00:31 +03:00
parent eee8c6b996
commit 2194c28ab5
6 changed files with 225 additions and 22 deletions
+29
View File
@@ -0,0 +1,29 @@
//! Read host boot identifier for tracking reboots without root privileges.
use blocker_core::engine::BootId;
use std::fs;
/// Read current system boot id from `/proc/sys/kernel/random/boot_id`.
/// If unavailable, falls back to `BootId(0)`.
pub fn current_boot_id() -> BootId {
if let Ok(content) = fs::read_to_string("/proc/sys/kernel/random/boot_id") {
let trimmed = content.trim().replace('-', "");
if trimmed.len() >= 16 {
if let Ok(id) = u64::from_str_radix(&trimmed[..16], 16) {
return BootId(id);
}
}
}
BootId(0)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reads_boot_id() {
let b = current_boot_id();
assert_ne!(b.0, 0);
}
}
+3
View File
@@ -39,8 +39,11 @@
//! - None of this can be made tamper-proof against a user with root. Section
//! 6.1 requires this be documented honestly rather than implied otherwise.
pub mod boot;
pub mod enforce;
pub mod kwin;
pub mod notify;
pub mod time;
pub mod watcher;
pub mod x11;
+31
View File
@@ -0,0 +1,31 @@
//! Desktop notifications for Blocker enforcement actions.
//!
//! Uses the standard freedesktop Notifications D-Bus specification
//! (`org.freedesktop.Notifications`).
use zbus::blocking::Connection;
/// Send a non-disruptive desktop notification when enforcement triggers.
pub fn send_notification(summary: &str, body: &str) -> Result<u32, zbus::Error> {
let connection = Connection::session()?;
let proxy = zbus::blocking::Proxy::new(
&connection,
"org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
"org.freedesktop.Notifications",
)?;
let id: u32 = proxy.call(
"Notify",
&(
"Blocker",
0u32,
"dialog-information",
summary,
body,
Vec::<String>::new(),
std::collections::HashMap::<String, zbus::zvariant::Value>::new(),
5000i32,
),
)?;
Ok(id)
}
+134
View File
@@ -0,0 +1,134 @@
//! Wall clock and timezone query implementation for Linux.
//!
//! Provides the current [`CivilTime`] with local timezone offset without pulling in
//! heavy third-party datetime crates, and purely in safe Rust (`forbid(unsafe_code)`).
use blocker_core::engine::CivilTime;
use std::fs;
use std::time::{SystemTime, UNIX_EPOCH};
/// Read current civil time and local UTC offset in minutes.
pub fn now() -> CivilTime {
let now = SystemTime::now();
let epoch_secs = now.duration_since(UNIX_EPOCH).map(|d| d.as_secs() as i64).unwrap_or(0);
let epoch_minutes = epoch_secs / 60;
let utc_offset_minutes = read_local_offset_minutes();
CivilTime { epoch_minutes, utc_offset_minutes }
}
/// Parse local UTC offset in minutes from `/etc/localtime` TZ string or standard fallback.
fn read_local_offset_minutes() -> i32 {
// Check TZ environment variable first
if let Ok(tz) = std::env::var("TZ") {
if let Some(offset) = parse_tz_string_offset(&tz) {
return offset;
}
}
// Try reading TZ footer string from /etc/localtime (POSIX TZ string at end of TZif file)
if let Ok(bytes) = fs::read("/etc/localtime") {
if let Some(offset) = parse_tzif_footer_offset(&bytes) {
return offset;
}
}
0
}
/// Parses the trailing newline-terminated POSIX TZ string in a TZif file (RFC 8536).
/// E.g. "\nMSK-3\n" -> +3 hours -> +180 minutes.
/// Note that POSIX TZ string syntax inverts the sign: "MSK-3" means GMT+3!
fn parse_tzif_footer_offset(bytes: &[u8]) -> Option<i32> {
if bytes.len() < 2 {
return None;
}
// Search backward for the last newline
let mut end = bytes.len();
if bytes[end - 1] == b'\n' {
end -= 1;
}
let start = bytes[..end].iter().rposition(|&b| b == b'\n')? + 1;
let tz_str = std::str::from_utf8(&bytes[start..end]).ok()?;
parse_tz_string_offset(tz_str)
}
/// Parses a POSIX TZ string like "MSK-3" or "UTC" or "EST5EDT".
fn parse_tz_string_offset(tz: &str) -> Option<i32> {
let bytes = tz.as_bytes();
let mut i = 0;
// Skip name (letters or <...>)
if i < bytes.len() && bytes[i] == b'<' {
i += 1;
while i < bytes.len() && bytes[i] != b'>' {
i += 1;
}
if i < bytes.len() {
i += 1;
}
} else {
while i < bytes.len() && (bytes[i].is_ascii_alphabetic()) {
i += 1;
}
}
if i >= bytes.len() {
return Some(0); // e.g. "UTC"
}
// Next is optional sign: '+' or '-'
let mut sign = 1; // Default no sign is positive in POSIX, which represents behind UTC (e.g. EST5 = UTC-5)
if bytes[i] == b'+' {
sign = 1;
i += 1;
} else if bytes[i] == b'-' {
sign = -1;
i += 1;
}
// Read hours
let h_start = i;
while i < bytes.len() && bytes[i].is_ascii_digit() {
i += 1;
}
if h_start == i {
return None;
}
let hours: i32 = std::str::from_utf8(&bytes[h_start..i]).ok()?.parse().ok()?;
let mut minutes = 0;
// Optional :mm
if i < bytes.len() && bytes[i] == b':' {
i += 1;
let m_start = i;
while i < bytes.len() && bytes[i].is_ascii_digit() {
i += 1;
}
if m_start < i {
minutes = std::str::from_utf8(&bytes[m_start..i]).ok()?.parse().ok().unwrap_or(0);
}
}
let total_mins = hours * 60 + minutes;
Some(-sign * total_mins)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_tz_examples() {
assert_eq!(parse_tz_string_offset("UTC"), Some(0));
assert_eq!(parse_tz_string_offset("MSK-3"), Some(180));
assert_eq!(parse_tz_string_offset("EST5"), Some(-300));
assert_eq!(parse_tz_string_offset("CST6CDT"), Some(-360));
}
#[test]
fn now_produces_reasonable_civil_time() {
let t = now();
assert!(t.epoch_minutes > 28_000_000);
assert!(t.utc_offset_minutes >= -720 && t.utc_offset_minutes <= 840);
}
}