From 760285218f2dc0a86da23b059ab0ef1b5b0e0f03 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Mon, 10 Jul 2017 15:51:06 +0200 Subject: [PATCH] sys_timex: fix update of TAI offset on non-Linux systems The tai field in struct timex is a Linux-specific feature. It's possible to read the current offset with ntp_gettime() (or ntp_gettimex() on Linux), but apparently not all libc implementations support it. Rework the code to save and adjust the last value instead of reading the current value from the kernel. --- sys_timex.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/sys_timex.c b/sys_timex.c index f41dcc9..4ecd70e 100644 --- a/sys_timex.c +++ b/sys_timex.c @@ -63,6 +63,9 @@ /* Saved timex status */ static int sys_status; +/* Saved TAI-UTC offset */ +static int sys_tai_offset; + /* ================================================== */ static double @@ -115,10 +118,17 @@ set_leap(int leap, int tai_offset) txc.status = sys_status; #ifdef MOD_TAI - if (tai_offset && tai_offset != txc.tai) { + if (tai_offset) { txc.modes |= MOD_TAI; txc.constant = tai_offset; - LOG(LOGS_INFO, "System clock TAI offset set to %d seconds", tai_offset); + + if (applied && !(sys_status & (STA_INS | STA_DEL))) + sys_tai_offset += prev_status & STA_INS ? 1 : -1; + + if (sys_tai_offset != tai_offset) { + sys_tai_offset = tai_offset; + LOG(LOGS_INFO, "System clock TAI offset set to %d seconds", tai_offset); + } } #endif @@ -177,6 +187,7 @@ initialise_timex(void) struct timex txc; sys_status = STA_UNSYNC; + sys_tai_offset = 0; /* Reset PLL offset */ txc.modes = MOD_OFFSET | MOD_STATUS;