Skip to content

Commit 1f129b7

Browse files
committed
timesyncd: when updating timestamp file, use best time we know, instead of system clock
We adjust the system clock slowly after all, even if we know a more accurate time. Use the accurate time for the timestamp.
1 parent 8705712 commit 1f129b7

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/timesync/timesyncd-manager.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "time-util.h"
3030
#include "timesyncd-conf.h"
3131
#include "timesyncd-manager.h"
32+
#include "user-util.h"
3233
#include "util.h"
3334

3435
#ifndef ADJ_SETOFFSET
@@ -60,7 +61,7 @@ static int manager_arm_timer(Manager *m, usec_t next);
6061
static int manager_clock_watch_setup(Manager *m);
6162
static int manager_listen_setup(Manager *m);
6263
static void manager_listen_stop(Manager *m);
63-
static int manager_save_time_and_rearm(Manager *m);
64+
static int manager_save_time_and_rearm(Manager *m, usec_t t);
6465

6566
static double ntp_ts_short_to_d(const struct ntp_ts_short *ts) {
6667
return be16toh(ts->sec) + (be16toh(ts->frac) / 65536.0);
@@ -411,6 +412,7 @@ static int manager_receive_response(sd_event_source *source, int fd, uint32_t re
411412
.msg_namelen = sizeof(server_addr),
412413
};
413414
struct timespec *recv_time = NULL;
415+
triple_timestamp dts;
414416
ssize_t len;
415417
double origin, receive, trans, dest, delay, offset, root_distance;
416418
bool spike;
@@ -564,12 +566,18 @@ static int manager_receive_response(sd_event_source *source, int fd, uint32_t re
564566
m->samples_jitter, spike ? " spike" : "",
565567
m->poll_interval_usec / USEC_PER_SEC);
566568

569+
/* Get current monotonic/realtime clocks immediately before adjusting the latter */
570+
triple_timestamp_get(&dts);
571+
567572
if (!spike) {
573+
/* Fix up our idea of the time. */
574+
dts.realtime = (usec_t) (dts.realtime + offset * USEC_PER_SEC);
575+
568576
r = manager_adjust_clock(m, offset, leap_sec);
569577
if (r < 0)
570578
log_error_errno(r, "Failed to call clock_adjtime(): %m");
571579

572-
(void) manager_save_time_and_rearm(m);
580+
(void) manager_save_time_and_rearm(m, dts.realtime);
573581

574582
/* If touch fails, there isn't much we can do. Maybe it'll work next time. */
575583
(void) touch("/run/systemd/timesync/synchronized");
@@ -1124,7 +1132,7 @@ static int manager_save_time_handler(sd_event_source *s, uint64_t usec, void *us
11241132

11251133
assert(m);
11261134

1127-
(void) manager_save_time_and_rearm(m);
1135+
(void) manager_save_time_and_rearm(m, USEC_INFINITY);
11281136
return 0;
11291137
}
11301138

@@ -1152,12 +1160,16 @@ int manager_setup_save_time_event(Manager *m) {
11521160
return 0;
11531161
}
11541162

1155-
static int manager_save_time_and_rearm(Manager *m) {
1163+
static int manager_save_time_and_rearm(Manager *m, usec_t t) {
11561164
int r;
11571165

11581166
assert(m);
11591167

1160-
r = touch(CLOCK_FILE);
1168+
/* Updates the timestamp file to the specified time. If 't' is USEC_INFINITY uses the current system
1169+
* clock, but otherwise uses the specified timestamp. Note that whenever we acquire an NTP sync the
1170+
* specified timestamp value might be more accurate than the system clock, since the latter is
1171+
* subject to slow adjustments. */
1172+
r = touch_file(CLOCK_FILE, false, t, UID_INVALID, GID_INVALID, MODE_INVALID);
11611173
if (r < 0)
11621174
log_debug_errno(r, "Failed to update " CLOCK_FILE ", ignoring: %m");
11631175

0 commit comments

Comments
 (0)