NTP_ADJTIME(2)
π NTP_ADJTIME β the system calls that let the NTP daemon vibe-check your clock π
okay bestie, welcome to ntp_adjtime(2) and ntp_gettime(2) β the Network Time Protocol (NTP) daemon interface system calls, living their best life in section 2 of the System Calls Manual. π°οΈβ¨
πΈ LIBRARY πΈ
Standard C Library (libc, -lc). she's classic, she's everywhere, she's giving main character energy fr fr π
β¨ SYNOPSIS β¨
#include <sys/timex.h>
int
ntp_adjtime(struct timex *);
int
ntp_gettime(struct ntptimeval *);
π¦ DESCRIPTION π¦
so. these two system calls β ntp_adjtime() and ntp_gettime() β are literally the kernel interface to the Network Time Protocol (NTP) daemon, ntpd(8). that's the whole vibe. the kernel and the NTP daemon are texting through these syscalls and honestly? iconic. π±
ntp_adjtime() is what the NTP daemon uses to adjust the system clock to some externally derived time. and the time offset + related variables that ntp_adjtime() sets? those get used by hardclock() to adjust the phase and frequency of the phase-lock loop (PLL) or frequency-lock loop (FLL) β whichever one is controlling your system clock. no cap, this is the machinery. π§
ntp_gettime() is the friendly one β it hands the time, the maximum error (sync distance), and the estimated error (dispersion) over to client user application programs. so generous of her. π«Ά
heads up bestie: everything below that refers to PPS is ONLY relevant if the PPS_SYNC option is enabled in the kernel. otherwise it's just decorative lore. π
ntp_adjtime() takes as its argument a struct timex *, shaped exactly like this:
struct timex {
unsigned int modes; /* clock mode bits (wo) */
long offset; /* time offset (us) (rw) */
long freq; /* frequency offset (scaled ppm) (rw) */
long maxerror; /* maximum error (us) (rw) */
long esterror; /* estimated error (us) (rw) */
int status; /* clock status bits (rw) */
long constant; /* pll time constant (rw) */
long precision; /* clock precision (us) (ro) */
long tolerance; /* clock frequency tolerance (scaled
* ppm) (ro) */
/*
* The following read-only structure members are implemented
* only if the PPS signal discipline is configured in the
* kernel.
*/
long ppsfreq; /* pps frequency (scaled ppm) (ro) */
long jitter; /* pps jitter (us) (ro) */
int shift; /* interval duration (s) (shift) (ro) */
long stabil; /* pps stability (scaled ppm) (ro) */
long jitcnt; /* jitter limit exceeded (ro) */
long calcnt; /* calibration intervals (ro) */
long errcnt; /* calibration errors (ro) */
long stbcnt; /* stability limit exceeded (ro) */
};
π the members, explained with love π
here's what each member of that struct actually means when it's used as the argument for ntp_adjtime():
modesβ defines what settings should be changed with the currentntp_adjtime()call (write-only). it's a bitwise OR of these:MOD_OFFSETβ set time offsetMOD_FREQUENCYβ set frequency offsetMOD_MAXERRORβ set maximum time errorMOD_ESTERRORβ set estimated time errorMOD_STATUSβ set clock status bitsMOD_TIMECONSTβ set PLL time constantMOD_CLKAβ set clock AMOD_CLKBβ set clock B
offsetβ time offset (in microseconds), used by the PLL/FLL to nudge the system time along in small increments (read-write).freqβ frequency offset (scaled ppm) (read-write).maxerrorβ maximum error (in microseconds). it gets initialized by anntp_adjtime()call, and then the kernel bumps it up once each second to reflect the maximum error bound growing (read-write).esterrorβ estimated error (in microseconds). set and read byntp_adjtime(), but the kernel literally doesn't use it (read-write). she's there for the plot.statusβ system clock status bits (read-write). bitwise OR of these:STA_PLLβ Enable PLL updates (read-write).STA_PPSFREQβ Enable PPS freq discipline (read-write).STA_PPSTIMEβ Enable PPS time discipline (read-write).STA_FLLβ Select frequency-lock mode (read-write).STA_INSβ Insert leap (read-write).STA_DELβ Delete leap (read-write).STA_UNSYNCβ Clock unsynchronized (read-write).STA_FREQHOLDβ Hold frequency (read-write).STA_PPSSIGNALβ PPS signal present (read-only).STA_PPSJITTERβ PPS signal jitter exceeded (read-only).STA_PPSWANDERβ PPS signal wander exceeded (read-only).STA_PPSERRORβ PPS signal calibration error (read-only).STA_CLOCKERRβ Clock hardware fault (read-only).
constantβ PLL time constant. this determines the bandwidth, or βstiffnessβ, of the PLL (read-write).precisionβ clock precision (in microseconds). in most cases it's the same as the kernel tick variable (peephz(9)). if a precision clock counter or external time-keeping signal is available, it could be much lower β and it could depend on the state of the signal (read-only).toleranceβ maximum frequency error, aka the tolerance of the CPU clock oscillator (scaled ppm). ordinarily it's a property of the architecture, but it could change under the influence of external time-keeping signals (read-only).ppsfreqβ PPS frequency offset produced by the frequency median filter (scaled ppm) (read-only).jitterβ PPS jitter measured by the time median filter, in microseconds (read-only).shiftβ logarithm to base 2 of the interval duration in seconds (PPS, read-only).stabilβ PPS stability (scaled ppm); dispersion (wander) measured by the frequency median filter (read-only).jitcntβ number of seconds that have been discarded because the jitter measured by the time median filter exceeded the limitMAXTIME(PPS, read-only).calcntβ count of calibration intervals (PPS, read-only).errcntβ number of calibration intervals that have been discarded because the wander exceeded the limitMAXFREQ, or where the calibration interval jitter exceeded two ticks (PPS, read-only).stbcntβ number of calibration intervals that have been discarded because the frequency wander exceeded the limitMAXFREQ/4(PPS, read-only).
and after the ntp_adjtime() call goes through, the struct timex * structure holds the current values of the corresponding variables. she updates herself, periodt. β¨
ntp_gettime() takes as its argument a struct ntptimeval * with these members:
struct ntptimeval {
struct timeval time; /* current time (ro) */
long maxerror; /* maximum error (us) (ro) */
long esterror; /* estimated error (us) (ro) */
};
and here's what they mean:
timeβ current time (read-only).maxerrorβ maximum error in microseconds (read-only).esterrorβ estimated error in microseconds (read-only).
π₯ RETURN VALUES π₯
ntp_adjtime() and ntp_gettime() return the current state of the clock on success β or any of the errors of copyin(9) and copyout(9). and ntp_adjtime() may additionally return EPERM if the user calling ntp_adjtime() doesn't have sufficient permissions. rude but valid. π
possible states of the clock are:
TIME_OKβ Everything okay, no leap second warning. she's thriving. πTIME_INSβ the βinsert leap secondβ warning. at the end of the day, a leap second will be inserted after23:59:59.TIME_DELβ the βdelete leap secondβ warning. at the end of the day, second23:59:59will be skipped. bye girl πTIME_OOPβ leap second in progress.TIME_WAITβ leap second has occurred within the last few seconds.TIME_ERRORβ clock not synchronized.
π ERRORS π
the ntp_adjtime() system call may return EPERM if the caller does not have sufficient permissions. that's it, that's the list. know your place bestie. π
π SEE ALSO π
options(4)ntpd(8)hardclock(9)hz(9)http://www.bipm.fr/enus/5_Scientific/c_time/time_1.htmlhttp://www.boulder.nist.gov/timefreq/general/faq.htmftp://time.nist.gov/pub/leap-seconds.list
π¨ BUGS π¨
take note, bestie: this API is extremely complex and stateful. users should not attempt modification without first reviewing the ntpd(8) sources in depth. like, in depth depth. do not freestyle this one, i am begging. π
and that's the whole page, no notes. this little slice of the System Calls Manual is brought to you by the good people at Debian, dated July 13, 2005 β yeah she's an elder but she still ate π β¨ stay synced bestie, mwah ππ¦πΈ
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2001 The NetBSD Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms