timesyncd-manager.c revision 2f905e821e0342c36f5a5d3a51d53aabccc800bd
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/***
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek This file is part of systemd.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Copyright 2014 Kay Sievers, Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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
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
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/>.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek***/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include <stdlib.h>
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include <errno.h>
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include <fcntl.h>
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include <unistd.h>
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include <string.h>
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include <time.h>
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include <math.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <arpa/inet.h>
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include <netinet/in.h>
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include <netinet/ip.h>
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include <sys/timerfd.h>
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering#include <sys/timex.h>
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering#include <sys/socket.h>
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include <resolv.h>
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include <sys/prctl.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <sys/types.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <grp.h>
0dec689b098cf62b948d43dc78bde859665056ecTom Gundersen
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "missing.h"
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering#include "util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "sparse-endian.h"
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering#include "log.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "socket-util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "list.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "ratelimit.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "strv.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "conf-parser.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "sd-daemon.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "event-util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "network-util.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "clock-util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "capability.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "mkdir.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "timesyncd-conf.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "timesyncd-manager.h"
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#ifndef ADJ_SETOFFSET
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#define ADJ_SETOFFSET 0x0100 /* add 'time' to current time */
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#endif
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/* expected accuracy of time synchronization; used to adjust the poll interval */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define NTP_ACCURACY_SEC 0.2
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk/*
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering * "A client MUST NOT under any conditions use a poll interval less
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering * than 15 seconds."
e150e82097211f09b911c7784a89ef9efed713caMichał Bartoszkiewicz */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define NTP_POLL_INTERVAL_MIN_SEC 32
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define NTP_POLL_INTERVAL_MAX_SEC 2048
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering/*
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 */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define NTP_MAX_ADJUST 0.4
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering/* NTP protocol, packet header */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define NTP_LEAP_PLUSSEC 1
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define NTP_LEAP_MINUSSEC 2
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define NTP_LEAP_NOTINSYNC 3
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer#define NTP_MODE_CLIENT 3
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define NTP_MODE_SERVER 4
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
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering/* Maximum acceptable root distance in seconds. */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define NTP_MAX_ROOT_DISTANCE 5.0
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/* Maximum number of missed replies before selecting another source. */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define NTP_MAX_MISSED_REPLIES 2
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering/*
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."
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define OFFSET_1900_1970 2208988800UL
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define RETRY_USEC (30*USEC_PER_SEC)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define RATELIMIT_INTERVAL_USEC (10*USEC_PER_SEC)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define RATELIMIT_BURST 10
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define TIMEOUT_USEC (10*USEC_PER_SEC)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstruct ntp_ts {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering be32_t sec;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek be32_t frac;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek} _packed_;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstruct ntp_ts_short {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering be16_t sec;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek be16_t frac;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek} _packed_;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstruct ntp_msg {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint8_t field;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint8_t stratum;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering int8_t poll;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering int8_t precision;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct ntp_ts_short root_delay;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering struct ntp_ts_short root_dispersion;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char refid[4];
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct ntp_ts reference_time;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct ntp_ts origin_time;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct ntp_ts recv_time;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct ntp_ts trans_time;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek} _packed_;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int manager_arm_timer(Manager *m, usec_t next);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int manager_clock_watch_setup(Manager *m);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
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 Poettering}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic double ntp_ts_to_d(const struct ntp_ts *ts) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return be32toh(ts->sec) + ((double)be32toh(ts->frac) / UINT_MAX);
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
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 Poettering}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic double square(double d) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return d * d;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek}
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
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 Manager *m = userdata;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poettering assert(m);
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poettering assert(m->current_server_name);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(m->current_server_address);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
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 Poettering
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek return manager_connect(m);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int manager_send_request(Manager *m) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering _cleanup_free_ char *pretty = NULL;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering struct ntp_msg ntpmsg = {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering /*
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 */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering .field = NTP_FIELD(0, 4, NTP_MODE_CLIENT),
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering };
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering ssize_t len;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering int r;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(m);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(m->current_server_name);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(m->current_server_address);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m->event_timeout = sd_event_source_unref(m->event_timeout);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering /*
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.
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek *
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 */
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
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek server_address_pretty(m->current_server_address, &pretty);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (len == sizeof(ntpmsg)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->pending = true;
4608af4333d0f7f5f8e3bc632801b04ef07d246dLennart Poettering log_debug("Sent NTP request to %s (%s).", strna(pretty), m->current_server_name->string);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else {
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering log_debug("Sending NTP request to %s (%s) failed: %m", strna(pretty), m->current_server_name->string);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return manager_connect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
34c10968cbe3b5591b3c0ce225b8694edd9709d0Lennart Poettering
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 > 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->retry_interval < NTP_POLL_INTERVAL_MAX_SEC * USEC_PER_SEC)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->retry_interval *= 2;
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt } else
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->retry_interval = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = manager_arm_timer(m, m->retry_interval);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to rearm timer: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->missed_replies++;
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt if (m->missed_replies > NTP_MAX_MISSED_REPLIES) {
0fb398316c6705dfc168733361650fdb6824896dLennart Poettering r = sd_event_add_time(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->event,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek &m->event_timeout,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek clock_boottime_or_monotonic(),
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) now(clock_boottime_or_monotonic()) + TIMEOUT_USEC, 0,
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) manager_timeout, m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0) {
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) log_error("Failed to arm timeout timer: %s", strerror(-r));
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) return r;
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt }
0fb398316c6705dfc168733361650fdb6824896dLennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_timer(sd_event_source *source, usec_t usec, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Manager *m = userdata;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ed375bebf46c1251f4baa170b39ee93761dbdb19Zbigniew Jędrzejewski-Szmek return manager_send_request(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_arm_timer(Manager *m, usec_t next) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m->event_receive);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (next == 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->event_timer = sd_event_source_unref(m->event_timer);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->event_timer) {
f7dc3ab9f43b67abcbd34062b9352ab42debec49Lennart Poettering r = sd_event_source_set_time(m->event_timer, now(clock_boottime_or_monotonic()) + next);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return sd_event_source_set_enabled(m->event_timer, SD_EVENT_ONESHOT);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt return sd_event_add_time(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->event,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek &m->event_timer,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek clock_boottime_or_monotonic(),
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek now(clock_boottime_or_monotonic()) + next, 0,
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek manager_timer, m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidtstatic int manager_clock_watch(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Manager *m = userdata;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* rearm timer */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_clock_watch_setup(m);
cbd671772c9ce053a7050ddd29de170eb9efac7eZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* skip our own jumps */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->jumped) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->jumped = false;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* resync */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("System time changed. Resyncing.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->poll_resync = true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return manager_send_request(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/* wake up when the system time changes underneath us */
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poetteringstatic int manager_clock_watch_setup(Manager *m) {
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering struct itimerspec its = {
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering .it_value.tv_sec = TIME_T_MAX
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering };
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering int r;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert(m);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek m->event_clock_watch = sd_event_source_unref(m->event_clock_watch);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek safe_close(m->clock_watch_fd);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek m->clock_watch_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (m->clock_watch_fd < 0) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek log_error("Failed to create timerfd: %m");
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering return -errno;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek }
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
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");
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering return -errno;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek }
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
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 if (r < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to create clock watch event source: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int manager_adjust_clock(Manager *m, double offset, int leap_sec) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering struct timex tmx = {};
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert(m);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt /*
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.
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt */
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;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek tmx.status = STA_PLL;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk tmx.offset = offset * NSEC_PER_SEC;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk tmx.constant = log2i(m->poll_interval_usec / USEC_PER_SEC) - 4;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk tmx.maxerror = 0;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk tmx.esterror = 0;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk log_debug(" adjust (slew): %+.3f sec\n", offset);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk } else {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk tmx.modes = ADJ_STATUS | ADJ_NANO | ADJ_SETOFFSET;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk /* ADJ_NANO uses nanoseconds in the microseconds field */
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering tmx.time.tv_sec = (long)offset;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk tmx.time.tv_usec = (offset - tmx.time.tv_sec) * NSEC_PER_SEC;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt /* the kernel expects -0.3s as {-1, 7000.000.000} */
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (tmx.time.tv_usec < 0) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk tmx.time.tv_sec -= 1;
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering tmx.time.tv_usec += NSEC_PER_SEC;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk }
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering m->jumped = true;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering log_debug(" adjust (jump): %+.3f sec\n", offset);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering }
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering /*
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 *
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.
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (m->rtc_local_time)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering tmx.status |= STA_UNSYNC;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering switch (leap_sec) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case 1:
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering tmx.status |= STA_INS;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering break;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case -1:
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek tmx.status |= STA_DEL;
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek break;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering r = clock_adjtime(CLOCK_REALTIME, &tmx);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (r < 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return r;
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek touch("/var/lib/systemd/clock");
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m->drift_ppm = tmx.freq / 65536;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering log_debug(" status : %04i %s\n"
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),
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek tmx.constant,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering (double)tmx.offset / NSEC_PER_SEC,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering tmx.freq, m->drift_ppm);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering double jitter;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek double j;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(m);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->packet_count++;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* ignore initial sample */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->packet_count == 1)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return false;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* store the current data in our samples array */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering idx_cur = m->samples_idx;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering idx_new = (idx_cur + 1) % ELEMENTSOF(m->samples);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering m->samples_idx = idx_new;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering m->samples[idx_new].offset = offset;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering m->samples[idx_new].delay = delay;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* calculate new jitter value from the RMS differences relative to the lowest delay sample */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering jitter = m->samples_jitter;
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 idx_min = i;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering j = 0;
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
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* ignore samples when resyncing */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (m->poll_resync)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return false;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* always accept offset if we are farther off than the round-trip delay */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (fabs(offset) > delay)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return false;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* we need a few samples before looking at them */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (m->packet_count < 4)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return false;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
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 return true;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
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 Poettering}
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poetteringstatic void manager_adjust_poll(Manager *m, double offset, bool spike) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert(m);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (m->poll_resync) {
8531ae707d4d0203e83304d4af948b8169a5fce1Lennart Poettering m->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->poll_resync = false;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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 return;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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 m->poll_interval_usec *= 2;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->poll_interval_usec /= 2;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poetteringstatic int manager_receive_response(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering Manager *m = userdata;
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering struct ntp_msg ntpmsg;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct iovec iov = {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .iov_base = &ntpmsg,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .iov_len = sizeof(ntpmsg),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek };
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek union {
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering struct cmsghdr cmsghdr;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint8_t buf[CMSG_SPACE(sizeof(struct timeval))];
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } control;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek union sockaddr_union server_addr;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct msghdr msghdr = {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .msg_iov = &iov,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .msg_iovlen = 1,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .msg_control = &control,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .msg_controllen = sizeof(control),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .msg_name = &server_addr,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .msg_namelen = sizeof(server_addr),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek };
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct cmsghdr *cmsg;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct timespec *recv_time;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ssize_t len;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek double origin, receive, trans, dest;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering double delay, offset;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek double root_distance;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek bool spike;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int leap_sec;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(source);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (revents & (EPOLLHUP|EPOLLERR)) {
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering log_warning("Server connection returned error.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return manager_connect(m);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek len = recvmsg(fd, &msghdr, MSG_DONTWAIT);
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering if (len < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (errno == EAGAIN)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning("Error receiving message. Disconnecting.");
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return manager_connect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (iov.iov_len < sizeof(struct ntp_msg)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning("Invalid response from server. Disconnecting.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return manager_connect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering if (!m->current_server_name ||
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering !m->current_server_address ||
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering !sockaddr_equal(&server_addr, &m->current_server_address->sockaddr)) {
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering log_debug("Response from unknown server.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek recv_time = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering if (cmsg->cmsg_level != SOL_SOCKET)
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering continue;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek switch (cmsg->cmsg_type) {
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering case SCM_TIMESTAMPNS:
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek recv_time = (struct timespec *) CMSG_DATA(cmsg);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering }
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (!recv_time) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_error("Invalid packet timestamp.");
d378991747d67fff1d4dc39e7fb2bc8f49f1b561Mirco Tischler return -EINVAL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (!m->pending) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_debug("Unexpected reply. Ignoring.");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return 0;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering m->missed_replies = 0;
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering
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.");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return 0;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering m->event_timeout = sd_event_source_unref(m->event_timeout);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
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 return manager_connect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
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 return manager_connect(m);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek }
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther
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 return manager_connect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
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 return manager_connect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
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.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return manager_connect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering /* valid packet */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->pending = false;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek m->retry_interval = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* announce leap seconds */
9bdbc2e2ec523dbefe1c1c7e164b5544aff0b185Lukas Nykryn if (NTP_FIELD_LEAP(ntpmsg.field) & NTP_LEAP_PLUSSEC)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek leap_sec = 1;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering else if (NTP_FIELD_LEAP(ntpmsg.field) & NTP_LEAP_MINUSSEC)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek leap_sec = -1;
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden else
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden leap_sec = 0;
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden /*
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
0a20e3c10761378869af7bbef2733e3ae879d0f1Holger Hans Peter Freyther *
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"
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek */
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;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek offset = ((receive - origin) + (trans - dest)) / 2;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering delay = (dest - origin) - (trans - receive);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering spike = manager_sample_spike_detection(m, offset, delay);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering manager_adjust_poll(m, offset, spike);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_debug("NTP response:\n"
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek " leap : %u\n"
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering " version : %u\n"
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek " mode : %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"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " packet count : %"PRIu64"\n"
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek " jitter : %.3f%s\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " poll interval: " USEC_FMT "\n",
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering NTP_FIELD_LEAP(ntpmsg.field),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek NTP_FIELD_VERSION(ntpmsg.field),
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering NTP_FIELD_MODE(ntpmsg.field),
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering ntpmsg.stratum,
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering exp2(ntpmsg.precision), ntpmsg.precision,
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering root_distance,
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering ntpmsg.stratum == 1 ? ntpmsg.refid : "n/a",
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering origin - OFFSET_1900_1970,
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering receive - OFFSET_1900_1970,
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering trans - OFFSET_1900_1970,
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering dest - OFFSET_1900_1970,
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering offset, delay,
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering m->packet_count,
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering m->samples_jitter, spike ? " spike" : "",
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek m->poll_interval_usec / USEC_PER_SEC);
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering if (!spike) {
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering m->sync = true;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering r = manager_adjust_clock(m, offset, leap_sec);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (r < 0)
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering log_error("Failed to call clock_adjtime(): %m");
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering }
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering
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,
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering spike ? " (ignored)" : "");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
2d43b190901902dbd98ccea77c1d1ddc9e2a9955Dan McGee r = manager_arm_timer(m, m->poll_interval_usec);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering if (r < 0) {
2d43b190901902dbd98ccea77c1d1ddc9e2a9955Dan McGee log_error("Failed to rearm timer: %s", strerror(-r));
ef1673d16907726d83bdff2e57b5261997a85020Mirco Tischler return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
6baa7db00812437bbc87e73faa1a11b6cf631958Lennart Poettering return 0;
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering}
f8294e4175918117ca6c131720bcf287eadcd029Josh Triplett
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poetteringstatic int manager_listen_setup(Manager *m) {
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering union sockaddr_union addr = {};
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering static const int tos = IPTOS_LOWDELAY;
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering static const int on = 1;
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering assert(m);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
e7ff4e7fe9f3abd2297e4ef7b95dcb2804e051c3Greg Kroah-Hartman assert(m->server_socket < 0);
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering assert(!m->event_receive);
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering assert(m->current_server_address);
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek addr.sa.sa_family = m->current_server_address->sockaddr.sa.sa_family;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->server_socket = socket(addr.sa.sa_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->server_socket < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return -errno;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = bind(m->server_socket, &addr.sa, m->current_server_address->socklen);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (r < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return -errno;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = setsockopt(m->server_socket, SOL_SOCKET, SO_TIMESTAMPNS, &on, sizeof(on));
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (r < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return -errno;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = setsockopt(m->server_socket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek if (r < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return -errno;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
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}
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poetteringstatic int manager_begin(Manager *m) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek _cleanup_free_ char *pretty = NULL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek int r;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert(m);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_return(m->current_server_name, -EHOSTUNREACH);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_return(m->current_server_address, -EHOSTUNREACH);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek m->missed_replies = NTP_MAX_MISSED_REPLIES;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (m->poll_interval_usec == 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek m->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
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
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = manager_listen_setup(m);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (r < 0) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_warning("Failed to setup connection socket: %s", strerror(-r));
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return r;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek r = manager_clock_watch_setup(m);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (r < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return r;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return manager_send_request(m);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek}
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekvoid manager_set_server_name(Manager *m, ServerName *n) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert(m);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (m->current_server_name == n)
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering return;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering m->current_server_name = n;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek m->current_server_address = NULL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek manager_disconnect(m);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering if (n)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_debug("Selected server %s.", n->string);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek}
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekvoid manager_set_server_address(Manager *m, ServerAddress *a) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert(m);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (m->current_server_address == a)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek 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. */
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering if (a)
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering m->current_server_name = a->name;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering manager_disconnect(m);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (a) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek _cleanup_free_ char *pretty = NULL;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering 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-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek}
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekstatic int manager_resolve_handler(sd_resolve_query *q, int ret, const struct addrinfo *ai, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Manager *m = userdata;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
a569398925430de1f8479262e8ab39502054f2e9Lennart Poettering assert(q);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m->current_server_name);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->resolve_query = sd_resolve_query_unref(m->resolve_query);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (ret != 0) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering log_debug("Failed to resolve %s: %s", m->current_server_name->string, gai_strerror(ret));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* Try next host */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return manager_connect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering for (; ai; ai = ai->ai_next) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_free_ char *pretty = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ServerAddress *a;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
da4993920cdf5527b8528f0a483b54ab3cbc1971Kay Sievers assert(ai->ai_addr);
40adcda869bda55f44b57fd3a2bd71d006dfb51bLennart Poettering assert(ai->ai_addrlen >= offsetof(struct sockaddr, sa_data));
759c945a43577d56e85a927f15e7d9aaa94a4e4aColin Walters
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);
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering continue;
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering }
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering
7517e17443225fafea86b21e42a36af69feb1dbcTorstein Husebø r = server_address_new(m->current_server_name, &a, (const union sockaddr_union*) ai->ai_addr, ai->ai_addrlen);
8a0889dfdafa3054c894e54852d8a9e3a7e8390bLennart Poettering if (r < 0) {
da4993920cdf5527b8528f0a483b54ab3cbc1971Kay Sievers log_error("Failed to add server address: %s", strerror(-r));
da4993920cdf5527b8528f0a483b54ab3cbc1971Kay Sievers return r;
759c945a43577d56e85a927f15e7d9aaa94a4e4aColin Walters }
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering
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 }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Try next host */
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek return manager_connect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_set_server_address(m, m->current_server_name->addresses);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchagin return manager_begin(m);
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchagin}
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchagin
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_retry_connect(sd_event_source *source, usec_t usec, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Manager *m = userdata;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return manager_connect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint manager_connect(Manager *m) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
e2cc6eca73cd1df8be552d7c23f9ff3d69c06f1eLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_disconnect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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.");
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering
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 if (r < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to create retry timer: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek /* If we already are operating on some address, switch to the
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek * next one. */
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);
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer else {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering struct addrinfo hints = {
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek .ai_flags = AI_NUMERICSERV|AI_ADDRCONFIG,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .ai_socktype = SOCK_DGRAM,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek };
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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 else {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ServerName *f;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek bool restart = true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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) {
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering f = m->system_servers;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering if (!f)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek f = m->link_servers;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek f = m->link_servers;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!f)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek f = m->system_servers;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek else
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek restart = false;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!f)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek f = m->fallback_servers;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!f) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_set_server_name(m, NULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("No server found.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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 if (r < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to create retry timer: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek m->exhausted_servers = true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Increase the polling interval */
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering if (m->poll_interval_usec < NTP_POLL_INTERVAL_MAX_SEC * USEC_PER_SEC)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->poll_interval_usec *= 2;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
caa2f4c0c9613b2e02aafa308c8fb092576014a9Zbigniew Jędrzejewski-Szmek }
84267e4043cf88bf540b5bf9cd65e194670a4ffaLennart Poettering
09eba4d46b1119c758fba38a520a38c8d19af739Lennart Poettering m->exhausted_servers = false;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_set_server_name(m, f);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
caa2f4c0c9613b2e02aafa308c8fb092576014a9Zbigniew Jędrzejewski-Szmek
caa2f4c0c9613b2e02aafa308c8fb092576014a9Zbigniew Jędrzejewski-Szmek /* Tell the resolver to reread /etc/resolv.conf, in
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * case it changed. */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek res_init();
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Resolving %s...", m->current_server_name->string);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ac892057c2ddd8f06323c73ebd80423cc3ec7190Dimitri John Ledkov r = sd_resolve_getaddrinfo(m->resolve, &m->resolve_query, m->current_server_name->string, "123", &hints, manager_resolve_handler, m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering log_error("Failed to create resolver: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 1;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering r = manager_begin(m);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (r < 0)
433dd100442e8197868def975c6fd38b48dc6439Lukas Nykryn return r;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek return 1;
433dd100442e8197868def975c6fd38b48dc6439Lukas Nykryn}
433dd100442e8197868def975c6fd38b48dc6439Lukas Nykryn
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekvoid manager_disconnect(Manager *m) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->resolve_query = sd_resolve_query_unref(m->resolve_query);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m->event_timer = sd_event_source_unref(m->event_timer);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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
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
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->event_timeout = sd_event_source_unref(m->event_timeout);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_notifyf(false, "STATUS=Idle.");
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekvoid manager_flush_server_names(Manager *m, ServerType t) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (t == SERVER_SYSTEM)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek while (m->system_servers)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek server_name_free(m->system_servers);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (t == SERVER_LINK)
fc1d70af2101e16c9e6f3c5bfd5ab315ee9e6daeLennart Poettering while (m->link_servers)
fc1d70af2101e16c9e6f3c5bfd5ab315ee9e6daeLennart Poettering server_name_free(m->link_servers);
fc1d70af2101e16c9e6f3c5bfd5ab315ee9e6daeLennart Poettering
fc1d70af2101e16c9e6f3c5bfd5ab315ee9e6daeLennart Poettering if (t == SERVER_FALLBACK)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek while (m->fallback_servers)
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt server_name_free(m->fallback_servers);
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekvoid manager_free(Manager *m) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (!m)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering manager_disconnect(m);
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
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_event_source_unref(m->event_retry);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_event_source_unref(m->network_event_source);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_network_monitor_unref(m->network_monitor);
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering sd_resolve_unref(m->resolve);
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering sd_event_unref(m->event);
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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 ServerName *n, *nx;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char **i;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_network_get_ntp(&ntp);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto clear;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LIST_FOREACH(names, n, m->link_servers)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek n->marked = true;
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering STRV_FOREACH(i, ntp) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek bool found = false;
00a168618906bea43c3c57e20b9152582c324bf8Olivier Brunel
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LIST_FOREACH(names, n, m->link_servers)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (streq(n->string, *i)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek n->marked = false;
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt found = true;
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
93b73b064c663d6248bebfbbbd82989b5ca10fc5Lennart Poettering
93b73b064c663d6248bebfbbbd82989b5ca10fc5Lennart Poettering if (!found) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = server_name_new(m, NULL, SERVER_LINK, *i);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto clear;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LIST_FOREACH_SAFE(names, n, nx, m->link_servers)
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering if (n->marked)
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering server_name_free(n);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekclear:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_flush_server_names(m, SERVER_LINK);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_network_event_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Manager *m = userdata;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek bool connected, online;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_network_monitor_flush(m->network_monitor);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_network_read_link_servers(m);
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering /* check if the machine is online */
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering online = network_is_online();
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering
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
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (connected && !online) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("No network connectivity, watching for changes.");
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt manager_disconnect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (!connected && online) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("Network configuration changed, trying to establish connection.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
804ae586d475d77946debb22c1bc9ee049d4750cLennart Poettering if (m->current_server_address)
804ae586d475d77946debb22c1bc9ee049d4750cLennart Poettering r = manager_begin(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek else
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = manager_connect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
804ae586d475d77946debb22c1bc9ee049d4750cLennart Poettering return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
c6878637502b1717a110a9a7e8bba32a8583fcdfLennart Poettering return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
763c7aa288485cf5ab627fe1d25ff58e76f9dacbZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_network_monitor_listen(Manager *m) {
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering int r, fd, events;
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_network_monitor_new(&m->network_monitor, NULL);
8531ae707d4d0203e83304d4af948b8169a5fce1Lennart Poettering if (r < 0)
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering return r;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering fd = sd_network_monitor_get_fd(m->network_monitor);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (fd < 0)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return fd;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering events = sd_network_monitor_get_events(m->network_monitor);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (events < 0)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return events;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering r = sd_event_add_io(m->event, &m->network_event_source, fd, events, manager_network_event_handler, m);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (r < 0)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return r;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return 0;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering}
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poetteringint manager_new(Manager **ret) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering _cleanup_(manager_freep) Manager *m = NULL;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering int r;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering assert(ret);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering m = new0(Manager, 1);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (!m)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return -ENOMEM;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering m->server_socket = m->clock_watch_fd = -1;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering RATELIMIT_INIT(m->ratelimit, RATELIMIT_INTERVAL_USEC, RATELIMIT_BURST);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering r = manager_parse_server_string(m, SERVER_FALLBACK, NTP_SERVERS);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (r < 0)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return r;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering r = sd_event_default(&m->event);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (r < 0)
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering sd_event_set_watchdog(m->event, true);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering r = sd_resolve_default(&m->resolve);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (r < 0)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return r;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering r = sd_resolve_attach_event(m->resolve, m->event, 0);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering r = manager_network_monitor_listen(m);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (r < 0)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering manager_network_read_link_servers(m);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering *ret = m;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering m = NULL;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering}
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering