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/>.
29 #include <sys/timex.h>
33 #include "bus-error.h"
35 #include "spawn-polkit-agent.h"
41 #define xstrftime(buf, fmt, tm) assert_se(strftime(buf, sizeof(buf), fmt, tm) > 0)
43 static bool arg_no_pager = false;
44 static bool arg_ask_password = true;
45 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
46 static char *arg_host = NULL;
47 static bool arg_adjust_system_clock = false;
49 static void pager_open_if_enabled(void) {
57 static void polkit_agent_open_if_enabled(void) {
59 /* Open the polkit agent as a child process if necessary */
60 if (!arg_ask_password)
63 if (arg_transport != BUS_TRANSPORT_LOCAL)
69 typedef struct StatusInfo {
81 static const char *jump_str(int delta_minutes, char *s, size_t size) {
82 if (delta_minutes == 60)
83 return "one hour forward";
84 if (delta_minutes == -60)
85 return "one hour backwards";
86 if (delta_minutes < 0) {
87 snprintf(s, size, "%i minutes backwards", -delta_minutes);
90 if (delta_minutes > 0) {
91 snprintf(s, size, "%i minutes forward", delta_minutes);
97 static void print_status_info(const StatusInfo *i) {
98 char a[FORMAT_TIMESTAMP_MAX];
99 char b[FORMAT_TIMESTAMP_MAX];
103 bool have_time = false;
104 _cleanup_free_ char *zc = NULL, *zn = NULL;
107 bool is_dstc = false, is_dstn = false;
112 /* Enforce the values of /etc/localtime */
114 fprintf(stderr, "Warning: Ignoring the TZ variable. Reading the system's time zone setting only.\n\n");
119 sec = (time_t) (i->time / USEC_PER_SEC);
121 } else if (arg_transport == BUS_TRANSPORT_LOCAL) {
125 fprintf(stderr, "Warning: Could not get time from timedated and not operating locally.\n\n");
128 xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm));
129 printf(" Local time: %.*s\n", (int) sizeof(a), a);
131 xstrftime(a, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm));
132 printf(" Universal time: %.*s\n", (int) sizeof(a), a);
134 printf(" Local time: %s\n", "n/a");
135 printf(" Universal time: %s\n", "n/a");
138 if (i->rtc_time > 0) {
141 rtc_sec = (time_t)(i->rtc_time / USEC_PER_SEC);
142 xstrftime(a, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec, &tm));
143 printf(" RTC time: %.*s\n", (int) sizeof(a), a);
145 printf(" RTC time: %s\n", "n/a");
148 xstrftime(a, "%Z, %z", localtime_r(&sec, &tm));
150 printf(" Time zone: %s (%.*s)\n"
152 "NTP synchronized: %s\n"
153 " RTC in local TZ: %s\n",
154 strna(i->timezone), (int) sizeof(a), have_time ? a : "n/a",
155 i->ntp_capable ? yes_no(i->ntp_enabled) : "n/a",
156 yes_no(i->ntp_synced),
157 yes_no(i->rtc_local));
160 r = time_get_dst(sec, "/etc/localtime",
162 &tn, &dn, &zn, &is_dstn);
164 printf(" DST active: %s\n", "n/a");
166 printf(" DST active: %s\n", yes_no(is_dstc));
169 xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm));
171 xstrftime(b, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tc, &tm));
172 printf(" Last DST change: DST %s at\n"
175 is_dstc ? "began" : "ended",
180 xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm));
181 xstrftime(b, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tn, &tm));
182 printf(" Next DST change: DST %s (the clock jumps %s) at\n"
185 is_dstn ? "begins" : "ends",
186 jump_str(dn, s, sizeof(s)),
191 printf(" DST active: %s\n", yes_no(is_dstc));
194 fputs("\n" ANSI_HIGHLIGHT_ON
195 "Warning: The system is configured to read the RTC time in the local time zone. This\n"
196 " mode can not be fully supported. It will create various problems with time\n"
197 " zone changes and daylight saving time adjustments. The RTC time is never updated,\n"
198 " it relies on external facilities to maintain it. If at all possible, use\n"
199 " RTC in UTC by calling 'timedatectl set-local-rtc 0'" ANSI_HIGHLIGHT_OFF ".\n", stdout);
202 static int show_status(sd_bus *bus, char **args, unsigned n) {
203 StatusInfo info = {};
204 static const struct bus_properties_map map[] = {
205 { "Timezone", "s", NULL, offsetof(StatusInfo, timezone) },
206 { "LocalRTC", "b", NULL, offsetof(StatusInfo, rtc_local) },
207 { "NTP", "b", NULL, offsetof(StatusInfo, ntp_enabled) },
208 { "CanNTP", "b", NULL, offsetof(StatusInfo, ntp_capable) },
209 { "NTPSynchronized", "b", NULL, offsetof(StatusInfo, ntp_synced) },
210 { "TimeUSec", "t", NULL, offsetof(StatusInfo, time) },
211 { "RTCTimeUSec", "t", NULL, offsetof(StatusInfo, rtc_time) },
218 r = bus_map_all_properties(bus,
219 "org.freedesktop.timedate1",
220 "/org/freedesktop/timedate1",
224 log_error_errno(r, "Failed to query server: %m");
228 print_status_info(&info);
235 static int set_time(sd_bus *bus, char **args, unsigned n) {
236 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
237 bool relative = false, interactive = arg_ask_password;
244 polkit_agent_open_if_enabled();
246 r = parse_timestamp(args[1], &t);
248 log_error("Failed to parse time specification: %s", args[1]);
252 r = sd_bus_call_method(bus,
253 "org.freedesktop.timedate1",
254 "/org/freedesktop/timedate1",
255 "org.freedesktop.timedate1",
259 "xbb", (int64_t)t, relative, interactive);
261 log_error("Failed to set time: %s", bus_error_message(&error, -r));
266 static int set_timezone(sd_bus *bus, char **args, unsigned n) {
267 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
273 polkit_agent_open_if_enabled();
275 r = sd_bus_call_method(bus,
276 "org.freedesktop.timedate1",
277 "/org/freedesktop/timedate1",
278 "org.freedesktop.timedate1",
282 "sb", args[1], arg_ask_password);
284 log_error("Failed to set time zone: %s", bus_error_message(&error, -r));
289 static int set_local_rtc(sd_bus *bus, char **args, unsigned n) {
290 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
296 polkit_agent_open_if_enabled();
298 b = parse_boolean(args[1]);
300 log_error("Failed to parse local RTC setting: %s", args[1]);
304 r = sd_bus_call_method(bus,
305 "org.freedesktop.timedate1",
306 "/org/freedesktop/timedate1",
307 "org.freedesktop.timedate1",
311 "bbb", b, arg_adjust_system_clock, arg_ask_password);
313 log_error("Failed to set local RTC: %s", bus_error_message(&error, -r));
318 static int set_ntp(sd_bus *bus, char **args, unsigned n) {
319 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
325 polkit_agent_open_if_enabled();
327 b = parse_boolean(args[1]);
329 log_error("Failed to parse NTP setting: %s", args[1]);
333 r = sd_bus_call_method(bus,
334 "org.freedesktop.timedate1",
335 "/org/freedesktop/timedate1",
336 "org.freedesktop.timedate1",
340 "bb", b, arg_ask_password);
342 log_error("Failed to set ntp: %s", bus_error_message(&error, -r));
347 static int list_timezones(sd_bus *bus, char **args, unsigned n) {
348 _cleanup_strv_free_ char **zones = NULL;
354 r = get_timezones(&zones);
356 return log_error_errno(r, "Failed to read list of time zones: %m");
358 pager_open_if_enabled();
364 static void help(void) {
365 printf("%s [OPTIONS...] COMMAND ...\n\n"
366 "Query or change system time and date settings.\n\n"
367 " -h --help Show this help message\n"
368 " --version Show package version\n"
369 " --no-pager Do not pipe output into a pager\n"
370 " --no-ask-password Do not prompt for password\n"
371 " -H --host=[USER@]HOST Operate on remote host\n"
372 " -M --machine=CONTAINER Operate on local container\n"
373 " --adjust-system-clock Adjust system clock when changing local RTC mode\n\n"
375 " status Show current time settings\n"
376 " set-time TIME Set system time\n"
377 " set-timezone ZONE Set system time zone\n"
378 " list-timezones Show known time zones\n"
379 " set-local-rtc BOOL Control whether RTC is in local time\n"
380 " set-ntp BOOL Control whether NTP is enabled\n",
381 program_invocation_short_name);
384 static int parse_argv(int argc, char *argv[]) {
389 ARG_ADJUST_SYSTEM_CLOCK,
393 static const struct option options[] = {
394 { "help", no_argument, NULL, 'h' },
395 { "version", no_argument, NULL, ARG_VERSION },
396 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
397 { "host", required_argument, NULL, 'H' },
398 { "machine", required_argument, NULL, 'M' },
399 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
400 { "adjust-system-clock", no_argument, NULL, ARG_ADJUST_SYSTEM_CLOCK },
409 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
418 puts(PACKAGE_STRING);
419 puts(SYSTEMD_FEATURES);
423 arg_transport = BUS_TRANSPORT_REMOTE;
428 arg_transport = BUS_TRANSPORT_MACHINE;
432 case ARG_NO_ASK_PASSWORD:
433 arg_ask_password = false;
436 case ARG_ADJUST_SYSTEM_CLOCK:
437 arg_adjust_system_clock = true;
448 assert_not_reached("Unhandled option");
454 static int timedatectl_main(sd_bus *bus, int argc, char *argv[]) {
456 static const struct {
464 int (* const dispatch)(sd_bus *bus, char **args, unsigned n);
466 { "status", LESS, 1, show_status },
467 { "set-time", EQUAL, 2, set_time },
468 { "set-timezone", EQUAL, 2, set_timezone },
469 { "list-timezones", EQUAL, 1, list_timezones },
470 { "set-local-rtc", EQUAL, 2, set_local_rtc },
471 { "set-ntp", EQUAL, 2, set_ntp, },
480 left = argc - optind;
483 /* Special rule: no arguments means "status" */
486 if (streq(argv[optind], "help")) {
491 for (i = 0; i < ELEMENTSOF(verbs); i++)
492 if (streq(argv[optind], verbs[i].verb))
495 if (i >= ELEMENTSOF(verbs)) {
496 log_error("Unknown operation %s", argv[optind]);
501 switch (verbs[i].argc_cmp) {
504 if (left != verbs[i].argc) {
505 log_error("Invalid number of arguments.");
512 if (left < verbs[i].argc) {
513 log_error("Too few arguments.");
520 if (left > verbs[i].argc) {
521 log_error("Too many arguments.");
528 assert_not_reached("Unknown comparison operator.");
531 return verbs[i].dispatch(bus, argv + optind, left);
534 int main(int argc, char *argv[]) {
535 _cleanup_bus_close_unref_ sd_bus *bus = NULL;
538 setlocale(LC_ALL, "");
539 log_parse_environment();
542 r = parse_argv(argc, argv);
546 r = bus_open_transport(arg_transport, arg_host, false, &bus);
548 log_error_errno(r, "Failed to create bus connection: %m");
552 r = timedatectl_main(bus, argc, argv);
557 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;