1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
11 #include "bus-error.h"
12 #include "bus-locator.h"
13 #include "bus-map-properties.h"
14 #include "bus-print-properties.h"
15 #include "format-table.h"
16 #include "in-addr-util.h"
17 #include "main-func.h"
19 #include "parse-util.h"
20 #include "pretty-print.h"
21 #include "sparse-endian.h"
22 #include "spawn-polkit-agent.h"
23 #include "string-table.h"
25 #include "terminal-util.h"
29 static PagerFlags arg_pager_flags
= 0;
30 static bool arg_ask_password
= true;
31 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
32 static char *arg_host
= NULL
;
33 static bool arg_adjust_system_clock
= false;
34 static bool arg_monitor
= false;
35 static char **arg_property
= NULL
;
36 static bool arg_value
= false;
37 static bool arg_all
= false;
39 typedef struct StatusInfo
{
51 static int print_status_info(const StatusInfo
*i
) {
52 _cleanup_(table_unrefp
) Table
*table
= NULL
;
53 const char *old_tz
= NULL
, *tz
, *tz_colon
;
54 bool have_time
= false;
64 table
= table_new("key", "value");
68 table_set_header(table
, false);
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);
74 assert_se(cell
= table_get_cell(table
, 0, 1));
75 (void) table_set_ellipsize_percent(table
, cell
, 100);
77 /* Save the old $TZ */
83 tz_colon
= strjoina(":", isempty(i
->timezone
) ? "UTC" : i
->timezone
);
84 if (setenv("TZ", tz_colon
, true) < 0)
85 log_warning_errno(errno
, "Failed to set TZ environment variable, ignoring: %m");
90 sec
= (time_t) (i
->time
/ USEC_PER_SEC
);
92 } else if (IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
)) {
96 log_warning("Could not get time from timedated and not operating locally, ignoring.");
99 n
= strftime(a
, sizeof a
, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec
, &tm
));
101 r
= table_add_many(table
,
102 TABLE_STRING
, "Local time:",
103 TABLE_STRING
, have_time
&& n
> 0 ? a
: "n/a");
105 return table_log_add_error(r
);
108 n
= strftime(a
, sizeof a
, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec
, &tm
));
110 r
= table_add_many(table
,
111 TABLE_STRING
, "Universal time:",
112 TABLE_STRING
, have_time
&& n
> 0 ? a
: "n/a");
114 return table_log_add_error(r
);
116 if (i
->rtc_time
> 0) {
119 rtc_sec
= (time_t) (i
->rtc_time
/ USEC_PER_SEC
);
120 n
= strftime(a
, sizeof a
, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec
, &tm
));
123 r
= table_add_many(table
,
124 TABLE_STRING
, "RTC time:",
125 TABLE_STRING
, i
->rtc_time
> 0 && n
> 0 ? a
: "n/a");
127 return table_log_add_error(r
);
130 n
= strftime(a
, sizeof a
, "%Z, %z", localtime_r(&sec
, &tm
));
132 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Time zone:");
134 return table_log_add_error(r
);
136 r
= table_add_cell_stringf(table
, NULL
, "%s (%s)", strna(i
->timezone
), have_time
&& n
> 0 ? a
: "n/a");
138 return table_log_add_error(r
);
141 /* Restore the $TZ */
143 r
= setenv("TZ", old_tz
, true);
147 log_warning_errno(errno
, "Failed to set TZ environment variable, ignoring: %m");
151 r
= table_add_many(table
,
152 TABLE_STRING
, "System clock synchronized:",
153 TABLE_BOOLEAN
, i
->ntp_synced
,
154 TABLE_STRING
, "NTP service:",
155 TABLE_STRING
, i
->ntp_capable
? (i
->ntp_active
? "active" : "inactive") : "n/a",
156 TABLE_STRING
, "RTC in local TZ:",
157 TABLE_BOOLEAN
, i
->rtc_local
);
159 return table_log_add_error(r
);
161 r
= table_print(table
, NULL
);
163 return table_log_print_error(r
);
167 "Warning: The system is configured to read the RTC time in the local time zone.\n"
168 " This mode cannot be fully supported. It will create various problems\n"
169 " with time zone changes and daylight saving time adjustments. The RTC\n"
170 " time is never updated, it relies on external facilities to maintain it.\n"
171 " If at all possible, use RTC in UTC by calling\n"
172 " 'timedatectl set-local-rtc 0'.%s\n", ansi_highlight(), ansi_normal());
177 static int show_status(int argc
, char **argv
, void *userdata
) {
178 StatusInfo info
= {};
179 static const struct bus_properties_map map
[] = {
180 { "Timezone", "s", NULL
, offsetof(StatusInfo
, timezone
) },
181 { "LocalRTC", "b", NULL
, offsetof(StatusInfo
, rtc_local
) },
182 { "NTP", "b", NULL
, offsetof(StatusInfo
, ntp_active
) },
183 { "CanNTP", "b", NULL
, offsetof(StatusInfo
, ntp_capable
) },
184 { "NTPSynchronized", "b", NULL
, offsetof(StatusInfo
, ntp_synced
) },
185 { "TimeUSec", "t", NULL
, offsetof(StatusInfo
, time
) },
186 { "RTCTimeUSec", "t", NULL
, offsetof(StatusInfo
, rtc_time
) },
190 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
191 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
192 sd_bus
*bus
= userdata
;
197 r
= bus_map_all_properties(bus
,
198 "org.freedesktop.timedate1",
199 "/org/freedesktop/timedate1",
201 BUS_MAP_BOOLEAN_AS_BOOL
,
206 return log_error_errno(r
, "Failed to query server: %s", bus_error_message(&error
, r
));
208 return print_status_info(&info
);
211 static int show_properties(int argc
, char **argv
, void *userdata
) {
212 sd_bus
*bus
= userdata
;
217 r
= bus_print_all_properties(bus
,
218 "org.freedesktop.timedate1",
219 "/org/freedesktop/timedate1",
226 return bus_log_parse_error(r
);
231 static int set_time(int argc
, char **argv
, void *userdata
) {
232 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
233 bool relative
= false, interactive
= arg_ask_password
;
234 sd_bus
*bus
= userdata
;
238 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
240 r
= parse_timestamp(argv
[1], &t
);
242 return log_error_errno(r
, "Failed to parse time specification '%s': %m", argv
[1]);
250 "xbb", (int64_t) t
, relative
, interactive
);
252 return log_error_errno(r
, "Failed to set time: %s", bus_error_message(&error
, r
));
257 static int set_timezone(int argc
, char **argv
, void *userdata
) {
258 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
259 sd_bus
*bus
= userdata
;
262 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
264 r
= bus_call_method(bus
, bus_timedate
, "SetTimezone", &error
, NULL
, "sb", argv
[1], arg_ask_password
);
266 return log_error_errno(r
, "Failed to set time zone: %s", bus_error_message(&error
, r
));
271 static int set_local_rtc(int argc
, char **argv
, void *userdata
) {
272 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
273 sd_bus
*bus
= userdata
;
276 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
278 b
= parse_boolean(argv
[1]);
280 return log_error_errno(b
, "Failed to parse local RTC setting '%s': %m", argv
[1]);
288 "bbb", b
, arg_adjust_system_clock
, arg_ask_password
);
290 return log_error_errno(r
, "Failed to set local RTC: %s", bus_error_message(&error
, r
));
295 static int set_ntp(int argc
, char **argv
, void *userdata
) {
296 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
297 sd_bus
*bus
= userdata
;
300 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
302 b
= parse_boolean(argv
[1]);
304 return log_error_errno(b
, "Failed to parse NTP setting '%s': %m", argv
[1]);
306 r
= bus_call_method(bus
, bus_timedate
, "SetNTP", &error
, NULL
, "bb", b
, arg_ask_password
);
308 return log_error_errno(r
, "Failed to set ntp: %s", bus_error_message(&error
, r
));
313 static int list_timezones(int argc
, char **argv
, void *userdata
) {
314 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
315 sd_bus
*bus
= userdata
;
316 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
320 r
= bus_call_method(bus
, bus_timedate
, "ListTimezones", &error
, &reply
, NULL
);
322 return log_error_errno(r
, "Failed to request list of time zones: %s",
323 bus_error_message(&error
, r
));
325 r
= sd_bus_message_read_strv(reply
, &zones
);
327 return bus_log_parse_error(r
);
329 (void) pager_open(arg_pager_flags
);
335 typedef struct NTPStatusInfo
{
336 const char *server_name
;
337 char *server_address
;
338 usec_t poll_interval
, poll_max
, poll_min
;
339 usec_t root_distance_max
;
341 uint32_t leap
, version
, mode
, stratum
;
343 usec_t root_delay
, root_dispersion
;
348 usec_t origin
, recv
, trans
, dest
;
351 uint64_t packet_count
;
357 static void ntp_status_info_clear(NTPStatusInfo
*p
) {
358 p
->server_address
= mfree(p
->server_address
);
361 static const char * const ntp_leap_table
[4] = {
363 [1] = "last minute of the day has 61 seconds",
364 [2] = "last minute of the day has 59 seconds",
365 [3] = "not synchronized",
368 DISABLE_WARNING_TYPE_LIMITS
;
369 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(ntp_leap
, uint32_t);
372 static int print_ntp_status_info(NTPStatusInfo
*i
) {
373 char ts
[FORMAT_TIMESPAN_MAX
], jitter
[FORMAT_TIMESPAN_MAX
],
374 tmin
[FORMAT_TIMESPAN_MAX
], tmax
[FORMAT_TIMESPAN_MAX
];
375 usec_t delay
, t14
, t23
, offset
, root_distance
;
376 _cleanup_(table_unrefp
) Table
*table
= NULL
;
383 table
= table_new("key", "value");
387 table_set_header(table
, false);
389 assert_se(cell
= table_get_cell(table
, 0, 0));
390 (void) table_set_ellipsize_percent(table
, cell
, 100);
391 (void) table_set_align_percent(table
, cell
, 100);
393 assert_se(cell
= table_get_cell(table
, 0, 1));
394 (void) table_set_ellipsize_percent(table
, cell
, 100);
397 * "Timestamp Name ID When Generated
398 * ------------------------------------------------------------
399 * Originate Timestamp T1 time request sent by client
400 * Receive Timestamp T2 time request received by server
401 * Transmit Timestamp T3 time reply sent by server
402 * Destination Timestamp T4 time reply received by client
404 * The round-trip delay, d, and system clock offset, t, are defined as:
405 * d = (T4 - T1) - (T3 - T2) t = ((T2 - T1) + (T3 - T4)) / 2"
408 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Server:");
410 return table_log_add_error(r
);
412 r
= table_add_cell_stringf(table
, NULL
, "%s (%s)", strna(i
->server_address
), strna(i
->server_name
));
414 return table_log_add_error(r
);
416 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Poll interval:");
418 return table_log_add_error(r
);
420 r
= table_add_cell_stringf(table
, NULL
, "%s (min: %s; max %s)",
421 format_timespan(ts
, sizeof(ts
), i
->poll_interval
, 0),
422 format_timespan(tmin
, sizeof(tmin
), i
->poll_min
, 0),
423 format_timespan(tmax
, sizeof(tmax
), i
->poll_max
, 0));
425 return table_log_add_error(r
);
427 if (i
->packet_count
== 0) {
428 r
= table_add_many(table
,
429 TABLE_STRING
, "Packet count:",
432 return table_log_add_error(r
);
434 r
= table_print(table
, NULL
);
436 return table_log_print_error(r
);
441 if (i
->dest
< i
->origin
|| i
->trans
< i
->recv
|| i
->dest
- i
->origin
< i
->trans
- i
->recv
) {
442 log_error("Invalid NTP response");
443 r
= table_print(table
, NULL
);
445 return table_log_print_error(r
);
450 delay
= (i
->dest
- i
->origin
) - (i
->trans
- i
->recv
);
452 t14
= i
->origin
+ i
->dest
;
453 t23
= i
->recv
+ i
->trans
;
454 offset_sign
= t14
< t23
;
455 offset
= (offset_sign
? t23
- t14
: t14
- t23
) / 2;
457 root_distance
= i
->root_delay
/ 2 + i
->root_dispersion
;
459 r
= table_add_many(table
,
460 TABLE_STRING
, "Leap:",
461 TABLE_STRING
, ntp_leap_to_string(i
->leap
),
462 TABLE_STRING
, "Version:",
463 TABLE_UINT32
, i
->version
,
464 TABLE_STRING
, "Stratum:",
465 TABLE_UINT32
, i
->stratum
,
466 TABLE_STRING
, "Reference:");
468 return table_log_add_error(r
);
471 r
= table_add_cell(table
, NULL
, TABLE_STRING
, i
->reference
.str
);
473 r
= table_add_cell_stringf(table
, NULL
, "%" PRIX32
, be32toh(i
->reference
.val
));
475 return table_log_add_error(r
);
477 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Precision:");
479 return table_log_add_error(r
);
481 r
= table_add_cell_stringf(table
, NULL
, "%s (%" PRIi32
")",
482 format_timespan(ts
, sizeof(ts
), DIV_ROUND_UP((nsec_t
) (exp2(i
->precision
) * NSEC_PER_SEC
), NSEC_PER_USEC
), 0),
485 return table_log_add_error(r
);
487 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Root distance:");
489 return table_log_add_error(r
);
491 r
= table_add_cell_stringf(table
, NULL
, "%s (max: %s)",
492 format_timespan(ts
, sizeof(ts
), root_distance
, 0),
493 format_timespan(tmax
, sizeof(tmax
), i
->root_distance_max
, 0));
495 return table_log_add_error(r
);
497 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Offset:");
499 return table_log_add_error(r
);
501 r
= table_add_cell_stringf(table
, NULL
, "%s%s",
502 offset_sign
? "+" : "-",
503 format_timespan(ts
, sizeof(ts
), offset
, 0));
505 return table_log_add_error(r
);
507 r
= table_add_many(table
,
508 TABLE_STRING
, "Delay:",
509 TABLE_STRING
, format_timespan(ts
, sizeof(ts
), delay
, 0),
510 TABLE_STRING
, "Jitter:",
511 TABLE_STRING
, format_timespan(jitter
, sizeof(jitter
), i
->jitter
, 0),
512 TABLE_STRING
, "Packet count:",
513 TABLE_UINT64
, i
->packet_count
);
515 return table_log_add_error(r
);
518 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Frequency:");
520 return table_log_add_error(r
);
522 r
= table_add_cell_stringf(table
, NULL
, "%+.3fppm", (double) i
->freq
/ 0x10000);
524 return table_log_add_error(r
);
527 r
= table_print(table
, NULL
);
529 return table_log_print_error(r
);
534 static int map_server_address(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
535 char **p
= (char **) userdata
;
542 r
= sd_bus_message_enter_container(m
, 'r', "iay");
546 r
= sd_bus_message_read(m
, "i", &family
);
550 r
= sd_bus_message_read_array(m
, 'y', &d
, &sz
);
554 r
= sd_bus_message_exit_container(m
);
558 if (sz
== 0 && family
== AF_UNSPEC
) {
563 if (!IN_SET(family
, AF_INET
, AF_INET6
))
564 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
565 "Unknown address family %i", family
);
567 if (sz
!= FAMILY_ADDRESS_SIZE(family
))
568 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
569 "Invalid address size");
571 r
= in_addr_to_string(family
, d
, p
);
578 static int map_ntp_message(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
579 NTPStatusInfo
*p
= userdata
;
587 r
= sd_bus_message_enter_container(m
, 'r', "uuuuittayttttbtt");
591 r
= sd_bus_message_read(m
, "uuuuitt",
592 &p
->leap
, &p
->version
, &p
->mode
, &p
->stratum
, &p
->precision
,
593 &p
->root_delay
, &p
->root_dispersion
);
597 r
= sd_bus_message_read_array(m
, 'y', &d
, &sz
);
601 r
= sd_bus_message_read(m
, "ttttbtt",
602 &p
->origin
, &p
->recv
, &p
->trans
, &p
->dest
,
603 &b
, &p
->packet_count
, &p
->jitter
);
607 r
= sd_bus_message_exit_container(m
);
614 memcpy(p
->reference
.str
, d
, sz
);
621 static int show_timesync_status_once(sd_bus
*bus
) {
622 static const struct bus_properties_map map_timesync
[] = {
623 { "ServerName", "s", NULL
, offsetof(NTPStatusInfo
, server_name
) },
624 { "ServerAddress", "(iay)", map_server_address
, offsetof(NTPStatusInfo
, server_address
) },
625 { "PollIntervalUSec", "t", NULL
, offsetof(NTPStatusInfo
, poll_interval
) },
626 { "PollIntervalMinUSec", "t", NULL
, offsetof(NTPStatusInfo
, poll_min
) },
627 { "PollIntervalMaxUSec", "t", NULL
, offsetof(NTPStatusInfo
, poll_max
) },
628 { "RootDistanceMaxUSec", "t", NULL
, offsetof(NTPStatusInfo
, root_distance_max
) },
629 { "NTPMessage", "(uuuuittayttttbtt)", map_ntp_message
, 0 },
630 { "Frequency", "x", NULL
, offsetof(NTPStatusInfo
, freq
) },
633 _cleanup_(ntp_status_info_clear
) NTPStatusInfo info
= {};
634 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
635 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
640 r
= bus_map_all_properties(bus
,
641 "org.freedesktop.timesync1",
642 "/org/freedesktop/timesync1",
644 BUS_MAP_BOOLEAN_AS_BOOL
,
649 return log_error_errno(r
, "Failed to query server: %s", bus_error_message(&error
, r
));
651 if (arg_monitor
&& !terminal_is_dumb())
652 fputs(ANSI_HOME_CLEAR
, stdout
);
654 print_ntp_status_info(&info
);
659 static int on_properties_changed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
665 r
= sd_bus_message_read(m
, "s", &name
);
667 return bus_log_parse_error(r
);
669 if (!streq_ptr(name
, "org.freedesktop.timesync1.Manager"))
672 return show_timesync_status_once(sd_bus_message_get_bus(m
));
675 static int show_timesync_status(int argc
, char **argv
, void *userdata
) {
676 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
677 sd_bus
*bus
= userdata
;
682 r
= show_timesync_status_once(bus
);
689 r
= sd_event_default(&event
);
691 return log_error_errno(r
, "Failed to get event loop: %m");
693 r
= sd_bus_match_signal(bus
,
695 "org.freedesktop.timesync1",
696 "/org/freedesktop/timesync1",
697 "org.freedesktop.DBus.Properties",
699 on_properties_changed
, NULL
);
701 return log_error_errno(r
, "Failed to request match for PropertiesChanged signal: %m");
703 r
= sd_bus_attach_event(bus
, event
, SD_EVENT_PRIORITY_NORMAL
);
705 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
707 r
= sd_event_loop(event
);
709 return log_error_errno(r
, "Failed to run event loop: %m");
714 static int print_timesync_property(const char *name
, const char *expected_value
, sd_bus_message
*m
, bool value
, bool all
) {
716 const char *contents
;
722 r
= sd_bus_message_peek_type(m
, &type
, &contents
);
728 case SD_BUS_TYPE_STRUCT
:
729 if (streq(name
, "NTPMessage")) {
730 _cleanup_(ntp_status_info_clear
) NTPStatusInfo i
= {};
731 char ts
[FORMAT_TIMESPAN_MAX
], stamp
[FORMAT_TIMESTAMP_MAX
];
733 r
= map_ntp_message(NULL
, NULL
, m
, NULL
, &i
);
737 if (i
.packet_count
== 0)
745 printf("{ Leap=%u, Version=%u, Mode=%u, Stratum=%u, Precision=%i,",
746 i
.leap
, i
.version
, i
.mode
, i
.stratum
, i
.precision
);
747 printf(" RootDelay=%s,",
748 format_timespan(ts
, sizeof(ts
), i
.root_delay
, 0));
749 printf(" RootDispersion=%s,",
750 format_timespan(ts
, sizeof(ts
), i
.root_dispersion
, 0));
753 printf(" Reference=%s,", i
.reference
.str
);
755 printf(" Reference=%" PRIX32
",", be32toh(i
.reference
.val
));
757 printf(" OriginateTimestamp=%s,",
758 format_timestamp(stamp
, sizeof(stamp
), i
.origin
));
759 printf(" ReceiveTimestamp=%s,",
760 format_timestamp(stamp
, sizeof(stamp
), i
.recv
));
761 printf(" TransmitTimestamp=%s,",
762 format_timestamp(stamp
, sizeof(stamp
), i
.trans
));
763 printf(" DestinationTimestamp=%s,",
764 format_timestamp(stamp
, sizeof(stamp
), i
.dest
));
765 printf(" Ignored=%s PacketCount=%" PRIu64
",",
766 yes_no(i
.spike
), i
.packet_count
);
767 printf(" Jitter=%s }\n",
768 format_timespan(ts
, sizeof(ts
), i
.jitter
, 0));
772 } else if (streq(name
, "ServerAddress")) {
773 _cleanup_free_
char *str
= NULL
;
775 r
= map_server_address(NULL
, NULL
, m
, NULL
, &str
);
779 if (arg_all
|| !isempty(str
))
780 bus_print_property_value(name
, expected_value
, value
, str
);
790 static int show_timesync(int argc
, char **argv
, void *userdata
) {
791 sd_bus
*bus
= userdata
;
796 r
= bus_print_all_properties(bus
,
797 "org.freedesktop.timesync1",
798 "/org/freedesktop/timesync1",
799 print_timesync_property
,
805 return bus_log_parse_error(r
);
810 static int parse_ifindex_bus(sd_bus
*bus
, const char *str
) {
811 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
812 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
819 r
= parse_ifindex(str
);
824 r
= bus_call_method(bus
, bus_network_mgr
, "GetLinkByName", &error
, &reply
, "s", str
);
826 return log_error_errno(r
, "Failed to get ifindex of interfaces %s: %s", str
, bus_error_message(&error
, r
));
828 r
= sd_bus_message_read(reply
, "io", &i
, NULL
);
830 return bus_log_create_error(r
);
835 static int verb_ntp_servers(int argc
, char **argv
, void *userdata
) {
836 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
837 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
;
838 sd_bus
*bus
= userdata
;
843 ifindex
= parse_ifindex_bus(bus
, argv
[1]);
847 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
849 r
= bus_message_new_method_call(bus
, &req
, bus_network_mgr
, "SetLinkNTP");
851 return bus_log_create_error(r
);
853 r
= sd_bus_message_append(req
, "i", ifindex
);
855 return bus_log_create_error(r
);
857 r
= sd_bus_message_append_strv(req
, argv
+ 2);
859 return bus_log_create_error(r
);
861 r
= sd_bus_call(bus
, req
, 0, &error
, NULL
);
863 return log_error_errno(r
, "Failed to set NTP servers: %s", bus_error_message(&error
, r
));
868 static int verb_revert(int argc
, char **argv
, void *userdata
) {
869 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
870 sd_bus
*bus
= userdata
;
875 ifindex
= parse_ifindex_bus(bus
, argv
[1]);
879 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
881 r
= bus_call_method(bus
, bus_network_mgr
, "RevertLinkNTP", &error
, NULL
, "i", ifindex
);
883 return log_error_errno(r
, "Failed to revert interface configuration: %s", bus_error_message(&error
, r
));
888 static int help(void) {
889 _cleanup_free_
char *link
= NULL
;
892 r
= terminal_urlify_man("timedatectl", "1", &link
);
896 printf("%s [OPTIONS...] COMMAND ...\n"
897 "\n%sQuery or change system time and date settings.%s\n"
899 " status Show current time settings\n"
900 " show Show properties of systemd-timedated\n"
901 " set-time TIME Set system time\n"
902 " set-timezone ZONE Set system time zone\n"
903 " list-timezones Show known time zones\n"
904 " set-local-rtc BOOL Control whether RTC is in local time\n"
905 " set-ntp BOOL Enable or disable network time synchronization\n"
906 "\nsystemd-timesyncd Commands:\n"
907 " timesync-status Show status of systemd-timesyncd\n"
908 " show-timesync Show properties of systemd-timesyncd\n"
910 " -h --help Show this help message\n"
911 " --version Show package version\n"
912 " --no-pager Do not pipe output into a pager\n"
913 " --no-ask-password Do not prompt for password\n"
914 " -H --host=[USER@]HOST Operate on remote host\n"
915 " -M --machine=CONTAINER Operate on local container\n"
916 " --adjust-system-clock Adjust system clock when changing local RTC mode\n"
917 " --monitor Monitor status of systemd-timesyncd\n"
918 " -p --property=NAME Show only properties by this name\n"
919 " -a --all Show all properties, including empty ones\n"
920 " --value When showing properties, only print the value\n"
921 "\nSee the %s for details.\n"
922 , program_invocation_short_name
931 static int verb_help(int argc
, char **argv
, void *userdata
) {
935 static int parse_argv(int argc
, char *argv
[]) {
940 ARG_ADJUST_SYSTEM_CLOCK
,
946 static const struct option options
[] = {
947 { "help", no_argument
, NULL
, 'h' },
948 { "version", no_argument
, NULL
, ARG_VERSION
},
949 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
950 { "host", required_argument
, NULL
, 'H' },
951 { "machine", required_argument
, NULL
, 'M' },
952 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
953 { "adjust-system-clock", no_argument
, NULL
, ARG_ADJUST_SYSTEM_CLOCK
},
954 { "monitor", no_argument
, NULL
, ARG_MONITOR
},
955 { "property", required_argument
, NULL
, 'p' },
956 { "all", no_argument
, NULL
, 'a' },
957 { "value", no_argument
, NULL
, ARG_VALUE
},
966 while ((c
= getopt_long(argc
, argv
, "hH:M:p:a", options
, NULL
)) >= 0)
977 arg_transport
= BUS_TRANSPORT_REMOTE
;
982 arg_transport
= BUS_TRANSPORT_MACHINE
;
986 case ARG_NO_ASK_PASSWORD
:
987 arg_ask_password
= false;
990 case ARG_ADJUST_SYSTEM_CLOCK
:
991 arg_adjust_system_clock
= true;
995 arg_pager_flags
|= PAGER_DISABLE
;
1003 r
= strv_extend(&arg_property
, optarg
);
1007 /* If the user asked for a particular
1008 * property, show it to them, even if it is
1026 assert_not_reached("Unhandled option");
1032 static int timedatectl_main(sd_bus
*bus
, int argc
, char *argv
[]) {
1033 static const Verb verbs
[] = {
1034 { "status", VERB_ANY
, 1, VERB_DEFAULT
, show_status
},
1035 { "show", VERB_ANY
, 1, 0, show_properties
},
1036 { "set-time", 2, 2, 0, set_time
},
1037 { "set-timezone", 2, 2, 0, set_timezone
},
1038 { "list-timezones", VERB_ANY
, 1, 0, list_timezones
},
1039 { "set-local-rtc", 2, 2, 0, set_local_rtc
},
1040 { "set-ntp", 2, 2, 0, set_ntp
},
1041 { "timesync-status", VERB_ANY
, 1, 0, show_timesync_status
},
1042 { "show-timesync", VERB_ANY
, 1, 0, show_timesync
},
1043 { "ntp-servers", 3, VERB_ANY
, 0, verb_ntp_servers
},
1044 { "revert", 2, 2, 0, verb_revert
},
1045 { "help", VERB_ANY
, VERB_ANY
, 0, verb_help
}, /* Not documented, but supported since it is created. */
1049 return dispatch_verb(argc
, argv
, verbs
, bus
);
1052 static int run(int argc
, char *argv
[]) {
1053 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1056 setlocale(LC_ALL
, "");
1059 r
= parse_argv(argc
, argv
);
1063 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
1065 return bus_log_connect_error(r
);
1067 return timedatectl_main(bus
, argc
, argv
);
1070 DEFINE_MAIN_FUNCTION(run
);