1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Lennart Poettering
7 Copyright 2013 Kay Sievers
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
30 #include "bus-error.h"
32 #include "spawn-polkit-agent.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 static void pager_open_if_enabled(void) {
52 static void polkit_agent_open_if_enabled(void) {
54 /* Open the polkit agent as a child process if necessary */
55 if (!arg_ask_password)
58 if (arg_transport != BUS_TRANSPORT_LOCAL)
64 typedef struct StatusInfo {
76 static const char *jump_str(int delta_minutes, char *s, size_t size) {
77 if (delta_minutes == 60)
78 return "one hour forward";
79 if (delta_minutes == -60)
80 return "one hour backwards";
81 if (delta_minutes < 0) {
82 snprintf(s, size, "%i minutes backwards", -delta_minutes);
85 if (delta_minutes > 0) {
86 snprintf(s, size, "%i minutes forward", delta_minutes);
92 static void print_status_info(const StatusInfo *i) {
93 char a[FORMAT_TIMESTAMP_MAX];
94 char b[FORMAT_TIMESTAMP_MAX];
98 bool have_time = false;
99 _cleanup_free_ char *zc = NULL, *zn = NULL;
102 bool is_dstc = false, is_dstn = false;
107 /* Enforce the values of /etc/localtime */
109 fprintf(stderr, "Warning: Ignoring the TZ variable.\n\n");
113 r = setenv("TZ", i->timezone, false);
115 log_error_errno(errno, "Failed to set TZ environment variable: %m");
121 sec = (time_t) (i->time / USEC_PER_SEC);
123 } else if (IN_SET(arg_transport, BUS_TRANSPORT_REMOTE, BUS_TRANSPORT_MACHINE)) {
127 fprintf(stderr, "Warning: Could not get time from timedated and not operating locally.\n\n");
130 xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm));
131 printf(" Local time: %.*s\n", (int) sizeof(a), a);
133 xstrftime(a, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm));
134 printf(" Universal time: %.*s\n", (int) sizeof(a), a);
136 printf(" Local time: %s\n", "n/a");
137 printf(" Universal time: %s\n", "n/a");
140 if (i->rtc_time > 0) {
143 rtc_sec = (time_t)(i->rtc_time / USEC_PER_SEC);
144 xstrftime(a, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec, &tm));
145 printf(" RTC time: %.*s\n", (int) sizeof(a), a);
147 printf(" RTC time: %s\n", "n/a");
150 xstrftime(a, "%Z, %z", localtime_r(&sec, &tm));
152 printf(" Time zone: %s (%.*s)\n"
154 "NTP synchronized: %s\n"
155 " RTC in local TZ: %s\n",
156 strna(i->timezone), (int) sizeof(a), have_time ? a : "n/a",
157 i->ntp_capable ? yes_no(i->ntp_enabled) : "n/a",
158 yes_no(i->ntp_synced),
159 yes_no(i->rtc_local));
162 r = time_get_dst(sec, "/etc/localtime",
164 &tn, &dn, &zn, &is_dstn);
166 printf(" DST active: %s\n", "n/a");
168 printf(" DST active: %s\n", yes_no(is_dstc));
171 xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm));
173 xstrftime(b, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tc, &tm));
174 printf(" Last DST change: DST %s at\n"
177 is_dstc ? "began" : "ended",
182 xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm));
183 xstrftime(b, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tn, &tm));
184 printf(" Next DST change: DST %s (the clock jumps %s) at\n"
187 is_dstn ? "begins" : "ends",
188 jump_str(dn, s, sizeof(s)),
193 printf(" DST active: %s\n", yes_no(is_dstc));
196 fputs("\n" ANSI_HIGHLIGHT_ON
197 "Warning: The system is configured to read the RTC time in the local time zone. This\n"
198 " mode can not be fully supported. It will create various problems with time\n"
199 " zone changes and daylight saving time adjustments. The RTC time is never updated,\n"
200 " it relies on external facilities to maintain it. If at all possible, use\n"
201 " RTC in UTC by calling 'timedatectl set-local-rtc 0'" ANSI_HIGHLIGHT_OFF ".\n", stdout);
204 static int show_status(sd_bus *bus, char **args, unsigned n) {
205 StatusInfo info = {};
206 static const struct bus_properties_map map[] = {
207 { "Timezone", "s", NULL, offsetof(StatusInfo, timezone) },
208 { "LocalRTC", "b", NULL, offsetof(StatusInfo, rtc_local) },
209 { "NTP", "b", NULL, offsetof(StatusInfo, ntp_enabled) },
210 { "CanNTP", "b", NULL, offsetof(StatusInfo, ntp_capable) },
211 { "NTPSynchronized", "b", NULL, offsetof(StatusInfo, ntp_synced) },
212 { "TimeUSec", "t", NULL, offsetof(StatusInfo, time) },
213 { "RTCTimeUSec", "t", NULL, offsetof(StatusInfo, rtc_time) },
220 r = bus_map_all_properties(bus,
221 "org.freedesktop.timedate1",
222 "/org/freedesktop/timedate1",
226 log_error_errno(r, "Failed to query server: %m");
230 print_status_info(&info);
237 static int set_time(sd_bus *bus, char **args, unsigned n) {
238 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
239 bool relative = false, interactive = arg_ask_password;
246 polkit_agent_open_if_enabled();
248 r = parse_timestamp(args[1], &t);
250 log_error("Failed to parse time specification: %s", args[1]);
254 r = sd_bus_call_method(bus,
255 "org.freedesktop.timedate1",
256 "/org/freedesktop/timedate1",
257 "org.freedesktop.timedate1",
261 "xbb", (int64_t)t, relative, interactive);
263 log_error("Failed to set time: %s", bus_error_message(&error, -r));
268 static int set_timezone(sd_bus *bus, char **args, unsigned n) {
269 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
275 polkit_agent_open_if_enabled();
277 r = sd_bus_call_method(bus,
278 "org.freedesktop.timedate1",
279 "/org/freedesktop/timedate1",
280 "org.freedesktop.timedate1",
284 "sb", args[1], arg_ask_password);
286 log_error("Failed to set time zone: %s", bus_error_message(&error, -r));
291 static int set_local_rtc(sd_bus *bus, char **args, unsigned n) {
292 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
298 polkit_agent_open_if_enabled();
300 b = parse_boolean(args[1]);
302 log_error("Failed to parse local RTC setting: %s", args[1]);
306 r = sd_bus_call_method(bus,
307 "org.freedesktop.timedate1",
308 "/org/freedesktop/timedate1",
309 "org.freedesktop.timedate1",
313 "bbb", b, arg_adjust_system_clock, arg_ask_password);
315 log_error("Failed to set local RTC: %s", bus_error_message(&error, -r));
320 static int set_ntp(sd_bus *bus, char **args, unsigned n) {
321 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
327 polkit_agent_open_if_enabled();
329 b = parse_boolean(args[1]);
331 log_error("Failed to parse NTP setting: %s", args[1]);
335 r = sd_bus_call_method(bus,
336 "org.freedesktop.timedate1",
337 "/org/freedesktop/timedate1",
338 "org.freedesktop.timedate1",
342 "bb", b, arg_ask_password);
344 log_error("Failed to set ntp: %s", bus_error_message(&error, -r));
349 static int list_timezones(sd_bus *bus, char **args, unsigned n) {
350 _cleanup_strv_free_ char **zones = NULL;
356 r = get_timezones(&zones);
358 return log_error_errno(r, "Failed to read list of time zones: %m");
360 pager_open_if_enabled();
366 static void help(void) {
367 printf("%s [OPTIONS...] COMMAND ...\n\n"
368 "Query or change system time and date settings.\n\n"
369 " -h --help Show this help message\n"
370 " --version Show package version\n"
371 " --no-pager Do not pipe output into a pager\n"
372 " --no-ask-password Do not prompt for password\n"
373 " -H --host=[USER@]HOST Operate on remote host\n"
374 " -M --machine=CONTAINER Operate on local container\n"
375 " --adjust-system-clock Adjust system clock when changing local RTC mode\n\n"
377 " status Show current time settings\n"
378 " set-time TIME Set system time\n"
379 " set-timezone ZONE Set system time zone\n"
380 " list-timezones Show known time zones\n"
381 " set-local-rtc BOOL Control whether RTC is in local time\n"
382 " set-ntp BOOL Control whether NTP is enabled\n",
383 program_invocation_short_name);
386 static int parse_argv(int argc, char *argv[]) {
391 ARG_ADJUST_SYSTEM_CLOCK,
395 static const struct option options[] = {
396 { "help", no_argument, NULL, 'h' },
397 { "version", no_argument, NULL, ARG_VERSION },
398 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
399 { "host", required_argument, NULL, 'H' },
400 { "machine", required_argument, NULL, 'M' },
401 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
402 { "adjust-system-clock", no_argument, NULL, ARG_ADJUST_SYSTEM_CLOCK },
411 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
420 puts(PACKAGE_STRING);
421 puts(SYSTEMD_FEATURES);
425 arg_transport = BUS_TRANSPORT_REMOTE;
430 arg_transport = BUS_TRANSPORT_MACHINE;
434 case ARG_NO_ASK_PASSWORD:
435 arg_ask_password = false;
438 case ARG_ADJUST_SYSTEM_CLOCK:
439 arg_adjust_system_clock = true;
450 assert_not_reached("Unhandled option");
456 static int timedatectl_main(sd_bus *bus, int argc, char *argv[]) {
458 static const struct {
466 int (* const dispatch)(sd_bus *bus, char **args, unsigned n);
468 { "status", LESS, 1, show_status },
469 { "set-time", EQUAL, 2, set_time },
470 { "set-timezone", EQUAL, 2, set_timezone },
471 { "list-timezones", EQUAL, 1, list_timezones },
472 { "set-local-rtc", EQUAL, 2, set_local_rtc },
473 { "set-ntp", EQUAL, 2, set_ntp, },
482 left = argc - optind;
485 /* Special rule: no arguments means "status" */
488 if (streq(argv[optind], "help")) {
493 for (i = 0; i < ELEMENTSOF(verbs); i++)
494 if (streq(argv[optind], verbs[i].verb))
497 if (i >= ELEMENTSOF(verbs)) {
498 log_error("Unknown operation %s", argv[optind]);
503 switch (verbs[i].argc_cmp) {
506 if (left != verbs[i].argc) {
507 log_error("Invalid number of arguments.");
514 if (left < verbs[i].argc) {
515 log_error("Too few arguments.");
522 if (left > verbs[i].argc) {
523 log_error("Too many arguments.");
530 assert_not_reached("Unknown comparison operator.");
533 return verbs[i].dispatch(bus, argv + optind, left);
536 int main(int argc, char *argv[]) {
537 _cleanup_bus_close_unref_ sd_bus *bus = NULL;
540 setlocale(LC_ALL, "");
541 log_parse_environment();
544 r = parse_argv(argc, argv);
548 r = bus_open_transport(arg_transport, arg_host, false, &bus);
550 log_error_errno(r, "Failed to create bus connection: %m");
554 r = timedatectl_main(bus, argc, argv);
559 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;