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 static bool arg_no_pager
= false;
42 static bool arg_ask_password
= true;
43 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
44 static char *arg_host
= NULL
;
45 static bool arg_adjust_system_clock
= false;
47 static void pager_open_if_enabled(void) {
55 static void polkit_agent_open_if_enabled(void) {
57 /* Open the polkit agent as a child process if necessary */
58 if (!arg_ask_password
)
61 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
67 typedef struct StatusInfo
{
79 static const char *jump_str(int delta_minutes
, char *s
, size_t size
) {
80 if (delta_minutes
== 60)
81 return "one hour forward";
82 if (delta_minutes
== -60)
83 return "one hour backwards";
84 if (delta_minutes
< 0) {
85 snprintf(s
, size
, "%i minutes backwards", -delta_minutes
);
88 if (delta_minutes
> 0) {
89 snprintf(s
, size
, "%i minutes forward", delta_minutes
);
95 static void print_status_info(const StatusInfo
*i
) {
96 char a
[FORMAT_TIMESTAMP_MAX
];
97 char b
[FORMAT_TIMESTAMP_MAX
];
101 bool have_time
= false;
102 _cleanup_free_
char *zc
= NULL
, *zn
= NULL
;
105 bool is_dstc
= false, is_dstn
= false;
110 /* Enforce the values of /etc/localtime */
112 fprintf(stderr
, "Warning: Ignoring the TZ variable. Reading the system's time zone setting only.\n\n");
117 sec
= (time_t) (i
->time
/ USEC_PER_SEC
);
119 } else if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
123 fprintf(stderr
, "Warning: Could not get time from timedated and not operating locally.\n\n");
127 assert_se(strftime(a
, sizeof(a
), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec
, &tm
)) > 0);
129 printf(" Local time: %s\n", a
);
132 assert_se(strftime(a
, sizeof(a
), "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec
, &tm
)) > 0);
134 printf(" Universal time: %s\n", a
);
136 printf(" Local time: %s\n", "n/a");
137 printf(" Universal time: %s\n", "n/a");
140 if (i
->rtc_time
> 0) {
143 rtc_sec
= (time_t)(i
->rtc_time
/ USEC_PER_SEC
);
145 assert_se(strftime(a
, sizeof(a
), "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec
, &tm
)) > 0);
147 printf(" RTC time: %s\n", a
);
149 printf(" RTC time: %s\n", "n/a");
153 assert_se(strftime(a
, sizeof(a
), "%Z, %z", localtime_r(&sec
, &tm
)) > 0);
157 printf(" Time zone: %s (%s)\n"
159 "NTP synchronized: %s\n"
160 " RTC in local TZ: %s\n",
161 strna(i
->timezone
), have_time
? a
: "n/a",
162 i
->ntp_capable
? yes_no(i
->ntp_enabled
) : "n/a",
163 yes_no(i
->ntp_synced
),
164 yes_no(i
->rtc_local
));
167 r
= time_get_dst(sec
, "/etc/localtime",
169 &tn
, &dn
, &zn
, &is_dstn
);
171 printf(" DST active: %s\n", "n/a");
173 printf(" DST active: %s\n", yes_no(is_dstc
));
177 assert_se(strftime(a
, sizeof(a
), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t
, &tm
)) > 0);
181 assert_se(strftime(b
, sizeof(b
), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tc
, &tm
)) > 0);
183 printf(" Last DST change: DST %s at\n"
186 is_dstc
? "began" : "ended", a
, b
);
190 assert_se(strftime(a
, sizeof(a
), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t
, &tm
)) > 0);
194 assert_se(strftime(b
, sizeof(b
), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tn
, &tm
)) > 0);
196 printf(" Next DST change: DST %s (the clock jumps %s) at\n"
199 is_dstn
? "begins" : "ends", jump_str(dn
, s
, sizeof(s
)), a
, b
);
202 printf(" DST active: %s\n", yes_no(is_dstc
));
205 fputs("\n" ANSI_HIGHLIGHT_ON
206 "Warning: The system is configured to read the RTC time in the local time zone. This\n"
207 " mode can not be fully supported. It will create various problems with time\n"
208 " zone changes and daylight saving time adjustments. The RTC time is never updated,\n"
209 " it relies on external facilities to maintain it. If at all possible, use\n"
210 " RTC in UTC by calling 'timedatectl set-local-rtc 0'" ANSI_HIGHLIGHT_OFF
".\n", stdout
);
213 static int show_status(sd_bus
*bus
, char **args
, unsigned n
) {
214 StatusInfo info
= {};
215 static const struct bus_properties_map map
[] = {
216 { "Timezone", "s", NULL
, offsetof(StatusInfo
, timezone
) },
217 { "LocalRTC", "b", NULL
, offsetof(StatusInfo
, rtc_local
) },
218 { "NTP", "b", NULL
, offsetof(StatusInfo
, ntp_enabled
) },
219 { "CanNTP", "b", NULL
, offsetof(StatusInfo
, ntp_capable
) },
220 { "NTPSynchronized", "b", NULL
, offsetof(StatusInfo
, ntp_synced
) },
221 { "TimeUSec", "t", NULL
, offsetof(StatusInfo
, time
) },
222 { "RTCTimeUSec", "t", NULL
, offsetof(StatusInfo
, rtc_time
) },
229 r
= bus_map_all_properties(bus
,
230 "org.freedesktop.timedate1",
231 "/org/freedesktop/timedate1",
235 log_error("Failed to query server: %s", strerror(-r
));
239 print_status_info(&info
);
246 static int set_time(sd_bus
*bus
, char **args
, unsigned n
) {
247 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
248 bool relative
= false, interactive
= arg_ask_password
;
255 polkit_agent_open_if_enabled();
257 r
= parse_timestamp(args
[1], &t
);
259 log_error("Failed to parse time specification: %s", args
[1]);
263 r
= sd_bus_call_method(bus
,
264 "org.freedesktop.timedate1",
265 "/org/freedesktop/timedate1",
266 "org.freedesktop.timedate1",
270 "xbb", (int64_t)t
, relative
, interactive
);
272 log_error("Failed to set time: %s", bus_error_message(&error
, -r
));
277 static int set_timezone(sd_bus
*bus
, char **args
, unsigned n
) {
278 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
284 polkit_agent_open_if_enabled();
286 r
= sd_bus_call_method(bus
,
287 "org.freedesktop.timedate1",
288 "/org/freedesktop/timedate1",
289 "org.freedesktop.timedate1",
293 "sb", args
[1], arg_ask_password
);
295 log_error("Failed to set time zone: %s", bus_error_message(&error
, -r
));
300 static int set_local_rtc(sd_bus
*bus
, char **args
, unsigned n
) {
301 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
307 polkit_agent_open_if_enabled();
309 b
= parse_boolean(args
[1]);
311 log_error("Failed to parse local RTC setting: %s", args
[1]);
315 r
= sd_bus_call_method(bus
,
316 "org.freedesktop.timedate1",
317 "/org/freedesktop/timedate1",
318 "org.freedesktop.timedate1",
322 "bbb", b
, arg_adjust_system_clock
, arg_ask_password
);
324 log_error("Failed to set local RTC: %s", bus_error_message(&error
, -r
));
329 static int set_ntp(sd_bus
*bus
, char **args
, unsigned n
) {
330 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
336 polkit_agent_open_if_enabled();
338 b
= parse_boolean(args
[1]);
340 log_error("Failed to parse NTP setting: %s", args
[1]);
344 r
= sd_bus_call_method(bus
,
345 "org.freedesktop.timedate1",
346 "/org/freedesktop/timedate1",
347 "org.freedesktop.timedate1",
351 "bb", b
, arg_ask_password
);
353 log_error("Failed to set ntp: %s", bus_error_message(&error
, -r
));
358 static int list_timezones(sd_bus
*bus
, char **args
, unsigned n
) {
359 _cleanup_strv_free_
char **zones
= NULL
;
365 r
= get_timezones(&zones
);
367 log_error("Failed to read list of time zones: %s", strerror(-r
));
371 pager_open_if_enabled();
377 static void help(void) {
378 printf("%s [OPTIONS...] COMMAND ...\n\n"
379 "Query or change system time and date settings.\n\n"
380 " -h --help Show this help message\n"
381 " --version Show package version\n"
382 " --no-pager Do not pipe output into a pager\n"
383 " --no-ask-password Do not prompt for password\n"
384 " -H --host=[USER@]HOST Operate on remote host\n"
385 " -M --machine=CONTAINER Operate on local container\n"
386 " --adjust-system-clock Adjust system clock when changing local RTC mode\n\n"
388 " status Show current time settings\n"
389 " set-time TIME Set system time\n"
390 " set-timezone ZONE Set system time zone\n"
391 " list-timezones Show known time zones\n"
392 " set-local-rtc BOOL Control whether RTC is in local time\n"
393 " set-ntp BOOL Control whether NTP is enabled\n",
394 program_invocation_short_name
);
397 static int parse_argv(int argc
, char *argv
[]) {
402 ARG_ADJUST_SYSTEM_CLOCK
,
406 static const struct option options
[] = {
407 { "help", no_argument
, NULL
, 'h' },
408 { "version", no_argument
, NULL
, ARG_VERSION
},
409 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
410 { "host", required_argument
, NULL
, 'H' },
411 { "machine", required_argument
, NULL
, 'M' },
412 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
413 { "adjust-system-clock", no_argument
, NULL
, ARG_ADJUST_SYSTEM_CLOCK
},
422 while ((c
= getopt_long(argc
, argv
, "hH:M:", options
, NULL
)) >= 0)
431 puts(PACKAGE_STRING
);
432 puts(SYSTEMD_FEATURES
);
436 arg_transport
= BUS_TRANSPORT_REMOTE
;
441 arg_transport
= BUS_TRANSPORT_CONTAINER
;
445 case ARG_NO_ASK_PASSWORD
:
446 arg_ask_password
= false;
449 case ARG_ADJUST_SYSTEM_CLOCK
:
450 arg_adjust_system_clock
= true;
461 assert_not_reached("Unhandled option");
467 static int timedatectl_main(sd_bus
*bus
, int argc
, char *argv
[]) {
469 static const struct {
477 int (* const dispatch
)(sd_bus
*bus
, char **args
, unsigned n
);
479 { "status", LESS
, 1, show_status
},
480 { "set-time", EQUAL
, 2, set_time
},
481 { "set-timezone", EQUAL
, 2, set_timezone
},
482 { "list-timezones", EQUAL
, 1, list_timezones
},
483 { "set-local-rtc", EQUAL
, 2, set_local_rtc
},
484 { "set-ntp", EQUAL
, 2, set_ntp
, },
493 left
= argc
- optind
;
496 /* Special rule: no arguments means "status" */
499 if (streq(argv
[optind
], "help")) {
504 for (i
= 0; i
< ELEMENTSOF(verbs
); i
++)
505 if (streq(argv
[optind
], verbs
[i
].verb
))
508 if (i
>= ELEMENTSOF(verbs
)) {
509 log_error("Unknown operation %s", argv
[optind
]);
514 switch (verbs
[i
].argc_cmp
) {
517 if (left
!= verbs
[i
].argc
) {
518 log_error("Invalid number of arguments.");
525 if (left
< verbs
[i
].argc
) {
526 log_error("Too few arguments.");
533 if (left
> verbs
[i
].argc
) {
534 log_error("Too many arguments.");
541 assert_not_reached("Unknown comparison operator.");
544 return verbs
[i
].dispatch(bus
, argv
+ optind
, left
);
547 int main(int argc
, char *argv
[]) {
548 _cleanup_bus_close_unref_ sd_bus
*bus
= NULL
;
551 setlocale(LC_ALL
, "");
552 log_parse_environment();
555 r
= parse_argv(argc
, argv
);
559 r
= bus_open_transport(arg_transport
, arg_host
, false, &bus
);
561 log_error("Failed to create bus connection: %s", strerror(-r
));
565 r
= timedatectl_main(bus
, argc
, argv
);
570 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;