]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
6d0274f1 | 2 | |
6d0274f1 | 3 | #include <getopt.h> |
a9cdc94f | 4 | #include <locale.h> |
6129ec85 | 5 | #include <math.h> |
3f6fd1ba LP |
6 | #include <stdbool.h> |
7 | #include <stdlib.h> | |
6d0274f1 | 8 | |
a281d9c7 | 9 | #include "sd-bus.h" |
3f6fd1ba | 10 | |
d6b4d1c7 | 11 | #include "build.h" |
a281d9c7 | 12 | #include "bus-error.h" |
9b71e4ab | 13 | #include "bus-locator.h" |
807542be | 14 | #include "bus-map-properties.h" |
9176326b | 15 | #include "bus-print-properties.h" |
063f9f0d | 16 | #include "env-util.h" |
cf57766d | 17 | #include "format-table.h" |
6129ec85 | 18 | #include "in-addr-util.h" |
50378e5d | 19 | #include "main-func.h" |
3f6fd1ba | 20 | #include "pager.h" |
6bedfcbb | 21 | #include "parse-util.h" |
294bf0c3 | 22 | #include "pretty-print.h" |
6129ec85 | 23 | #include "sparse-endian.h" |
9b71e4ab | 24 | #include "spawn-polkit-agent.h" |
6129ec85 | 25 | #include "string-table.h" |
6d0274f1 | 26 | #include "strv.h" |
288a74cc | 27 | #include "terminal-util.h" |
be90a886 | 28 | #include "verbs.h" |
6d0274f1 | 29 | |
0221d68a | 30 | static PagerFlags arg_pager_flags = 0; |
6d0274f1 | 31 | static bool arg_ask_password = true; |
e1636421 | 32 | static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; |
7085053a | 33 | static char *arg_host = NULL; |
e1636421 | 34 | static bool arg_adjust_system_clock = false; |
6129ec85 YW |
35 | static bool arg_monitor = false; |
36 | static char **arg_property = NULL; | |
255b1fc8 | 37 | static BusPrintPropertyFlags arg_print_flags = 0; |
6d0274f1 | 38 | |
6d0274f1 | 39 | typedef struct StatusInfo { |
2f6a5907 | 40 | usec_t time; |
f37f8a61 | 41 | const char *timezone; |
2f6a5907 KS |
42 | |
43 | usec_t rtc_time; | |
f37f8a61 | 44 | bool rtc_local; |
2f6a5907 | 45 | |
f37f8a61 | 46 | bool ntp_capable; |
f567f086 | 47 | bool ntp_active; |
f37f8a61 | 48 | bool ntp_synced; |
6d0274f1 LP |
49 | } StatusInfo; |
50 | ||
cf57766d YW |
51 | static int print_status_info(const StatusInfo *i) { |
52 | _cleanup_(table_unrefp) Table *table = NULL; | |
437f48a4 | 53 | const char *old_tz = NULL, *tz, *tz_colon; |
f567f086 | 54 | bool have_time = false; |
14ce0c25 | 55 | char a[LINE_MAX]; |
cf57766d | 56 | TableCell *cell; |
6d0274f1 LP |
57 | struct tm tm; |
58 | time_t sec; | |
14ce0c25 | 59 | size_t n; |
f567f086 | 60 | int r; |
6d0274f1 | 61 | |
59965986 LP |
62 | assert(i); |
63 | ||
cf57766d YW |
64 | table = table_new("key", "value"); |
65 | if (!table) | |
66 | return log_oom(); | |
67 | ||
68 | table_set_header(table, false); | |
69 | ||
70 | assert_se(cell = table_get_cell(table, 0, 0)); | |
71 | (void) table_set_ellipsize_percent(table, cell, 100); | |
72 | (void) table_set_align_percent(table, cell, 100); | |
73 | ||
74 | assert_se(cell = table_get_cell(table, 0, 1)); | |
75 | (void) table_set_ellipsize_percent(table, cell, 100); | |
76 | ||
d95a74ed LP |
77 | /* Save the old $TZ */ |
78 | tz = getenv("TZ"); | |
79 | if (tz) | |
2f82562b | 80 | old_tz = strdupa_safe(tz); |
2311eb2f | 81 | |
d95a74ed | 82 | /* Set the new $TZ */ |
437f48a4 LP |
83 | tz_colon = strjoina(":", isempty(i->timezone) ? "UTC" : i->timezone); |
84 | if (setenv("TZ", tz_colon, true) < 0) | |
d95a74ed LP |
85 | log_warning_errno(errno, "Failed to set TZ environment variable, ignoring: %m"); |
86 | else | |
87 | tzset(); | |
3e5e74d5 | 88 | |
9ff09bcb SL |
89 | if (i->time != 0) { |
90 | sec = (time_t) (i->time / USEC_PER_SEC); | |
91 | have_time = true; | |
d95a74ed | 92 | } else if (IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE)) { |
9ff09bcb SL |
93 | sec = time(NULL); |
94 | have_time = true; | |
95 | } else | |
d95a74ed | 96 | log_warning("Could not get time from timedated and not operating locally, ignoring."); |
6d0274f1 | 97 | |
a03e335b | 98 | n = have_time ? strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm)) : 0; |
cf57766d YW |
99 | r = table_add_many(table, |
100 | TABLE_STRING, "Local time:", | |
a03e335b | 101 | TABLE_STRING, n > 0 ? a : "n/a"); |
cf57766d | 102 | if (r < 0) |
bd17fa8c | 103 | return table_log_add_error(r); |
cf57766d | 104 | |
a03e335b | 105 | n = have_time ? strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm)) : 0; |
cf57766d YW |
106 | r = table_add_many(table, |
107 | TABLE_STRING, "Universal time:", | |
a03e335b | 108 | TABLE_STRING, n > 0 ? a : "n/a"); |
cf57766d | 109 | if (r < 0) |
bd17fa8c | 110 | return table_log_add_error(r); |
6d0274f1 | 111 | |
2f6a5907 KS |
112 | if (i->rtc_time > 0) { |
113 | time_t rtc_sec; | |
6d0274f1 | 114 | |
d95a74ed | 115 | rtc_sec = (time_t) (i->rtc_time / USEC_PER_SEC); |
14ce0c25 | 116 | n = strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec, &tm)); |
a03e335b ZJS |
117 | } else |
118 | n = 0; | |
cf57766d YW |
119 | r = table_add_many(table, |
120 | TABLE_STRING, "RTC time:", | |
a03e335b | 121 | TABLE_STRING, n > 0 ? a : "n/a"); |
cf57766d | 122 | if (r < 0) |
bd17fa8c | 123 | return table_log_add_error(r); |
6d0274f1 | 124 | |
cf57766d YW |
125 | r = table_add_cell(table, NULL, TABLE_STRING, "Time zone:"); |
126 | if (r < 0) | |
bd17fa8c | 127 | return table_log_add_error(r); |
cf57766d | 128 | |
a03e335b ZJS |
129 | n = have_time ? strftime(a, sizeof a, "%Z, %z", localtime_r(&sec, &tm)) : 0; |
130 | r = table_add_cell_stringf(table, NULL, "%s (%s)", strna(i->timezone), n > 0 ? a : "n/a"); | |
cf57766d | 131 | if (r < 0) |
bd17fa8c | 132 | return table_log_add_error(r); |
cf57766d | 133 | |
d95a74ed | 134 | /* Restore the $TZ */ |
063f9f0d | 135 | r = set_unset_env("TZ", old_tz, true); |
d95a74ed | 136 | if (r < 0) |
063f9f0d | 137 | log_warning_errno(r, "Failed to set TZ environment variable, ignoring: %m"); |
d95a74ed LP |
138 | else |
139 | tzset(); | |
140 | ||
cf57766d YW |
141 | r = table_add_many(table, |
142 | TABLE_STRING, "System clock synchronized:", | |
143 | TABLE_BOOLEAN, i->ntp_synced, | |
144 | TABLE_STRING, "NTP service:", | |
145 | TABLE_STRING, i->ntp_capable ? (i->ntp_active ? "active" : "inactive") : "n/a", | |
146 | TABLE_STRING, "RTC in local TZ:", | |
147 | TABLE_BOOLEAN, i->rtc_local); | |
148 | if (r < 0) | |
bd17fa8c | 149 | return table_log_add_error(r); |
cf57766d YW |
150 | |
151 | r = table_print(table, NULL); | |
152 | if (r < 0) | |
4b6607d9 | 153 | return table_log_print_error(r); |
6d0274f1 | 154 | |
2f6a5907 | 155 | if (i->rtc_local) |
54f8c958 LP |
156 | printf("\n%s" |
157 | "Warning: The system is configured to read the RTC time in the local time zone.\n" | |
87ac8d99 | 158 | " This mode cannot be fully supported. It will create various problems\n" |
54f8c958 LP |
159 | " with time zone changes and daylight saving time adjustments. The RTC\n" |
160 | " time is never updated, it relies on external facilities to maintain it.\n" | |
161 | " If at all possible, use RTC in UTC by calling\n" | |
162 | " 'timedatectl set-local-rtc 0'.%s\n", ansi_highlight(), ansi_normal()); | |
cf57766d YW |
163 | |
164 | return 0; | |
6d0274f1 LP |
165 | } |
166 | ||
be90a886 | 167 | static int show_status(int argc, char **argv, void *userdata) { |
f37f8a61 | 168 | StatusInfo info = {}; |
9f6eb1cd | 169 | static const struct bus_properties_map map[] = { |
f567f086 YW |
170 | { "Timezone", "s", NULL, offsetof(StatusInfo, timezone) }, |
171 | { "LocalRTC", "b", NULL, offsetof(StatusInfo, rtc_local) }, | |
172 | { "NTP", "b", NULL, offsetof(StatusInfo, ntp_active) }, | |
9f6eb1cd | 173 | { "CanNTP", "b", NULL, offsetof(StatusInfo, ntp_capable) }, |
f567f086 YW |
174 | { "NTPSynchronized", "b", NULL, offsetof(StatusInfo, ntp_synced) }, |
175 | { "TimeUSec", "t", NULL, offsetof(StatusInfo, time) }, | |
176 | { "RTCTimeUSec", "t", NULL, offsetof(StatusInfo, rtc_time) }, | |
ffc06c35 KS |
177 | {} |
178 | }; | |
f9e0eefc LP |
179 | |
180 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
f37f8a61 | 181 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
99534007 | 182 | sd_bus *bus = ASSERT_PTR(userdata); |
ffc06c35 | 183 | int r; |
6d0274f1 | 184 | |
ffc06c35 KS |
185 | r = bus_map_all_properties(bus, |
186 | "org.freedesktop.timedate1", | |
187 | "/org/freedesktop/timedate1", | |
9f6eb1cd | 188 | map, |
a7e4861c | 189 | BUS_MAP_BOOLEAN_AS_BOOL, |
f9e0eefc | 190 | &error, |
f37f8a61 | 191 | &m, |
9f6eb1cd | 192 | &info); |
e7e55dbd | 193 | if (r < 0) |
f9e0eefc | 194 | return log_error_errno(r, "Failed to query server: %s", bus_error_message(&error, r)); |
6d0274f1 | 195 | |
cf57766d | 196 | return print_status_info(&info); |
6d0274f1 LP |
197 | } |
198 | ||
ead0adb1 | 199 | static int show_properties(int argc, char **argv, void *userdata) { |
99534007 | 200 | sd_bus *bus = ASSERT_PTR(userdata); |
ead0adb1 YW |
201 | int r; |
202 | ||
ead0adb1 YW |
203 | r = bus_print_all_properties(bus, |
204 | "org.freedesktop.timedate1", | |
205 | "/org/freedesktop/timedate1", | |
206 | NULL, | |
207 | arg_property, | |
255b1fc8 | 208 | arg_print_flags, |
ead0adb1 YW |
209 | NULL); |
210 | if (r < 0) | |
211 | return bus_log_parse_error(r); | |
212 | ||
213 | return 0; | |
214 | } | |
215 | ||
be90a886 | 216 | static int set_time(int argc, char **argv, void *userdata) { |
4afd3348 | 217 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
a281d9c7 | 218 | bool relative = false, interactive = arg_ask_password; |
be90a886 | 219 | sd_bus *bus = userdata; |
6d0274f1 | 220 | usec_t t; |
6d0274f1 LP |
221 | int r; |
222 | ||
8a4b13c5 | 223 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
6d0274f1 | 224 | |
be90a886 YW |
225 | r = parse_timestamp(argv[1], &t); |
226 | if (r < 0) | |
227 | return log_error_errno(r, "Failed to parse time specification '%s': %m", argv[1]); | |
6d0274f1 | 228 | |
43fe4f76 VC |
229 | r = bus_call_method( |
230 | bus, | |
231 | bus_timedate, | |
232 | "SetTime", | |
233 | &error, | |
234 | NULL, | |
235 | "xbb", (int64_t) t, relative, interactive); | |
a281d9c7 | 236 | if (r < 0) |
4ae25393 | 237 | return log_error_errno(r, "Failed to set time: %s", bus_error_message(&error, r)); |
a281d9c7 | 238 | |
4ae25393 | 239 | return 0; |
6d0274f1 LP |
240 | } |
241 | ||
be90a886 | 242 | static int set_timezone(int argc, char **argv, void *userdata) { |
4afd3348 | 243 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
be90a886 | 244 | sd_bus *bus = userdata; |
a281d9c7 | 245 | int r; |
6d0274f1 | 246 | |
8a4b13c5 | 247 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
6d0274f1 | 248 | |
43fe4f76 | 249 | r = bus_call_method(bus, bus_timedate, "SetTimezone", &error, NULL, "sb", argv[1], arg_ask_password); |
a281d9c7 | 250 | if (r < 0) |
4ae25393 | 251 | return log_error_errno(r, "Failed to set time zone: %s", bus_error_message(&error, r)); |
a281d9c7 | 252 | |
4ae25393 | 253 | return 0; |
6d0274f1 LP |
254 | } |
255 | ||
be90a886 | 256 | static int set_local_rtc(int argc, char **argv, void *userdata) { |
4afd3348 | 257 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
be90a886 | 258 | sd_bus *bus = userdata; |
e5609878 | 259 | int r, b; |
6d0274f1 | 260 | |
8a4b13c5 | 261 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
6d0274f1 | 262 | |
be90a886 YW |
263 | b = parse_boolean(argv[1]); |
264 | if (b < 0) | |
265 | return log_error_errno(b, "Failed to parse local RTC setting '%s': %m", argv[1]); | |
6d0274f1 | 266 | |
43fe4f76 VC |
267 | r = bus_call_method( |
268 | bus, | |
269 | bus_timedate, | |
270 | "SetLocalRTC", | |
271 | &error, | |
272 | NULL, | |
273 | "bbb", b, arg_adjust_system_clock, arg_ask_password); | |
a281d9c7 | 274 | if (r < 0) |
4ae25393 | 275 | return log_error_errno(r, "Failed to set local RTC: %s", bus_error_message(&error, r)); |
a281d9c7 | 276 | |
4ae25393 | 277 | return 0; |
6d0274f1 LP |
278 | } |
279 | ||
be90a886 | 280 | static int set_ntp(int argc, char **argv, void *userdata) { |
4afd3348 | 281 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
be90a886 | 282 | sd_bus *bus = userdata; |
e5609878 | 283 | int b, r; |
6d0274f1 | 284 | |
8a4b13c5 | 285 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
6d0274f1 | 286 | |
be90a886 YW |
287 | b = parse_boolean(argv[1]); |
288 | if (b < 0) | |
289 | return log_error_errno(b, "Failed to parse NTP setting '%s': %m", argv[1]); | |
6d0274f1 | 290 | |
43fe4f76 | 291 | r = bus_call_method(bus, bus_timedate, "SetNTP", &error, NULL, "bb", b, arg_ask_password); |
a281d9c7 | 292 | if (r < 0) |
4ae25393 | 293 | return log_error_errno(r, "Failed to set ntp: %s", bus_error_message(&error, r)); |
a281d9c7 | 294 | |
4ae25393 | 295 | return 0; |
6d0274f1 LP |
296 | } |
297 | ||
be90a886 | 298 | static int list_timezones(int argc, char **argv, void *userdata) { |
2cf0b2fe NT |
299 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
300 | sd_bus *bus = userdata; | |
301 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
75683450 | 302 | int r; |
a2e37d52 | 303 | _cleanup_strv_free_ char **zones = NULL; |
2cf0b2fe | 304 | |
43fe4f76 | 305 | r = bus_call_method(bus, bus_timedate, "ListTimezones", &error, &reply, NULL); |
2cf0b2fe NT |
306 | if (r < 0) |
307 | return log_error_errno(r, "Failed to request list of time zones: %s", | |
308 | bus_error_message(&error, r)); | |
6d0274f1 | 309 | |
2cf0b2fe | 310 | r = sd_bus_message_read_strv(reply, &zones); |
f647962d | 311 | if (r < 0) |
2cf0b2fe | 312 | return bus_log_parse_error(r); |
6d0274f1 | 313 | |
384c2c32 | 314 | pager_open(arg_pager_flags); |
7c2d8094 | 315 | strv_print(zones); |
6d0274f1 LP |
316 | |
317 | return 0; | |
318 | } | |
319 | ||
6129ec85 YW |
320 | typedef struct NTPStatusInfo { |
321 | const char *server_name; | |
322 | char *server_address; | |
323 | usec_t poll_interval, poll_max, poll_min; | |
324 | usec_t root_distance_max; | |
325 | ||
326 | uint32_t leap, version, mode, stratum; | |
327 | int32_t precision; | |
328 | usec_t root_delay, root_dispersion; | |
329 | union { | |
330 | char str[5]; | |
331 | uint32_t val; | |
332 | } reference; | |
333 | usec_t origin, recv, trans, dest; | |
334 | ||
335 | bool spike; | |
336 | uint64_t packet_count; | |
337 | usec_t jitter; | |
338 | ||
339 | int64_t freq; | |
340 | } NTPStatusInfo; | |
341 | ||
342 | static void ntp_status_info_clear(NTPStatusInfo *p) { | |
343 | p->server_address = mfree(p->server_address); | |
344 | } | |
345 | ||
346 | static const char * const ntp_leap_table[4] = { | |
347 | [0] = "normal", | |
348 | [1] = "last minute of the day has 61 seconds", | |
349 | [2] = "last minute of the day has 59 seconds", | |
350 | [3] = "not synchronized", | |
351 | }; | |
352 | ||
6028d766 | 353 | DISABLE_WARNING_TYPE_LIMITS; |
6129ec85 | 354 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(ntp_leap, uint32_t); |
6028d766 | 355 | REENABLE_WARNING; |
6129ec85 | 356 | |
cf57766d | 357 | static int print_ntp_status_info(NTPStatusInfo *i) { |
6129ec85 | 358 | usec_t delay, t14, t23, offset, root_distance; |
cf57766d | 359 | _cleanup_(table_unrefp) Table *table = NULL; |
6129ec85 | 360 | bool offset_sign; |
cf57766d YW |
361 | TableCell *cell; |
362 | int r; | |
6129ec85 YW |
363 | |
364 | assert(i); | |
365 | ||
cf57766d YW |
366 | table = table_new("key", "value"); |
367 | if (!table) | |
368 | return log_oom(); | |
369 | ||
370 | table_set_header(table, false); | |
371 | ||
372 | assert_se(cell = table_get_cell(table, 0, 0)); | |
373 | (void) table_set_ellipsize_percent(table, cell, 100); | |
374 | (void) table_set_align_percent(table, cell, 100); | |
375 | ||
376 | assert_se(cell = table_get_cell(table, 0, 1)); | |
377 | (void) table_set_ellipsize_percent(table, cell, 100); | |
378 | ||
6129ec85 YW |
379 | /* |
380 | * "Timestamp Name ID When Generated | |
381 | * ------------------------------------------------------------ | |
382 | * Originate Timestamp T1 time request sent by client | |
383 | * Receive Timestamp T2 time request received by server | |
384 | * Transmit Timestamp T3 time reply sent by server | |
385 | * Destination Timestamp T4 time reply received by client | |
386 | * | |
387 | * The round-trip delay, d, and system clock offset, t, are defined as: | |
388 | * d = (T4 - T1) - (T3 - T2) t = ((T2 - T1) + (T3 - T4)) / 2" | |
389 | */ | |
390 | ||
cf57766d YW |
391 | r = table_add_cell(table, NULL, TABLE_STRING, "Server:"); |
392 | if (r < 0) | |
bd17fa8c | 393 | return table_log_add_error(r); |
cf57766d | 394 | |
94ec163a | 395 | r = table_add_cell_stringf(table, NULL, "%s (%s)", strna(i->server_address), strna(i->server_name)); |
cf57766d | 396 | if (r < 0) |
bd17fa8c | 397 | return table_log_add_error(r); |
cf57766d YW |
398 | |
399 | r = table_add_cell(table, NULL, TABLE_STRING, "Poll interval:"); | |
400 | if (r < 0) | |
bd17fa8c | 401 | return table_log_add_error(r); |
cf57766d YW |
402 | |
403 | r = table_add_cell_stringf(table, NULL, "%s (min: %s; max %s)", | |
5291f26d ZJS |
404 | FORMAT_TIMESPAN(i->poll_interval, 0), |
405 | FORMAT_TIMESPAN(i->poll_min, 0), | |
406 | FORMAT_TIMESPAN(i->poll_max, 0)); | |
cf57766d | 407 | if (r < 0) |
bd17fa8c | 408 | return table_log_add_error(r); |
6129ec85 YW |
409 | |
410 | if (i->packet_count == 0) { | |
cf57766d YW |
411 | r = table_add_many(table, |
412 | TABLE_STRING, "Packet count:", | |
413 | TABLE_STRING, "0"); | |
414 | if (r < 0) | |
bd17fa8c | 415 | return table_log_add_error(r); |
cf57766d YW |
416 | |
417 | r = table_print(table, NULL); | |
418 | if (r < 0) | |
4b6607d9 | 419 | return table_log_print_error(r); |
cf57766d YW |
420 | |
421 | return 0; | |
6129ec85 YW |
422 | } |
423 | ||
424 | if (i->dest < i->origin || i->trans < i->recv || i->dest - i->origin < i->trans - i->recv) { | |
425 | log_error("Invalid NTP response"); | |
cf57766d YW |
426 | r = table_print(table, NULL); |
427 | if (r < 0) | |
4b6607d9 | 428 | return table_log_print_error(r); |
cf57766d YW |
429 | |
430 | return 0; | |
6129ec85 YW |
431 | } |
432 | ||
433 | delay = (i->dest - i->origin) - (i->trans - i->recv); | |
434 | ||
435 | t14 = i->origin + i->dest; | |
436 | t23 = i->recv + i->trans; | |
437 | offset_sign = t14 < t23; | |
438 | offset = (offset_sign ? t23 - t14 : t14 - t23) / 2; | |
439 | ||
440 | root_distance = i->root_delay / 2 + i->root_dispersion; | |
441 | ||
cf57766d YW |
442 | r = table_add_many(table, |
443 | TABLE_STRING, "Leap:", | |
444 | TABLE_STRING, ntp_leap_to_string(i->leap), | |
445 | TABLE_STRING, "Version:", | |
446 | TABLE_UINT32, i->version, | |
447 | TABLE_STRING, "Stratum:", | |
448 | TABLE_UINT32, i->stratum, | |
449 | TABLE_STRING, "Reference:"); | |
450 | if (r < 0) | |
bd17fa8c | 451 | return table_log_add_error(r); |
cf57766d | 452 | |
6129ec85 | 453 | if (i->stratum <= 1) |
cf57766d | 454 | r = table_add_cell(table, NULL, TABLE_STRING, i->reference.str); |
6129ec85 | 455 | else |
cf57766d YW |
456 | r = table_add_cell_stringf(table, NULL, "%" PRIX32, be32toh(i->reference.val)); |
457 | if (r < 0) | |
bd17fa8c | 458 | return table_log_add_error(r); |
cf57766d YW |
459 | |
460 | r = table_add_cell(table, NULL, TABLE_STRING, "Precision:"); | |
461 | if (r < 0) | |
bd17fa8c | 462 | return table_log_add_error(r); |
cf57766d YW |
463 | |
464 | r = table_add_cell_stringf(table, NULL, "%s (%" PRIi32 ")", | |
5291f26d | 465 | FORMAT_TIMESPAN(DIV_ROUND_UP((nsec_t) (exp2(i->precision) * NSEC_PER_SEC), NSEC_PER_USEC), 0), |
cf57766d YW |
466 | i->precision); |
467 | if (r < 0) | |
bd17fa8c | 468 | return table_log_add_error(r); |
cf57766d YW |
469 | |
470 | r = table_add_cell(table, NULL, TABLE_STRING, "Root distance:"); | |
471 | if (r < 0) | |
bd17fa8c | 472 | return table_log_add_error(r); |
cf57766d YW |
473 | |
474 | r = table_add_cell_stringf(table, NULL, "%s (max: %s)", | |
5291f26d ZJS |
475 | FORMAT_TIMESPAN(root_distance, 0), |
476 | FORMAT_TIMESPAN(i->root_distance_max, 0)); | |
cf57766d | 477 | if (r < 0) |
bd17fa8c | 478 | return table_log_add_error(r); |
cf57766d YW |
479 | |
480 | r = table_add_cell(table, NULL, TABLE_STRING, "Offset:"); | |
481 | if (r < 0) | |
bd17fa8c | 482 | return table_log_add_error(r); |
cf57766d YW |
483 | |
484 | r = table_add_cell_stringf(table, NULL, "%s%s", | |
485 | offset_sign ? "+" : "-", | |
5291f26d | 486 | FORMAT_TIMESPAN(offset, 0)); |
cf57766d | 487 | if (r < 0) |
bd17fa8c | 488 | return table_log_add_error(r); |
cf57766d YW |
489 | |
490 | r = table_add_many(table, | |
491 | TABLE_STRING, "Delay:", | |
5291f26d | 492 | TABLE_STRING, FORMAT_TIMESPAN(delay, 0), |
cf57766d | 493 | TABLE_STRING, "Jitter:", |
5291f26d | 494 | TABLE_STRING, FORMAT_TIMESPAN(i->jitter, 0), |
cf57766d YW |
495 | TABLE_STRING, "Packet count:", |
496 | TABLE_UINT64, i->packet_count); | |
497 | if (r < 0) | |
bd17fa8c | 498 | return table_log_add_error(r); |
cf57766d YW |
499 | |
500 | if (!i->spike) { | |
501 | r = table_add_cell(table, NULL, TABLE_STRING, "Frequency:"); | |
502 | if (r < 0) | |
bd17fa8c | 503 | return table_log_add_error(r); |
cf57766d YW |
504 | |
505 | r = table_add_cell_stringf(table, NULL, "%+.3fppm", (double) i->freq / 0x10000); | |
506 | if (r < 0) | |
bd17fa8c | 507 | return table_log_add_error(r); |
cf57766d YW |
508 | } |
509 | ||
510 | r = table_print(table, NULL); | |
511 | if (r < 0) | |
4b6607d9 | 512 | return table_log_print_error(r); |
cf57766d YW |
513 | |
514 | return 0; | |
6129ec85 YW |
515 | } |
516 | ||
517 | static int map_server_address(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
518 | char **p = (char **) userdata; | |
519 | const void *d; | |
520 | int family, r; | |
521 | size_t sz; | |
522 | ||
523 | assert(p); | |
524 | ||
525 | r = sd_bus_message_enter_container(m, 'r', "iay"); | |
526 | if (r < 0) | |
527 | return r; | |
528 | ||
529 | r = sd_bus_message_read(m, "i", &family); | |
530 | if (r < 0) | |
531 | return r; | |
532 | ||
533 | r = sd_bus_message_read_array(m, 'y', &d, &sz); | |
534 | if (r < 0) | |
535 | return r; | |
536 | ||
537 | r = sd_bus_message_exit_container(m); | |
538 | if (r < 0) | |
539 | return r; | |
540 | ||
541 | if (sz == 0 && family == AF_UNSPEC) { | |
542 | *p = mfree(*p); | |
543 | return 0; | |
544 | } | |
545 | ||
baaa35ad ZJS |
546 | if (!IN_SET(family, AF_INET, AF_INET6)) |
547 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
548 | "Unknown address family %i", family); | |
6129ec85 | 549 | |
baaa35ad ZJS |
550 | if (sz != FAMILY_ADDRESS_SIZE(family)) |
551 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
552 | "Invalid address size"); | |
6129ec85 YW |
553 | |
554 | r = in_addr_to_string(family, d, p); | |
555 | if (r < 0) | |
556 | return r; | |
557 | ||
558 | return 0; | |
559 | } | |
560 | ||
561 | static int map_ntp_message(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
99534007 | 562 | NTPStatusInfo *p = ASSERT_PTR(userdata); |
6129ec85 YW |
563 | const void *d; |
564 | size_t sz; | |
565 | int32_t b; | |
566 | int r; | |
567 | ||
6129ec85 YW |
568 | r = sd_bus_message_enter_container(m, 'r', "uuuuittayttttbtt"); |
569 | if (r < 0) | |
570 | return r; | |
571 | ||
572 | r = sd_bus_message_read(m, "uuuuitt", | |
573 | &p->leap, &p->version, &p->mode, &p->stratum, &p->precision, | |
574 | &p->root_delay, &p->root_dispersion); | |
575 | if (r < 0) | |
576 | return r; | |
577 | ||
578 | r = sd_bus_message_read_array(m, 'y', &d, &sz); | |
579 | if (r < 0) | |
580 | return r; | |
581 | ||
582 | r = sd_bus_message_read(m, "ttttbtt", | |
583 | &p->origin, &p->recv, &p->trans, &p->dest, | |
584 | &b, &p->packet_count, &p->jitter); | |
585 | if (r < 0) | |
586 | return r; | |
587 | ||
588 | r = sd_bus_message_exit_container(m); | |
589 | if (r < 0) | |
590 | return r; | |
591 | ||
592 | if (sz != 4) | |
593 | return -EINVAL; | |
594 | ||
595 | memcpy(p->reference.str, d, sz); | |
596 | ||
5d904a6a | 597 | p->spike = b; |
6129ec85 YW |
598 | |
599 | return 0; | |
600 | } | |
601 | ||
602 | static int show_timesync_status_once(sd_bus *bus) { | |
603 | static const struct bus_properties_map map_timesync[] = { | |
604 | { "ServerName", "s", NULL, offsetof(NTPStatusInfo, server_name) }, | |
605 | { "ServerAddress", "(iay)", map_server_address, offsetof(NTPStatusInfo, server_address) }, | |
606 | { "PollIntervalUSec", "t", NULL, offsetof(NTPStatusInfo, poll_interval) }, | |
607 | { "PollIntervalMinUSec", "t", NULL, offsetof(NTPStatusInfo, poll_min) }, | |
608 | { "PollIntervalMaxUSec", "t", NULL, offsetof(NTPStatusInfo, poll_max) }, | |
609 | { "RootDistanceMaxUSec", "t", NULL, offsetof(NTPStatusInfo, root_distance_max) }, | |
610 | { "NTPMessage", "(uuuuittayttttbtt)", map_ntp_message, 0 }, | |
611 | { "Frequency", "x", NULL, offsetof(NTPStatusInfo, freq) }, | |
612 | {} | |
613 | }; | |
614 | _cleanup_(ntp_status_info_clear) NTPStatusInfo info = {}; | |
615 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
616 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
617 | int r; | |
618 | ||
619 | assert(bus); | |
620 | ||
621 | r = bus_map_all_properties(bus, | |
622 | "org.freedesktop.timesync1", | |
623 | "/org/freedesktop/timesync1", | |
624 | map_timesync, | |
625 | BUS_MAP_BOOLEAN_AS_BOOL, | |
626 | &error, | |
627 | &m, | |
628 | &info); | |
629 | if (r < 0) | |
630 | return log_error_errno(r, "Failed to query server: %s", bus_error_message(&error, r)); | |
631 | ||
632 | if (arg_monitor && !terminal_is_dumb()) | |
633 | fputs(ANSI_HOME_CLEAR, stdout); | |
634 | ||
635 | print_ntp_status_info(&info); | |
636 | ||
637 | return 0; | |
638 | } | |
639 | ||
640 | static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *error) { | |
641 | const char *name; | |
642 | int r; | |
643 | ||
644 | assert(m); | |
645 | ||
646 | r = sd_bus_message_read(m, "s", &name); | |
647 | if (r < 0) | |
d67b1d18 | 648 | return bus_log_parse_error(r); |
6129ec85 YW |
649 | |
650 | if (!streq_ptr(name, "org.freedesktop.timesync1.Manager")) | |
651 | return 0; | |
652 | ||
653 | return show_timesync_status_once(sd_bus_message_get_bus(m)); | |
654 | } | |
655 | ||
656 | static int show_timesync_status(int argc, char **argv, void *userdata) { | |
657 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; | |
99534007 | 658 | sd_bus *bus = ASSERT_PTR(userdata); |
6129ec85 YW |
659 | int r; |
660 | ||
6129ec85 YW |
661 | r = show_timesync_status_once(bus); |
662 | if (r < 0) | |
663 | return r; | |
664 | ||
665 | if (!arg_monitor) | |
666 | return 0; | |
667 | ||
668 | r = sd_event_default(&event); | |
669 | if (r < 0) | |
670 | return log_error_errno(r, "Failed to get event loop: %m"); | |
671 | ||
672 | r = sd_bus_match_signal(bus, | |
673 | NULL, | |
674 | "org.freedesktop.timesync1", | |
675 | "/org/freedesktop/timesync1", | |
676 | "org.freedesktop.DBus.Properties", | |
677 | "PropertiesChanged", | |
678 | on_properties_changed, NULL); | |
679 | if (r < 0) | |
680 | return log_error_errno(r, "Failed to request match for PropertiesChanged signal: %m"); | |
681 | ||
682 | r = sd_bus_attach_event(bus, event, SD_EVENT_PRIORITY_NORMAL); | |
683 | if (r < 0) | |
684 | return log_error_errno(r, "Failed to attach bus to event loop: %m"); | |
685 | ||
686 | r = sd_event_loop(event); | |
687 | if (r < 0) | |
688 | return log_error_errno(r, "Failed to run event loop: %m"); | |
689 | ||
690 | return 0; | |
691 | } | |
692 | ||
255b1fc8 | 693 | static int print_timesync_property(const char *name, const char *expected_value, sd_bus_message *m, BusPrintPropertyFlags flags) { |
6129ec85 YW |
694 | char type; |
695 | const char *contents; | |
696 | int r; | |
697 | ||
698 | assert(name); | |
699 | assert(m); | |
700 | ||
701 | r = sd_bus_message_peek_type(m, &type, &contents); | |
702 | if (r < 0) | |
703 | return r; | |
704 | ||
705 | switch (type) { | |
706 | ||
707 | case SD_BUS_TYPE_STRUCT: | |
708 | if (streq(name, "NTPMessage")) { | |
709 | _cleanup_(ntp_status_info_clear) NTPStatusInfo i = {}; | |
6129ec85 YW |
710 | |
711 | r = map_ntp_message(NULL, NULL, m, NULL, &i); | |
712 | if (r < 0) | |
713 | return r; | |
714 | ||
715 | if (i.packet_count == 0) | |
716 | return 1; | |
717 | ||
255b1fc8 | 718 | if (!FLAGS_SET(flags, BUS_PRINT_PROPERTY_ONLY_VALUE)) { |
6129ec85 YW |
719 | fputs(name, stdout); |
720 | fputc('=', stdout); | |
721 | } | |
722 | ||
723 | printf("{ Leap=%u, Version=%u, Mode=%u, Stratum=%u, Precision=%i,", | |
724 | i.leap, i.version, i.mode, i.stratum, i.precision); | |
5291f26d ZJS |
725 | printf(" RootDelay=%s,", FORMAT_TIMESPAN(i.root_delay, 0)); |
726 | printf(" RootDispersion=%s,", FORMAT_TIMESPAN(i.root_dispersion, 0)); | |
6129ec85 YW |
727 | |
728 | if (i.stratum == 1) | |
729 | printf(" Reference=%s,", i.reference.str); | |
730 | else | |
731 | printf(" Reference=%" PRIX32 ",", be32toh(i.reference.val)); | |
732 | ||
04f5c018 ZJS |
733 | printf(" OriginateTimestamp=%s,", FORMAT_TIMESTAMP(i.origin)); |
734 | printf(" ReceiveTimestamp=%s,", FORMAT_TIMESTAMP(i.recv)); | |
735 | printf(" TransmitTimestamp=%s,", FORMAT_TIMESTAMP(i.trans)); | |
736 | printf(" DestinationTimestamp=%s,", FORMAT_TIMESTAMP(i.dest)); | |
77ec2816 | 737 | printf(" Ignored=%s, PacketCount=%" PRIu64 ",", |
6129ec85 | 738 | yes_no(i.spike), i.packet_count); |
5291f26d | 739 | printf(" Jitter=%s }\n", FORMAT_TIMESPAN(i.jitter, 0)); |
6129ec85 YW |
740 | |
741 | return 1; | |
742 | ||
743 | } else if (streq(name, "ServerAddress")) { | |
744 | _cleanup_free_ char *str = NULL; | |
745 | ||
746 | r = map_server_address(NULL, NULL, m, NULL, &str); | |
747 | if (r < 0) | |
748 | return r; | |
749 | ||
255b1fc8 | 750 | bus_print_property_value(name, expected_value, flags, str); |
6129ec85 YW |
751 | |
752 | return 1; | |
753 | } | |
754 | break; | |
755 | } | |
756 | ||
757 | return 0; | |
758 | } | |
759 | ||
760 | static int show_timesync(int argc, char **argv, void *userdata) { | |
99534007 | 761 | sd_bus *bus = ASSERT_PTR(userdata); |
6129ec85 YW |
762 | int r; |
763 | ||
6129ec85 YW |
764 | r = bus_print_all_properties(bus, |
765 | "org.freedesktop.timesync1", | |
766 | "/org/freedesktop/timesync1", | |
767 | print_timesync_property, | |
768 | arg_property, | |
255b1fc8 | 769 | arg_print_flags, |
6129ec85 YW |
770 | NULL); |
771 | if (r < 0) | |
772 | return bus_log_parse_error(r); | |
773 | ||
774 | return 0; | |
775 | } | |
776 | ||
9030b50a | 777 | static int parse_ifindex_bus(sd_bus *bus, const char *str) { |
159a855b YW |
778 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
779 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
780 | int32_t i; | |
781 | int r; | |
782 | ||
783 | assert(bus); | |
784 | assert(str); | |
159a855b | 785 | |
597da51b | 786 | r = parse_ifindex(str); |
9030b50a ZJS |
787 | if (r > 0) |
788 | return r; | |
789 | assert(r < 0); | |
159a855b | 790 | |
43fe4f76 | 791 | r = bus_call_method(bus, bus_network_mgr, "GetLinkByName", &error, &reply, "s", str); |
159a855b YW |
792 | if (r < 0) |
793 | return log_error_errno(r, "Failed to get ifindex of interfaces %s: %s", str, bus_error_message(&error, r)); | |
794 | ||
795 | r = sd_bus_message_read(reply, "io", &i, NULL); | |
796 | if (r < 0) | |
797 | return bus_log_create_error(r); | |
798 | ||
9030b50a | 799 | return i; |
159a855b YW |
800 | } |
801 | ||
802 | static int verb_ntp_servers(int argc, char **argv, void *userdata) { | |
803 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
804 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL; | |
99534007 | 805 | sd_bus *bus = ASSERT_PTR(userdata); |
159a855b YW |
806 | int ifindex, r; |
807 | ||
9030b50a ZJS |
808 | ifindex = parse_ifindex_bus(bus, argv[1]); |
809 | if (ifindex < 0) | |
810 | return ifindex; | |
159a855b YW |
811 | |
812 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); | |
813 | ||
43fe4f76 | 814 | r = bus_message_new_method_call(bus, &req, bus_network_mgr, "SetLinkNTP"); |
159a855b YW |
815 | if (r < 0) |
816 | return bus_log_create_error(r); | |
817 | ||
818 | r = sd_bus_message_append(req, "i", ifindex); | |
819 | if (r < 0) | |
820 | return bus_log_create_error(r); | |
821 | ||
822 | r = sd_bus_message_append_strv(req, argv + 2); | |
823 | if (r < 0) | |
824 | return bus_log_create_error(r); | |
825 | ||
826 | r = sd_bus_call(bus, req, 0, &error, NULL); | |
827 | if (r < 0) | |
828 | return log_error_errno(r, "Failed to set NTP servers: %s", bus_error_message(&error, r)); | |
829 | ||
830 | return 0; | |
831 | } | |
832 | ||
833 | static int verb_revert(int argc, char **argv, void *userdata) { | |
834 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
99534007 | 835 | sd_bus *bus = ASSERT_PTR(userdata); |
159a855b YW |
836 | int ifindex, r; |
837 | ||
9030b50a ZJS |
838 | ifindex = parse_ifindex_bus(bus, argv[1]); |
839 | if (ifindex < 0) | |
840 | return ifindex; | |
159a855b YW |
841 | |
842 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); | |
843 | ||
43fe4f76 | 844 | r = bus_call_method(bus, bus_network_mgr, "RevertLinkNTP", &error, NULL, "i", ifindex); |
159a855b YW |
845 | if (r < 0) |
846 | return log_error_errno(r, "Failed to revert interface configuration: %s", bus_error_message(&error, r)); | |
847 | ||
848 | return 0; | |
849 | } | |
850 | ||
be90a886 | 851 | static int help(void) { |
37ec0fdd LP |
852 | _cleanup_free_ char *link = NULL; |
853 | int r; | |
854 | ||
855 | r = terminal_urlify_man("timedatectl", "1", &link); | |
856 | if (r < 0) | |
857 | return log_oom(); | |
858 | ||
353b2baa LP |
859 | printf("%s [OPTIONS...] COMMAND ...\n" |
860 | "\n%sQuery or change system time and date settings.%s\n" | |
e1fac8a6 | 861 | "\nCommands:\n" |
4f8f66cb | 862 | " status Show current time settings\n" |
ead0adb1 | 863 | " show Show properties of systemd-timedated\n" |
4f8f66cb | 864 | " set-time TIME Set system time\n" |
07a062a7 JSJ |
865 | " set-timezone ZONE Set system time zone\n" |
866 | " list-timezones Show known time zones\n" | |
4f8f66cb | 867 | " set-local-rtc BOOL Control whether RTC is in local time\n" |
6129ec85 | 868 | " set-ntp BOOL Enable or disable network time synchronization\n" |
353b2baa | 869 | "\nsystemd-timesyncd Commands:\n" |
6129ec85 YW |
870 | " timesync-status Show status of systemd-timesyncd\n" |
871 | " show-timesync Show properties of systemd-timesyncd\n" | |
353b2baa | 872 | "\nOptions:\n" |
e1fac8a6 ZJS |
873 | " -h --help Show this help message\n" |
874 | " --version Show package version\n" | |
875 | " --no-pager Do not pipe output into a pager\n" | |
876 | " --no-ask-password Do not prompt for password\n" | |
877 | " -H --host=[USER@]HOST Operate on remote host\n" | |
878 | " -M --machine=CONTAINER Operate on local container\n" | |
879 | " --adjust-system-clock Adjust system clock when changing local RTC mode\n" | |
880 | " --monitor Monitor status of systemd-timesyncd\n" | |
881 | " -p --property=NAME Show only properties by this name\n" | |
882 | " -a --all Show all properties, including empty ones\n" | |
883 | " --value When showing properties, only print the value\n" | |
bc556335 DDM |
884 | "\nSee the %s for details.\n", |
885 | program_invocation_short_name, | |
886 | ansi_highlight(), | |
887 | ansi_normal(), | |
888 | link); | |
be90a886 YW |
889 | |
890 | return 0; | |
891 | } | |
892 | ||
893 | static int verb_help(int argc, char **argv, void *userdata) { | |
894 | return help(); | |
6d0274f1 LP |
895 | } |
896 | ||
897 | static int parse_argv(int argc, char *argv[]) { | |
898 | ||
899 | enum { | |
900 | ARG_VERSION = 0x100, | |
901 | ARG_NO_PAGER, | |
c9783430 | 902 | ARG_ADJUST_SYSTEM_CLOCK, |
6129ec85 YW |
903 | ARG_NO_ASK_PASSWORD, |
904 | ARG_MONITOR, | |
905 | ARG_VALUE, | |
6d0274f1 LP |
906 | }; |
907 | ||
908 | static const struct option options[] = { | |
c9783430 LP |
909 | { "help", no_argument, NULL, 'h' }, |
910 | { "version", no_argument, NULL, ARG_VERSION }, | |
911 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
912 | { "host", required_argument, NULL, 'H' }, | |
a281d9c7 | 913 | { "machine", required_argument, NULL, 'M' }, |
c9783430 LP |
914 | { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD }, |
915 | { "adjust-system-clock", no_argument, NULL, ARG_ADJUST_SYSTEM_CLOCK }, | |
6129ec85 YW |
916 | { "monitor", no_argument, NULL, ARG_MONITOR }, |
917 | { "property", required_argument, NULL, 'p' }, | |
918 | { "all", no_argument, NULL, 'a' }, | |
919 | { "value", no_argument, NULL, ARG_VALUE }, | |
eb9da376 | 920 | {} |
6d0274f1 LP |
921 | }; |
922 | ||
6129ec85 | 923 | int c, r; |
6d0274f1 LP |
924 | |
925 | assert(argc >= 0); | |
926 | assert(argv); | |
927 | ||
6129ec85 | 928 | while ((c = getopt_long(argc, argv, "hH:M:p:a", options, NULL)) >= 0) |
6d0274f1 LP |
929 | |
930 | switch (c) { | |
931 | ||
932 | case 'h': | |
be90a886 | 933 | return help(); |
6d0274f1 LP |
934 | |
935 | case ARG_VERSION: | |
3f6fd1ba | 936 | return version(); |
6d0274f1 | 937 | |
a281d9c7 TA |
938 | case 'H': |
939 | arg_transport = BUS_TRANSPORT_REMOTE; | |
940 | arg_host = optarg; | |
6d0274f1 LP |
941 | break; |
942 | ||
a281d9c7 | 943 | case 'M': |
de33fc62 | 944 | arg_transport = BUS_TRANSPORT_MACHINE; |
a281d9c7 | 945 | arg_host = optarg; |
6d0274f1 LP |
946 | break; |
947 | ||
546158bc JJ |
948 | case ARG_NO_ASK_PASSWORD: |
949 | arg_ask_password = false; | |
950 | break; | |
951 | ||
c9783430 LP |
952 | case ARG_ADJUST_SYSTEM_CLOCK: |
953 | arg_adjust_system_clock = true; | |
6d0274f1 LP |
954 | break; |
955 | ||
956 | case ARG_NO_PAGER: | |
0221d68a | 957 | arg_pager_flags |= PAGER_DISABLE; |
6d0274f1 LP |
958 | break; |
959 | ||
6129ec85 YW |
960 | case ARG_MONITOR: |
961 | arg_monitor = true; | |
962 | break; | |
963 | ||
964 | case 'p': { | |
965 | r = strv_extend(&arg_property, optarg); | |
966 | if (r < 0) | |
967 | return log_oom(); | |
968 | ||
969 | /* If the user asked for a particular | |
e8607daf | 970 | * property, show it to them, even if it is |
6129ec85 | 971 | * empty. */ |
255b1fc8 | 972 | SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY, true); |
6129ec85 YW |
973 | break; |
974 | } | |
975 | ||
976 | case 'a': | |
255b1fc8 | 977 | SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY, true); |
6129ec85 YW |
978 | break; |
979 | ||
980 | case ARG_VALUE: | |
255b1fc8 | 981 | SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_ONLY_VALUE, true); |
6129ec85 YW |
982 | break; |
983 | ||
6d0274f1 LP |
984 | case '?': |
985 | return -EINVAL; | |
986 | ||
987 | default: | |
04499a70 | 988 | assert_not_reached(); |
6d0274f1 | 989 | } |
6d0274f1 LP |
990 | |
991 | return 1; | |
992 | } | |
993 | ||
a281d9c7 | 994 | static int timedatectl_main(sd_bus *bus, int argc, char *argv[]) { |
be90a886 | 995 | static const Verb verbs[] = { |
6129ec85 | 996 | { "status", VERB_ANY, 1, VERB_DEFAULT, show_status }, |
ead0adb1 | 997 | { "show", VERB_ANY, 1, 0, show_properties }, |
6129ec85 YW |
998 | { "set-time", 2, 2, 0, set_time }, |
999 | { "set-timezone", 2, 2, 0, set_timezone }, | |
1000 | { "list-timezones", VERB_ANY, 1, 0, list_timezones }, | |
1001 | { "set-local-rtc", 2, 2, 0, set_local_rtc }, | |
1002 | { "set-ntp", 2, 2, 0, set_ntp }, | |
1003 | { "timesync-status", VERB_ANY, 1, 0, show_timesync_status }, | |
1004 | { "show-timesync", VERB_ANY, 1, 0, show_timesync }, | |
159a855b YW |
1005 | { "ntp-servers", 3, VERB_ANY, 0, verb_ntp_servers }, |
1006 | { "revert", 2, 2, 0, verb_revert }, | |
6129ec85 | 1007 | { "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */ |
be90a886 | 1008 | {} |
6d0274f1 LP |
1009 | }; |
1010 | ||
be90a886 | 1011 | return dispatch_verb(argc, argv, verbs, bus); |
6d0274f1 LP |
1012 | } |
1013 | ||
50378e5d ZJS |
1014 | static int run(int argc, char *argv[]) { |
1015 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
84f6181c | 1016 | int r; |
6d0274f1 | 1017 | |
a9cdc94f | 1018 | setlocale(LC_ALL, ""); |
d2acb93d | 1019 | log_setup(); |
6d0274f1 LP |
1020 | |
1021 | r = parse_argv(argc, argv); | |
84f6181c | 1022 | if (r <= 0) |
50378e5d | 1023 | return r; |
6d0274f1 | 1024 | |
266f3e26 | 1025 | r = bus_connect_transport(arg_transport, arg_host, false, &bus); |
50378e5d | 1026 | if (r < 0) |
10a7340a | 1027 | return bus_log_connect_error(r, arg_transport); |
6d0274f1 | 1028 | |
50378e5d | 1029 | return timedatectl_main(bus, argc, argv); |
6d0274f1 | 1030 | } |
50378e5d ZJS |
1031 | |
1032 | DEFINE_MAIN_FUNCTION(run); |