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;
84 /* Enforce the values of /etc/localtime */
86 fprintf(stderr
, "Warning: Ignoring the TZ variable.\n\n");
90 r
= setenv("TZ", i
->timezone
, false);
92 log_error_errno(errno
, "Failed to set TZ environment variable: %m");
98 sec
= (time_t) (i
->time
/ USEC_PER_SEC
);
100 } else if (IN_SET(arg_transport
, BUS_TRANSPORT_REMOTE
, BUS_TRANSPORT_MACHINE
)) {
104 fprintf(stderr
, "Warning: Could not get time from timedated and not operating locally.\n\n");
107 xstrftime(a
, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec
, &tm
));
108 printf(" Local time: %.*s\n", (int) sizeof(a
), a
);
110 xstrftime(a
, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec
, &tm
));
111 printf(" Universal time: %.*s\n", (int) sizeof(a
), a
);
113 printf(" Local time: %s\n", "n/a");
114 printf(" Universal time: %s\n", "n/a");
117 if (i
->rtc_time
> 0) {
120 rtc_sec
= (time_t)(i
->rtc_time
/ USEC_PER_SEC
);
121 xstrftime(a
, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec
, &tm
));
122 printf(" RTC time: %.*s\n", (int) sizeof(a
), a
);
124 printf(" RTC time: %s\n", "n/a");
127 xstrftime(a
, "%Z, %z", localtime_r(&sec
, &tm
));
129 printf(" Time zone: %s (%.*s)\n"
131 "NTP synchronized: %s\n"
132 " RTC in local TZ: %s\n",
133 strna(i
->timezone
), (int) sizeof(a
), have_time
? a
: "n/a",
134 i
->ntp_capable
? yes_no(i
->ntp_enabled
) : "n/a",
135 yes_no(i
->ntp_synced
),
136 yes_no(i
->rtc_local
));
139 fputs("\n" ANSI_HIGHLIGHT_ON
140 "Warning: The system is configured to read the RTC time in the local time zone. This\n"
141 " mode can not be fully supported. It will create various problems with time\n"
142 " zone changes and daylight saving time adjustments. The RTC time is never updated,\n"
143 " it relies on external facilities to maintain it. If at all possible, use\n"
144 " RTC in UTC by calling 'timedatectl set-local-rtc 0'" ANSI_HIGHLIGHT_OFF
".\n", stdout
);
147 static int show_status(sd_bus
*bus
, char **args
, unsigned n
) {
148 StatusInfo info
= {};
149 static const struct bus_properties_map map
[] = {
150 { "Timezone", "s", NULL
, offsetof(StatusInfo
, timezone
) },
151 { "LocalRTC", "b", NULL
, offsetof(StatusInfo
, rtc_local
) },
152 { "NTP", "b", NULL
, offsetof(StatusInfo
, ntp_enabled
) },
153 { "CanNTP", "b", NULL
, offsetof(StatusInfo
, ntp_capable
) },
154 { "NTPSynchronized", "b", NULL
, offsetof(StatusInfo
, ntp_synced
) },
155 { "TimeUSec", "t", NULL
, offsetof(StatusInfo
, time
) },
156 { "RTCTimeUSec", "t", NULL
, offsetof(StatusInfo
, rtc_time
) },
163 r
= bus_map_all_properties(bus
,
164 "org.freedesktop.timedate1",
165 "/org/freedesktop/timedate1",
169 log_error_errno(r
, "Failed to query server: %m");
173 print_status_info(&info
);
180 static int set_time(sd_bus
*bus
, char **args
, unsigned n
) {
181 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
182 bool relative
= false, interactive
= arg_ask_password
;
189 polkit_agent_open_if_enabled();
191 r
= parse_timestamp(args
[1], &t
);
193 log_error("Failed to parse time specification: %s", args
[1]);
197 r
= sd_bus_call_method(bus
,
198 "org.freedesktop.timedate1",
199 "/org/freedesktop/timedate1",
200 "org.freedesktop.timedate1",
204 "xbb", (int64_t)t
, relative
, interactive
);
206 log_error("Failed to set time: %s", bus_error_message(&error
, -r
));
211 static int set_timezone(sd_bus
*bus
, char **args
, unsigned n
) {
212 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
218 polkit_agent_open_if_enabled();
220 r
= sd_bus_call_method(bus
,
221 "org.freedesktop.timedate1",
222 "/org/freedesktop/timedate1",
223 "org.freedesktop.timedate1",
227 "sb", args
[1], arg_ask_password
);
229 log_error("Failed to set time zone: %s", bus_error_message(&error
, -r
));
234 static int set_local_rtc(sd_bus
*bus
, char **args
, unsigned n
) {
235 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
241 polkit_agent_open_if_enabled();
243 b
= parse_boolean(args
[1]);
245 log_error("Failed to parse local RTC setting: %s", args
[1]);
249 r
= sd_bus_call_method(bus
,
250 "org.freedesktop.timedate1",
251 "/org/freedesktop/timedate1",
252 "org.freedesktop.timedate1",
256 "bbb", b
, arg_adjust_system_clock
, arg_ask_password
);
258 log_error("Failed to set local RTC: %s", bus_error_message(&error
, -r
));
263 static int set_ntp(sd_bus
*bus
, char **args
, unsigned n
) {
264 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
270 polkit_agent_open_if_enabled();
272 b
= parse_boolean(args
[1]);
274 log_error("Failed to parse NTP setting: %s", args
[1]);
278 r
= sd_bus_call_method(bus
,
279 "org.freedesktop.timedate1",
280 "/org/freedesktop/timedate1",
281 "org.freedesktop.timedate1",
285 "bb", b
, arg_ask_password
);
287 log_error("Failed to set ntp: %s", bus_error_message(&error
, -r
));
292 static int list_timezones(sd_bus
*bus
, char **args
, unsigned n
) {
293 _cleanup_strv_free_
char **zones
= NULL
;
299 r
= get_timezones(&zones
);
301 return log_error_errno(r
, "Failed to read list of time zones: %m");
303 pager_open_if_enabled();
309 static void help(void) {
310 printf("%s [OPTIONS...] COMMAND ...\n\n"
311 "Query or change system time and date settings.\n\n"
312 " -h --help Show this help message\n"
313 " --version Show package version\n"
314 " --no-pager Do not pipe output into a pager\n"
315 " --no-ask-password Do not prompt for password\n"
316 " -H --host=[USER@]HOST Operate on remote host\n"
317 " -M --machine=CONTAINER Operate on local container\n"
318 " --adjust-system-clock Adjust system clock when changing local RTC mode\n\n"
320 " status Show current time settings\n"
321 " set-time TIME Set system time\n"
322 " set-timezone ZONE Set system time zone\n"
323 " list-timezones Show known time zones\n"
324 " set-local-rtc BOOL Control whether RTC is in local time\n"
325 " set-ntp BOOL Control whether NTP is enabled\n",
326 program_invocation_short_name
);
329 static int parse_argv(int argc
, char *argv
[]) {
334 ARG_ADJUST_SYSTEM_CLOCK
,
338 static const struct option options
[] = {
339 { "help", no_argument
, NULL
, 'h' },
340 { "version", no_argument
, NULL
, ARG_VERSION
},
341 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
342 { "host", required_argument
, NULL
, 'H' },
343 { "machine", required_argument
, NULL
, 'M' },
344 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
345 { "adjust-system-clock", no_argument
, NULL
, ARG_ADJUST_SYSTEM_CLOCK
},
354 while ((c
= getopt_long(argc
, argv
, "hH:M:", options
, NULL
)) >= 0)
363 puts(PACKAGE_STRING
);
364 puts(SYSTEMD_FEATURES
);
368 arg_transport
= BUS_TRANSPORT_REMOTE
;
373 arg_transport
= BUS_TRANSPORT_MACHINE
;
377 case ARG_NO_ASK_PASSWORD
:
378 arg_ask_password
= false;
381 case ARG_ADJUST_SYSTEM_CLOCK
:
382 arg_adjust_system_clock
= true;
393 assert_not_reached("Unhandled option");
399 static int timedatectl_main(sd_bus
*bus
, int argc
, char *argv
[]) {
401 static const struct {
409 int (* const dispatch
)(sd_bus
*bus
, char **args
, unsigned n
);
411 { "status", LESS
, 1, show_status
},
412 { "set-time", EQUAL
, 2, set_time
},
413 { "set-timezone", EQUAL
, 2, set_timezone
},
414 { "list-timezones", EQUAL
, 1, list_timezones
},
415 { "set-local-rtc", EQUAL
, 2, set_local_rtc
},
416 { "set-ntp", EQUAL
, 2, set_ntp
, },
425 left
= argc
- optind
;
428 /* Special rule: no arguments means "status" */
431 if (streq(argv
[optind
], "help")) {
436 for (i
= 0; i
< ELEMENTSOF(verbs
); i
++)
437 if (streq(argv
[optind
], verbs
[i
].verb
))
440 if (i
>= ELEMENTSOF(verbs
)) {
441 log_error("Unknown operation %s", argv
[optind
]);
446 switch (verbs
[i
].argc_cmp
) {
449 if (left
!= verbs
[i
].argc
) {
450 log_error("Invalid number of arguments.");
457 if (left
< verbs
[i
].argc
) {
458 log_error("Too few arguments.");
465 if (left
> verbs
[i
].argc
) {
466 log_error("Too many arguments.");
473 assert_not_reached("Unknown comparison operator.");
476 return verbs
[i
].dispatch(bus
, argv
+ optind
, left
);
479 int main(int argc
, char *argv
[]) {
480 _cleanup_bus_close_unref_ sd_bus
*bus
= NULL
;
483 setlocale(LC_ALL
, "");
484 log_parse_environment();
487 r
= parse_argv(argc
, argv
);
491 r
= bus_open_transport(arg_transport
, arg_host
, false, &bus
);
493 log_error_errno(r
, "Failed to create bus connection: %m");
497 r
= timedatectl_main(bus
, argc
, argv
);
502 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;