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