]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/timedate/timedatectl.c
ffba6b543750c52c6bee0ceb1767c489a8993fff
[thirdparty/systemd.git] / src / timedate / timedatectl.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2013 Kay Sievers
4 ***/
5
6 #include <getopt.h>
7 #include <locale.h>
8 #include <math.h>
9 #include <stdbool.h>
10 #include <stdlib.h>
11
12 #include "sd-bus.h"
13
14 #include "bus-error.h"
15 #include "bus-util.h"
16 #include "in-addr-util.h"
17 #include "pager.h"
18 #include "parse-util.h"
19 #include "spawn-polkit-agent.h"
20 #include "sparse-endian.h"
21 #include "string-table.h"
22 #include "strv.h"
23 #include "terminal-util.h"
24 #include "util.h"
25 #include "verbs.h"
26
27 static bool arg_no_pager = false;
28 static bool arg_ask_password = true;
29 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
30 static char *arg_host = NULL;
31 static bool arg_adjust_system_clock = false;
32 static bool arg_monitor = false;
33 static char **arg_property = NULL;
34 static bool arg_value = false;
35 static bool arg_all = false;
36
37 typedef struct StatusInfo {
38 usec_t time;
39 const char *timezone;
40
41 usec_t rtc_time;
42 bool rtc_local;
43
44 bool ntp_capable;
45 bool ntp_active;
46 bool ntp_synced;
47 } StatusInfo;
48
49 static void print_status_info(const StatusInfo *i) {
50 const char *old_tz = NULL, *tz;
51 bool have_time = false;
52 char a[LINE_MAX];
53 struct tm tm;
54 time_t sec;
55 size_t n;
56 int r;
57
58 assert(i);
59
60 /* Save the old $TZ */
61 tz = getenv("TZ");
62 if (tz)
63 old_tz = strdupa(tz);
64
65 /* Set the new $TZ */
66 if (setenv("TZ", isempty(i->timezone) ? "UTC" : i->timezone, true) < 0)
67 log_warning_errno(errno, "Failed to set TZ environment variable, ignoring: %m");
68 else
69 tzset();
70
71 if (i->time != 0) {
72 sec = (time_t) (i->time / USEC_PER_SEC);
73 have_time = true;
74 } else if (IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE)) {
75 sec = time(NULL);
76 have_time = true;
77 } else
78 log_warning("Could not get time from timedated and not operating locally, ignoring.");
79
80 if (have_time) {
81 n = strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm));
82 printf(" Local time: %s\n", n > 0 ? a : "n/a");
83
84 n = strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm));
85 printf(" Universal time: %s\n", n > 0 ? a : "n/a");
86 } else {
87 printf(" Local time: %s\n", "n/a");
88 printf(" Universal time: %s\n", "n/a");
89 }
90
91 if (i->rtc_time > 0) {
92 time_t rtc_sec;
93
94 rtc_sec = (time_t) (i->rtc_time / USEC_PER_SEC);
95 n = strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec, &tm));
96 printf(" RTC time: %s\n", n > 0 ? a : "n/a");
97 } else
98 printf(" RTC time: %s\n", "n/a");
99
100 if (have_time)
101 n = strftime(a, sizeof a, "%Z, %z", localtime_r(&sec, &tm));
102
103 /* Restore the $TZ */
104 if (old_tz)
105 r = setenv("TZ", old_tz, true);
106 else
107 r = unsetenv("TZ");
108 if (r < 0)
109 log_warning_errno(errno, "Failed to set TZ environment variable, ignoring: %m");
110 else
111 tzset();
112
113 printf(" Time zone: %s (%s)\n"
114 "System clock synchronized: %s\n"
115 " NTP service: %s\n"
116 " RTC in local TZ: %s\n",
117 strna(i->timezone), have_time && n > 0 ? a : "n/a",
118 yes_no(i->ntp_synced),
119 i->ntp_capable ? (i->ntp_active ? "active" : "inactive") : "n/a",
120 yes_no(i->rtc_local));
121
122 if (i->rtc_local)
123 printf("\n%s"
124 "Warning: The system is configured to read the RTC time in the local time zone.\n"
125 " This mode cannot be fully supported. It will create various problems\n"
126 " with time zone changes and daylight saving time adjustments. The RTC\n"
127 " time is never updated, it relies on external facilities to maintain it.\n"
128 " If at all possible, use RTC in UTC by calling\n"
129 " 'timedatectl set-local-rtc 0'.%s\n", ansi_highlight(), ansi_normal());
130 }
131
132 static int show_status(int argc, char **argv, void *userdata) {
133 StatusInfo info = {};
134 static const struct bus_properties_map map[] = {
135 { "Timezone", "s", NULL, offsetof(StatusInfo, timezone) },
136 { "LocalRTC", "b", NULL, offsetof(StatusInfo, rtc_local) },
137 { "NTP", "b", NULL, offsetof(StatusInfo, ntp_active) },
138 { "CanNTP", "b", NULL, offsetof(StatusInfo, ntp_capable) },
139 { "NTPSynchronized", "b", NULL, offsetof(StatusInfo, ntp_synced) },
140 { "TimeUSec", "t", NULL, offsetof(StatusInfo, time) },
141 { "RTCTimeUSec", "t", NULL, offsetof(StatusInfo, rtc_time) },
142 {}
143 };
144
145 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
146 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
147 sd_bus *bus = userdata;
148 int r;
149
150 assert(bus);
151
152 r = bus_map_all_properties(bus,
153 "org.freedesktop.timedate1",
154 "/org/freedesktop/timedate1",
155 map,
156 BUS_MAP_BOOLEAN_AS_BOOL,
157 &error,
158 &m,
159 &info);
160 if (r < 0)
161 return log_error_errno(r, "Failed to query server: %s", bus_error_message(&error, r));
162
163 print_status_info(&info);
164
165 return r;
166 }
167
168 static int show_properties(int argc, char **argv, void *userdata) {
169 sd_bus *bus = userdata;
170 int r;
171
172 assert(bus);
173
174 r = bus_print_all_properties(bus,
175 "org.freedesktop.timedate1",
176 "/org/freedesktop/timedate1",
177 NULL,
178 arg_property,
179 arg_value,
180 arg_all,
181 NULL);
182 if (r < 0)
183 return bus_log_parse_error(r);
184
185 return 0;
186 }
187
188 static int set_time(int argc, char **argv, void *userdata) {
189 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
190 bool relative = false, interactive = arg_ask_password;
191 sd_bus *bus = userdata;
192 usec_t t;
193 int r;
194
195 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
196
197 r = parse_timestamp(argv[1], &t);
198 if (r < 0)
199 return log_error_errno(r, "Failed to parse time specification '%s': %m", argv[1]);
200
201 r = sd_bus_call_method(bus,
202 "org.freedesktop.timedate1",
203 "/org/freedesktop/timedate1",
204 "org.freedesktop.timedate1",
205 "SetTime",
206 &error,
207 NULL,
208 "xbb", (int64_t) t, relative, interactive);
209 if (r < 0)
210 log_error("Failed to set time: %s", bus_error_message(&error, r));
211
212 return r;
213 }
214
215 static int set_timezone(int argc, char **argv, void *userdata) {
216 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
217 sd_bus *bus = userdata;
218 int r;
219
220 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
221
222 r = sd_bus_call_method(bus,
223 "org.freedesktop.timedate1",
224 "/org/freedesktop/timedate1",
225 "org.freedesktop.timedate1",
226 "SetTimezone",
227 &error,
228 NULL,
229 "sb", argv[1], arg_ask_password);
230 if (r < 0)
231 log_error("Failed to set time zone: %s", bus_error_message(&error, r));
232
233 return r;
234 }
235
236 static int set_local_rtc(int argc, char **argv, void *userdata) {
237 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
238 sd_bus *bus = userdata;
239 int r, b;
240
241 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
242
243 b = parse_boolean(argv[1]);
244 if (b < 0)
245 return log_error_errno(b, "Failed to parse local RTC setting '%s': %m", argv[1]);
246
247 r = sd_bus_call_method(bus,
248 "org.freedesktop.timedate1",
249 "/org/freedesktop/timedate1",
250 "org.freedesktop.timedate1",
251 "SetLocalRTC",
252 &error,
253 NULL,
254 "bbb", b, arg_adjust_system_clock, arg_ask_password);
255 if (r < 0)
256 log_error("Failed to set local RTC: %s", bus_error_message(&error, r));
257
258 return r;
259 }
260
261 static int set_ntp(int argc, char **argv, void *userdata) {
262 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
263 sd_bus *bus = userdata;
264 int b, r;
265
266 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
267
268 b = parse_boolean(argv[1]);
269 if (b < 0)
270 return log_error_errno(b, "Failed to parse NTP setting '%s': %m", argv[1]);
271
272 r = sd_bus_call_method(bus,
273 "org.freedesktop.timedate1",
274 "/org/freedesktop/timedate1",
275 "org.freedesktop.timedate1",
276 "SetNTP",
277 &error,
278 NULL,
279 "bb", b, arg_ask_password);
280 if (r < 0)
281 log_error("Failed to set ntp: %s", bus_error_message(&error, r));
282
283 return r;
284 }
285
286 static int list_timezones(int argc, char **argv, void *userdata) {
287 _cleanup_strv_free_ char **zones = NULL;
288 int r;
289
290 r = get_timezones(&zones);
291 if (r < 0)
292 return log_error_errno(r, "Failed to read list of time zones: %m");
293
294 (void) pager_open(arg_no_pager, false);
295 strv_print(zones);
296
297 return 0;
298 }
299
300 typedef struct NTPStatusInfo {
301 const char *server_name;
302 char *server_address;
303 usec_t poll_interval, poll_max, poll_min;
304 usec_t root_distance_max;
305
306 uint32_t leap, version, mode, stratum;
307 int32_t precision;
308 usec_t root_delay, root_dispersion;
309 union {
310 char str[5];
311 uint32_t val;
312 } reference;
313 usec_t origin, recv, trans, dest;
314
315 bool spike;
316 uint64_t packet_count;
317 usec_t jitter;
318
319 int64_t freq;
320 } NTPStatusInfo;
321
322 static void ntp_status_info_clear(NTPStatusInfo *p) {
323 p->server_address = mfree(p->server_address);
324 }
325
326 static const char * const ntp_leap_table[4] = {
327 [0] = "normal",
328 [1] = "last minute of the day has 61 seconds",
329 [2] = "last minute of the day has 59 seconds",
330 [3] = "not synchronized",
331 };
332
333 #pragma GCC diagnostic push
334 #pragma GCC diagnostic ignored "-Wtype-limits"
335 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(ntp_leap, uint32_t);
336 #pragma GCC diagnostic pop
337
338 static void print_ntp_status_info(NTPStatusInfo *i) {
339 char ts[FORMAT_TIMESPAN_MAX], tmin[FORMAT_TIMESPAN_MAX], tmax[FORMAT_TIMESPAN_MAX];
340 usec_t delay, t14, t23, offset, root_distance;
341 bool offset_sign;
342
343 assert(i);
344
345 /*
346 * "Timestamp Name ID When Generated
347 * ------------------------------------------------------------
348 * Originate Timestamp T1 time request sent by client
349 * Receive Timestamp T2 time request received by server
350 * Transmit Timestamp T3 time reply sent by server
351 * Destination Timestamp T4 time reply received by client
352 *
353 * The round-trip delay, d, and system clock offset, t, are defined as:
354 * d = (T4 - T1) - (T3 - T2) t = ((T2 - T1) + (T3 - T4)) / 2"
355 */
356
357 printf(" Server: %s (%s)\n",
358 i->server_address, i->server_name);
359 printf("Poll interval: %s (min: %s; max %s)\n",
360 format_timespan(ts, sizeof(ts), i->poll_interval, 0),
361 format_timespan(tmin, sizeof(tmin), i->poll_min, 0),
362 format_timespan(tmax, sizeof(tmax), i->poll_max, 0));
363
364 if (i->packet_count == 0) {
365 printf(" Packet count: 0\n");
366 return;
367 }
368
369 if (i->dest < i->origin || i->trans < i->recv || i->dest - i->origin < i->trans - i->recv) {
370 log_error("Invalid NTP response");
371 return;
372 }
373
374 delay = (i->dest - i->origin) - (i->trans - i->recv);
375
376 t14 = i->origin + i->dest;
377 t23 = i->recv + i->trans;
378 offset_sign = t14 < t23;
379 offset = (offset_sign ? t23 - t14 : t14 - t23) / 2;
380
381 root_distance = i->root_delay / 2 + i->root_dispersion;
382
383 printf(" Leap: %s\n"
384 " Version: %" PRIu32 "\n"
385 " Stratum: %" PRIu32 "\n",
386 ntp_leap_to_string(i->leap),
387 i->version,
388 i->stratum);
389 if (i->stratum <= 1)
390 printf(" Reference: %s\n", i->reference.str);
391 else
392 printf(" Reference: %" PRIX32 "\n", be32toh(i->reference.val));
393 printf(" Precision: %s (%" PRIi32 ")\n",
394 format_timespan(ts, sizeof(ts), DIV_ROUND_UP((nsec_t) (exp2(i->precision) * NSEC_PER_SEC), NSEC_PER_USEC), 0),
395 i->precision);
396 printf("Root distance: %s (max: %s)\n",
397 format_timespan(ts, sizeof(ts), root_distance, 0),
398 format_timespan(tmax, sizeof(tmax), i->root_distance_max, 0));
399 printf(" Offset: %s%s\n",
400 offset_sign ? "+" : "-",
401 format_timespan(ts, sizeof(ts), offset, 0));
402 printf(" Delay: %s\n",
403 format_timespan(ts, sizeof(ts), delay, 0));
404 printf(" Jitter: %s\n",
405 format_timespan(ts, sizeof(ts), i->jitter, 0));
406 printf(" Packet count: %" PRIu64 "\n", i->packet_count);
407
408 if (!i->spike)
409 printf(" Frequency: %+.3fppm\n",
410 (double) i->freq / 0x10000);
411 }
412
413 static int map_server_address(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
414 char **p = (char **) userdata;
415 const void *d;
416 int family, r;
417 size_t sz;
418
419 assert(p);
420
421 r = sd_bus_message_enter_container(m, 'r', "iay");
422 if (r < 0)
423 return r;
424
425 r = sd_bus_message_read(m, "i", &family);
426 if (r < 0)
427 return r;
428
429 r = sd_bus_message_read_array(m, 'y', &d, &sz);
430 if (r < 0)
431 return r;
432
433 r = sd_bus_message_exit_container(m);
434 if (r < 0)
435 return r;
436
437 if (sz == 0 && family == AF_UNSPEC) {
438 *p = mfree(*p);
439 return 0;
440 }
441
442 if (!IN_SET(family, AF_INET, AF_INET6)) {
443 log_error("Unknown address family %i", family);
444 return -EINVAL;
445 }
446
447 if (sz != FAMILY_ADDRESS_SIZE(family)) {
448 log_error("Invalid address size");
449 return -EINVAL;
450 }
451
452 r = in_addr_to_string(family, d, p);
453 if (r < 0)
454 return r;
455
456 return 0;
457 }
458
459 static int map_ntp_message(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
460 NTPStatusInfo *p = userdata;
461 const void *d;
462 size_t sz;
463 int32_t b;
464 int r;
465
466 assert(p);
467
468 r = sd_bus_message_enter_container(m, 'r', "uuuuittayttttbtt");
469 if (r < 0)
470 return r;
471
472 r = sd_bus_message_read(m, "uuuuitt",
473 &p->leap, &p->version, &p->mode, &p->stratum, &p->precision,
474 &p->root_delay, &p->root_dispersion);
475 if (r < 0)
476 return r;
477
478 r = sd_bus_message_read_array(m, 'y', &d, &sz);
479 if (r < 0)
480 return r;
481
482 r = sd_bus_message_read(m, "ttttbtt",
483 &p->origin, &p->recv, &p->trans, &p->dest,
484 &b, &p->packet_count, &p->jitter);
485 if (r < 0)
486 return r;
487
488 r = sd_bus_message_exit_container(m);
489 if (r < 0)
490 return r;
491
492 if (sz != 4)
493 return -EINVAL;
494
495 memcpy(p->reference.str, d, sz);
496
497 p->spike = b;
498
499 return 0;
500 }
501
502 static int show_timesync_status_once(sd_bus *bus) {
503 static const struct bus_properties_map map_timesync[] = {
504 { "ServerName", "s", NULL, offsetof(NTPStatusInfo, server_name) },
505 { "ServerAddress", "(iay)", map_server_address, offsetof(NTPStatusInfo, server_address) },
506 { "PollIntervalUSec", "t", NULL, offsetof(NTPStatusInfo, poll_interval) },
507 { "PollIntervalMinUSec", "t", NULL, offsetof(NTPStatusInfo, poll_min) },
508 { "PollIntervalMaxUSec", "t", NULL, offsetof(NTPStatusInfo, poll_max) },
509 { "RootDistanceMaxUSec", "t", NULL, offsetof(NTPStatusInfo, root_distance_max) },
510 { "NTPMessage", "(uuuuittayttttbtt)", map_ntp_message, 0 },
511 { "Frequency", "x", NULL, offsetof(NTPStatusInfo, freq) },
512 {}
513 };
514 _cleanup_(ntp_status_info_clear) NTPStatusInfo info = {};
515 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
516 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
517 int r;
518
519 assert(bus);
520
521 r = bus_map_all_properties(bus,
522 "org.freedesktop.timesync1",
523 "/org/freedesktop/timesync1",
524 map_timesync,
525 BUS_MAP_BOOLEAN_AS_BOOL,
526 &error,
527 &m,
528 &info);
529 if (r < 0)
530 return log_error_errno(r, "Failed to query server: %s", bus_error_message(&error, r));
531
532 if (arg_monitor && !terminal_is_dumb())
533 fputs(ANSI_HOME_CLEAR, stdout);
534
535 print_ntp_status_info(&info);
536
537 return 0;
538 }
539
540 static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *error) {
541 const char *name;
542 int r;
543
544 assert(m);
545
546 r = sd_bus_message_read(m, "s", &name);
547 if (r < 0)
548 return log_error_errno(r, "Failed to read interface name: %m");
549
550 if (!streq_ptr(name, "org.freedesktop.timesync1.Manager"))
551 return 0;
552
553 return show_timesync_status_once(sd_bus_message_get_bus(m));
554 }
555
556 static int show_timesync_status(int argc, char **argv, void *userdata) {
557 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
558 sd_bus *bus = userdata;
559 int r;
560
561 assert(bus);
562
563 r = show_timesync_status_once(bus);
564 if (r < 0)
565 return r;
566
567 if (!arg_monitor)
568 return 0;
569
570 r = sd_event_default(&event);
571 if (r < 0)
572 return log_error_errno(r, "Failed to get event loop: %m");
573
574 r = sd_bus_match_signal(bus,
575 NULL,
576 "org.freedesktop.timesync1",
577 "/org/freedesktop/timesync1",
578 "org.freedesktop.DBus.Properties",
579 "PropertiesChanged",
580 on_properties_changed, NULL);
581 if (r < 0)
582 return log_error_errno(r, "Failed to request match for PropertiesChanged signal: %m");
583
584 r = sd_bus_attach_event(bus, event, SD_EVENT_PRIORITY_NORMAL);
585 if (r < 0)
586 return log_error_errno(r, "Failed to attach bus to event loop: %m");
587
588 r = sd_event_loop(event);
589 if (r < 0)
590 return log_error_errno(r, "Failed to run event loop: %m");
591
592 return 0;
593 }
594
595 #define property(name, fmt, ...) \
596 do { \
597 if (value) \
598 printf(fmt "\n", __VA_ARGS__); \
599 else \
600 printf("%s=" fmt "\n", name, __VA_ARGS__); \
601 } while (0)
602
603 static int print_timesync_property(const char *name, sd_bus_message *m, bool value, bool all) {
604 char type;
605 const char *contents;
606 int r;
607
608 assert(name);
609 assert(m);
610
611 r = sd_bus_message_peek_type(m, &type, &contents);
612 if (r < 0)
613 return r;
614
615 switch (type) {
616
617 case SD_BUS_TYPE_STRUCT:
618 if (streq(name, "NTPMessage")) {
619 _cleanup_(ntp_status_info_clear) NTPStatusInfo i = {};
620 char ts[FORMAT_TIMESPAN_MAX], stamp[FORMAT_TIMESTAMP_MAX];
621
622 r = map_ntp_message(NULL, NULL, m, NULL, &i);
623 if (r < 0)
624 return r;
625
626 if (i.packet_count == 0)
627 return 1;
628
629 if (!value) {
630 fputs(name, stdout);
631 fputc('=', stdout);
632 }
633
634 printf("{ Leap=%u, Version=%u, Mode=%u, Stratum=%u, Precision=%i,",
635 i.leap, i.version, i.mode, i.stratum, i.precision);
636 printf(" RootDelay=%s,",
637 format_timespan(ts, sizeof(ts), i.root_delay, 0));
638 printf(" RootDispersion=%s,",
639 format_timespan(ts, sizeof(ts), i.root_dispersion, 0));
640
641 if (i.stratum == 1)
642 printf(" Reference=%s,", i.reference.str);
643 else
644 printf(" Reference=%" PRIX32 ",", be32toh(i.reference.val));
645
646 printf(" OriginateTimestamp=%s,",
647 format_timestamp(stamp, sizeof(stamp), i.origin));
648 printf(" ReceiveTimestamp=%s,",
649 format_timestamp(stamp, sizeof(stamp), i.recv));
650 printf(" TransmitTimestamp=%s,",
651 format_timestamp(stamp, sizeof(stamp), i.trans));
652 printf(" DestinationTimestamp=%s,",
653 format_timestamp(stamp, sizeof(stamp), i.dest));
654 printf(" Ignored=%s PacketCount=%" PRIu64 ",",
655 yes_no(i.spike), i.packet_count);
656 printf(" Jitter=%s }\n",
657 format_timespan(ts, sizeof(ts), i.jitter, 0));
658
659 return 1;
660
661 } else if (streq(name, "ServerAddress")) {
662 _cleanup_free_ char *str = NULL;
663
664 r = map_server_address(NULL, NULL, m, NULL, &str);
665 if (r < 0)
666 return r;
667
668 if (arg_all || !isempty(str))
669 property(name, "%s", str);
670
671 return 1;
672 }
673 break;
674 }
675
676 return 0;
677 }
678
679 static int show_timesync(int argc, char **argv, void *userdata) {
680 sd_bus *bus = userdata;
681 int r;
682
683 assert(bus);
684
685 r = bus_print_all_properties(bus,
686 "org.freedesktop.timesync1",
687 "/org/freedesktop/timesync1",
688 print_timesync_property,
689 arg_property,
690 arg_value,
691 arg_all,
692 NULL);
693 if (r < 0)
694 return bus_log_parse_error(r);
695
696 return 0;
697 }
698
699 static int help(void) {
700 printf("%s [OPTIONS...] COMMAND ...\n\n"
701 "Query or change system time and date settings.\n\n"
702 " -h --help Show this help message\n"
703 " --version Show package version\n"
704 " --no-pager Do not pipe output into a pager\n"
705 " --no-ask-password Do not prompt for password\n"
706 " -H --host=[USER@]HOST Operate on remote host\n"
707 " -M --machine=CONTAINER Operate on local container\n"
708 " --adjust-system-clock Adjust system clock when changing local RTC mode\n"
709 " --monitor Monitor status of systemd-timesyncd\n"
710 " -p --property=NAME Show only properties by this name\n"
711 " -a --all Show all properties, including empty ones\n"
712 " --value When showing properties, only print the value\n"
713 "\n"
714 "Commands:\n"
715 " status Show current time settings\n"
716 " show Show properties of systemd-timedated\n"
717 " set-time TIME Set system time\n"
718 " set-timezone ZONE Set system time zone\n"
719 " list-timezones Show known time zones\n"
720 " set-local-rtc BOOL Control whether RTC is in local time\n"
721 " set-ntp BOOL Enable or disable network time synchronization\n"
722 "\n"
723 "systemd-timesyncd Commands:\n"
724 " timesync-status Show status of systemd-timesyncd\n"
725 " show-timesync Show properties of systemd-timesyncd\n"
726 , program_invocation_short_name);
727
728 return 0;
729 }
730
731 static int verb_help(int argc, char **argv, void *userdata) {
732 return help();
733 }
734
735 static int parse_argv(int argc, char *argv[]) {
736
737 enum {
738 ARG_VERSION = 0x100,
739 ARG_NO_PAGER,
740 ARG_ADJUST_SYSTEM_CLOCK,
741 ARG_NO_ASK_PASSWORD,
742 ARG_MONITOR,
743 ARG_VALUE,
744 };
745
746 static const struct option options[] = {
747 { "help", no_argument, NULL, 'h' },
748 { "version", no_argument, NULL, ARG_VERSION },
749 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
750 { "host", required_argument, NULL, 'H' },
751 { "machine", required_argument, NULL, 'M' },
752 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
753 { "adjust-system-clock", no_argument, NULL, ARG_ADJUST_SYSTEM_CLOCK },
754 { "monitor", no_argument, NULL, ARG_MONITOR },
755 { "property", required_argument, NULL, 'p' },
756 { "all", no_argument, NULL, 'a' },
757 { "value", no_argument, NULL, ARG_VALUE },
758 {}
759 };
760
761 int c, r;
762
763 assert(argc >= 0);
764 assert(argv);
765
766 while ((c = getopt_long(argc, argv, "hH:M:p:a", options, NULL)) >= 0)
767
768 switch (c) {
769
770 case 'h':
771 return help();
772
773 case ARG_VERSION:
774 return version();
775
776 case 'H':
777 arg_transport = BUS_TRANSPORT_REMOTE;
778 arg_host = optarg;
779 break;
780
781 case 'M':
782 arg_transport = BUS_TRANSPORT_MACHINE;
783 arg_host = optarg;
784 break;
785
786 case ARG_NO_ASK_PASSWORD:
787 arg_ask_password = false;
788 break;
789
790 case ARG_ADJUST_SYSTEM_CLOCK:
791 arg_adjust_system_clock = true;
792 break;
793
794 case ARG_NO_PAGER:
795 arg_no_pager = true;
796 break;
797
798 case ARG_MONITOR:
799 arg_monitor = true;
800 break;
801
802 case 'p': {
803 r = strv_extend(&arg_property, optarg);
804 if (r < 0)
805 return log_oom();
806
807 /* If the user asked for a particular
808 * property, show it to him, even if it is
809 * empty. */
810 arg_all = true;
811 break;
812 }
813
814 case 'a':
815 arg_all = true;
816 break;
817
818 case ARG_VALUE:
819 arg_value = true;
820 break;
821
822 case '?':
823 return -EINVAL;
824
825 default:
826 assert_not_reached("Unhandled option");
827 }
828
829 return 1;
830 }
831
832 static int timedatectl_main(sd_bus *bus, int argc, char *argv[]) {
833
834 static const Verb verbs[] = {
835 { "status", VERB_ANY, 1, VERB_DEFAULT, show_status },
836 { "show", VERB_ANY, 1, 0, show_properties },
837 { "set-time", 2, 2, 0, set_time },
838 { "set-timezone", 2, 2, 0, set_timezone },
839 { "list-timezones", VERB_ANY, 1, 0, list_timezones },
840 { "set-local-rtc", 2, 2, 0, set_local_rtc },
841 { "set-ntp", 2, 2, 0, set_ntp },
842 { "timesync-status", VERB_ANY, 1, 0, show_timesync_status },
843 { "show-timesync", VERB_ANY, 1, 0, show_timesync },
844 { "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */
845 {}
846 };
847
848 return dispatch_verb(argc, argv, verbs, bus);
849 }
850
851 int main(int argc, char *argv[]) {
852 sd_bus *bus = NULL;
853 int r;
854
855 setlocale(LC_ALL, "");
856 log_parse_environment();
857 log_open();
858
859 r = parse_argv(argc, argv);
860 if (r <= 0)
861 goto finish;
862
863 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
864 if (r < 0) {
865 log_error_errno(r, "Failed to create bus connection: %m");
866 goto finish;
867 }
868
869 r = timedatectl_main(bus, argc, argv);
870
871 finish:
872 /* make sure we terminate the bus connection first, and then close the
873 * pager, see issue #3543 for the details. */
874 sd_bus_flush_close_unref(bus);
875 pager_close();
876
877 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
878 }