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 const char *old_tz = NULL, *tz;
85 /* Save the old $TZ */
91 if (setenv("TZ", i->timezone, true) < 0)
92 log_warning_errno(errno, "Failed to set TZ environment variable, ignoring: %m");
97 sec = (time_t) (i->time / USEC_PER_SEC);
99 } else if (IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE)) {
103 log_warning("Could not get time from timedated and not operating locally, ignoring.");
106 xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm));
107 printf(" Local time: %.*s\n", (int) sizeof(a), a);
109 xstrftime(a, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm));
110 printf(" Universal time: %.*s\n", (int) sizeof(a), a);
112 printf(" Local time: %s\n", "n/a");
113 printf(" Universal time: %s\n", "n/a");
116 if (i->rtc_time > 0) {
119 rtc_sec = (time_t) (i->rtc_time / USEC_PER_SEC);
120 xstrftime(a, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec, &tm));
121 printf(" RTC time: %.*s\n", (int) sizeof(a), a);
123 printf(" RTC time: %s\n", "n/a");
126 xstrftime(a, "%Z, %z", localtime_r(&sec, &tm));
128 /* Restore the $TZ */
130 r = setenv("TZ", old_tz, true);
134 log_warning_errno(errno, "Failed to set TZ environment variable, ignoring: %m");
138 printf(" Time zone: %s (%.*s)\n"
140 "NTP synchronized: %s\n"
141 " RTC in local TZ: %s\n",
142 strna(i->timezone), (int) sizeof(a), have_time ? a : "n/a",
143 i->ntp_capable ? yes_no(i->ntp_enabled) : "n/a",
144 yes_no(i->ntp_synced),
145 yes_no(i->rtc_local));
148 fputs("\n" ANSI_HIGHLIGHT_ON
149 "Warning: The system is configured to read the RTC time in the local time zone. This\n"
150 " mode can not be fully supported. It will create various problems with time\n"
151 " zone changes and daylight saving time adjustments. The RTC time is never updated,\n"
152 " it relies on external facilities to maintain it. If at all possible, use\n"
153 " RTC in UTC by calling 'timedatectl set-local-rtc 0'" ANSI_HIGHLIGHT_OFF ".\n", stdout);
156 static int show_status(sd_bus *bus, char **args, unsigned n) {
157 StatusInfo info = {};
158 static const struct bus_properties_map map[] = {
159 { "Timezone", "s", NULL, offsetof(StatusInfo, timezone) },
160 { "LocalRTC", "b", NULL, offsetof(StatusInfo, rtc_local) },
161 { "NTP", "b", NULL, offsetof(StatusInfo, ntp_enabled) },
162 { "CanNTP", "b", NULL, offsetof(StatusInfo, ntp_capable) },
163 { "NTPSynchronized", "b", NULL, offsetof(StatusInfo, ntp_synced) },
164 { "TimeUSec", "t", NULL, offsetof(StatusInfo, time) },
165 { "RTCTimeUSec", "t", NULL, offsetof(StatusInfo, rtc_time) },
172 r = bus_map_all_properties(bus,
173 "org.freedesktop.timedate1",
174 "/org/freedesktop/timedate1",
178 log_error_errno(r, "Failed to query server: %m");
182 print_status_info(&info);
189 static int set_time(sd_bus *bus, char **args, unsigned n) {
190 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
191 bool relative = false, interactive = arg_ask_password;
198 polkit_agent_open_if_enabled();
200 r = parse_timestamp(args[1], &t);
202 log_error("Failed to parse time specification: %s", args[1]);
206 r = sd_bus_call_method(bus,
207 "org.freedesktop.timedate1",
208 "/org/freedesktop/timedate1",
209 "org.freedesktop.timedate1",
213 "xbb", (int64_t)t, relative, interactive);
215 log_error("Failed to set time: %s", bus_error_message(&error, -r));
220 static int set_timezone(sd_bus *bus, char **args, unsigned n) {
221 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
227 polkit_agent_open_if_enabled();
229 r = sd_bus_call_method(bus,
230 "org.freedesktop.timedate1",
231 "/org/freedesktop/timedate1",
232 "org.freedesktop.timedate1",
236 "sb", args[1], arg_ask_password);
238 log_error("Failed to set time zone: %s", bus_error_message(&error, -r));
243 static int set_local_rtc(sd_bus *bus, char **args, unsigned n) {
244 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
250 polkit_agent_open_if_enabled();
252 b = parse_boolean(args[1]);
254 log_error("Failed to parse local RTC setting: %s", args[1]);
258 r = sd_bus_call_method(bus,
259 "org.freedesktop.timedate1",
260 "/org/freedesktop/timedate1",
261 "org.freedesktop.timedate1",
265 "bbb", b, arg_adjust_system_clock, arg_ask_password);
267 log_error("Failed to set local RTC: %s", bus_error_message(&error, -r));
272 static int set_ntp(sd_bus *bus, char **args, unsigned n) {
273 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
279 polkit_agent_open_if_enabled();
281 b = parse_boolean(args[1]);
283 log_error("Failed to parse NTP setting: %s", args[1]);
287 r = sd_bus_call_method(bus,
288 "org.freedesktop.timedate1",
289 "/org/freedesktop/timedate1",
290 "org.freedesktop.timedate1",
294 "bb", b, arg_ask_password);
296 log_error("Failed to set ntp: %s", bus_error_message(&error, -r));
301 static int list_timezones(sd_bus *bus, char **args, unsigned n) {
302 _cleanup_strv_free_ char **zones = NULL;
308 r = get_timezones(&zones);
310 return log_error_errno(r, "Failed to read list of time zones: %m");
312 pager_open_if_enabled();
318 static void help(void) {
319 printf("%s [OPTIONS...] COMMAND ...\n\n"
320 "Query or change system time and date settings.\n\n"
321 " -h --help Show this help message\n"
322 " --version Show package version\n"
323 " --no-pager Do not pipe output into a pager\n"
324 " --no-ask-password Do not prompt for password\n"
325 " -H --host=[USER@]HOST Operate on remote host\n"
326 " -M --machine=CONTAINER Operate on local container\n"
327 " --adjust-system-clock Adjust system clock when changing local RTC mode\n\n"
329 " status Show current time settings\n"
330 " set-time TIME Set system time\n"
331 " set-timezone ZONE Set system time zone\n"
332 " list-timezones Show known time zones\n"
333 " set-local-rtc BOOL Control whether RTC is in local time\n"
334 " set-ntp BOOL Control whether NTP is enabled\n",
335 program_invocation_short_name);
338 static int parse_argv(int argc, char *argv[]) {
343 ARG_ADJUST_SYSTEM_CLOCK,
347 static const struct option options[] = {
348 { "help", no_argument, NULL, 'h' },
349 { "version", no_argument, NULL, ARG_VERSION },
350 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
351 { "host", required_argument, NULL, 'H' },
352 { "machine", required_argument, NULL, 'M' },
353 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
354 { "adjust-system-clock", no_argument, NULL, ARG_ADJUST_SYSTEM_CLOCK },
363 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
372 puts(PACKAGE_STRING);
373 puts(SYSTEMD_FEATURES);
377 arg_transport = BUS_TRANSPORT_REMOTE;
382 arg_transport = BUS_TRANSPORT_MACHINE;
386 case ARG_NO_ASK_PASSWORD:
387 arg_ask_password = false;
390 case ARG_ADJUST_SYSTEM_CLOCK:
391 arg_adjust_system_clock = true;
402 assert_not_reached("Unhandled option");
408 static int timedatectl_main(sd_bus *bus, int argc, char *argv[]) {
410 static const struct {
418 int (* const dispatch)(sd_bus *bus, char **args, unsigned n);
420 { "status", LESS, 1, show_status },
421 { "set-time", EQUAL, 2, set_time },
422 { "set-timezone", EQUAL, 2, set_timezone },
423 { "list-timezones", EQUAL, 1, list_timezones },
424 { "set-local-rtc", EQUAL, 2, set_local_rtc },
425 { "set-ntp", EQUAL, 2, set_ntp, },
434 left = argc - optind;
437 /* Special rule: no arguments means "status" */
440 if (streq(argv[optind], "help")) {
445 for (i = 0; i < ELEMENTSOF(verbs); i++)
446 if (streq(argv[optind], verbs[i].verb))
449 if (i >= ELEMENTSOF(verbs)) {
450 log_error("Unknown operation %s", argv[optind]);
455 switch (verbs[i].argc_cmp) {
458 if (left != verbs[i].argc) {
459 log_error("Invalid number of arguments.");
466 if (left < verbs[i].argc) {
467 log_error("Too few arguments.");
474 if (left > verbs[i].argc) {
475 log_error("Too many arguments.");
482 assert_not_reached("Unknown comparison operator.");
485 return verbs[i].dispatch(bus, argv + optind, left);
488 int main(int argc, char *argv[]) {
489 _cleanup_bus_close_unref_ sd_bus *bus = NULL;
492 setlocale(LC_ALL, "");
493 log_parse_environment();
496 r = parse_argv(argc, argv);
500 r = bus_open_transport(arg_transport, arg_host, false, &bus);
502 log_error_errno(r, "Failed to create bus connection: %m");
506 r = timedatectl_main(bus, argc, argv);
511 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;