Repository navigation
Add clock_adjtime and Timex - #1755
Draft
windsornguyen wants to merge 1 commit into
Draft
windsornguyen wants to merge 1 commit into
windsornguyen wants to merge 1 commit into
Conversation
Add `clock_adjtime`, which reads or adjusts a clock's kernel NTP state, and `clock_adjtime_query`, which only reads it. Reading needs no privileges, and it is how a process sees the clock's synchronization status and the time daemon's `maxerror` and `esterror`. `Timex` has the layout of Linux's `struct __kernel_timex`, with 64-bit fields on every architecture. The linux_raw backend passes it to `clock_adjtime64` on 32-bit targets and `clock_adjtime` on 64-bit ones. The libc backend converts it to and from libc's `struct timex`. The `ADJ_*` and `STA_*` constants are `TimexModes` and `TimexStatus` bitflags, and the returned `TIME_*` value is a `ClockState`. Signed-off-by: Windsor Nguyen <win@dedaluslabs.ai> Assisted-by: Claude:Opus-5.5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds
clock_adjtimefor Linux. Today a program that wants to read the kernel's NTP state, such as whetherSTA_UNSYNCis set or the time daemon'smaxerrorandesterror, has to calllibc::clock_adjtimeorlibc::adjtimexin anunsafeblock, since neither rustix nor nix wraps it.The commit adds:
rustix::time::clock_adjtime(id, &mut Timex) -> io::Result<ClockState>, which applies the fields named intimex.modesand reads the clock's full state back, as clock_adjtime(2) does.rustix::time::clock_adjtime_query(id) -> io::Result<(Timex, ClockState)>, the read-only case with nomodesset, which needs no privileges.Timex, with the layout ofstruct __kernel_timexfrominclude/uapi/linux/timex.h. Its fields are 64-bit on every architecture. The padding is private, so callers start fromTimex::default().TimexModes(ADJ_*) andTimexStatus(STA_*) as bitflags, andClockState(TIME_*) for the return value.The linux_raw backend passes
Timexstraight toclock_adjtime64on 32-bit targets andclock_adjtimeon 64-bit ones. The libc backend converts to and from libc'sstruct timex. A value that doesn't fit libc'slongfails withOVERFLOWinstead of being truncated.linux-raw-sys does not generate
timex.hyet, so the struct and constants are written out here. A unit test checks the struct's layout and every constant against the libc crate's definitions. I'm happy to move them into linux-raw-sys if you'd prefer that.Open questions:
target_os = "linux". Android's bionic hasclock_adjtime, but the libc crate doesn't expose it for Android, so the libc backend can't call it there.clock_adjtime64, and older kernels returnNOSYS.clock_settimeinstead falls back to the old syscall. I can add the same fallback here, with the old 32-bitstruct timexlayout.adjtimexalias, since it isclock_adjtime(ClockId::Realtime, ..). I can add it if you want it.DynamicClockId, so this works on PTP hardware clocks, if you want that in this PR.Tests, in
tests/time/adjtime.rs:CLOCK_REALTIMEsucceeds. The kernel fillstolerance(500 ppm << 16) andprecision(1) with constants, so these confirm that each field lands whereTimexreads it.CLOCK_MONOTONICfails, like the existingclock_settimetest.Checks run:
cargo test --features=all-apispassescargo check --testscleancargo check --testscleanaarch64-linux-androidcargo check --testsfails the same way on main (cannot find io_uring in crate), so it isn't related to this change.I wrote this with help from Claude (Opus 5.5), which drafted the code and tests, and I reviewed every line. The commit carries an
Assisted-bytrailer.