use crate::{ClockDrive, ClockError, ClockRead};
use rusty_time_core::ClockCommand;
pub struct SystemClock;
const SLEW_RESOLUTION_PPM: f64 = 1.0 / 65_536.0;
fn nominal_tick_us() -> i64 {
let hz = unsafe { libc::sysconf(libc::_SC_CLK_TCK) };
if hz <= 0 {
10_000
} else {
1_000_000 / hz as i64
}
}
fn max_slew_ppm() -> f64 {
crate::slew::linux_max_slew_ppm(nominal_tick_us())
}
fn errno_detail(op: &'static str) -> ClockError {
ClockError {
op,
detail: std::io::Error::last_os_error().to_string(),
}
}
pub fn can_discipline() -> bool {
if unsafe { libc::geteuid() } == 0 {
return true;
}
let Ok(status) = std::fs::read_to_string("/proc/self/status") else {
return false;
};
status
.lines()
.find_map(|line| line.strip_prefix("CapEff:"))
.and_then(|hex| u64::from_str_radix(hex.trim(), 16).ok())
.map(|caps| caps & (1 << 25) != 0)
.unwrap_or(false)
}
pub(crate) fn platform_capabilities() -> crate::ClockCapabilities {
crate::ClockCapabilities {
os: "linux",
arch: std::env::consts::ARCH,
can_read: true,
can_discipline: can_discipline(),
discipline_requirement: "CAP_SYS_TIME (run as root, or grant the binary the file \
capability: setcap cap_sys_time+ep /usr/sbin/rtimed)",
slew_resolution_ppm: Some(SLEW_RESOLUTION_PPM),
max_slew_ppm: max_slew_ppm(),
batch_receive: true,
mono_resolution_ns: None,
}
}
impl ClockRead for SystemClock {
fn wall_ns(&self) -> Result<i128, ClockError> {
let mut ts = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
let rc = unsafe { libc::clock_gettime(libc::CLOCK_REALTIME, &mut ts) };
if rc != 0 {
return Err(errno_detail("clock_gettime(REALTIME)"));
}
Ok(ts.tv_sec as i128 * 1_000_000_000 + ts.tv_nsec as i128)
}
fn wall_parts(&self) -> Result<(i64, u32), ClockError> {
let mut ts = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
let rc = unsafe { libc::clock_gettime(libc::CLOCK_REALTIME, &mut ts) };
if rc != 0 {
return Err(errno_detail("clock_gettime(REALTIME)"));
}
Ok((ts.tv_sec, ts.tv_nsec as u32))
}
fn mono_s(&self) -> Result<f64, ClockError> {
let mut ts = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
let rc = unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC_RAW, &mut ts) };
if rc != 0 {
return Err(errno_detail("clock_gettime(MONOTONIC_RAW)"));
}
Ok(ts.tv_sec as f64 + ts.tv_nsec as f64 * 1e-9)
}
}
const FREQ_SCALE: f64 = 65_536.0;
impl ClockDrive for SystemClock {
fn apply(&mut self, cmd: &ClockCommand) -> Result<(), ClockError> {
match *cmd {
ClockCommand::Step { add_seconds } => {
let mut tx: libc::timex = unsafe { core::mem::zeroed() };
tx.modes = libc::ADJ_SETOFFSET | libc::ADJ_NANO;
let ns_total = (add_seconds * 1e9) as i64;
let mut sec = ns_total / 1_000_000_000;
let mut nsec = ns_total % 1_000_000_000;
if nsec < 0 {
sec -= 1;
nsec += 1_000_000_000;
}
tx.time.tv_sec = sec;
tx.time.tv_usec = nsec; let rc = unsafe { libc::clock_adjtime(libc::CLOCK_REALTIME, &mut tx) };
if rc < 0 {
return Err(errno_detail("clock_adjtime(ADJ_SETOFFSET)"));
}
Ok(())
}
ClockCommand::Slew {
freq_ppm,
drain_offset,
drain_rate_ppm,
} => {
let total_ppm =
crate::slew::total_ppm(freq_ppm, drain_offset, drain_rate_ppm, max_slew_ppm());
let (tick_us, residual_ppm) =
crate::slew::linux_tick_and_freq(total_ppm, nominal_tick_us());
let mut tx: libc::timex =
unsafe { core::mem::zeroed() };
tx.modes = libc::ADJ_FREQUENCY | libc::ADJ_TICK;
tx.freq = (residual_ppm * FREQ_SCALE) as libc::c_long;
tx.tick = tick_us as libc::c_long;
let rc = unsafe { libc::clock_adjtime(libc::CLOCK_REALTIME, &mut tx) };
if rc < 0 {
return Err(errno_detail("clock_adjtime(ADJ_FREQUENCY|ADJ_TICK)"));
}
Ok(())
}
}
}
}