Skip to content

Add clock_adjtime and Timex - #1755

Draft
windsornguyen wants to merge 1 commit into
bytecodealliance:mainfrom
windsornguyen:clock-adjtime
Draft

windsornguyen wants to merge 1 commit into
bytecodealliance:mainfrom
windsornguyen:clock-adjtime

Conversation

@windsornguyen

Copy link
Copy Markdown

This adds clock_adjtime for Linux. Today a program that wants to read the kernel's NTP state, such as whether STA_UNSYNC is set or the time daemon's maxerror and esterror, has to call libc::clock_adjtime or libc::adjtimex in an unsafe block, 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 in timex.modes and 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 no modes set, which needs no privileges.
  • Timex, with the layout of struct __kernel_timex from include/uapi/linux/timex.h. Its fields are 64-bit on every architecture. The padding is private, so callers start from Timex::default().
  • TimexModes (ADJ_*) and TimexStatus (STA_*) as bitflags, and ClockState (TIME_*) for the return value.

The linux_raw backend passes Timex straight to clock_adjtime64 on 32-bit targets and clock_adjtime on 64-bit ones. The libc backend converts to and from libc's struct timex. A value that doesn't fit libc's long fails with OVERFLOW instead of being truncated.

linux-raw-sys does not generate timex.h yet, 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:

  • The API is gated on target_os = "linux". Android's bionic has clock_adjtime, but the libc crate doesn't expose it for Android, so the libc backend can't call it there.
  • On 32-bit targets the linux_raw backend needs Linux 5.1's clock_adjtime64, and older kernels return NOSYS. clock_settime instead falls back to the old syscall. I can add the same fallback here, with the old 32-bit struct timex layout.
  • I didn't add an adjtimex alias, since it is clock_adjtime(ClockId::Realtime, ..). I can add it if you want it.
  • I can also take DynamicClockId, so this works on PTP hardware clocks, if you want that in this PR.

Tests, in tests/time/adjtime.rs:

  • Querying CLOCK_REALTIME succeeds. The kernel fills tolerance (500 ppm << 16) and precision (1) with constants, so these confirm that each field lands where Timex reads it.
  • Adjusting CLOCK_MONOTONIC fails, like the existing clock_settime test.

Checks run:

Target linux_raw use-libc
x86_64-unknown-linux-gnu cargo test --features=all-apis passes passes, plus the layout and constant tests
i686-unknown-linux-musl adjtime tests pass adjtime tests pass
i686-unknown-linux-gnu, aarch64-unknown-linux-gnu, armv7-unknown-linux-gnueabihf, x86_64-unknown-linux-musl cargo check --tests clean cargo check --tests clean

aarch64-linux-android cargo check --tests fails 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-by trailer.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant