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. Reading the system's time zone setting only.\n\n");
114 sec = (time_t) (i->time / USEC_PER_SEC);
116 } else if (arg_transport == BUS_TRANSPORT_LOCAL) {
120 fprintf(stderr, "Warning: Could not get time from timedated and not operating locally.\n\n");
123 xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm));
124 printf(" Local time: %.*s\n", (int) sizeof(a), a);
126 xstrftime(a, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm));
127 printf(" Universal time: %.*s\n", (int) sizeof(a), a);
129 printf(" Local time: %s\n", "n/a");
130 printf(" Universal time: %s\n", "n/a");
133 if (i->rtc_time > 0) {
136 rtc_sec = (time_t)(i->rtc_time / USEC_PER_SEC);
137 xstrftime(a, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec, &tm));
138 printf(" RTC time: %.*s\n", (int) sizeof(a), a);
140 printf(" RTC time: %s\n", "n/a");
143 xstrftime(a, "%Z, %z", localtime_r(&sec, &tm));
145 printf(" Time zone: %s (%.*s)\n"
147 "NTP synchronized: %s\n"
148 " RTC in local TZ: %s\n",
149 strna(i->timezone), (int) sizeof(a), have_time ? a : "n/a",
150 i->ntp_capable ? yes_no(i->ntp_enabled) : "n/a",
151 yes_no(i->ntp_synced),
152 yes_no(i->rtc_local));
155 r = time_get_dst(sec, "/etc/localtime",
157 &tn, &dn, &zn, &is_dstn);
159 printf(" DST active: %s\n", "n/a");
161 printf(" DST active: %s\n", yes_no(is_dstc));
164 xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm));
166 xstrftime(b, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tc, &tm));
167 printf(" Last DST change: DST %s at\n"
170 is_dstc ? "began" : "ended",
175 xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm));
176 xstrftime(b, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tn, &tm));
177 printf(" Next DST change: DST %s (the clock jumps %s) at\n"
180 is_dstn ? "begins" : "ends",
181 jump_str(dn, s, sizeof(s)),
186 printf(" DST active: %s\n", yes_no(is_dstc));
189 fputs("\n" ANSI_HIGHLIGHT_ON
190 "Warning: The system is configured to read the RTC time in the local time zone. This\n"
191 " mode can not be fully supported. It will create various problems with time\n"
192 " zone changes and daylight saving time adjustments. The RTC time is never updated,\n"
193 " it relies on external facilities to maintain it. If at all possible, use\n"
194 " RTC in UTC by calling 'timedatectl set-local-rtc 0'" ANSI_HIGHLIGHT_OFF ".\n", stdout);
197 static int show_status(sd_bus *bus, char **args, unsigned n) {
198 StatusInfo info = {};
199 static const struct bus_properties_map map[] = {
200 { "Timezone", "s", NULL, offsetof(StatusInfo, timezone) },
201 { "LocalRTC", "b", NULL, offsetof(StatusInfo, rtc_local) },
202 { "NTP", "b", NULL, offsetof(StatusInfo, ntp_enabled) },
203 { "CanNTP", "b", NULL, offsetof(StatusInfo, ntp_capable) },
204 { "NTPSynchronized", "b", NULL, offsetof(StatusInfo, ntp_synced) },
205 { "TimeUSec", "t", NULL, offsetof(StatusInfo, time) },
206 { "RTCTimeUSec", "t", NULL, offsetof(StatusInfo, rtc_time) },
213 r = bus_map_all_properties(bus,
214 "org.freedesktop.timedate1",
215 "/org/freedesktop/timedate1",
219 log_error_errno(r, "Failed to query server: %m");
223 print_status_info(&info);
230 static int set_time(sd_bus *bus, char **args, unsigned n) {
231 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
232 bool relative = false, interactive = arg_ask_password;
239 polkit_agent_open_if_enabled();
241 r = parse_timestamp(args[1], &t);
243 log_error("Failed to parse time specification: %s", args[1]);
247 r = sd_bus_call_method(bus,
248 "org.freedesktop.timedate1",
249 "/org/freedesktop/timedate1",
250 "org.freedesktop.timedate1",
254 "xbb", (int64_t)t, relative, interactive);
256 log_error("Failed to set time: %s", bus_error_message(&error, -r));
261 static int set_timezone(sd_bus *bus, char **args, unsigned n) {
262 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
268 polkit_agent_open_if_enabled();
270 r = sd_bus_call_method(bus,
271 "org.freedesktop.timedate1",
272 "/org/freedesktop/timedate1",
273 "org.freedesktop.timedate1",
277 "sb", args[1], arg_ask_password);
279 log_error("Failed to set time zone: %s", bus_error_message(&error, -r));
284 static int set_local_rtc(sd_bus *bus, char **args, unsigned n) {
285 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
291 polkit_agent_open_if_enabled();
293 b = parse_boolean(args[1]);
295 log_error("Failed to parse local RTC setting: %s", args[1]);
299 r = sd_bus_call_method(bus,
300 "org.freedesktop.timedate1",
301 "/org/freedesktop/timedate1",
302 "org.freedesktop.timedate1",
306 "bbb", b, arg_adjust_system_clock, arg_ask_password);
308 log_error("Failed to set local RTC: %s", bus_error_message(&error, -r));
313 static int set_ntp(sd_bus *bus, char **args, unsigned n) {
314 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
320 polkit_agent_open_if_enabled();
322 b = parse_boolean(args[1]);
324 log_error("Failed to parse NTP setting: %s", args[1]);
328 r = sd_bus_call_method(bus,
329 "org.freedesktop.timedate1",
330 "/org/freedesktop/timedate1",
331 "org.freedesktop.timedate1",
335 "bb", b, arg_ask_password);
337 log_error("Failed to set ntp: %s", bus_error_message(&error, -r));
342 static int list_timezones(sd_bus *bus, char **args, unsigned n) {
343 _cleanup_strv_free_ char **zones = NULL;
349 r = get_timezones(&zones);
351 return log_error_errno(r, "Failed to read list of time zones: %m");
353 pager_open_if_enabled();
359 static void help(void) {
360 printf("%s [OPTIONS...] COMMAND ...\n\n"
361 "Query or change system time and date settings.\n\n"
362 " -h --help Show this help message\n"
363 " --version Show package version\n"
364 " --no-pager Do not pipe output into a pager\n"
365 " --no-ask-password Do not prompt for password\n"
366 " -H --host=[USER@]HOST Operate on remote host\n"
367 " -M --machine=CONTAINER Operate on local container\n"
368 " --adjust-system-clock Adjust system clock when changing local RTC mode\n\n"
370 " status Show current time settings\n"
371 " set-time TIME Set system time\n"
372 " set-timezone ZONE Set system time zone\n"
373 " list-timezones Show known time zones\n"
374 " set-local-rtc BOOL Control whether RTC is in local time\n"
375 " set-ntp BOOL Control whether NTP is enabled\n",
376 program_invocation_short_name);
379 static int parse_argv(int argc, char *argv[]) {
384 ARG_ADJUST_SYSTEM_CLOCK,
388 static const struct option options[] = {
389 { "help", no_argument, NULL, 'h' },
390 { "version", no_argument, NULL, ARG_VERSION },
391 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
392 { "host", required_argument, NULL, 'H' },
393 { "machine", required_argument, NULL, 'M' },
394 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
395 { "adjust-system-clock", no_argument, NULL, ARG_ADJUST_SYSTEM_CLOCK },
404 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
413 puts(PACKAGE_STRING);
414 puts(SYSTEMD_FEATURES);
418 arg_transport = BUS_TRANSPORT_REMOTE;
423 arg_transport = BUS_TRANSPORT_MACHINE;
427 case ARG_NO_ASK_PASSWORD:
428 arg_ask_password = false;
431 case ARG_ADJUST_SYSTEM_CLOCK:
432 arg_adjust_system_clock = true;
443 assert_not_reached("Unhandled option");
449 static int timedatectl_main(sd_bus *bus, int argc, char *argv[]) {
451 static const struct {
459 int (* const dispatch)(sd_bus *bus, char **args, unsigned n);
461 { "status", LESS, 1, show_status },
462 { "set-time", EQUAL, 2, set_time },
463 { "set-timezone", EQUAL, 2, set_timezone },
464 { "list-timezones", EQUAL, 1, list_timezones },
465 { "set-local-rtc", EQUAL, 2, set_local_rtc },
466 { "set-ntp", EQUAL, 2, set_ntp, },
475 left = argc - optind;
478 /* Special rule: no arguments means "status" */
481 if (streq(argv[optind], "help")) {
486 for (i = 0; i < ELEMENTSOF(verbs); i++)
487 if (streq(argv[optind], verbs[i].verb))
490 if (i >= ELEMENTSOF(verbs)) {
491 log_error("Unknown operation %s", argv[optind]);
496 switch (verbs[i].argc_cmp) {
499 if (left != verbs[i].argc) {
500 log_error("Invalid number of arguments.");
507 if (left < verbs[i].argc) {
508 log_error("Too few arguments.");
515 if (left > verbs[i].argc) {
516 log_error("Too many arguments.");
523 assert_not_reached("Unknown comparison operator.");
526 return verbs[i].dispatch(bus, argv + optind, left);
529 int main(int argc, char *argv[]) {
530 _cleanup_bus_close_unref_ sd_bus *bus = NULL;
533 setlocale(LC_ALL, "");
534 log_parse_environment();
537 r = parse_argv(argc, argv);
541 r = bus_open_transport(arg_transport, arg_host, false, &bus);
543 log_error_errno(r, "Failed to create bus connection: %m");
547 r = timedatectl_main(bus, argc, argv);
552 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;