timesyncd-manager.c revision 2f905e821e0342c36f5a5d3a51d53aabccc800bd
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek This file is part of systemd.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Copyright 2014 Kay Sievers, Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (at your option) any later version.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#define ADJ_SETOFFSET 0x0100 /* add 'time' to current time */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/* expected accuracy of time synchronization; used to adjust the poll interval */
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering * "A client MUST NOT under any conditions use a poll interval less
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering * than 15 seconds."
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define NTP_POLL_INTERVAL_MIN_SEC 32
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * Maximum delta in seconds which the system clock is gradually adjusted
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * (slew) to approach the network time. Deltas larger that this are set by
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * letting the system time jump. The kernel's limit for adjtime is 0.5s.
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering/* NTP protocol, packet header */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define NTP_FIELD_LEAP(f) (((f) >> 6) & 3)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define NTP_FIELD_VERSION(f) (((f) >> 3) & 7)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define NTP_FIELD_MODE(f) ((f) & 7)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define NTP_FIELD(l, v, m) (((l) << 6) | ((v) << 3) | (m))
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering/* Maximum acceptable root distance in seconds. */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/* Maximum number of missed replies before selecting another source. */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * "NTP timestamps are represented as a 64-bit unsigned fixed-point number,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * in seconds relative to 0h on 1 January 1900."
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define RETRY_USEC (30*USEC_PER_SEC)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define RATELIMIT_INTERVAL_USEC (10*USEC_PER_SEC)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int manager_arm_timer(Manager *m, usec_t next);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int manager_clock_watch_setup(Manager *m);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic double ntp_ts_short_to_d(const struct ntp_ts_short *ts) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return be16toh(ts->sec) + (be16toh(ts->frac) / 65536.0);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic double ntp_ts_to_d(const struct ntp_ts *ts) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return be32toh(ts->sec) + ((double)be32toh(ts->frac) / UINT_MAX);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic double ts_to_d(const struct timespec *ts) {
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek return ts->tv_sec + (1.0e-9 * ts->tv_nsec);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic double square(double d) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int manager_timeout(sd_event_source *source, usec_t usec, void *userdata) {
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *pretty = NULL;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek server_address_pretty(m->current_server_address, &pretty);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering log_info("Timed out waiting for reply from %s (%s).", strna(pretty), m->current_server_name->string);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int manager_send_request(Manager *m) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * "The client initializes the NTP message header, sends the request
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * to the server, and strips the time of day from the Transmit
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * Timestamp field of the reply. For this purpose, all the NTP
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * header fields are set to 0, except the Mode, VN, and optional
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * Transmit Timestamp fields."
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering .field = NTP_FIELD(0, 4, NTP_MODE_CLIENT),
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m->event_timeout = sd_event_source_unref(m->event_timeout);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * Set transmit timestamp, remember it; the server will send that back
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * as the origin timestamp and we have an indication that this is the
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek * matching answer to our request.
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * The actual value does not matter, We do not care about the correct
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * NTP UINT_MAX fraction; we just pass the plain nanosecond value.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(clock_gettime(clock_boottime_or_monotonic(), &m->trans_time_mon) >= 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(clock_gettime(CLOCK_REALTIME, &m->trans_time) >= 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ntpmsg.trans_time.sec = htobe32(m->trans_time.tv_sec + OFFSET_1900_1970);
0fb398316c6705dfc168733361650fdb6824896dLennart Poettering ntpmsg.trans_time.frac = htobe32(m->trans_time.tv_nsec);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek server_address_pretty(m->current_server_address, &pretty);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek len = sendto(m->server_socket, &ntpmsg, sizeof(ntpmsg), MSG_DONTWAIT, &m->current_server_address->sockaddr.sa, m->current_server_address->socklen);
4608af4333d0f7f5f8e3bc632801b04ef07d246dLennart Poettering log_debug("Sent NTP request to %s (%s).", strna(pretty), m->current_server_name->string);
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering log_debug("Sending NTP request to %s (%s) failed: %m", strna(pretty), m->current_server_name->string);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* re-arm timer with increasing timeout, in case the packets never arrive back */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->retry_interval < NTP_POLL_INTERVAL_MAX_SEC * USEC_PER_SEC)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->retry_interval = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = manager_arm_timer(m, m->retry_interval);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to rearm timer: %s", strerror(-r));
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt if (m->missed_replies > NTP_MAX_MISSED_REPLIES) {
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) now(clock_boottime_or_monotonic()) + TIMEOUT_USEC, 0,
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) log_error("Failed to arm timeout timer: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_timer(sd_event_source *source, usec_t usec, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_arm_timer(Manager *m, usec_t next) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->event_timer = sd_event_source_unref(m->event_timer);
f7dc3ab9f43b67abcbd34062b9352ab42debec49Lennart Poettering r = sd_event_source_set_time(m->event_timer, now(clock_boottime_or_monotonic()) + next);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return sd_event_source_set_enabled(m->event_timer, SD_EVENT_ONESHOT);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek now(clock_boottime_or_monotonic()) + next, 0,
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidtstatic int manager_clock_watch(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* rearm timer */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* skip our own jumps */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("System time changed. Resyncing.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/* wake up when the system time changes underneath us */
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poetteringstatic int manager_clock_watch_setup(Manager *m) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek m->event_clock_watch = sd_event_source_unref(m->event_clock_watch);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek m->clock_watch_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek log_error("Failed to create timerfd: %m");
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (timerfd_settime(m->clock_watch_fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) < 0) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek log_error("Failed to set up timerfd: %m");
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek r = sd_event_add_io(m->event, &m->event_clock_watch, m->clock_watch_fd, EPOLLIN, manager_clock_watch, m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to create clock watch event source: %s", strerror(-r));
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int manager_adjust_clock(Manager *m, double offset, int leap_sec) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek * For small deltas, tell the kernel to gradually adjust the system
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek * clock to the NTP time, larger deltas are just directly set.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (fabs(offset) < NTP_MAX_ADJUST) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek tmx.modes = ADJ_STATUS | ADJ_NANO | ADJ_OFFSET | ADJ_TIMECONST | ADJ_MAXERROR | ADJ_ESTERROR;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk tmx.constant = log2i(m->poll_interval_usec / USEC_PER_SEC) - 4;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk log_debug(" adjust (slew): %+.3f sec\n", offset);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk tmx.modes = ADJ_STATUS | ADJ_NANO | ADJ_SETOFFSET;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk /* ADJ_NANO uses nanoseconds in the microseconds field */
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk tmx.time.tv_usec = (offset - tmx.time.tv_sec) * NSEC_PER_SEC;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt /* the kernel expects -0.3s as {-1, 7000.000.000} */
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering log_debug(" adjust (jump): %+.3f sec\n", offset);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk * An unset STA_UNSYNC will enable the kernel's 11-minute mode,
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk * which syncs the system time periodically to the RTC.
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk * In case the RTC runs in local time, never touch the RTC,
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering * we have no way to properly handle daylight saving changes and
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering * mobile devices moving between time zones.
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek touch("/var/lib/systemd/clock");
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering " time now : %li.%03llu\n"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering " constant : %li\n"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering " offset : %+.3f sec\n"
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek " freq offset : %+li (%i ppm)\n",
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering tmx.status, tmx.status & STA_UNSYNC ? "unsync" : "sync",
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek tmx.time.tv_sec, (unsigned long long) (tmx.time.tv_usec / NSEC_PER_MSEC),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic bool manager_sample_spike_detection(Manager *m, double offset, double delay) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering unsigned int i, idx_cur, idx_new, idx_min;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* ignore initial sample */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* store the current data in our samples array */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering idx_new = (idx_cur + 1) % ELEMENTSOF(m->samples);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* calculate new jitter value from the RMS differences relative to the lowest delay sample */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering for (idx_min = idx_cur, i = 0; i < ELEMENTSOF(m->samples); i++)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (m->samples[i].delay > 0 && m->samples[i].delay < m->samples[idx_min].delay)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering for (i = 0; i < ELEMENTSOF(m->samples); i++)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering j += square(m->samples[i].offset - m->samples[idx_min].offset);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering m->samples_jitter = sqrt(j / (ELEMENTSOF(m->samples) - 1));
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* ignore samples when resyncing */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* always accept offset if we are farther off than the round-trip delay */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* we need a few samples before looking at them */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* do not accept anything worse than the maximum possible error of the best sample */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (fabs(offset) > m->samples[idx_min].delay)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* compare the difference between the current offset to the previous offset and jitter */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return fabs(offset - m->samples[idx_cur].offset) > 3 * jitter;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poetteringstatic void manager_adjust_poll(Manager *m, double offset, bool spike) {
8531ae707d4d0203e83304d4af948b8169a5fce1Lennart Poettering m->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* set to minimal poll interval */
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering if (!spike && fabs(offset) > NTP_ACCURACY_SEC) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* increase polling interval */
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering if (fabs(offset) < NTP_ACCURACY_SEC * 0.25) {
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering if (m->poll_interval_usec < NTP_POLL_INTERVAL_MAX_SEC * USEC_PER_SEC)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* decrease polling interval */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (spike || fabs(offset) > NTP_ACCURACY_SEC * 0.75) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->poll_interval_usec > NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC)
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poetteringstatic int manager_receive_response(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint8_t buf[CMSG_SPACE(sizeof(struct timeval))];
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek union sockaddr_union server_addr;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .msg_controllen = sizeof(control),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .msg_namelen = sizeof(server_addr),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek double origin, receive, trans, dest;
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering log_warning("Server connection returned error.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek len = recvmsg(fd, &msghdr, MSG_DONTWAIT);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning("Error receiving message. Disconnecting.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (iov.iov_len < sizeof(struct ntp_msg)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning("Invalid response from server. Disconnecting.");
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering !sockaddr_equal(&server_addr, &m->current_server_address->sockaddr)) {
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering log_debug("Response from unknown server.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek recv_time = (struct timespec *) CMSG_DATA(cmsg);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_debug("Unexpected reply. Ignoring.");
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering /* check our "time cookie" (we just stored nanoseconds in the fraction field) */
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (be32toh(ntpmsg.origin_time.sec) != m->trans_time.tv_sec + OFFSET_1900_1970 ||
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek be32toh(ntpmsg.origin_time.frac) != m->trans_time.tv_nsec) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_debug("Invalid reply; not our transmit time. Ignoring.");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering m->event_timeout = sd_event_source_unref(m->event_timeout);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (be32toh(ntpmsg.recv_time.sec) < TIME_EPOCH + OFFSET_1900_1970 ||
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek be32toh(ntpmsg.trans_time.sec) < TIME_EPOCH + OFFSET_1900_1970) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Invalid reply, returned times before epoch. Ignoring.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (NTP_FIELD_LEAP(ntpmsg.field) == NTP_LEAP_NOTINSYNC ||
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ntpmsg.stratum == 0 || ntpmsg.stratum >= 16) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Server is not synchronized. Disconnecting.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!IN_SET(NTP_FIELD_VERSION(ntpmsg.field), 3, 4)) {
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek log_debug("Response NTPv%d. Disconnecting.", NTP_FIELD_VERSION(ntpmsg.field));
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther if (NTP_FIELD_MODE(ntpmsg.field) != NTP_MODE_SERVER) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Unsupported mode %d. Disconnecting.", NTP_FIELD_MODE(ntpmsg.field));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek root_distance = ntp_ts_short_to_d(&ntpmsg.root_delay) / 2 + ntp_ts_short_to_d(&ntpmsg.root_dispersion);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (root_distance > NTP_MAX_ROOT_DISTANCE) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Server has too large root distance. Disconnecting.");
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering /* valid packet */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* announce leap seconds */
9bdbc2e2ec523dbefe1c1c7e164b5544aff0b185Lukas Nykryn if (NTP_FIELD_LEAP(ntpmsg.field) & NTP_LEAP_PLUSSEC)
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering else if (NTP_FIELD_LEAP(ntpmsg.field) & NTP_LEAP_MINUSSEC)
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden * "Timestamp Name ID When Generated
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering * ------------------------------------------------------------
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden * Originate Timestamp T1 time request sent by client
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek * Receive Timestamp T2 time request received by server
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Transmit Timestamp T3 time reply sent by server
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Destination Timestamp T4 time reply received by client
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * The round-trip delay, d, and system clock offset, t, are defined as:
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering * d = (T4 - T1) - (T3 - T2) t = ((T2 - T1) + (T3 - T4)) / 2"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering origin = ts_to_d(&m->trans_time) + OFFSET_1900_1970;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering receive = ntp_ts_to_d(&ntpmsg.recv_time);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek trans = ntp_ts_to_d(&ntpmsg.trans_time);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek dest = ts_to_d(recv_time) + OFFSET_1900_1970;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek offset = ((receive - origin) + (trans - dest)) / 2;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering delay = (dest - origin) - (trans - receive);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering spike = manager_sample_spike_detection(m, offset, delay);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering " version : %u\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " stratum : %u\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " precision : %.6f sec (%d)\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " root distance: %.6f sec\n"
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering " reference : %.4s\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " origin : %.3f\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " receive : %.3f\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " transmit : %.3f\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " dest : %.3f\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " offset : %+.3f sec\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " delay : %+.3f sec\n"
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek " jitter : %.3f%s\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " poll interval: " USEC_FMT "\n",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek NTP_FIELD_VERSION(ntpmsg.field),
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering exp2(ntpmsg.precision), ntpmsg.precision,
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering ntpmsg.stratum == 1 ? ntpmsg.refid : "n/a",
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering m->samples_jitter, spike ? " spike" : "",
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek m->poll_interval_usec / USEC_PER_SEC);
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering r = manager_adjust_clock(m, offset, leap_sec);
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering log_error("Failed to call clock_adjtime(): %m");
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering log_info("interval/delta/delay/jitter/drift " USEC_FMT "s/%+.3fs/%.3fs/%.3fs/%+ippm%s",
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering m->poll_interval_usec / USEC_PER_SEC, offset, delay, m->samples_jitter, m->drift_ppm,
2d43b190901902dbd98ccea77c1d1ddc9e2a9955Dan McGee log_error("Failed to rearm timer: %s", strerror(-r));
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poetteringstatic int manager_listen_setup(Manager *m) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek addr.sa.sa_family = m->current_server_address->sockaddr.sa.sa_family;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->server_socket = socket(addr.sa.sa_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = bind(m->server_socket, &addr.sa, m->current_server_address->socklen);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = setsockopt(m->server_socket, SOL_SOCKET, SO_TIMESTAMPNS, &on, sizeof(on));
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = setsockopt(m->server_socket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return sd_event_add_io(m->event, &m->event_receive, m->server_socket, EPOLLIN, manager_receive_response, m);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek _cleanup_free_ char *pretty = NULL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_return(m->current_server_name, -EHOSTUNREACH);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_return(m->current_server_address, -EHOSTUNREACH);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek m->missed_replies = NTP_MAX_MISSED_REPLIES;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek m->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek server_address_pretty(m->current_server_address, &pretty);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_info("Using NTP server %s (%s).", strna(pretty), m->current_server_name->string);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering sd_notifyf(false, "STATUS=Using Time Server %s (%s).", strna(pretty), m->current_server_name->string);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_warning("Failed to setup connection socket: %s", strerror(-r));
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek r = manager_clock_watch_setup(m);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekvoid manager_set_server_name(Manager *m, ServerName *n) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (m->current_server_name == n)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek m->current_server_address = NULL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_debug("Selected server %s.", n->string);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekvoid manager_set_server_address(Manager *m, ServerAddress *a) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (m->current_server_address == a)
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering /* If a is NULL, we are just clearing the address, without
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek * changing the name. Keep the existing name in that case. */
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek _cleanup_free_ char *pretty = NULL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_debug("Selected address %s of server %s.", strna(pretty), a->name->string);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekstatic int manager_resolve_handler(sd_resolve_query *q, int ret, const struct addrinfo *ai, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->resolve_query = sd_resolve_query_unref(m->resolve_query);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering log_debug("Failed to resolve %s: %s", m->current_server_name->string, gai_strerror(ret));
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* Try next host */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_free_ char *pretty = NULL;
40adcda869bda55f44b57fd3a2bd71d006dfb51bLennart Poettering assert(ai->ai_addrlen >= offsetof(struct sockaddr, sa_data));
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering if (!IN_SET(ai->ai_addr->sa_family, AF_INET, AF_INET6)) {
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering log_warning("Unsuitable address protocol for %s", m->current_server_name->string);
7517e17443225fafea86b21e42a36af69feb1dbcTorstein Husebø r = server_address_new(m->current_server_name, &a, (const union sockaddr_union*) ai->ai_addr, ai->ai_addrlen);
da4993920cdf5527b8528f0a483b54ab3cbc1971Kay Sievers log_error("Failed to add server address: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek server_address_pretty(a, &pretty);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Resolved address %s for %s.", pretty, m->current_server_name->string);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!m->current_server_name->addresses) {
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchagin log_error("Failed to find suitable address for host %s.", m->current_server_name->string);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Try next host */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_set_server_address(m, m->current_server_name->addresses);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_retry_connect(sd_event_source *source, usec_t usec, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint manager_connect(Manager *m) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->event_retry = sd_event_source_unref(m->event_retry);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!ratelimit_test(&m->ratelimit)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Slowing down attempts to contact servers.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_event_add_time(m->event, &m->event_retry, clock_boottime_or_monotonic(), now(clock_boottime_or_monotonic()) + RETRY_USEC, 0, manager_retry_connect, m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to create retry timer: %s", strerror(-r));
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek /* If we already are operating on some address, switch to the
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->current_server_address && m->current_server_address->addresses_next)
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering manager_set_server_address(m, m->current_server_address->addresses_next);
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek .ai_flags = AI_NUMERICSERV|AI_ADDRCONFIG,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Hmm, we are through all addresses, let's look for the next host instead */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->current_server_name && m->current_server_name->names_next)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_set_server_name(m, m->current_server_name->names_next);
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther /* Our current server name list is exhausted,
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther * let's find the next one to iterate. First
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther * we try the system list, then the link list.
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther * After having processed the link list we
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther * jump back to the system list. However, if
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * both lists are empty, we change to the
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * fallback list. */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!m->current_server_name || m->current_server_name->type == SERVER_LINK) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_set_server_name(m, NULL);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (restart && !m->exhausted_servers && m->poll_interval_usec) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering log_debug("Waiting after exhausting servers.");
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek r = sd_event_add_time(m->event, &m->event_retry, clock_boottime_or_monotonic(), now(clock_boottime_or_monotonic()) + m->poll_interval_usec, 0, manager_retry_connect, m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to create retry timer: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Increase the polling interval */
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering if (m->poll_interval_usec < NTP_POLL_INTERVAL_MAX_SEC * USEC_PER_SEC)
caa2f4c0c9613b2e02aafa308c8fb092576014a9Zbigniew Jędrzejewski-Szmek /* Tell the resolver to reread /etc/resolv.conf, in
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * case it changed. */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Flush out any previously resolved addresses */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek server_name_flush_addresses(m->current_server_name);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Resolving %s...", m->current_server_name->string);
ac892057c2ddd8f06323c73ebd80423cc3ec7190Dimitri John Ledkov r = sd_resolve_getaddrinfo(m->resolve, &m->resolve_query, m->current_server_name->string, "123", &hints, manager_resolve_handler, m);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering log_error("Failed to create resolver: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekvoid manager_disconnect(Manager *m) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->resolve_query = sd_resolve_query_unref(m->resolve_query);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m->event_timer = sd_event_source_unref(m->event_timer);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->event_receive = sd_event_source_unref(m->event_receive);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->server_socket = safe_close(m->server_socket);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->event_clock_watch = sd_event_source_unref(m->event_clock_watch);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->clock_watch_fd = safe_close(m->clock_watch_fd);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->event_timeout = sd_event_source_unref(m->event_timeout);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_notifyf(false, "STATUS=Idle.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekvoid manager_flush_server_names(Manager *m, ServerType t) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek server_name_free(m->system_servers);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_flush_server_names(m, SERVER_SYSTEM);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_flush_server_names(m, SERVER_LINK);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_flush_server_names(m, SERVER_FALLBACK);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_event_source_unref(m->event_retry);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_event_source_unref(m->network_event_source);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_network_monitor_unref(m->network_monitor);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_network_read_link_servers(Manager *m) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_strv_free_ char **ntp = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LIST_FOREACH(names, n, m->link_servers)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LIST_FOREACH(names, n, m->link_servers)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = server_name_new(m, NULL, SERVER_LINK, *i);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LIST_FOREACH_SAFE(names, n, nx, m->link_servers)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_flush_server_names(m, SERVER_LINK);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_network_event_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_network_monitor_flush(m->network_monitor);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_network_read_link_servers(m);
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering /* check if the machine is online */
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering /* check if the client is currently connected */
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering connected = m->server_socket >= 0 || m->resolve_query || m->exhausted_servers;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("No network connectivity, watching for changes.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (!connected && online) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("Network configuration changed, trying to establish connection.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_network_monitor_listen(Manager *m) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_network_monitor_new(&m->network_monitor, NULL);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering fd = sd_network_monitor_get_fd(m->network_monitor);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering events = sd_network_monitor_get_events(m->network_monitor);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering r = sd_event_add_io(m->event, &m->network_event_source, fd, events, manager_network_event_handler, m);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering _cleanup_(manager_freep) Manager *m = NULL;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering m->server_socket = m->clock_watch_fd = -1;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering RATELIMIT_INIT(m->ratelimit, RATELIMIT_INTERVAL_USEC, RATELIMIT_BURST);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering r = manager_parse_server_string(m, SERVER_FALLBACK, NTP_SERVERS);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);