]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/timesync/timesyncd-bus.c
Merge pull request #10134 from keszybz/test-runner
[thirdparty/systemd.git] / src / timesync / timesyncd-bus.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "sd-bus.h"
4
5 #include "alloc-util.h"
6 #include "bus-internal.h"
7 #include "bus-protocol.h"
8 #include "bus-util.h"
9 #include "in-addr-util.h"
10 #include "log.h"
11 #include "macro.h"
12 #include "time-util.h"
13 #include "timesyncd-bus.h"
14
15 static int property_get_servers(
16 sd_bus *bus,
17 const char *path,
18 const char *interface,
19 const char *property,
20 sd_bus_message *reply,
21 void *userdata,
22 sd_bus_error *error) {
23
24 ServerName *p, **s = userdata;
25 int r;
26
27 assert(s);
28 assert(bus);
29 assert(reply);
30
31 r = sd_bus_message_open_container(reply, 'a', "s");
32 if (r < 0)
33 return r;
34
35 LIST_FOREACH(names, p, *s) {
36 r = sd_bus_message_append(reply, "s", p->string);
37 if (r < 0)
38 return r;
39 }
40
41 return sd_bus_message_close_container(reply);
42 }
43
44 static int property_get_current_server_name(
45 sd_bus *bus,
46 const char *path,
47 const char *interface,
48 const char *property,
49 sd_bus_message *reply,
50 void *userdata,
51 sd_bus_error *error) {
52
53 ServerName **s = userdata;
54
55 assert(s);
56 assert(bus);
57 assert(reply);
58
59 return sd_bus_message_append(reply, "s", *s ? (*s)->string : NULL);
60 }
61
62 static int property_get_current_server_address(
63 sd_bus *bus,
64 const char *path,
65 const char *interface,
66 const char *property,
67 sd_bus_message *reply,
68 void *userdata,
69 sd_bus_error *error) {
70
71 ServerAddress *a;
72 int r;
73
74 assert(bus);
75 assert(reply);
76 assert(userdata);
77
78 a = *(ServerAddress **) userdata;
79
80 if (!a)
81 return sd_bus_message_append(reply, "(iay)", AF_UNSPEC, 0);
82
83 r = sd_bus_message_open_container(reply, 'r', "iay");
84 if (r < 0)
85 return r;
86
87 r = sd_bus_message_append(reply, "i", a->sockaddr.sa.sa_family);
88 if (r < 0)
89 return r;
90
91 r = sd_bus_message_append_array(reply, 'y', &a->sockaddr.in.sin_addr, FAMILY_ADDRESS_SIZE(a->sockaddr.sa.sa_family));
92 if (r < 0)
93 return r;
94
95 return sd_bus_message_close_container(reply);
96 }
97
98 static usec_t ntp_ts_short_to_usec(const struct ntp_ts_short *ts) {
99 return be16toh(ts->sec) * USEC_PER_SEC + (be16toh(ts->frac) * USEC_PER_SEC) / (usec_t) 0x10000ULL;
100 }
101
102 static usec_t ntp_ts_to_usec(const struct ntp_ts *ts) {
103 return (be32toh(ts->sec) - OFFSET_1900_1970) * USEC_PER_SEC + (be32toh(ts->frac) * USEC_PER_SEC) / (usec_t) 0x100000000ULL;
104 }
105
106 static int property_get_ntp_message(
107 sd_bus *bus,
108 const char *path,
109 const char *interface,
110 const char *property,
111 sd_bus_message *reply,
112 void *userdata,
113 sd_bus_error *error) {
114
115 Manager *m = userdata;
116 int r;
117
118 assert(m);
119 assert(reply);
120
121 r = sd_bus_message_open_container(reply, 'r', "uuuuittayttttbtt");
122 if (r < 0)
123 return r;
124
125 r = sd_bus_message_append(reply, "uuuuitt",
126 NTP_FIELD_LEAP(m->ntpmsg.field),
127 NTP_FIELD_VERSION(m->ntpmsg.field),
128 NTP_FIELD_MODE(m->ntpmsg.field),
129 m->ntpmsg.stratum,
130 m->ntpmsg.precision,
131 ntp_ts_short_to_usec(&m->ntpmsg.root_delay),
132 ntp_ts_short_to_usec(&m->ntpmsg.root_dispersion));
133 if (r < 0)
134 return r;
135
136 r = sd_bus_message_append_array(reply, 'y', m->ntpmsg.refid, 4);
137 if (r < 0)
138 return r;
139
140 r = sd_bus_message_append(reply, "ttttbtt",
141 timespec_load(&m->origin_time),
142 ntp_ts_to_usec(&m->ntpmsg.recv_time),
143 ntp_ts_to_usec(&m->ntpmsg.trans_time),
144 timespec_load(&m->dest_time),
145 m->spike,
146 m->packet_count,
147 (usec_t) (m->samples_jitter * USEC_PER_SEC));
148 if (r < 0)
149 return r;
150
151 return sd_bus_message_close_container(reply);
152 }
153
154 static const sd_bus_vtable manager_vtable[] = {
155 SD_BUS_VTABLE_START(0),
156
157 SD_BUS_PROPERTY("LinkNTPServers", "as", property_get_servers, offsetof(Manager, link_servers), 0),
158 SD_BUS_PROPERTY("SystemNTPServers", "as", property_get_servers, offsetof(Manager, system_servers), SD_BUS_VTABLE_PROPERTY_CONST),
159 SD_BUS_PROPERTY("FallbackNTPServers", "as", property_get_servers, offsetof(Manager, fallback_servers), SD_BUS_VTABLE_PROPERTY_CONST),
160 SD_BUS_PROPERTY("ServerName", "s", property_get_current_server_name, offsetof(Manager, current_server_name), 0),
161 SD_BUS_PROPERTY("ServerAddress", "(iay)", property_get_current_server_address, offsetof(Manager, current_server_address), 0),
162 SD_BUS_PROPERTY("RootDistanceMaxUSec", "t", bus_property_get_usec, offsetof(Manager, max_root_distance_usec), SD_BUS_VTABLE_PROPERTY_CONST),
163 SD_BUS_PROPERTY("PollIntervalMinUSec", "t", bus_property_get_usec, offsetof(Manager, poll_interval_min_usec), SD_BUS_VTABLE_PROPERTY_CONST),
164 SD_BUS_PROPERTY("PollIntervalMaxUSec", "t", bus_property_get_usec, offsetof(Manager, poll_interval_max_usec), SD_BUS_VTABLE_PROPERTY_CONST),
165 SD_BUS_PROPERTY("PollIntervalUSec", "t", bus_property_get_usec, offsetof(Manager, poll_interval_usec), 0),
166 SD_BUS_PROPERTY("NTPMessage", "(uuuuittayttttbtt)", property_get_ntp_message, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
167 SD_BUS_PROPERTY("Frequency", "x", NULL, offsetof(Manager, drift_freq), 0),
168
169 SD_BUS_VTABLE_END
170 };
171
172 int manager_connect_bus(Manager *m) {
173 int r;
174
175 assert(m);
176
177 if (m->bus)
178 return 0;
179
180 r = bus_open_system_watch_bind_with_description(&m->bus, "bus-api-timesync");
181 if (r < 0)
182 return log_error_errno(r, "Failed to connect to bus: %m");
183
184 r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/timesync1", "org.freedesktop.timesync1.Manager", manager_vtable, m);
185 if (r < 0)
186 return log_error_errno(r, "Failed to add manager object vtable: %m");
187
188 r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.timesync1", 0, NULL, NULL);
189 if (r < 0)
190 return log_error_errno(r, "Failed to request name: %m");
191
192 r = sd_bus_attach_event(m->bus, m->event, 0);
193 if (r < 0)
194 return log_error_errno(r, "Failed to attach bus to event loop: %m");
195
196 return 0;
197 }