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"
37 static bool arg_no_pager = false;
38 static bool arg_ask_password = true;
39 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
40 static char *arg_host = NULL;
41 static bool arg_adjust_system_clock = false;
43 static void pager_open_if_enabled(void) {
51 static void polkit_agent_open_if_enabled(void) {
53 /* Open the polkit agent as a child process if necessary */
54 if (!arg_ask_password)
57 if (arg_transport != BUS_TRANSPORT_LOCAL)
63 typedef struct StatusInfo {
75 static void print_status_info(const StatusInfo *i) {
76 char a[FORMAT_TIMESTAMP_MAX];
79 bool have_time = false;
80 _cleanup_free_ char *zc = NULL, *zn = NULL;
85 /* Enforce the values of /etc/localtime */
87 fprintf(stderr, "Warning: Ignoring the TZ variable.\n\n");
91 r = setenv("TZ", i->timezone, false);
93 log_error_errno(errno, "Failed to set TZ environment variable: %m");
99 sec = (time_t) (i->time / USEC_PER_SEC);
101 } else if (IN_SET(arg_transport, BUS_TRANSPORT_REMOTE, BUS_TRANSPORT_MACHINE)) {
105 fprintf(stderr, "Warning: Could not get time from timedated and not operating locally.\n\n");
108 xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm));
109 printf(" Local time: %.*s\n", (int) sizeof(a), a);
111 xstrftime(a, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm));
112 printf(" Universal time: %.*s\n", (int) sizeof(a), a);
114 printf(" Local time: %s\n", "n/a");
115 printf(" Universal time: %s\n", "n/a");
118 if (i->rtc_time > 0) {
121 rtc_sec = (time_t)(i->rtc_time / USEC_PER_SEC);
122 xstrftime(a, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec, &tm));
123 printf(" RTC time: %.*s\n", (int) sizeof(a), a);
125 printf(" RTC time: %s\n", "n/a");
128 xstrftime(a, "%Z, %z", localtime_r(&sec, &tm));
130 printf(" Time zone: %s (%.*s)\n"
132 "NTP synchronized: %s\n"
133 " RTC in local TZ: %s\n",
134 strna(i->timezone), (int) sizeof(a), have_time ? a : "n/a",
135 i->ntp_capable ? yes_no(i->ntp_enabled) : "n/a",
136 yes_no(i->ntp_synced),
137 yes_no(i->rtc_local));
140 fputs("\n" ANSI_HIGHLIGHT_ON
141 "Warning: The system is configured to read the RTC time in the local time zone. This\n"
142 " mode can not be fully supported. It will create various problems with time\n"
143 " zone changes and daylight saving time adjustments. The RTC time is never updated,\n"
144 " it relies on external facilities to maintain it. If at all possible, use\n"
145 " RTC in UTC by calling 'timedatectl set-local-rtc 0'" ANSI_HIGHLIGHT_OFF ".\n", stdout);
148 static int show_status(sd_bus *bus, char **args, unsigned n) {
149 StatusInfo info = {};
150 static const struct bus_properties_map map[] = {
151 { "Timezone", "s", NULL, offsetof(StatusInfo, timezone) },
152 { "LocalRTC", "b", NULL, offsetof(StatusInfo, rtc_local) },
153 { "NTP", "b", NULL, offsetof(StatusInfo, ntp_enabled) },
154 { "CanNTP", "b", NULL, offsetof(StatusInfo, ntp_capable) },
155 { "NTPSynchronized", "b", NULL, offsetof(StatusInfo, ntp_synced) },
156 { "TimeUSec", "t", NULL, offsetof(StatusInfo, time) },
157 { "RTCTimeUSec", "t", NULL, offsetof(StatusInfo, rtc_time) },
164 r = bus_map_all_properties(bus,
165 "org.freedesktop.timedate1",
166 "/org/freedesktop/timedate1",
170 log_error_errno(r, "Failed to query server: %m");
174 print_status_info(&info);
181 static int set_time(sd_bus *bus, char **args, unsigned n) {
182 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
183 bool relative = false, interactive = arg_ask_password;
190 polkit_agent_open_if_enabled();
192 r = parse_timestamp(args[1], &t);
194 log_error("Failed to parse time specification: %s", args[1]);
198 r = sd_bus_call_method(bus,
199 "org.freedesktop.timedate1",
200 "/org/freedesktop/timedate1",
201 "org.freedesktop.timedate1",
205 "xbb", (int64_t)t, relative, interactive);
207 log_error("Failed to set time: %s", bus_error_message(&error, -r));
212 static int set_timezone(sd_bus *bus, char **args, unsigned n) {
213 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
219 polkit_agent_open_if_enabled();
221 r = sd_bus_call_method(bus,
222 "org.freedesktop.timedate1",
223 "/org/freedesktop/timedate1",
224 "org.freedesktop.timedate1",
228 "sb", args[1], arg_ask_password);
230 log_error("Failed to set time zone: %s", bus_error_message(&error, -r));
235 static int set_local_rtc(sd_bus *bus, char **args, unsigned n) {
236 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
242 polkit_agent_open_if_enabled();
244 b = parse_boolean(args[1]);
246 log_error("Failed to parse local RTC setting: %s", args[1]);
250 r = sd_bus_call_method(bus,
251 "org.freedesktop.timedate1",
252 "/org/freedesktop/timedate1",
253 "org.freedesktop.timedate1",
257 "bbb", b, arg_adjust_system_clock, arg_ask_password);
259 log_error("Failed to set local RTC: %s", bus_error_message(&error, -r));
264 static int set_ntp(sd_bus *bus, char **args, unsigned n) {
265 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
271 polkit_agent_open_if_enabled();
273 b = parse_boolean(args[1]);
275 log_error("Failed to parse NTP setting: %s", args[1]);
279 r = sd_bus_call_method(bus,
280 "org.freedesktop.timedate1",
281 "/org/freedesktop/timedate1",
282 "org.freedesktop.timedate1",
286 "bb", b, arg_ask_password);
288 log_error("Failed to set ntp: %s", bus_error_message(&error, -r));
293 static int list_timezones(sd_bus *bus, char **args, unsigned n) {
294 _cleanup_strv_free_ char **zones = NULL;
300 r = get_timezones(&zones);
302 return log_error_errno(r, "Failed to read list of time zones: %m");
304 pager_open_if_enabled();
310 static void help(void) {
311 printf("%s [OPTIONS...] COMMAND ...\n\n"
312 "Query or change system time and date settings.\n\n"
313 " -h --help Show this help message\n"
314 " --version Show package version\n"
315 " --no-pager Do not pipe output into a pager\n"
316 " --no-ask-password Do not prompt for password\n"
317 " -H --host=[USER@]HOST Operate on remote host\n"
318 " -M --machine=CONTAINER Operate on local container\n"
319 " --adjust-system-clock Adjust system clock when changing local RTC mode\n\n"
321 " status Show current time settings\n"
322 " set-time TIME Set system time\n"
323 " set-timezone ZONE Set system time zone\n"
324 " list-timezones Show known time zones\n"
325 " set-local-rtc BOOL Control whether RTC is in local time\n"
326 " set-ntp BOOL Control whether NTP is enabled\n",
327 program_invocation_short_name);
330 static int parse_argv(int argc, char *argv[]) {
335 ARG_ADJUST_SYSTEM_CLOCK,
339 static const struct option options[] = {
340 { "help", no_argument, NULL, 'h' },
341 { "version", no_argument, NULL, ARG_VERSION },
342 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
343 { "host", required_argument, NULL, 'H' },
344 { "machine", required_argument, NULL, 'M' },
345 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
346 { "adjust-system-clock", no_argument, NULL, ARG_ADJUST_SYSTEM_CLOCK },
355 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
364 puts(PACKAGE_STRING);
365 puts(SYSTEMD_FEATURES);
369 arg_transport = BUS_TRANSPORT_REMOTE;
374 arg_transport = BUS_TRANSPORT_MACHINE;
378 case ARG_NO_ASK_PASSWORD:
379 arg_ask_password = false;
382 case ARG_ADJUST_SYSTEM_CLOCK:
383 arg_adjust_system_clock = true;
394 assert_not_reached("Unhandled option");
400 static int timedatectl_main(sd_bus *bus, int argc, char *argv[]) {
402 static const struct {
410 int (* const dispatch)(sd_bus *bus, char **args, unsigned n);
412 { "status", LESS, 1, show_status },
413 { "set-time", EQUAL, 2, set_time },
414 { "set-timezone", EQUAL, 2, set_timezone },
415 { "list-timezones", EQUAL, 1, list_timezones },
416 { "set-local-rtc", EQUAL, 2, set_local_rtc },
417 { "set-ntp", EQUAL, 2, set_ntp, },
426 left = argc - optind;
429 /* Special rule: no arguments means "status" */
432 if (streq(argv[optind], "help")) {
437 for (i = 0; i < ELEMENTSOF(verbs); i++)
438 if (streq(argv[optind], verbs[i].verb))
441 if (i >= ELEMENTSOF(verbs)) {
442 log_error("Unknown operation %s", argv[optind]);
447 switch (verbs[i].argc_cmp) {
450 if (left != verbs[i].argc) {
451 log_error("Invalid number of arguments.");
458 if (left < verbs[i].argc) {
459 log_error("Too few arguments.");
466 if (left > verbs[i].argc) {
467 log_error("Too many arguments.");
474 assert_not_reached("Unknown comparison operator.");
477 return verbs[i].dispatch(bus, argv + optind, left);
480 int main(int argc, char *argv[]) {
481 _cleanup_bus_close_unref_ sd_bus *bus = NULL;
484 setlocale(LC_ALL, "");
485 log_parse_environment();
488 r = parse_argv(argc, argv);
492 r = bus_open_transport(arg_transport, arg_host, false, &bus);
494 log_error_errno(r, "Failed to create bus connection: %m");
498 r = timedatectl_main(bus, argc, argv);
503 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;