timesyncd-manager.c revision c572db3ccf6678426c8c1c714b594973db84eebe
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/>.
3ffd4af22052963e7a29431721ee204e634bea75Lennart 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 */
4e731273edfe852a3eee2949cd20f49fd5b4f6d7Lennart Poettering/* expected accuracy of time synchronization; used to adjust the poll interval */
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering * "A client MUST NOT under any conditions use a poll interval less
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering * than 15 seconds."
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Maximum delta in seconds which the system clock is gradually adjusted
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk * (slew) to approach the network time. Deltas larger that this are set by
7f1ad696a273703789b624fe0b209fb63e953016Lennart 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)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define NTP_FIELD(l, v, m) (((l) << 6) | ((v) << 3) | (m))
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * "NTP timestamps are represented as a 64-bit unsigned fixed-point number,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * in seconds relative to 0h on 1 January 1900."
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define RATELIMIT_INTERVAL_USEC (10*USEC_PER_SEC)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct ntp_ts_short root_dispersion;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_arm_timer(Manager *m, usec_t next);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_clock_watch_setup(Manager *m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic double ntp_ts_to_d(const struct ntp_ts *ts) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return be32toh(ts->sec) + ((double)be32toh(ts->frac) / UINT_MAX);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic double ts_to_d(const struct timespec *ts) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return ts->tv_sec + (1.0e-9 * ts->tv_nsec);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic double tv_to_d(const struct timeval *tv) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return tv->tv_sec + (1.0e-6 * tv->tv_usec);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic double square(double d) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int manager_timeout(sd_event_source *source, usec_t usec, void *userdata) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering 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);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_send_request(Manager *m) {
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *pretty = NULL;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek * "The client initializes the NTP message header, sends the request
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek * 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
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poettering * header fields are set to 0, except the Mode, VN, and optional
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart 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
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * matching answer to our request.
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * The actual value does not matter, We do not care about the correct
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * NTP UINT_MAX fraction; we just pass the plain nanosecond value.
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(clock_gettime(clock_boottime_or_monotonic(), &m->trans_time_mon) >= 0);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(clock_gettime(CLOCK_REALTIME, &m->trans_time) >= 0);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering ntpmsg.trans_time.sec = htobe32(m->trans_time.tv_sec + OFFSET_1900_1970);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering ntpmsg.trans_time.frac = htobe32(m->trans_time.tv_nsec);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering server_address_pretty(m->current_server_address, &pretty);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering len = sendto(m->server_socket, &ntpmsg, sizeof(ntpmsg), MSG_DONTWAIT, &m->current_server_address->sockaddr.sa, m->current_server_address->socklen);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering log_debug("Sent NTP request to %s (%s).", strna(pretty), m->current_server_name->string);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek 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));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek now(clock_boottime_or_monotonic()) + TIMEOUT_USEC, 0,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek 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);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek 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);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek now(clock_boottime_or_monotonic()) + next, 0,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_clock_watch(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* rearm timer */
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering /* skip our own jumps */
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek log_info("System time changed. Resyncing.");
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek/* wake up when the system time changes underneath us */
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmekstatic int manager_clock_watch_setup(Manager *m) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek m->event_clock_watch = sd_event_source_unref(m->event_clock_watch);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->clock_watch_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to create timerfd: %m");
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (timerfd_settime(m->clock_watch_fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) < 0) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering 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);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek log_error("Failed to create clock watch event source: %s", strerror(-r));
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchukstatic int manager_adjust_clock(Manager *m, double offset, int leap_sec) {
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering * For small deltas, tell the kernel to gradually adjust the system
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk * clock to the NTP time, larger deltas are just directly set.
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk 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;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering log_debug(" adjust (slew): %+.3f sec\n", offset);
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt 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;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering /* the kernel expects -0.3s as {-1, 7000.000.000} */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering log_debug(" adjust (jump): %+.3f sec\n", offset);
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek * An unset STA_UNSYNC will enable the kernel's 11-minute mode,
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek * which syncs the system time periodically to the RTC.
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * In case the RTC runs in local time, never touch the RTC,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * we have no way to properly handle daylight saving changes and
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * mobile devices moving between time zones.
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek r = clock_adjtime(CLOCK_REALTIME, &tmx);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek touch("/var/lib/systemd/clock");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->drift_ppm = tmx.freq / 65536;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering " time now : %li.%03llu\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " constant : %li\n"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering " offset : %+.3f sec\n"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering " freq offset : %+li (%i ppm)\n",
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering tmx.status, tmx.status & STA_UNSYNC ? "unsync" : "sync",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek tmx.time.tv_sec, (unsigned long long) (tmx.time.tv_usec / NSEC_PER_MSEC),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (double)tmx.offset / NSEC_PER_SEC,
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poetteringstatic bool manager_sample_spike_detection(Manager *m, double offset, double delay) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering unsigned int i, idx_cur, idx_new, idx_min;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* 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 */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* do not accept anything worse than the maximum possible error of the best sample */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (fabs(offset) > m->samples[idx_min].delay)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* compare the difference between the current offset to the previous offset and jitter */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return fabs(offset - m->samples[idx_cur].offset) > 3 * jitter;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void manager_adjust_poll(Manager *m, double offset, bool spike) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* set to minimal poll interval */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!spike && fabs(offset) > NTP_ACCURACY_SEC) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering /* 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) {
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering if (m->poll_interval_usec > NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_receive_response(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering 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 if (revents & (EPOLLHUP|EPOLLERR)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning("Server connection returned error.");
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering len = recvmsg(fd, &msghdr, MSG_DONTWAIT);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning("Error receiving message. Disconnecting.");
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering if (iov.iov_len < sizeof(struct ntp_msg)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning("Invalid response from server. Disconnecting.");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering !sockaddr_equal(&server_addr, &m->current_server_address->sockaddr)) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_debug("Response from unknown server.");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (cmsg->cmsg_level != SOL_SOCKET)
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering recv_time = (struct timeval *) CMSG_DATA(cmsg);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_error("Invalid packet timestamp.");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_debug("Unexpected reply. Ignoring.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* check our "time cookie" (we just stored nanoseconds in the fraction field) */
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (be32toh(ntpmsg.origin_time.sec) != m->trans_time.tv_sec + OFFSET_1900_1970 ||
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek be32toh(ntpmsg.origin_time.frac) != m->trans_time.tv_nsec) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Invalid reply; not our transmit time. Ignoring.");
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther m->event_timeout = sd_event_source_unref(m->event_timeout);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek if (be32toh(ntpmsg.recv_time.sec) < TIME_EPOCH + OFFSET_1900_1970 ||
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther 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 log_debug("Server is not synchronized. Disconnecting.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!IN_SET(NTP_FIELD_VERSION(ntpmsg.field), 3, 4)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Response NTPv%d. Disconnecting.", NTP_FIELD_VERSION(ntpmsg.field));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (NTP_FIELD_MODE(ntpmsg.field) != NTP_MODE_SERVER) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_debug("Unsupported mode %d. Disconnecting.", NTP_FIELD_MODE(ntpmsg.field));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* valid packet */
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden /* announce leap seconds */
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden if (NTP_FIELD_LEAP(ntpmsg.field) & NTP_LEAP_PLUSSEC)
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden else if (NTP_FIELD_LEAP(ntpmsg.field) & NTP_LEAP_MINUSSEC)
0a20e3c10761378869af7bbef2733e3ae879d0f1Holger Hans Peter Freyther * "Timestamp Name ID When Generated
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * ------------------------------------------------------------
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering * Originate Timestamp T1 time request sent by client
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek * Receive Timestamp T2 time request received by server
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering * Transmit Timestamp T3 time reply sent by server
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering * 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:
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering * d = (T4 - T1) - (T3 - T2) t = ((T2 - T1) + (T3 - T4)) / 2"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering assert_se(clock_gettime(clock_boottime_or_monotonic(), &now_ts) >= 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek origin = tv_to_d(recv_time) - (ts_to_d(&now_ts) - ts_to_d(&m->trans_time_mon)) + 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);
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering dest = tv_to_d(recv_time) + OFFSET_1900_1970;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek offset = ((receive - origin) + (trans - dest)) / 2;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek delay = (dest - origin) - (trans - receive);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek spike = manager_sample_spike_detection(m, offset, delay);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " leap : %u\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " version : %u\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " mode : %u\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " stratum : %u\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " precision : %.6f sec (%d)\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " reference : %.4s\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " origin : %.3f\n"
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek " receive : %.3f\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " transmit : %.3f\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " dest : %.3f\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " offset : %+.3f sec\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " delay : %+.3f sec\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " jitter : %.3f%s\n"
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering exp2(ntpmsg.precision), ntpmsg.precision,
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering ntpmsg.stratum == 1 ? ntpmsg.refid : "n/a",
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering m->samples_jitter, spike ? " spike" : "",
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering r = manager_adjust_clock(m, offset, leap_sec);
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering log_error("Failed to call clock_adjtime(): %m");
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering log_info("interval/delta/delay/jitter/drift " USEC_FMT "s/%+.3fs/%.3fs/%.3fs/%+ippm%s",
2d43b190901902dbd98ccea77c1d1ddc9e2a9955Dan McGee m->poll_interval_usec / USEC_PER_SEC, offset, delay, m->samples_jitter, m->drift_ppm,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = manager_arm_timer(m, m->poll_interval_usec);
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering log_error("Failed to rearm timer: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_listen_setup(Manager *m) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m->current_server_address);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek addr.sa.sa_family = m->current_server_address->sockaddr.sa.sa_family;
968f319679d9069af037240d0c3bcd126181cdacZbigniew 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);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek r = setsockopt(m->server_socket, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on));
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek setsockopt(m->server_socket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering return sd_event_add_io(m->event, &m->event_receive, m->server_socket, EPOLLIN, manager_receive_response, m);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekstatic int manager_begin(Manager *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->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek server_address_pretty(m->current_server_address, &pretty);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering log_info("Using NTP server %s (%s).", strna(pretty), m->current_server_name->string);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek 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));
968f319679d9069af037240d0c3bcd126181cdacZbigniew 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 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 m->current_server_name = a ? a->name : NULL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek server_address_pretty(a, &pretty);
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);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Failed to resolve %s: %s", m->current_server_name->string, gai_strerror(ret));
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* Try next host */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(ai->ai_addrlen >= offsetof(struct sockaddr, sa_data));
40adcda869bda55f44b57fd3a2bd71d006dfb51bLennart Poettering if (!IN_SET(ai->ai_addr->sa_family, AF_INET, AF_INET6)) {
759c945a43577d56e85a927f15e7d9aaa94a4e4aColin Walters log_warning("Unsuitable address protocol for %s", m->current_server_name->string);
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering r = server_address_new(m->current_server_name, &a, (const union sockaddr_union*) ai->ai_addr, ai->ai_addrlen);
7517e17443225fafea86b21e42a36af69feb1dbcTorstein Husebø log_error("Failed to add server address: %s", strerror(-r));
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering log_debug("Resolved address %s for %s.", pretty, m->current_server_name->string);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!m->current_server_name->addresses) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to find suitable address for host %s.", m->current_server_name->string);
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchagin /* Try next host */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_set_server_address(m, m->current_server_name->addresses);
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchaginstatic 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));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* If we already are operating on some address, switch to the
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (m->current_server_address && m->current_server_address->addresses_next)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek manager_set_server_address(m, m->current_server_address->addresses_next);
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);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Our current server name list is exhausted,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * let's find the next one to iterate. First
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * 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
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther * both lists are empty, we change to the
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther * fallback list. */
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther if (!m->current_server_name || m->current_server_name->type == SERVER_LINK) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_set_server_name(m, NULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Tell the resolver to reread /etc/resolv.conf, in
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * case it changed. */
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew 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);
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek r = sd_resolve_getaddrinfo(m->resolve, &m->resolve_query, m->current_server_name->string, "123", &hints, manager_resolve_handler, m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek 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);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->event_timer = sd_event_source_unref(m->event_timer);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->event_receive = sd_event_source_unref(m->event_receive);
ac892057c2ddd8f06323c73ebd80423cc3ec7190Dimitri John Ledkov m->server_socket = safe_close(m->server_socket);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering 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);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering m->event_timeout = sd_event_source_unref(m->event_timeout);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringvoid manager_flush_server_names(Manager *m, ServerType t) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek server_name_free(m->link_servers);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek server_name_free(m->fallback_servers);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_flush_server_names(m, SERVER_SYSTEM);
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 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)
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering 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);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* check if the machine is online */
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering /* check if the client is currently connected */
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering connected = m->server_socket >= 0 || m->resolve_query;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt 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.");
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poetteringstatic int manager_network_monitor_listen(Manager *m) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_network_monitor_new(&m->network_monitor, NULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fd = sd_network_monitor_get_fd(m->network_monitor);
804ae586d475d77946debb22c1bc9ee049d4750cLennart Poettering events = sd_network_monitor_get_events(m->network_monitor);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_event_add_io(m->event, &m->network_event_source, fd, events, manager_network_event_handler, m);
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart 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);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);