1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "bus-error.h"
12 #include "bus-locator.h"
13 #include "format-table.h"
14 #include "in-addr-util.h"
15 #include "main-func.h"
17 #include "parse-util.h"
18 #include "pretty-print.h"
19 #include "sparse-endian.h"
20 #include "spawn-polkit-agent.h"
21 #include "string-table.h"
23 #include "terminal-util.h"
27 static PagerFlags arg_pager_flags
= 0;
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;
37 typedef struct StatusInfo
{
49 static int print_status_info(const StatusInfo
*i
) {
50 _cleanup_(table_unrefp
) Table
*table
= NULL
;
51 const char *old_tz
= NULL
, *tz
, *tz_colon
;
52 bool have_time
= false;
62 table
= table_new("key", "value");
66 table_set_header(table
, false);
68 assert_se(cell
= table_get_cell(table
, 0, 0));
69 (void) table_set_ellipsize_percent(table
, cell
, 100);
70 (void) table_set_align_percent(table
, cell
, 100);
72 assert_se(cell
= table_get_cell(table
, 0, 1));
73 (void) table_set_ellipsize_percent(table
, cell
, 100);
75 /* Save the old $TZ */
81 tz_colon
= strjoina(":", isempty(i
->timezone
) ? "UTC" : i
->timezone
);
82 if (setenv("TZ", tz_colon
, true) < 0)
83 log_warning_errno(errno
, "Failed to set TZ environment variable, ignoring: %m");
88 sec
= (time_t) (i
->time
/ USEC_PER_SEC
);
90 } else if (IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
)) {
94 log_warning("Could not get time from timedated and not operating locally, ignoring.");
97 n
= strftime(a
, sizeof a
, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec
, &tm
));
99 r
= table_add_many(table
,
100 TABLE_STRING
, "Local time:",
101 TABLE_STRING
, have_time
&& n
> 0 ? a
: "n/a");
103 return table_log_add_error(r
);
106 n
= strftime(a
, sizeof a
, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec
, &tm
));
108 r
= table_add_many(table
,
109 TABLE_STRING
, "Universal time:",
110 TABLE_STRING
, have_time
&& n
> 0 ? a
: "n/a");
112 return table_log_add_error(r
);
114 if (i
->rtc_time
> 0) {
117 rtc_sec
= (time_t) (i
->rtc_time
/ USEC_PER_SEC
);
118 n
= strftime(a
, sizeof a
, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec
, &tm
));
121 r
= table_add_many(table
,
122 TABLE_STRING
, "RTC time:",
123 TABLE_STRING
, i
->rtc_time
> 0 && n
> 0 ? a
: "n/a");
125 return table_log_add_error(r
);
128 n
= strftime(a
, sizeof a
, "%Z, %z", localtime_r(&sec
, &tm
));
130 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Time zone:");
132 return table_log_add_error(r
);
134 r
= table_add_cell_stringf(table
, NULL
, "%s (%s)", strna(i
->timezone
), have_time
&& n
> 0 ? a
: "n/a");
136 return table_log_add_error(r
);
139 /* Restore the $TZ */
141 r
= setenv("TZ", old_tz
, true);
145 log_warning_errno(errno
, "Failed to set TZ environment variable, ignoring: %m");
149 r
= table_add_many(table
,
150 TABLE_STRING
, "System clock synchronized:",
151 TABLE_BOOLEAN
, i
->ntp_synced
,
152 TABLE_STRING
, "NTP service:",
153 TABLE_STRING
, i
->ntp_capable
? (i
->ntp_active
? "active" : "inactive") : "n/a",
154 TABLE_STRING
, "RTC in local TZ:",
155 TABLE_BOOLEAN
, i
->rtc_local
);
157 return table_log_add_error(r
);
159 r
= table_print(table
, NULL
);
161 return log_error_errno(r
, "Failed to show table: %m");
165 "Warning: The system is configured to read the RTC time in the local time zone.\n"
166 " This mode cannot be fully supported. It will create various problems\n"
167 " with time zone changes and daylight saving time adjustments. The RTC\n"
168 " time is never updated, it relies on external facilities to maintain it.\n"
169 " If at all possible, use RTC in UTC by calling\n"
170 " 'timedatectl set-local-rtc 0'.%s\n", ansi_highlight(), ansi_normal());
175 static int show_status(int argc
, char **argv
, void *userdata
) {
176 StatusInfo info
= {};
177 static const struct bus_properties_map map
[] = {
178 { "Timezone", "s", NULL
, offsetof(StatusInfo
, timezone
) },
179 { "LocalRTC", "b", NULL
, offsetof(StatusInfo
, rtc_local
) },
180 { "NTP", "b", NULL
, offsetof(StatusInfo
, ntp_active
) },
181 { "CanNTP", "b", NULL
, offsetof(StatusInfo
, ntp_capable
) },
182 { "NTPSynchronized", "b", NULL
, offsetof(StatusInfo
, ntp_synced
) },
183 { "TimeUSec", "t", NULL
, offsetof(StatusInfo
, time
) },
184 { "RTCTimeUSec", "t", NULL
, offsetof(StatusInfo
, rtc_time
) },
188 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
189 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
190 sd_bus
*bus
= userdata
;
195 r
= bus_map_all_properties(bus
,
196 "org.freedesktop.timedate1",
197 "/org/freedesktop/timedate1",
199 BUS_MAP_BOOLEAN_AS_BOOL
,
204 return log_error_errno(r
, "Failed to query server: %s", bus_error_message(&error
, r
));
206 return print_status_info(&info
);
209 static int show_properties(int argc
, char **argv
, void *userdata
) {
210 sd_bus
*bus
= userdata
;
215 r
= bus_print_all_properties(bus
,
216 "org.freedesktop.timedate1",
217 "/org/freedesktop/timedate1",
224 return bus_log_parse_error(r
);
229 static int set_time(int argc
, char **argv
, void *userdata
) {
230 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
231 bool relative
= false, interactive
= arg_ask_password
;
232 sd_bus
*bus
= userdata
;
236 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
238 r
= parse_timestamp(argv
[1], &t
);
240 return log_error_errno(r
, "Failed to parse time specification '%s': %m", argv
[1]);
248 "xbb", (int64_t) t
, relative
, interactive
);
250 return log_error_errno(r
, "Failed to set time: %s", bus_error_message(&error
, r
));
255 static int set_timezone(int argc
, char **argv
, void *userdata
) {
256 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
257 sd_bus
*bus
= userdata
;
260 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
262 r
= bus_call_method(bus
, bus_timedate
, "SetTimezone", &error
, NULL
, "sb", argv
[1], arg_ask_password
);
264 return log_error_errno(r
, "Failed to set time zone: %s", bus_error_message(&error
, r
));
269 static int set_local_rtc(int argc
, char **argv
, void *userdata
) {
270 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
271 sd_bus
*bus
= userdata
;
274 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
276 b
= parse_boolean(argv
[1]);
278 return log_error_errno(b
, "Failed to parse local RTC setting '%s': %m", argv
[1]);
286 "bbb", b
, arg_adjust_system_clock
, arg_ask_password
);
288 return log_error_errno(r
, "Failed to set local RTC: %s", bus_error_message(&error
, r
));
293 static int set_ntp(int argc
, char **argv
, void *userdata
) {
294 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
295 sd_bus
*bus
= userdata
;
298 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
300 b
= parse_boolean(argv
[1]);
302 return log_error_errno(b
, "Failed to parse NTP setting '%s': %m", argv
[1]);
304 r
= bus_call_method(bus
, bus_timedate
, "SetNTP", &error
, NULL
, "bb", b
, arg_ask_password
);
306 return log_error_errno(r
, "Failed to set ntp: %s", bus_error_message(&error
, r
));
311 static int list_timezones(int argc
, char **argv
, void *userdata
) {
312 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
313 sd_bus
*bus
= userdata
;
314 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
318 r
= bus_call_method(bus
, bus_timedate
, "ListTimezones", &error
, &reply
, NULL
);
320 return log_error_errno(r
, "Failed to request list of time zones: %s",
321 bus_error_message(&error
, r
));
323 r
= sd_bus_message_read_strv(reply
, &zones
);
325 return bus_log_parse_error(r
);
327 (void) pager_open(arg_pager_flags
);
333 typedef struct NTPStatusInfo
{
334 const char *server_name
;
335 char *server_address
;
336 usec_t poll_interval
, poll_max
, poll_min
;
337 usec_t root_distance_max
;
339 uint32_t leap
, version
, mode
, stratum
;
341 usec_t root_delay
, root_dispersion
;
346 usec_t origin
, recv
, trans
, dest
;
349 uint64_t packet_count
;
355 static void ntp_status_info_clear(NTPStatusInfo
*p
) {
356 p
->server_address
= mfree(p
->server_address
);
359 static const char * const ntp_leap_table
[4] = {
361 [1] = "last minute of the day has 61 seconds",
362 [2] = "last minute of the day has 59 seconds",
363 [3] = "not synchronized",
366 DISABLE_WARNING_TYPE_LIMITS
;
367 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(ntp_leap
, uint32_t);
370 static int print_ntp_status_info(NTPStatusInfo
*i
) {
371 char ts
[FORMAT_TIMESPAN_MAX
], jitter
[FORMAT_TIMESPAN_MAX
],
372 tmin
[FORMAT_TIMESPAN_MAX
], tmax
[FORMAT_TIMESPAN_MAX
];
373 usec_t delay
, t14
, t23
, offset
, root_distance
;
374 _cleanup_(table_unrefp
) Table
*table
= NULL
;
381 table
= table_new("key", "value");
385 table_set_header(table
, false);
387 assert_se(cell
= table_get_cell(table
, 0, 0));
388 (void) table_set_ellipsize_percent(table
, cell
, 100);
389 (void) table_set_align_percent(table
, cell
, 100);
391 assert_se(cell
= table_get_cell(table
, 0, 1));
392 (void) table_set_ellipsize_percent(table
, cell
, 100);
395 * "Timestamp Name ID When Generated
396 * ------------------------------------------------------------
397 * Originate Timestamp T1 time request sent by client
398 * Receive Timestamp T2 time request received by server
399 * Transmit Timestamp T3 time reply sent by server
400 * Destination Timestamp T4 time reply received by client
402 * The round-trip delay, d, and system clock offset, t, are defined as:
403 * d = (T4 - T1) - (T3 - T2) t = ((T2 - T1) + (T3 - T4)) / 2"
406 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Server:");
408 return table_log_add_error(r
);
410 r
= table_add_cell_stringf(table
, NULL
, "%s (%s)", i
->server_address
, i
->server_name
);
412 return table_log_add_error(r
);
414 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Poll interval:");
416 return table_log_add_error(r
);
418 r
= table_add_cell_stringf(table
, NULL
, "%s (min: %s; max %s)",
419 format_timespan(ts
, sizeof(ts
), i
->poll_interval
, 0),
420 format_timespan(tmin
, sizeof(tmin
), i
->poll_min
, 0),
421 format_timespan(tmax
, sizeof(tmax
), i
->poll_max
, 0));
423 return table_log_add_error(r
);
425 if (i
->packet_count
== 0) {
426 r
= table_add_many(table
,
427 TABLE_STRING
, "Packet count:",
430 return table_log_add_error(r
);
432 r
= table_print(table
, NULL
);
434 return log_error_errno(r
, "Failed to show table: %m");
439 if (i
->dest
< i
->origin
|| i
->trans
< i
->recv
|| i
->dest
- i
->origin
< i
->trans
- i
->recv
) {
440 log_error("Invalid NTP response");
441 r
= table_print(table
, NULL
);
443 return log_error_errno(r
, "Failed to show table: %m");
448 delay
= (i
->dest
- i
->origin
) - (i
->trans
- i
->recv
);
450 t14
= i
->origin
+ i
->dest
;
451 t23
= i
->recv
+ i
->trans
;
452 offset_sign
= t14
< t23
;
453 offset
= (offset_sign
? t23
- t14
: t14
- t23
) / 2;
455 root_distance
= i
->root_delay
/ 2 + i
->root_dispersion
;
457 r
= table_add_many(table
,
458 TABLE_STRING
, "Leap:",
459 TABLE_STRING
, ntp_leap_to_string(i
->leap
),
460 TABLE_STRING
, "Version:",
461 TABLE_UINT32
, i
->version
,
462 TABLE_STRING
, "Stratum:",
463 TABLE_UINT32
, i
->stratum
,
464 TABLE_STRING
, "Reference:");
466 return table_log_add_error(r
);
469 r
= table_add_cell(table
, NULL
, TABLE_STRING
, i
->reference
.str
);
471 r
= table_add_cell_stringf(table
, NULL
, "%" PRIX32
, be32toh(i
->reference
.val
));
473 return table_log_add_error(r
);
475 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Precision:");
477 return table_log_add_error(r
);
479 r
= table_add_cell_stringf(table
, NULL
, "%s (%" PRIi32
")",
480 format_timespan(ts
, sizeof(ts
), DIV_ROUND_UP((nsec_t
) (exp2(i
->precision
) * NSEC_PER_SEC
), NSEC_PER_USEC
), 0),
483 return table_log_add_error(r
);
485 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Root distance:");
487 return table_log_add_error(r
);
489 r
= table_add_cell_stringf(table
, NULL
, "%s (max: %s)",
490 format_timespan(ts
, sizeof(ts
), root_distance
, 0),
491 format_timespan(tmax
, sizeof(tmax
), i
->root_distance_max
, 0));
493 return table_log_add_error(r
);
495 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Offset:");
497 return table_log_add_error(r
);
499 r
= table_add_cell_stringf(table
, NULL
, "%s%s",
500 offset_sign
? "+" : "-",
501 format_timespan(ts
, sizeof(ts
), offset
, 0));
503 return table_log_add_error(r
);
505 r
= table_add_many(table
,
506 TABLE_STRING
, "Delay:",
507 TABLE_STRING
, format_timespan(ts
, sizeof(ts
), delay
, 0),
508 TABLE_STRING
, "Jitter:",
509 TABLE_STRING
, format_timespan(jitter
, sizeof(jitter
), i
->jitter
, 0),
510 TABLE_STRING
, "Packet count:",
511 TABLE_UINT64
, i
->packet_count
);
513 return table_log_add_error(r
);
516 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Frequency:");
518 return table_log_add_error(r
);
520 r
= table_add_cell_stringf(table
, NULL
, "%+.3fppm", (double) i
->freq
/ 0x10000);
522 return table_log_add_error(r
);
525 r
= table_print(table
, NULL
);
527 log_error_errno(r
, "Failed to show table: %m");
532 static int map_server_address(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
533 char **p
= (char **) userdata
;
540 r
= sd_bus_message_enter_container(m
, 'r', "iay");
544 r
= sd_bus_message_read(m
, "i", &family
);
548 r
= sd_bus_message_read_array(m
, 'y', &d
, &sz
);
552 r
= sd_bus_message_exit_container(m
);
556 if (sz
== 0 && family
== AF_UNSPEC
) {
561 if (!IN_SET(family
, AF_INET
, AF_INET6
))
562 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
563 "Unknown address family %i", family
);
565 if (sz
!= FAMILY_ADDRESS_SIZE(family
))
566 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
567 "Invalid address size");
569 r
= in_addr_to_string(family
, d
, p
);
576 static int map_ntp_message(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
577 NTPStatusInfo
*p
= userdata
;
585 r
= sd_bus_message_enter_container(m
, 'r', "uuuuittayttttbtt");
589 r
= sd_bus_message_read(m
, "uuuuitt",
590 &p
->leap
, &p
->version
, &p
->mode
, &p
->stratum
, &p
->precision
,
591 &p
->root_delay
, &p
->root_dispersion
);
595 r
= sd_bus_message_read_array(m
, 'y', &d
, &sz
);
599 r
= sd_bus_message_read(m
, "ttttbtt",
600 &p
->origin
, &p
->recv
, &p
->trans
, &p
->dest
,
601 &b
, &p
->packet_count
, &p
->jitter
);
605 r
= sd_bus_message_exit_container(m
);
612 memcpy(p
->reference
.str
, d
, sz
);
619 static int show_timesync_status_once(sd_bus
*bus
) {
620 static const struct bus_properties_map map_timesync
[] = {
621 { "ServerName", "s", NULL
, offsetof(NTPStatusInfo
, server_name
) },
622 { "ServerAddress", "(iay)", map_server_address
, offsetof(NTPStatusInfo
, server_address
) },
623 { "PollIntervalUSec", "t", NULL
, offsetof(NTPStatusInfo
, poll_interval
) },
624 { "PollIntervalMinUSec", "t", NULL
, offsetof(NTPStatusInfo
, poll_min
) },
625 { "PollIntervalMaxUSec", "t", NULL
, offsetof(NTPStatusInfo
, poll_max
) },
626 { "RootDistanceMaxUSec", "t", NULL
, offsetof(NTPStatusInfo
, root_distance_max
) },
627 { "NTPMessage", "(uuuuittayttttbtt)", map_ntp_message
, 0 },
628 { "Frequency", "x", NULL
, offsetof(NTPStatusInfo
, freq
) },
631 _cleanup_(ntp_status_info_clear
) NTPStatusInfo info
= {};
632 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
633 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
638 r
= bus_map_all_properties(bus
,
639 "org.freedesktop.timesync1",
640 "/org/freedesktop/timesync1",
642 BUS_MAP_BOOLEAN_AS_BOOL
,
647 return log_error_errno(r
, "Failed to query server: %s", bus_error_message(&error
, r
));
649 if (arg_monitor
&& !terminal_is_dumb())
650 fputs(ANSI_HOME_CLEAR
, stdout
);
652 print_ntp_status_info(&info
);
657 static int on_properties_changed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
663 r
= sd_bus_message_read(m
, "s", &name
);
665 return log_error_errno(r
, "Failed to read interface name: %m");
667 if (!streq_ptr(name
, "org.freedesktop.timesync1.Manager"))
670 return show_timesync_status_once(sd_bus_message_get_bus(m
));
673 static int show_timesync_status(int argc
, char **argv
, void *userdata
) {
674 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
675 sd_bus
*bus
= userdata
;
680 r
= show_timesync_status_once(bus
);
687 r
= sd_event_default(&event
);
689 return log_error_errno(r
, "Failed to get event loop: %m");
691 r
= sd_bus_match_signal(bus
,
693 "org.freedesktop.timesync1",
694 "/org/freedesktop/timesync1",
695 "org.freedesktop.DBus.Properties",
697 on_properties_changed
, NULL
);
699 return log_error_errno(r
, "Failed to request match for PropertiesChanged signal: %m");
701 r
= sd_bus_attach_event(bus
, event
, SD_EVENT_PRIORITY_NORMAL
);
703 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
705 r
= sd_event_loop(event
);
707 return log_error_errno(r
, "Failed to run event loop: %m");
712 static int print_timesync_property(const char *name
, const char *expected_value
, sd_bus_message
*m
, bool value
, bool all
) {
714 const char *contents
;
720 r
= sd_bus_message_peek_type(m
, &type
, &contents
);
726 case SD_BUS_TYPE_STRUCT
:
727 if (streq(name
, "NTPMessage")) {
728 _cleanup_(ntp_status_info_clear
) NTPStatusInfo i
= {};
729 char ts
[FORMAT_TIMESPAN_MAX
], stamp
[FORMAT_TIMESTAMP_MAX
];
731 r
= map_ntp_message(NULL
, NULL
, m
, NULL
, &i
);
735 if (i
.packet_count
== 0)
743 printf("{ Leap=%u, Version=%u, Mode=%u, Stratum=%u, Precision=%i,",
744 i
.leap
, i
.version
, i
.mode
, i
.stratum
, i
.precision
);
745 printf(" RootDelay=%s,",
746 format_timespan(ts
, sizeof(ts
), i
.root_delay
, 0));
747 printf(" RootDispersion=%s,",
748 format_timespan(ts
, sizeof(ts
), i
.root_dispersion
, 0));
751 printf(" Reference=%s,", i
.reference
.str
);
753 printf(" Reference=%" PRIX32
",", be32toh(i
.reference
.val
));
755 printf(" OriginateTimestamp=%s,",
756 format_timestamp(stamp
, sizeof(stamp
), i
.origin
));
757 printf(" ReceiveTimestamp=%s,",
758 format_timestamp(stamp
, sizeof(stamp
), i
.recv
));
759 printf(" TransmitTimestamp=%s,",
760 format_timestamp(stamp
, sizeof(stamp
), i
.trans
));
761 printf(" DestinationTimestamp=%s,",
762 format_timestamp(stamp
, sizeof(stamp
), i
.dest
));
763 printf(" Ignored=%s PacketCount=%" PRIu64
",",
764 yes_no(i
.spike
), i
.packet_count
);
765 printf(" Jitter=%s }\n",
766 format_timespan(ts
, sizeof(ts
), i
.jitter
, 0));
770 } else if (streq(name
, "ServerAddress")) {
771 _cleanup_free_
char *str
= NULL
;
773 r
= map_server_address(NULL
, NULL
, m
, NULL
, &str
);
777 if (arg_all
|| !isempty(str
))
778 bus_print_property_value(name
, expected_value
, value
, str
);
788 static int show_timesync(int argc
, char **argv
, void *userdata
) {
789 sd_bus
*bus
= userdata
;
794 r
= bus_print_all_properties(bus
,
795 "org.freedesktop.timesync1",
796 "/org/freedesktop/timesync1",
797 print_timesync_property
,
803 return bus_log_parse_error(r
);
808 static int parse_ifindex_bus(sd_bus
*bus
, const char *str
) {
809 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
810 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
817 r
= parse_ifindex(str
);
822 r
= bus_call_method(bus
, bus_network_mgr
, "GetLinkByName", &error
, &reply
, "s", str
);
824 return log_error_errno(r
, "Failed to get ifindex of interfaces %s: %s", str
, bus_error_message(&error
, r
));
826 r
= sd_bus_message_read(reply
, "io", &i
, NULL
);
828 return bus_log_create_error(r
);
833 static int verb_ntp_servers(int argc
, char **argv
, void *userdata
) {
834 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
835 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
;
836 sd_bus
*bus
= userdata
;
841 ifindex
= parse_ifindex_bus(bus
, argv
[1]);
845 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
847 r
= bus_message_new_method_call(bus
, &req
, bus_network_mgr
, "SetLinkNTP");
849 return bus_log_create_error(r
);
851 r
= sd_bus_message_append(req
, "i", ifindex
);
853 return bus_log_create_error(r
);
855 r
= sd_bus_message_append_strv(req
, argv
+ 2);
857 return bus_log_create_error(r
);
859 r
= sd_bus_call(bus
, req
, 0, &error
, NULL
);
861 return log_error_errno(r
, "Failed to set NTP servers: %s", bus_error_message(&error
, r
));
866 static int verb_revert(int argc
, char **argv
, void *userdata
) {
867 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
868 sd_bus
*bus
= userdata
;
873 ifindex
= parse_ifindex_bus(bus
, argv
[1]);
877 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
879 r
= bus_call_method(bus
, bus_network_mgr
, "RevertLinkNTP", &error
, NULL
, "i", ifindex
);
881 return log_error_errno(r
, "Failed to revert interface configuration: %s", bus_error_message(&error
, r
));
886 static int help(void) {
887 _cleanup_free_
char *link
= NULL
;
890 r
= terminal_urlify_man("timedatectl", "1", &link
);
894 printf("%s [OPTIONS...] COMMAND ...\n"
895 "\n%sQuery or change system time and date settings.%s\n"
897 " status Show current time settings\n"
898 " show Show properties of systemd-timedated\n"
899 " set-time TIME Set system time\n"
900 " set-timezone ZONE Set system time zone\n"
901 " list-timezones Show known time zones\n"
902 " set-local-rtc BOOL Control whether RTC is in local time\n"
903 " set-ntp BOOL Enable or disable network time synchronization\n"
904 "\nsystemd-timesyncd Commands:\n"
905 " timesync-status Show status of systemd-timesyncd\n"
906 " show-timesync Show properties of systemd-timesyncd\n"
908 " -h --help Show this help message\n"
909 " --version Show package version\n"
910 " --no-pager Do not pipe output into a pager\n"
911 " --no-ask-password Do not prompt for password\n"
912 " -H --host=[USER@]HOST Operate on remote host\n"
913 " -M --machine=CONTAINER Operate on local container\n"
914 " --adjust-system-clock Adjust system clock when changing local RTC mode\n"
915 " --monitor Monitor status of systemd-timesyncd\n"
916 " -p --property=NAME Show only properties by this name\n"
917 " -a --all Show all properties, including empty ones\n"
918 " --value When showing properties, only print the value\n"
919 "\nSee the %s for details.\n"
920 , program_invocation_short_name
929 static int verb_help(int argc
, char **argv
, void *userdata
) {
933 static int parse_argv(int argc
, char *argv
[]) {
938 ARG_ADJUST_SYSTEM_CLOCK
,
944 static const struct option options
[] = {
945 { "help", no_argument
, NULL
, 'h' },
946 { "version", no_argument
, NULL
, ARG_VERSION
},
947 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
948 { "host", required_argument
, NULL
, 'H' },
949 { "machine", required_argument
, NULL
, 'M' },
950 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
951 { "adjust-system-clock", no_argument
, NULL
, ARG_ADJUST_SYSTEM_CLOCK
},
952 { "monitor", no_argument
, NULL
, ARG_MONITOR
},
953 { "property", required_argument
, NULL
, 'p' },
954 { "all", no_argument
, NULL
, 'a' },
955 { "value", no_argument
, NULL
, ARG_VALUE
},
964 while ((c
= getopt_long(argc
, argv
, "hH:M:p:a", options
, NULL
)) >= 0)
975 arg_transport
= BUS_TRANSPORT_REMOTE
;
980 arg_transport
= BUS_TRANSPORT_MACHINE
;
984 case ARG_NO_ASK_PASSWORD
:
985 arg_ask_password
= false;
988 case ARG_ADJUST_SYSTEM_CLOCK
:
989 arg_adjust_system_clock
= true;
993 arg_pager_flags
|= PAGER_DISABLE
;
1001 r
= strv_extend(&arg_property
, optarg
);
1005 /* If the user asked for a particular
1006 * property, show it to him, even if it is
1024 assert_not_reached("Unhandled option");
1030 static int timedatectl_main(sd_bus
*bus
, int argc
, char *argv
[]) {
1031 static const Verb verbs
[] = {
1032 { "status", VERB_ANY
, 1, VERB_DEFAULT
, show_status
},
1033 { "show", VERB_ANY
, 1, 0, show_properties
},
1034 { "set-time", 2, 2, 0, set_time
},
1035 { "set-timezone", 2, 2, 0, set_timezone
},
1036 { "list-timezones", VERB_ANY
, 1, 0, list_timezones
},
1037 { "set-local-rtc", 2, 2, 0, set_local_rtc
},
1038 { "set-ntp", 2, 2, 0, set_ntp
},
1039 { "timesync-status", VERB_ANY
, 1, 0, show_timesync_status
},
1040 { "show-timesync", VERB_ANY
, 1, 0, show_timesync
},
1041 { "ntp-servers", 3, VERB_ANY
, 0, verb_ntp_servers
},
1042 { "revert", 2, 2, 0, verb_revert
},
1043 { "help", VERB_ANY
, VERB_ANY
, 0, verb_help
}, /* Not documented, but supported since it is created. */
1047 return dispatch_verb(argc
, argv
, verbs
, bus
);
1050 static int run(int argc
, char *argv
[]) {
1051 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1054 setlocale(LC_ALL
, "");
1057 r
= parse_argv(argc
, argv
);
1061 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
1063 return log_error_errno(r
, "Failed to create bus connection: %m");
1065 return timedatectl_main(bus
, argc
, argv
);
1068 DEFINE_MAIN_FUNCTION(run
);