1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <sys/timex.h>
5 
6 #include "sd-bus.h"
7 #include "sd-event.h"
8 #include "sd-network.h"
9 #include "sd-resolve.h"
10 
11 #include "hashmap.h"
12 #include "list.h"
13 #include "ratelimit.h"
14 #include "time-util.h"
15 #include "timesyncd-ntp-message.h"
16 
17 typedef struct Manager Manager;
18 
19 #include "timesyncd-server.h"
20 
21 /*
22  * "A client MUST NOT under any conditions use a poll interval less
23  * than 15 seconds."
24  */
25 #define NTP_POLL_INTERVAL_MIN_USEC      (32 * USEC_PER_SEC)
26 #define NTP_POLL_INTERVAL_MAX_USEC      (2048 * USEC_PER_SEC)
27 
28 #define NTP_RETRY_INTERVAL_MIN_USEC     (15 * USEC_PER_SEC)
29 #define NTP_RETRY_INTERVAL_MAX_USEC     (6 * 60 * USEC_PER_SEC) /* 6 minutes */
30 
31 #define DEFAULT_CONNECTION_RETRY_USEC   (30 * USEC_PER_SEC)
32 
33 #define DEFAULT_SAVE_TIME_INTERVAL_USEC (60 * USEC_PER_SEC)
34 
35 #define STATE_DIR   "/var/lib/systemd/timesync"
36 #define CLOCK_FILE  STATE_DIR "/clock"
37 
38 struct Manager {
39         sd_bus *bus;
40         sd_event *event;
41         sd_resolve *resolve;
42 
43         LIST_HEAD(ServerName, system_servers);
44         LIST_HEAD(ServerName, link_servers);
45         LIST_HEAD(ServerName, runtime_servers);
46         LIST_HEAD(ServerName, fallback_servers);
47 
48         bool have_fallbacks:1;
49 
50         RateLimit ratelimit;
51         bool exhausted_servers;
52 
53         /* network */
54         sd_event_source *network_event_source;
55         sd_network_monitor *network_monitor;
56 
57         /* peer */
58         sd_resolve_query *resolve_query;
59         sd_event_source *event_receive;
60         ServerName *current_server_name;
61         ServerAddress *current_server_address;
62         int server_socket;
63         int missed_replies;
64         uint64_t packet_count;
65         sd_event_source *event_timeout;
66         bool talking;
67 
68         /* PolicyKit */
69         Hashmap *polkit_registry;
70 
71         /* last sent packet */
72         struct timespec trans_time_mon;
73         struct timespec trans_time;
74         usec_t retry_interval;
75         usec_t connection_retry_usec;
76         bool pending;
77 
78         /* poll timer */
79         sd_event_source *event_timer;
80         usec_t poll_interval_usec;
81         usec_t poll_interval_min_usec;
82         usec_t poll_interval_max_usec;
83         bool poll_resync;
84 
85         /* history data */
86         struct {
87                 double offset;
88                 double delay;
89         } samples[8];
90         unsigned samples_idx;
91         double samples_jitter;
92         usec_t root_distance_max_usec;
93 
94         /* last change */
95         bool jumped;
96         int64_t drift_freq;
97 
98         /* watch for time changes */
99         sd_event_source *event_clock_watch;
100 
101         /* Retry connections */
102         sd_event_source *event_retry;
103 
104         /* RTC runs in local time, leave it alone */
105         bool rtc_local_time;
106 
107         /* NTP response */
108         struct ntp_msg ntpmsg;
109         struct timespec origin_time, dest_time;
110         bool spike;
111 
112         /* Indicates whether we ever managed to set the local clock from NTP */
113         bool synchronized;
114 
115         /* save time event */
116         sd_event_source *event_save_time;
117         usec_t save_time_interval_usec;
118         bool save_on_exit;
119 };
120 
121 int manager_new(Manager **ret);
122 Manager* manager_free(Manager *m);
123 
124 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
125 
126 void manager_set_server_name(Manager *m, ServerName *n);
127 void manager_set_server_address(Manager *m, ServerAddress *a);
128 void manager_flush_server_names(Manager *m, ServerType t);
129 void manager_flush_runtime_servers(Manager *m);
130 
131 int manager_connect(Manager *m);
132 void manager_disconnect(Manager *m);
133 bool manager_is_connected(Manager *m);
134 
135 int manager_setup_save_time_event(Manager *m);
136