Skip to content

Commit b016e77

Browse files
committed
timesyncd: generate a structure log message the first time we set the clock correctly
Usecase: later on we can use this to retroactively adjust log output in journalctl or similar on systems lacking an RTC: we just have to search for this sructured log message that indicates the first sync point and can then retroactively adjust the incorrect timestamps collected before that.
1 parent 1aa7ca2 commit b016e77

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

catalog/systemd.catalog.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,11 @@ be updated to operate in a hotplug fashion without depending on
526526
systemd-udev-settle.service:
527527

528528
@OFFENDING_UNITS@
529+
530+
-- 7c8a41f37b764941a0e1780b1be2f037
531+
Subject: Initial clock synchronization
532+
Defined-By: systemd
533+
Support: %SUPPORT_URL%
534+
535+
For the first time during the current boot an NTP synchronization has been
536+
acquired and the local system clock adjustment has been initiated.

src/systemd/sd-messages.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ _SD_BEGIN_DECLARATIONS;
200200
#define SD_MESSAGE_SYSTEMD_UDEV_SETTLE_DEPRECATED_STR \
201201
SD_ID128_MAKE_STR(1c,04,54,c1,bd,22,41,e0,ac,6f,ef,b4,bc,63,14,33)
202202

203+
#define SD_MESSAGE_TIME_SYNC SD_ID128_MAKE(7c,8a,41,f3,7b,76,49,41,a0,e1,78,0b,1b,e2,f0,37)
204+
#define SD_MESSAGE_TIME_SYNC_STR SD_ID128_MAKE_STR(7c,8a,41,f3,7b,76,49,41,a0,e1,78,0b,1b,e2,f0,37)
205+
203206
_SD_END_DECLARATIONS;
204207

205208
#endif

src/timesync/timesyncd-manager.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <sys/types.h>
1212

1313
#include "sd-daemon.h"
14+
#include "sd-messages.h"
1415

1516
#include "alloc-util.h"
1617
#include "dns-domain.h"
@@ -615,6 +616,17 @@ static int manager_receive_response(sd_event_source *source, int fd, uint32_t re
615616
(void) sd_notifyf(false, "STATUS=Initial synchronization to time server %s (%s).", strna(pretty), m->current_server_name->string);
616617
}
617618

619+
if (!spike && !m->synchronized) {
620+
m->synchronized = true;
621+
622+
log_struct(LOG_INFO,
623+
LOG_MESSAGE("Initial clock synchronization to %s.", FORMAT_TIMESTAMP_STYLE(dts.realtime, TIMESTAMP_US)),
624+
"MESSAGE_ID=" SD_MESSAGE_TIME_SYNC_STR,
625+
"MONOTONIC_USEC=" USEC_FMT, dts.monotonic,
626+
"REALTIME_USEC=" USEC_FMT, dts.realtime,
627+
"BOOTIME_USEC=" USEC_FMT, dts.boottime);
628+
}
629+
618630
r = manager_arm_timer(m, m->poll_interval_usec);
619631
if (r < 0)
620632
return log_error_errno(r, "Failed to rearm timer: %m");
@@ -1110,6 +1122,12 @@ int manager_new(Manager **ret) {
11101122

11111123
(void) sd_event_set_watchdog(m->event, true);
11121124

1125+
/* Load previous synchronization state */
1126+
r = access("/run/systemd/timesync/synchronized", F_OK);
1127+
if (r < 0 && errno != ENOENT)
1128+
log_debug_errno(errno, "Failed to determine whether /run/systemd/timesync/synchronized exists, ignoring: %m");
1129+
m->synchronized = r >= 0;
1130+
11131131
r = sd_resolve_default(&m->resolve);
11141132
if (r < 0)
11151133
return r;

src/timesync/timesyncd-manager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ struct Manager {
104104
struct timespec origin_time, dest_time;
105105
bool spike;
106106

107+
/* Indicates whether we ever managed to set the local clock from NTP */
108+
bool synchronized;
109+
107110
/* save time event */
108111
sd_event_source *event_save_time;
109112
usec_t save_time_interval_usec;

0 commit comments

Comments
 (0)