| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
/* systemd service to wait until kernel realtime clock is synchronized */ |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <sys/inotify.h> |
| 6 |
#include <sys/timex.h> |
| 7 |
#include <unistd.h> |
| 8 |
|
| 9 |
#include "sd-event.h" |
| 10 |
|
| 11 |
#include "errno-util.h" |
| 12 |
#include "fd-util.h" |
| 13 |
#include "inotify-util.h" |
| 14 |
#include "log.h" |
| 15 |
#include "main-func.h" |
| 16 |
#include "time-util.h" |
| 17 |
|
| 18 |
typedef struct ClockState { |
| 19 |
int timerfd_fd; /* non-negative is descriptor from timerfd_create */ |
| 20 |
int adjtime_state; /* return value from last adjtimex(2) call */ |
| 21 |
sd_event_source *timerfd_event_source; /* non-null is the active io event source */ |
| 22 |
int inotify_fd; |
| 23 |
sd_event_source *inotify_event_source; |
| 24 |
int run_systemd_wd; |
| 25 |
int run_systemd_timesync_wd; |
| 26 |
bool has_watchfile; |
| 27 |
} ClockState; |
| 28 |
|
| 29 |
static void clock_state_release_timerfd(ClockState *sp) { |
| 30 |
sp->timerfd_event_source = sd_event_source_unref(sp->timerfd_event_source); |
| 31 |
sp->timerfd_fd = safe_close(sp->timerfd_fd); |
| 32 |
} |
| 33 |
|
| 34 |
static void clock_state_release(ClockState *sp) { |
| 35 |
clock_state_release_timerfd(sp); |
| 36 |
sp->inotify_event_source = sd_event_source_unref(sp->inotify_event_source); |
| 37 |
sp->inotify_fd = safe_close(sp->inotify_fd); |
| 38 |
} |
| 39 |
|
| 40 |
static int clock_state_update(ClockState *sp, sd_event *event); |
| 41 |
|
| 42 |
static int update_notify_run_systemd_timesync(ClockState *sp) { |
| 43 |
sp->run_systemd_timesync_wd = inotify_add_watch(sp->inotify_fd, "/run/systemd/timesync", IN_CREATE|IN_DELETE_SELF); |
| 44 |
return sp->run_systemd_timesync_wd; |
| 45 |
} |
| 46 |
|
| 47 |
static int timerfd_handler(sd_event_source *s, |
| 48 |
int fd, |
| 49 |
uint32_t revents, |
| 50 |
void *userdata) { |
| 51 |
ClockState *sp = userdata; |
| 52 |
|
| 53 |
return clock_state_update(sp, sd_event_source_get_event(s)); |
| 54 |
} |
| 55 |
|
| 56 |
static void process_inotify_event(sd_event *event, ClockState *sp, struct inotify_event *e) { |
| 57 |
if (e->wd == sp->run_systemd_wd) { |
| 58 |
/* Only thing we care about is seeing if we can start watching /run/systemd/timesync. */ |
| 59 |
if (sp->run_systemd_timesync_wd < 0) |
| 60 |
update_notify_run_systemd_timesync(sp); |
| 61 |
} else if (e->wd == sp->run_systemd_timesync_wd) { |
| 62 |
if (e->mask & IN_DELETE_SELF) { |
| 63 |
/* Somebody removed /run/systemd/timesync. */ |
| 64 |
(void) inotify_rm_watch(sp->inotify_fd, sp->run_systemd_timesync_wd); |
| 65 |
sp->run_systemd_timesync_wd = -1; |
| 66 |
} else |
| 67 |
/* Somebody might have created /run/systemd/timesync/synchronized. */ |
| 68 |
clock_state_update(sp, event); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
static int inotify_handler(sd_event_source *s, |
| 73 |
int fd, |
| 74 |
uint32_t revents, |
| 75 |
void *userdata) { |
| 76 |
sd_event *event = sd_event_source_get_event(s); |
| 77 |
ClockState *sp = userdata; |
| 78 |
union inotify_event_buffer buffer; |
| 79 |
ssize_t l; |
| 80 |
|
| 81 |
l = read(fd, &buffer, sizeof(buffer)); |
| 82 |
if (l < 0) { |
| 83 |
if (ERRNO_IS_TRANSIENT(errno)) |
| 84 |
return 0; |
| 85 |
|
| 86 |
return log_warning_errno(errno, "Lost access to inotify: %m"); |
| 87 |
} |
| 88 |
FOREACH_INOTIFY_EVENT_WARN(e, buffer, l) |
| 89 |
process_inotify_event(event, sp, e); |
| 90 |
|
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
static int clock_state_update( |
| 95 |
ClockState *sp, |
| 96 |
sd_event *event) { |
| 97 |
|
| 98 |
struct timex tx = {}; |
| 99 |
usec_t t; |
| 100 |
int r; |
| 101 |
|
| 102 |
clock_state_release_timerfd(sp); |
| 103 |
|
| 104 |
/* The kernel supports cancelling timers whenever its realtime clock is "set" (which can happen in a variety of |
| 105 |
* ways, generally adjustments of at least 500 ms). The way this module works is we set up a timerfd that will |
| 106 |
* wake when the clock is set, and when that happens we read the clock synchronization state from the return |
| 107 |
* value of adjtimex(2), which supports the NTP time adjustment protocol. |
| 108 |
* |
| 109 |
* The kernel determines whether the clock is synchronized using driver-specific tests, based on time |
| 110 |
* information passed by an application, generally through adjtimex(2). If the application asserts the clock is |
| 111 |
* synchronized, but does not also do something that "sets the clock", the timer will not be cancelled and |
| 112 |
* synchronization will not be detected. |
| 113 |
* |
| 114 |
* Similarly, this service will never complete if the application sets the time without also providing |
| 115 |
* information that adjtimex(2) can use to determine that the clock is synchronized. This generally doesn't |
| 116 |
* happen, but can if the system has a hardware clock that is accurate enough that the adjustment is too small |
| 117 |
* to be a "set". |
| 118 |
* |
| 119 |
* Both these failure-to-detect situations are covered by having the presence/creation of |
| 120 |
* /run/systemd/timesync/synchronized, which is considered sufficient to indicate a synchronized clock even if |
| 121 |
* the kernel has not been updated. |
| 122 |
* |
| 123 |
* For timesyncd the initial setting of the time uses settimeofday(2), which sets the clock but does not mark |
| 124 |
* it synchronized. When an NTP source is selected it sets the clock again with clock_adjtime(2) which marks it |
| 125 |
* synchronized and also touches /run/systemd/timesync/synchronized which covers the case when the clock wasn't |
| 126 |
* "set". */ |
| 127 |
|
| 128 |
r = time_change_fd(); |
| 129 |
if (r < 0) { |
| 130 |
log_error_errno(r, "Failed to create timerfd: %m"); |
| 131 |
goto finish; |
| 132 |
} |
| 133 |
sp->timerfd_fd = r; |
| 134 |
|
| 135 |
r = adjtimex(&tx); |
| 136 |
if (r < 0) { |
| 137 |
log_error_errno(errno, "Failed to read adjtimex state: %m"); |
| 138 |
goto finish; |
| 139 |
} |
| 140 |
sp->adjtime_state = r; |
| 141 |
|
| 142 |
if (tx.status & STA_NANO) |
| 143 |
tx.time.tv_usec /= 1000; |
| 144 |
t = timeval_load(&tx.time); |
| 145 |
|
| 146 |
log_info("adjtime state %i status %x time %s", sp->adjtime_state, (unsigned) tx.status, |
| 147 |
FORMAT_TIMESTAMP_STYLE(t, TIMESTAMP_US_UTC) ?: "unrepresentable"); |
| 148 |
|
| 149 |
sp->has_watchfile = access("/run/systemd/timesync/synchronized", F_OK) >= 0; |
| 150 |
if (sp->has_watchfile) |
| 151 |
/* Presence of watch file overrides adjtime_state */ |
| 152 |
r = 0; |
| 153 |
else if (sp->adjtime_state == TIME_ERROR) { |
| 154 |
/* Not synchronized. Do a one-shot wait on the descriptor and inform the caller we need to keep |
| 155 |
* running. */ |
| 156 |
r = sd_event_add_io(event, &sp->timerfd_event_source, sp->timerfd_fd, |
| 157 |
EPOLLIN, timerfd_handler, sp); |
| 158 |
if (r < 0) { |
| 159 |
log_error_errno(r, "Failed to create time change monitor source: %m"); |
| 160 |
goto finish; |
| 161 |
} |
| 162 |
r = 1; |
| 163 |
} else |
| 164 |
/* Synchronized; we can exit. */ |
| 165 |
r = 0; |
| 166 |
|
| 167 |
finish: |
| 168 |
if (r <= 0) |
| 169 |
(void) sd_event_exit(event, r); |
| 170 |
return r; |
| 171 |
} |
| 172 |
|
| 173 |
static int run(int argc, char * argv[]) { |
| 174 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 175 |
_cleanup_(clock_state_release) ClockState state = { |
| 176 |
.timerfd_fd = -EBADF, |
| 177 |
.inotify_fd = -EBADF, |
| 178 |
.run_systemd_wd = -1, |
| 179 |
.run_systemd_timesync_wd = -1, |
| 180 |
}; |
| 181 |
int r; |
| 182 |
|
| 183 |
r = sd_event_default(&event); |
| 184 |
if (r < 0) |
| 185 |
return log_error_errno(r, "Failed to allocate event loop: %m"); |
| 186 |
|
| 187 |
r = sd_event_set_signal_exit(event, true); |
| 188 |
if (r < 0) |
| 189 |
return log_error_errno(r, "Failed to enable SIGTERM/SIGINT handling: %m"); |
| 190 |
|
| 191 |
r = sd_event_set_watchdog(event, true); |
| 192 |
if (r < 0) |
| 193 |
return log_error_errno(r, "Failed to create watchdog event source: %m"); |
| 194 |
|
| 195 |
r = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); |
| 196 |
if (r < 0) |
| 197 |
return log_error_errno(errno, "Failed to create inotify descriptor: %m"); |
| 198 |
|
| 199 |
state.inotify_fd = r; |
| 200 |
|
| 201 |
r = sd_event_add_io(event, &state.inotify_event_source, state.inotify_fd, |
| 202 |
EPOLLIN, inotify_handler, &state); |
| 203 |
if (r < 0) |
| 204 |
return log_error_errno(r, "Failed to create notify event source: %m"); |
| 205 |
|
| 206 |
r = inotify_add_watch_and_warn(state.inotify_fd, "/run/systemd/", IN_CREATE); |
| 207 |
if (r < 0) |
| 208 |
return r; |
| 209 |
|
| 210 |
state.run_systemd_wd = r; |
| 211 |
|
| 212 |
(void) update_notify_run_systemd_timesync(&state); |
| 213 |
|
| 214 |
r = clock_state_update(&state, event); |
| 215 |
if (r > 0) { |
| 216 |
r = sd_event_loop(event); |
| 217 |
if (r < 0) |
| 218 |
log_error_errno(r, "Failed in event loop: %m"); |
| 219 |
} |
| 220 |
|
| 221 |
if (state.has_watchfile) |
| 222 |
log_debug("Exit enabled by: /run/systemd/timesync/synchronized"); |
| 223 |
|
| 224 |
if (state.adjtime_state == TIME_ERROR) |
| 225 |
log_info("Exit without adjtimex synchronized."); |
| 226 |
|
| 227 |
return r; |
| 228 |
} |
| 229 |
|
| 230 |
DEFINE_MAIN_FUNCTION(run); |