| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/stat.h> |
| 4 |
#include <sys/time.h> |
| 5 |
|
| 6 |
#include "sd-messages.h" |
| 7 |
|
| 8 |
#include "clock-util.h" |
| 9 |
#include "clock-warp.h" |
| 10 |
#include "errno-util.h" |
| 11 |
#include "log.h" |
| 12 |
#include "time-util.h" |
| 13 |
|
| 14 |
int clock_reset_timewarp(void) { |
| 15 |
static const struct timezone tz = { |
| 16 |
.tz_minuteswest = 0, |
| 17 |
.tz_dsttime = 0, /* DST_NONE */ |
| 18 |
}; |
| 19 |
|
| 20 |
/* The very first call to settimeofday() does time warp magic. Do a dummy call here, so the time |
| 21 |
* warping is sealed and all later calls behave as expected. */ |
| 22 |
return RET_NERRNO(settimeofday(NULL, &tz)); |
| 23 |
} |
| 24 |
|
| 25 |
void clock_apply_epoch(bool allow_backwards) { |
| 26 |
usec_t epoch_usec = 0, timesyncd_usec = 0; |
| 27 |
struct stat st; |
| 28 |
int r; |
| 29 |
|
| 30 |
r = RET_NERRNO(stat(TIMESYNCD_CLOCK_FILE, &st)); |
| 31 |
if (r >= 0) |
| 32 |
timesyncd_usec = timespec_load(&st.st_mtim); |
| 33 |
else if (r != -ENOENT) |
| 34 |
log_warning_errno(r, "Could not stat %s, ignoring: %m", TIMESYNCD_CLOCK_FILE); |
| 35 |
|
| 36 |
r = RET_NERRNO(stat(EPOCH_CLOCK_FILE, &st)); |
| 37 |
if (r >= 0) |
| 38 |
epoch_usec = timespec_load(&st.st_mtim); |
| 39 |
else if (r != -ENOENT) |
| 40 |
log_warning_errno(r, "Could not stat %s, ignoring: %m", EPOCH_CLOCK_FILE); |
| 41 |
|
| 42 |
epoch_usec = MAX3(epoch_usec, |
| 43 |
timesyncd_usec, |
| 44 |
(usec_t) TIME_EPOCH * USEC_PER_SEC); |
| 45 |
|
| 46 |
if (epoch_usec == 0) /* Weird, but may happen if mtimes were reset to 0 during compilation. */ |
| 47 |
return log_debug("Clock epoch is 0, skipping clock adjustment."); |
| 48 |
|
| 49 |
usec_t now_usec = now(CLOCK_REALTIME); |
| 50 |
bool advance; |
| 51 |
|
| 52 |
if (now_usec < epoch_usec) |
| 53 |
advance = true; |
| 54 |
else if (CLOCK_VALID_RANGE_USEC_MAX > 0 && |
| 55 |
now_usec > usec_add(epoch_usec, CLOCK_VALID_RANGE_USEC_MAX) && |
| 56 |
allow_backwards) |
| 57 |
advance = false; |
| 58 |
else |
| 59 |
return; /* Nothing to do. */ |
| 60 |
|
| 61 |
r = RET_NERRNO(clock_settime(CLOCK_REALTIME, TIMESPEC_STORE(epoch_usec))); |
| 62 |
if (r < 0) { |
| 63 |
if (advance) |
| 64 |
return (void) log_error_errno(r, "Current system time is before epoch, but cannot correct: %m"); |
| 65 |
else |
| 66 |
return (void) log_error_errno(r, "Current system time is further ahead than %s after epoch, but cannot correct: %m", |
| 67 |
FORMAT_TIMESPAN(CLOCK_VALID_RANGE_USEC_MAX, USEC_PER_DAY)); |
| 68 |
} |
| 69 |
|
| 70 |
const char *from = |
| 71 |
epoch_usec == (usec_t) TIME_EPOCH * USEC_PER_SEC ? "built-in epoch" : |
| 72 |
epoch_usec == timesyncd_usec ? "timestamp on "TIMESYNCD_CLOCK_FILE : |
| 73 |
"timestamp on "EPOCH_CLOCK_FILE; |
| 74 |
if (advance) |
| 75 |
log_struct(LOG_INFO, |
| 76 |
LOG_MESSAGE_ID(SD_MESSAGE_TIME_BUMP_STR), |
| 77 |
LOG_ITEM("REALTIME_USEC=" USEC_FMT, epoch_usec), |
| 78 |
LOG_ITEM("DIRECTION=forwards"), |
| 79 |
LOG_MESSAGE("System time advanced to %s: %s", |
| 80 |
from, |
| 81 |
FORMAT_TIMESTAMP(epoch_usec))); |
| 82 |
else |
| 83 |
log_struct(LOG_INFO, |
| 84 |
LOG_MESSAGE_ID(SD_MESSAGE_TIME_BUMP_STR), |
| 85 |
LOG_ITEM("REALTIME_USEC=" USEC_FMT, epoch_usec), |
| 86 |
LOG_ITEM("DIRECTION=backwards"), |
| 87 |
LOG_MESSAGE("System time was further ahead than %s after %s, clock reset to %s", |
| 88 |
FORMAT_TIMESPAN(CLOCK_VALID_RANGE_USEC_MAX, USEC_PER_DAY), |
| 89 |
from, |
| 90 |
FORMAT_TIMESTAMP(epoch_usec))); |
| 91 |
} |