1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2012 Lennart Poettering
6 Copyright 2013 Kay Sievers
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "bus-error.h"
32 #include "parse-util.h"
33 #include "spawn-polkit-agent.h"
35 #include "terminal-util.h"
38 static bool arg_no_pager
= false;
39 static bool arg_ask_password
= true;
40 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
41 static char *arg_host
= NULL
;
42 static bool arg_adjust_system_clock
= false;
44 typedef struct StatusInfo
{
56 static void status_info_clear(StatusInfo
*info
) {
63 static void print_status_info(const StatusInfo
*i
) {
67 bool have_time
= false;
68 const char *old_tz
= NULL
, *tz
;
74 /* Save the old $TZ */
80 if (setenv("TZ", isempty(i
->timezone
) ? "UTC" : i
->timezone
, true) < 0)
81 log_warning_errno(errno
, "Failed to set TZ environment variable, ignoring: %m");
86 sec
= (time_t) (i
->time
/ USEC_PER_SEC
);
88 } else if (IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
)) {
92 log_warning("Could not get time from timedated and not operating locally, ignoring.");
95 n
= strftime(a
, sizeof a
, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec
, &tm
));
96 printf(" Local time: %s\n", n
> 0 ? a
: "n/a");
98 n
= strftime(a
, sizeof a
, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec
, &tm
));
99 printf(" Universal time: %s\n", n
> 0 ? a
: "n/a");
101 printf(" Local time: %s\n", "n/a");
102 printf(" Universal time: %s\n", "n/a");
105 if (i
->rtc_time
> 0) {
108 rtc_sec
= (time_t) (i
->rtc_time
/ USEC_PER_SEC
);
109 n
= strftime(a
, sizeof a
, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec
, &tm
));
110 printf(" RTC time: %s\n", n
> 0 ? a
: "n/a");
112 printf(" RTC time: %s\n", "n/a");
115 n
= strftime(a
, sizeof a
, "%Z, %z", localtime_r(&sec
, &tm
));
117 /* Restore the $TZ */
119 r
= setenv("TZ", old_tz
, true);
123 log_warning_errno(errno
, "Failed to set TZ environment variable, ignoring: %m");
127 printf(" Time zone: %s (%s)\n"
128 " System clock synchronized: %s\n"
129 "systemd-timesyncd.service active: %s\n"
130 " RTC in local TZ: %s\n",
131 strna(i
->timezone
), have_time
&& n
> 0 ? a
: "n/a",
132 i
->ntp_capable
? yes_no(i
->ntp_enabled
) : "n/a",
133 yes_no(i
->ntp_synced
),
134 yes_no(i
->rtc_local
));
138 "Warning: The system is configured to read the RTC time in the local time zone.\n"
139 " This mode can not be fully supported. It will create various problems\n"
140 " with time zone changes and daylight saving time adjustments. The RTC\n"
141 " time is never updated, it relies on external facilities to maintain it.\n"
142 " If at all possible, use RTC in UTC by calling\n"
143 " 'timedatectl set-local-rtc 0'.%s\n", ansi_highlight(), ansi_normal());
146 static int show_status(sd_bus
*bus
, char **args
, unsigned n
) {
147 _cleanup_(status_info_clear
) StatusInfo info
= {};
148 static const struct bus_properties_map map
[] = {
149 { "Timezone", "s", NULL
, offsetof(StatusInfo
, timezone
) },
150 { "LocalRTC", "b", NULL
, offsetof(StatusInfo
, rtc_local
) },
151 { "NTP", "b", NULL
, offsetof(StatusInfo
, ntp_enabled
) },
152 { "CanNTP", "b", NULL
, offsetof(StatusInfo
, ntp_capable
) },
153 { "NTPSynchronized", "b", NULL
, offsetof(StatusInfo
, ntp_synced
) },
154 { "TimeUSec", "t", NULL
, offsetof(StatusInfo
, time
) },
155 { "RTCTimeUSec", "t", NULL
, offsetof(StatusInfo
, rtc_time
) },
159 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
164 r
= bus_map_all_properties(bus
,
165 "org.freedesktop.timedate1",
166 "/org/freedesktop/timedate1",
171 return log_error_errno(r
, "Failed to query server: %s", bus_error_message(&error
, r
));
173 print_status_info(&info
);
178 static int set_time(sd_bus
*bus
, char **args
, unsigned n
) {
179 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
180 bool relative
= false, interactive
= arg_ask_password
;
187 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
189 r
= parse_timestamp(args
[1], &t
);
191 log_error("Failed to parse time specification: %s", args
[1]);
195 r
= sd_bus_call_method(bus
,
196 "org.freedesktop.timedate1",
197 "/org/freedesktop/timedate1",
198 "org.freedesktop.timedate1",
202 "xbb", (int64_t)t
, relative
, interactive
);
204 log_error("Failed to set time: %s", bus_error_message(&error
, -r
));
209 static int set_timezone(sd_bus
*bus
, char **args
, unsigned n
) {
210 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
216 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
218 r
= sd_bus_call_method(bus
,
219 "org.freedesktop.timedate1",
220 "/org/freedesktop/timedate1",
221 "org.freedesktop.timedate1",
225 "sb", args
[1], arg_ask_password
);
227 log_error("Failed to set time zone: %s", bus_error_message(&error
, -r
));
232 static int set_local_rtc(sd_bus
*bus
, char **args
, unsigned n
) {
233 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
239 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
241 b
= parse_boolean(args
[1]);
243 log_error("Failed to parse local RTC setting: %s", args
[1]);
247 r
= sd_bus_call_method(bus
,
248 "org.freedesktop.timedate1",
249 "/org/freedesktop/timedate1",
250 "org.freedesktop.timedate1",
254 "bbb", b
, arg_adjust_system_clock
, arg_ask_password
);
256 log_error("Failed to set local RTC: %s", bus_error_message(&error
, -r
));
261 static int set_ntp(sd_bus
*bus
, char **args
, unsigned n
) {
262 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
268 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
270 b
= parse_boolean(args
[1]);
272 log_error("Failed to parse NTP setting: %s", args
[1]);
276 r
= sd_bus_call_method(bus
,
277 "org.freedesktop.timedate1",
278 "/org/freedesktop/timedate1",
279 "org.freedesktop.timedate1",
283 "bb", b
, arg_ask_password
);
285 log_error("Failed to set ntp: %s", bus_error_message(&error
, -r
));
290 static int list_timezones(sd_bus
*bus
, char **args
, unsigned n
) {
291 _cleanup_strv_free_
char **zones
= NULL
;
297 r
= get_timezones(&zones
);
299 return log_error_errno(r
, "Failed to read list of time zones: %m");
301 pager_open(arg_no_pager
, false);
307 static void help(void) {
308 printf("%s [OPTIONS...] COMMAND ...\n\n"
309 "Query or change system time and date settings.\n\n"
310 " -h --help Show this help message\n"
311 " --version Show package version\n"
312 " --no-pager Do not pipe output into a pager\n"
313 " --no-ask-password Do not prompt for password\n"
314 " -H --host=[USER@]HOST Operate on remote host\n"
315 " -M --machine=CONTAINER Operate on local container\n"
316 " --adjust-system-clock Adjust system clock when changing local RTC mode\n\n"
318 " status Show current time settings\n"
319 " set-time TIME Set system time\n"
320 " set-timezone ZONE Set system time zone\n"
321 " list-timezones Show known time zones\n"
322 " set-local-rtc BOOL Control whether RTC is in local time\n"
323 " set-ntp BOOL Enable or disable network time synchronization\n",
324 program_invocation_short_name
);
327 static int parse_argv(int argc
, char *argv
[]) {
332 ARG_ADJUST_SYSTEM_CLOCK
,
336 static const struct option options
[] = {
337 { "help", no_argument
, NULL
, 'h' },
338 { "version", no_argument
, NULL
, ARG_VERSION
},
339 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
340 { "host", required_argument
, NULL
, 'H' },
341 { "machine", required_argument
, NULL
, 'M' },
342 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
343 { "adjust-system-clock", no_argument
, NULL
, ARG_ADJUST_SYSTEM_CLOCK
},
352 while ((c
= getopt_long(argc
, argv
, "hH:M:", options
, NULL
)) >= 0)
364 arg_transport
= BUS_TRANSPORT_REMOTE
;
369 arg_transport
= BUS_TRANSPORT_MACHINE
;
373 case ARG_NO_ASK_PASSWORD
:
374 arg_ask_password
= false;
377 case ARG_ADJUST_SYSTEM_CLOCK
:
378 arg_adjust_system_clock
= true;
389 assert_not_reached("Unhandled option");
395 static int timedatectl_main(sd_bus
*bus
, int argc
, char *argv
[]) {
397 static const struct {
405 int (* const dispatch
)(sd_bus
*bus
, char **args
, unsigned n
);
407 { "status", LESS
, 1, show_status
},
408 { "set-time", EQUAL
, 2, set_time
},
409 { "set-timezone", EQUAL
, 2, set_timezone
},
410 { "list-timezones", EQUAL
, 1, list_timezones
},
411 { "set-local-rtc", EQUAL
, 2, set_local_rtc
},
412 { "set-ntp", EQUAL
, 2, set_ntp
, },
421 left
= argc
- optind
;
424 /* Special rule: no arguments means "status" */
427 if (streq(argv
[optind
], "help")) {
432 for (i
= 0; i
< ELEMENTSOF(verbs
); i
++)
433 if (streq(argv
[optind
], verbs
[i
].verb
))
436 if (i
>= ELEMENTSOF(verbs
)) {
437 log_error("Unknown operation %s", argv
[optind
]);
442 switch (verbs
[i
].argc_cmp
) {
445 if (left
!= verbs
[i
].argc
) {
446 log_error("Invalid number of arguments.");
453 if (left
< verbs
[i
].argc
) {
454 log_error("Too few arguments.");
461 if (left
> verbs
[i
].argc
) {
462 log_error("Too many arguments.");
469 assert_not_reached("Unknown comparison operator.");
472 return verbs
[i
].dispatch(bus
, argv
+ optind
, left
);
475 int main(int argc
, char *argv
[]) {
476 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
479 setlocale(LC_ALL
, "");
480 log_parse_environment();
483 r
= parse_argv(argc
, argv
);
487 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
489 log_error_errno(r
, "Failed to create bus connection: %m");
493 r
= timedatectl_main(bus
, argc
, argv
);
498 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;