timesyncd-manager.c revision c572db3ccf6678426c8c1c714b594973db84eebe
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>
b4bbcaa9c44260e88402cb8f9a5fb8ac7f35e123Thomas Hindoe Paaboel Andersen#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>
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include <sys/types.h>
430f0182b72373145c839dbfe99d2382855cb8f8Lennart Poettering#include <grp.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "missing.h"
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering#include "util.h"
0dec689b098cf62b948d43dc78bde859665056ecTom Gundersen#include "sparse-endian.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "log.h"
33d52ab92f2f0bfd706e6f343d172618d1e03f3dLennart Poettering#include "socket-util.h"
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering#include "list.h"
f4f15635ec05293ffcc83a5b39f624bbabbd8fd0Lennart Poettering#include "ratelimit.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "strv.h"
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering#include "conf-parser.h"
afc5dbf37fd2399d37976388d9dd9ab470ecf446Lennart Poettering#include "sd-daemon.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "event-util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "network-util.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "clock-util.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "capability.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "mkdir.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "timesyncd-conf.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "timesyncd-manager.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#ifndef ADJ_SETOFFSET
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#define ADJ_SETOFFSET 0x0100 /* add 'time' to current time */
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#endif
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
4e731273edfe852a3eee2949cd20f49fd5b4f6d7Lennart Poettering/* expected accuracy of time synchronization; used to adjust the poll interval */
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#define NTP_ACCURACY_SEC 0.2
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering/*
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering * "A client MUST NOT under any conditions use a poll interval less
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering * than 15 seconds."
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering */
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#define NTP_POLL_INTERVAL_MIN_SEC 32
4a0b58c4a30ecaa61202f845ed86f75b36370cd0Lennart Poettering#define NTP_POLL_INTERVAL_MAX_SEC 2048
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Maximum delta in seconds which the system clock is gradually adjusted
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk * (slew) to approach the network time. Deltas larger that this are set by
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering * letting the system time jump. The kernel's limit for adjtime is 0.5s.
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering */
e150e82097211f09b911c7784a89ef9efed713caMichał Bartoszkiewicz#define NTP_MAX_ADJUST 0.4
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering/* NTP protocol, packet header */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define NTP_LEAP_PLUSSEC 1
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering#define NTP_LEAP_MINUSSEC 2
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering#define NTP_LEAP_NOTINSYNC 3
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#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)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define NTP_FIELD_MODE(f) ((f) & 7)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define NTP_FIELD(l, v, m) (((l) << 6) | ((v) << 3) | (m))
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering/*
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * "NTP timestamps are represented as a 64-bit unsigned fixed-point number,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * in seconds relative to 0h on 1 January 1900."
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering */
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer#define OFFSET_1900_1970 2208988800UL
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define RETRY_USEC (30*USEC_PER_SEC)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define RATELIMIT_INTERVAL_USEC (10*USEC_PER_SEC)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define RATELIMIT_BURST 10
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define TIMEOUT_USEC (10*USEC_PER_SEC)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstruct ntp_ts {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering be32_t sec;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek be32_t frac;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering} _packed_;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstruct ntp_ts_short {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek be16_t sec;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering be16_t frac;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering} _packed_;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstruct ntp_msg {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint8_t field;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint8_t stratum;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering int8_t poll;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int8_t precision;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering struct ntp_ts_short root_delay;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct ntp_ts_short root_dispersion;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char refid[4];
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering struct ntp_ts reference_time;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct ntp_ts origin_time;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct ntp_ts recv_time;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering struct ntp_ts trans_time;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek} _packed_;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_arm_timer(Manager *m, usec_t next);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_clock_watch_setup(Manager *m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic double ntp_ts_to_d(const struct ntp_ts *ts) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return be32toh(ts->sec) + ((double)be32toh(ts->frac) / UINT_MAX);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic double ts_to_d(const struct timespec *ts) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return ts->tv_sec + (1.0e-9 * ts->tv_nsec);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic double tv_to_d(const struct timeval *tv) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return tv->tv_sec + (1.0e-6 * tv->tv_usec);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic double square(double d) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return d * d;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int manager_timeout(sd_event_source *source, usec_t usec, void *userdata) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering _cleanup_free_ char *pretty = NULL;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering Manager *m = userdata;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(m->current_server_name);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(m->current_server_address);
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering server_address_pretty(m->current_server_address, &pretty);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering log_info("Timed out waiting for reply from %s (%s).", strna(pretty), m->current_server_name->string);
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return manager_connect(m);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_send_request(Manager *m) {
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *pretty = NULL;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek struct ntp_msg ntpmsg = {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering /*
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek * "The client initializes the NTP message header, sends the request
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek * to the server, and strips the time of day from the Transmit
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * Timestamp field of the reply. For this purpose, all the NTP
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poettering * header fields are set to 0, except the Mode, VN, and optional
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poettering * Transmit Timestamp fields."
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering .field = NTP_FIELD(0, 4, NTP_MODE_CLIENT),
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek };
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering ssize_t len;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering int r;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
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
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * matching answer to our request.
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering *
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * The actual value does not matter, We do not care about the correct
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * NTP UINT_MAX fraction; we just pass the plain nanosecond value.
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(clock_gettime(clock_boottime_or_monotonic(), &m->trans_time_mon) >= 0);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(clock_gettime(CLOCK_REALTIME, &m->trans_time) >= 0);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering ntpmsg.trans_time.sec = htobe32(m->trans_time.tv_sec + OFFSET_1900_1970);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering ntpmsg.trans_time.frac = htobe32(m->trans_time.tv_nsec);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering server_address_pretty(m->current_server_address, &pretty);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering len = sendto(m->server_socket, &ntpmsg, sizeof(ntpmsg), MSG_DONTWAIT, &m->current_server_address->sockaddr.sa, m->current_server_address->socklen);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (len == sizeof(ntpmsg)) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m->pending = true;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering log_debug("Sent NTP request to %s (%s).", strna(pretty), m->current_server_name->string);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering } else {
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek log_debug("Sending NTP request to %s (%s) failed: %m", strna(pretty), m->current_server_name->string);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek return manager_connect(m);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* re-arm timer with increasing timeout, in case the packets never arrive back */
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek if (m->retry_interval > 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->retry_interval < NTP_POLL_INTERVAL_MAX_SEC * USEC_PER_SEC)
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek m->retry_interval *= 2;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } 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);
34c10968cbe3b5591b3c0ce225b8694edd9709d0Lennart Poettering if (r < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to rearm timer: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek }
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek 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(),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek now(clock_boottime_or_monotonic()) + TIMEOUT_USEC, 0,
ed375bebf46c1251f4baa170b39ee93761dbdb19Zbigniew Jędrzejewski-Szmek manager_timeout, m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to arm timeout timer: %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
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
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return manager_send_request(m);
f7dc3ab9f43b67abcbd34062b9352ab42debec49Lennart Poettering}
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);
4a0b58c4a30ecaa61202f845ed86f75b36370cd0Lennart Poettering
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;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek }
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->event_timer) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_event_source_set_time(m->event_timer, now(clock_boottime_or_monotonic()) + next);
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return sd_event_source_set_enabled(m->event_timer, SD_EVENT_ONESHOT);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return sd_event_add_time(
cbd671772c9ce053a7050ddd29de170eb9efac7eZbigniew Jędrzejewski-Szmek m->event,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek &m->event_timer,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek clock_boottime_or_monotonic(),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek now(clock_boottime_or_monotonic()) + next, 0,
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek manager_timer, m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
4a0b58c4a30ecaa61202f845ed86f75b36370cd0Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_clock_watch(sd_event_source *source, int fd, uint32_t revents, 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 /* rearm timer */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_clock_watch_setup(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering /* skip our own jumps */
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering if (m->jumped) {
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering m->jumped = false;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering return 0;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering }
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering /* resync */
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek log_info("System time changed. Resyncing.");
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek m->poll_resync = true;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return manager_send_request(m);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek}
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek/* wake up when the system time changes underneath us */
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmekstatic int manager_clock_watch_setup(Manager *m) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering struct itimerspec its = {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek .it_value.tv_sec = TIME_T_MAX
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering };
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek int r;
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering
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);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek safe_close(m->clock_watch_fd);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->clock_watch_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->clock_watch_fd < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to create timerfd: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -errno;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (timerfd_settime(m->clock_watch_fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) < 0) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering log_error("Failed to set up timerfd: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -errno;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt }
4a0b58c4a30ecaa61202f845ed86f75b36370cd0Lennart Poettering
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek r = sd_event_add_io(m->event, &m->event_clock_watch, m->clock_watch_fd, EPOLLIN, manager_clock_watch, m);
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt if (r < 0) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek log_error("Failed to create clock watch event source: %s", strerror(-r));
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return r;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchukstatic int manager_adjust_clock(Manager *m, double offset, int leap_sec) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk struct timex tmx = {};
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk int r;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk assert(m);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk /*
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering * For small deltas, tell the kernel to gradually adjust the system
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk * clock to the NTP time, larger deltas are just directly set.
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk */
65c1d46b0923771955519329160a1e4c7cd027b0Lennart Poettering if (fabs(offset) < NTP_MAX_ADJUST) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk tmx.modes = ADJ_STATUS | ADJ_NANO | ADJ_OFFSET | ADJ_TIMECONST | ADJ_MAXERROR | ADJ_ESTERROR;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk tmx.status = STA_PLL;
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering tmx.offset = offset * NSEC_PER_SEC;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk tmx.constant = log2i(m->poll_interval_usec / USEC_PER_SEC) - 4;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk tmx.maxerror = 0;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering tmx.esterror = 0;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering log_debug(" adjust (slew): %+.3f sec\n", offset);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering } else {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt tmx.modes = ADJ_STATUS | ADJ_NANO | ADJ_SETOFFSET;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk /* ADJ_NANO uses nanoseconds in the microseconds field */
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk tmx.time.tv_sec = (long)offset;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk tmx.time.tv_usec = (offset - tmx.time.tv_sec) * NSEC_PER_SEC;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering /* the kernel expects -0.3s as {-1, 7000.000.000} */
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering if (tmx.time.tv_usec < 0) {
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering tmx.time.tv_sec -= 1;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering tmx.time.tv_usec += NSEC_PER_SEC;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m->jumped = true;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering log_debug(" adjust (jump): %+.3f sec\n", offset);
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering }
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering /*
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek * An unset STA_UNSYNC will enable the kernel's 11-minute mode,
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek * which syncs the system time periodically to the RTC.
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering *
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * In case the RTC runs in local time, never touch the RTC,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * we have no way to properly handle daylight saving changes and
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * mobile devices moving between time zones.
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering */
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek if (m->rtc_local_time)
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek tmx.status |= STA_UNSYNC;
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering switch (leap_sec) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case 1:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering tmx.status |= STA_INS;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering break;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case -1:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering tmx.status |= STA_DEL;
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek break;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek r = clock_adjtime(CLOCK_REALTIME, &tmx);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (r < 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek touch("/var/lib/systemd/clock");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->drift_ppm = tmx.freq / 65536;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering log_debug(" status : %04i %s\n"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering " time now : %li.%03llu\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " constant : %li\n"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering " offset : %+.3f sec\n"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering " freq offset : %+li (%i ppm)\n",
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering tmx.status, tmx.status & STA_UNSYNC ? "unsync" : "sync",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek tmx.time.tv_sec, (unsigned long long) (tmx.time.tv_usec / NSEC_PER_MSEC),
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering tmx.constant,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (double)tmx.offset / NSEC_PER_SEC,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek tmx.freq, m->drift_ppm);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return 0;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering}
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poetteringstatic bool manager_sample_spike_detection(Manager *m, double offset, double delay) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering unsigned int i, idx_cur, idx_new, idx_min;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering double jitter;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering double j;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert(m);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering m->packet_count++;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* ignore initial sample */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering 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 */
8531ae707d4d0203e83304d4af948b8169a5fce1Lennart Poettering if (m->packet_count < 4)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return false;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* do not accept anything worse than the maximum possible error of the best sample */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (fabs(offset) > m->samples[idx_min].delay)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return true;
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* compare the difference between the current offset to the previous offset and jitter */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return fabs(offset - m->samples[idx_cur].offset) > 3 * jitter;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void manager_adjust_poll(Manager *m, double offset, bool spike) {
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering assert(m);
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->poll_resync) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek 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 */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!spike && fabs(offset) > NTP_ACCURACY_SEC) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering /* increase polling interval */
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering if (fabs(offset) < NTP_ACCURACY_SEC * 0.25) {
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering if (m->poll_interval_usec < NTP_POLL_INTERVAL_MAX_SEC * USEC_PER_SEC)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek 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) {
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering 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}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_receive_response(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Manager *m = userdata;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek 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 {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct cmsghdr cmsghdr;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering 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,
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk .msg_namelen = sizeof(server_addr),
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering };
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct cmsghdr *cmsg;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk struct timespec now_ts;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct timeval *recv_time;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ssize_t len;
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering double origin, receive, trans, dest;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek double delay, offset;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek bool spike;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int leap_sec;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(source);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (revents & (EPOLLHUP|EPOLLERR)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning("Server connection returned error.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return manager_connect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering len = recvmsg(fd, &msghdr, MSG_DONTWAIT);
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering if (len < 0) {
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering if (errno == EAGAIN)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning("Error receiving message. Disconnecting.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return manager_connect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering if (iov.iov_len < sizeof(struct ntp_msg)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning("Invalid response from server. Disconnecting.");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return manager_connect(m);
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!m->current_server_name ||
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek !m->current_server_address ||
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering !sockaddr_equal(&server_addr, &m->current_server_address->sockaddr)) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_debug("Response from unknown server.");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return 0;
d378991747d67fff1d4dc39e7fb2bc8f49f1b561Mirco Tischler }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek recv_time = NULL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (cmsg->cmsg_level != SOL_SOCKET)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek continue;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek switch (cmsg->cmsg_type) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering case SCM_TIMESTAMP:
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering recv_time = (struct timeval *) CMSG_DATA(cmsg);
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering break;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (!recv_time) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_error("Invalid packet timestamp.");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return -EINVAL;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering }
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (!m->pending) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_debug("Unexpected reply. Ignoring.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* check our "time cookie" (we just stored nanoseconds in the fraction field) */
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (be32toh(ntpmsg.origin_time.sec) != m->trans_time.tv_sec + OFFSET_1900_1970 ||
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek be32toh(ntpmsg.origin_time.frac) != m->trans_time.tv_nsec) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Invalid reply; not our transmit time. Ignoring.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther m->event_timeout = sd_event_source_unref(m->event_timeout);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek if (be32toh(ntpmsg.recv_time.sec) < TIME_EPOCH + OFFSET_1900_1970 ||
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther be32toh(ntpmsg.trans_time.sec) < TIME_EPOCH + OFFSET_1900_1970) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Invalid reply, returned times before epoch. Ignoring.");
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek return manager_connect(m);
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (NTP_FIELD_LEAP(ntpmsg.field) == NTP_LEAP_NOTINSYNC) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Server is not synchronized. Disconnecting.");
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering return manager_connect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!IN_SET(NTP_FIELD_VERSION(ntpmsg.field), 3, 4)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Response NTPv%d. Disconnecting.", NTP_FIELD_VERSION(ntpmsg.field));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return manager_connect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (NTP_FIELD_MODE(ntpmsg.field) != NTP_MODE_SERVER) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_debug("Unsupported mode %d. Disconnecting.", NTP_FIELD_MODE(ntpmsg.field));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return manager_connect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
9bdbc2e2ec523dbefe1c1c7e164b5544aff0b185Lukas Nykryn
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* valid packet */
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering m->pending = false;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->retry_interval = 0;
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden /* announce leap seconds */
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden if (NTP_FIELD_LEAP(ntpmsg.field) & NTP_LEAP_PLUSSEC)
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden leap_sec = 1;
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden else if (NTP_FIELD_LEAP(ntpmsg.field) & NTP_LEAP_MINUSSEC)
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering leap_sec = -1;
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden else
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek leap_sec = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /*
0a20e3c10761378869af7bbef2733e3ae879d0f1Holger Hans Peter Freyther * "Timestamp Name ID When Generated
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * ------------------------------------------------------------
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering * Originate Timestamp T1 time request sent by client
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek * Receive Timestamp T2 time request received by server
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering * Transmit Timestamp T3 time reply sent by server
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering * Destination Timestamp T4 time reply received by client
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * The round-trip delay, d, and system clock offset, t, are defined as:
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering * d = (T4 - T1) - (T3 - T2) t = ((T2 - T1) + (T3 - T4)) / 2"
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek */
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering assert_se(clock_gettime(clock_boottime_or_monotonic(), &now_ts) >= 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek origin = tv_to_d(recv_time) - (ts_to_d(&now_ts) - ts_to_d(&m->trans_time_mon)) + OFFSET_1900_1970;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering receive = ntp_ts_to_d(&ntpmsg.recv_time);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek trans = ntp_ts_to_d(&ntpmsg.trans_time);
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering dest = tv_to_d(recv_time) + OFFSET_1900_1970;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek offset = ((receive - origin) + (trans - dest)) / 2;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek delay = (dest - origin) - (trans - receive);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek spike = manager_sample_spike_detection(m, offset, delay);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering manager_adjust_poll(m, offset, spike);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering log_debug("NTP response:\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " leap : %u\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " version : %u\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " mode : %u\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " stratum : %u\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " precision : %.6f sec (%d)\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " reference : %.4s\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " origin : %.3f\n"
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek " receive : %.3f\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " transmit : %.3f\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " dest : %.3f\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " offset : %+.3f sec\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " delay : %+.3f sec\n"
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering " packet count : %"PRIu64"\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " jitter : %.3f%s\n"
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering " poll interval: " USEC_FMT "\n",
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering NTP_FIELD_LEAP(ntpmsg.field),
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering NTP_FIELD_VERSION(ntpmsg.field),
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering NTP_FIELD_MODE(ntpmsg.field),
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering ntpmsg.stratum,
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering exp2(ntpmsg.precision), ntpmsg.precision,
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering ntpmsg.stratum == 1 ? ntpmsg.refid : "n/a",
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering origin - OFFSET_1900_1970,
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering receive - OFFSET_1900_1970,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek trans - OFFSET_1900_1970,
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering dest - OFFSET_1900_1970,
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering offset, delay,
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering m->packet_count,
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering m->samples_jitter, spike ? " spike" : "",
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering m->poll_interval_usec / USEC_PER_SEC);
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering if (!spike) {
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering m->sync = true;
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering r = manager_adjust_clock(m, offset, leap_sec);
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering if (r < 0)
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering log_error("Failed to call clock_adjtime(): %m");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering }
2d43b190901902dbd98ccea77c1d1ddc9e2a9955Dan McGee
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering log_info("interval/delta/delay/jitter/drift " USEC_FMT "s/%+.3fs/%.3fs/%.3fs/%+ippm%s",
2d43b190901902dbd98ccea77c1d1ddc9e2a9955Dan McGee m->poll_interval_usec / USEC_PER_SEC, offset, delay, m->samples_jitter, m->drift_ppm,
ef1673d16907726d83bdff2e57b5261997a85020Mirco Tischler spike ? " (ignored)" : "");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = manager_arm_timer(m, m->poll_interval_usec);
6355e75610a8d47fc3ba5ab8bd442172a2cfe574Lennart Poettering if (r < 0) {
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering log_error("Failed to rearm timer: %s", strerror(-r));
f8294e4175918117ca6c131720bcf287eadcd029Josh Triplett return r;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering }
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering return 0;
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering}
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_listen_setup(Manager *m) {
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering union sockaddr_union addr = {};
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering static const int tos = IPTOS_LOWDELAY;
e7ff4e7fe9f3abd2297e4ef7b95dcb2804e051c3Greg Kroah-Hartman static const int on = 1;
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering int r;
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering assert(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m->server_socket < 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(!m->event_receive);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m->current_server_address);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek addr.sa.sa_family = m->current_server_address->sockaddr.sa.sa_family;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek m->server_socket = socket(addr.sa.sa_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (m->server_socket < 0)
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew 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;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek r = setsockopt(m->server_socket, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on));
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (r < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return -errno;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek setsockopt(m->server_socket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering 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
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekstatic int manager_begin(Manager *m) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek _cleanup_free_ char *pretty = NULL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek int r;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering assert(m);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_return(m->current_server_name, -EHOSTUNREACH);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_return(m->current_server_address, -EHOSTUNREACH);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
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);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering log_info("Using NTP server %s (%s).", strna(pretty), m->current_server_name->string);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek sd_notifyf(false, "STATUS=Using Time Server %s (%s).", strna(pretty), m->current_server_name->string);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
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 }
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew 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);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek}
968f319679d9069af037240d0c3bcd126181cdacZbigniew 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
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering if (m->current_server_name == n)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek 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);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek 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) {
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek assert(m);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (m->current_server_address == a)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering m->current_server_name = a ? a->name : NULL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek m->current_server_address = a;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering manager_disconnect(m);
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering if (a) {
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering _cleanup_free_ char *pretty = NULL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek server_address_pretty(a, &pretty);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_debug("Selected address %s of server %s.", strna(pretty), a->name->string);
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering }
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) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek Manager *m = userdata;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(q);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering assert(m);
a569398925430de1f8479262e8ab39502054f2e9Lennart Poettering 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
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (ret != 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Failed to resolve %s: %s", m->current_server_name->string, gai_strerror(ret));
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* Try next host */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return manager_connect(m);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek for (; ai; ai = ai->ai_next) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering _cleanup_free_ char *pretty = NULL;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering ServerAddress *a;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(ai->ai_addr);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(ai->ai_addrlen >= offsetof(struct sockaddr, sa_data));
da4993920cdf5527b8528f0a483b54ab3cbc1971Kay Sievers
40adcda869bda55f44b57fd3a2bd71d006dfb51bLennart Poettering if (!IN_SET(ai->ai_addr->sa_family, AF_INET, AF_INET6)) {
759c945a43577d56e85a927f15e7d9aaa94a4e4aColin Walters log_warning("Unsuitable address protocol for %s", m->current_server_name->string);
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering continue;
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering }
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering r = server_address_new(m->current_server_name, &a, (const union sockaddr_union*) ai->ai_addr, ai->ai_addrlen);
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering if (r < 0) {
7517e17443225fafea86b21e42a36af69feb1dbcTorstein Husebø log_error("Failed to add server address: %s", strerror(-r));
8a0889dfdafa3054c894e54852d8a9e3a7e8390bLennart Poettering return r;
da4993920cdf5527b8528f0a483b54ab3cbc1971Kay Sievers }
da4993920cdf5527b8528f0a483b54ab3cbc1971Kay Sievers
759c945a43577d56e85a927f15e7d9aaa94a4e4aColin Walters server_address_pretty(a, &pretty);
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering 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) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to find suitable address for host %s.", m->current_server_name->string);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchagin /* Try next host */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return manager_connect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_set_server_address(m, m->current_server_name->addresses);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return manager_begin(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchagin
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchaginstatic int manager_retry_connect(sd_event_source *source, usec_t usec, void *userdata) {
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchagin 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;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
e2cc6eca73cd1df8be552d7c23f9ff3d69c06f1eLennart Poettering
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.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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);
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering 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 }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering }
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* If we already are operating on some address, switch to the
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * next one. */
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (m->current_server_address && m->current_server_address->addresses_next)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek manager_set_server_address(m, m->current_server_address->addresses_next);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek else {
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering struct addrinfo hints = {
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer .ai_flags = AI_NUMERICSERV|AI_ADDRCONFIG,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering .ai_socktype = SOCK_DGRAM,
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew 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
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Our current server name list is exhausted,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * let's find the next one to iterate. First
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * we try the system list, then the link list.
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther * After having processed the link list we
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther * jump back to the system list. However, if
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther * both lists are empty, we change to the
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther * fallback list. */
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther if (!m->current_server_name || m->current_server_name->type == SERVER_LINK) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek f = m->system_servers;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!f)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek f = m->link_servers;
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering } else {
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering f = m->link_servers;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!f)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek f = m->system_servers;
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
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_set_server_name(m, f);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Tell the resolver to reread /etc/resolv.conf, in
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * case it changed. */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering res_init();
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek /* Flush out any previously resolved addresses */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek server_name_flush_addresses(m->current_server_name);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Resolving %s...", m->current_server_name->string);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek r = sd_resolve_getaddrinfo(m->resolve, &m->resolve_query, m->current_server_name->string, "123", &hints, manager_resolve_handler, m);
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek if (r < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to create resolver: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
caa2f4c0c9613b2e02aafa308c8fb092576014a9Zbigniew Jędrzejewski-Szmek
84267e4043cf88bf540b5bf9cd65e194670a4ffaLennart Poettering r = manager_begin(m);
09eba4d46b1119c758fba38a520a38c8d19af739Lennart Poettering if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 1;
caa2f4c0c9613b2e02aafa308c8fb092576014a9Zbigniew Jędrzejewski-Szmek}
caa2f4c0c9613b2e02aafa308c8fb092576014a9Zbigniew Jędrzejewski-Szmek
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
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek 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);
ac892057c2ddd8f06323c73ebd80423cc3ec7190Dimitri John Ledkov m->server_socket = safe_close(m->server_socket);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m->event_clock_watch = sd_event_source_unref(m->event_clock_watch);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->clock_watch_fd = safe_close(m->clock_watch_fd);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering m->event_timeout = sd_event_source_unref(m->event_timeout);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering sd_notifyf(false, "STATUS=Idle.");
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringvoid manager_flush_server_names(Manager *m, ServerType t) {
433dd100442e8197868def975c6fd38b48dc6439Lukas Nykryn assert(m);
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek if (t == SERVER_SYSTEM)
433dd100442e8197868def975c6fd38b48dc6439Lukas Nykryn while (m->system_servers)
433dd100442e8197868def975c6fd38b48dc6439Lukas Nykryn server_name_free(m->system_servers);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (t == SERVER_LINK)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek while (m->link_servers)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek server_name_free(m->link_servers);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (t == SERVER_FALLBACK)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek while (m->fallback_servers)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek server_name_free(m->fallback_servers);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekvoid manager_free(Manager *m) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!m)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_disconnect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_flush_server_names(m, SERVER_SYSTEM);
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt 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);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_resolve_unref(m->resolve);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_event_unref(m->event);
fc1d70af2101e16c9e6f3c5bfd5ab315ee9e6daeLennart Poettering
fc1d70af2101e16c9e6f3c5bfd5ab315ee9e6daeLennart Poettering free(m);
fc1d70af2101e16c9e6f3c5bfd5ab315ee9e6daeLennart Poettering}
fc1d70af2101e16c9e6f3c5bfd5ab315ee9e6daeLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int manager_network_read_link_servers(Manager *m) {
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt _cleanup_strv_free_ char **ntp = NULL;
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt ServerName *n, *nx;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char **i;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek assert(m);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering 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;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek STRV_FOREACH(i, ntp) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek bool found = false;
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering LIST_FOREACH(names, n, m->link_servers)
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering if (streq(n->string, *i)) {
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering n->marked = false;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek found = true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek 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 }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LIST_FOREACH_SAFE(names, n, nx, m->link_servers)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (n->marked)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek server_name_free(n);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poetteringclear:
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering manager_flush_server_names(m, SERVER_LINK);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
00a168618906bea43c3c57e20b9152582c324bf8Olivier Brunel}
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;
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt bool connected, online;
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
93b73b064c663d6248bebfbbbd82989b5ca10fc5Lennart Poettering assert(m);
93b73b064c663d6248bebfbbbd82989b5ca10fc5Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_network_monitor_flush(m->network_monitor);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_network_read_link_servers(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* check if the machine is online */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek online = network_is_online();
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering /* check if the client is currently connected */
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering connected = m->server_socket >= 0 || m->resolve_query;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (connected && !online) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_info("No network connectivity, watching for changes.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek 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
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->current_server_address)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = manager_begin(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek else
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt r = manager_connect(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poetteringstatic int manager_network_monitor_listen(Manager *m) {
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering int r, fd, events;
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering assert(m);
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_network_monitor_new(&m->network_monitor, NULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fd = sd_network_monitor_get_fd(m->network_monitor);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (fd < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return fd;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
804ae586d475d77946debb22c1bc9ee049d4750cLennart Poettering events = sd_network_monitor_get_events(m->network_monitor);
804ae586d475d77946debb22c1bc9ee049d4750cLennart Poettering if (events < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return events;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_event_add_io(m->event, &m->network_event_source, fd, events, manager_network_event_handler, m);
804ae586d475d77946debb22c1bc9ee049d4750cLennart Poettering if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
c6878637502b1717a110a9a7e8bba32a8583fcdfLennart Poettering return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
763c7aa288485cf5ab627fe1d25ff58e76f9dacbZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint manager_new(Manager **ret) {
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering _cleanup_(manager_freep) Manager *m = NULL;
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(ret);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8531ae707d4d0203e83304d4af948b8169a5fce1Lennart Poettering m = new0(Manager, 1);
f9a810bedacf1da7c505c1786a2416d592665926Lennart 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)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return r;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering sd_event_set_watchdog(m->event, true);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering r = sd_resolve_default(&m->resolve);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart 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)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return r;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering r = manager_network_monitor_listen(m);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (r < 0)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return r;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering manager_network_read_link_servers(m);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering *ret = m;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering m = NULL;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering