2 This file is part of systemd.
4 Copyright 2012 Lennart Poettering
5 Copyright 2013 Kay Sievers
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "bus-error.h"
31 #include "parse-util.h"
32 #include "spawn-polkit-agent.h"
34 #include "terminal-util.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 polkit_agent_open_if_enabled(void) {
45 /* Open the polkit agent as a child process if necessary */
46 if (!arg_ask_password
)
49 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
55 typedef struct StatusInfo
{
67 static void status_info_clear(StatusInfo
*info
) {
74 static void print_status_info(const StatusInfo
*i
) {
75 char a
[FORMAT_TIMESTAMP_MAX
];
78 bool have_time
= false;
79 const char *old_tz
= NULL
, *tz
;
84 /* Save the old $TZ */
90 if (setenv("TZ", isempty(i
->timezone
) ? "UTC" : i
->timezone
, true) < 0)
91 log_warning_errno(errno
, "Failed to set TZ environment variable, ignoring: %m");
96 sec
= (time_t) (i
->time
/ USEC_PER_SEC
);
98 } else if (IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
)) {
102 log_warning("Could not get time from timedated and not operating locally, ignoring.");
105 xstrftime(a
, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec
, &tm
));
106 printf(" Local time: %.*s\n", (int) sizeof(a
), a
);
108 xstrftime(a
, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec
, &tm
));
109 printf(" Universal time: %.*s\n", (int) sizeof(a
), a
);
111 printf(" Local time: %s\n", "n/a");
112 printf(" Universal time: %s\n", "n/a");
115 if (i
->rtc_time
> 0) {
118 rtc_sec
= (time_t) (i
->rtc_time
/ USEC_PER_SEC
);
119 xstrftime(a
, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec
, &tm
));
120 printf(" RTC time: %.*s\n", (int) sizeof(a
), a
);
122 printf(" RTC time: %s\n", "n/a");
125 xstrftime(a
, "%Z, %z", localtime_r(&sec
, &tm
));
127 /* Restore the $TZ */
129 r
= setenv("TZ", old_tz
, true);
133 log_warning_errno(errno
, "Failed to set TZ environment variable, ignoring: %m");
137 printf(" Time zone: %s (%.*s)\n"
138 " Network time on: %s\n"
139 "NTP synchronized: %s\n"
140 " RTC in local TZ: %s\n",
141 strna(i
->timezone
), (int) sizeof(a
), have_time
? a
: "n/a",
142 i
->ntp_capable
? yes_no(i
->ntp_enabled
) : "n/a",
143 yes_no(i
->ntp_synced
),
144 yes_no(i
->rtc_local
));
147 fputs("\n" ANSI_HIGHLIGHT
148 "Warning: The system is configured to read the RTC time in the local time zone.\n"
149 " This mode can not be fully supported. It will create various problems\n"
150 " with time zone changes and daylight saving time adjustments. The RTC\n"
151 " time is never updated, it relies on external facilities to maintain it.\n"
152 " If at all possible, use RTC in UTC by calling\n"
153 " 'timedatectl set-local-rtc 0'." ANSI_NORMAL
"\n", stdout
);
156 static int show_status(sd_bus
*bus
, char **args
, unsigned n
) {
157 _cleanup_(status_info_clear
) 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 return log_error_errno(r
, "Failed to query server: %m");
180 print_status_info(&info
);
185 static int set_time(sd_bus
*bus
, char **args
, unsigned n
) {
186 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
187 bool relative
= false, interactive
= arg_ask_password
;
194 polkit_agent_open_if_enabled();
196 r
= parse_timestamp(args
[1], &t
);
198 log_error("Failed to parse time specification: %s", args
[1]);
202 r
= sd_bus_call_method(bus
,
203 "org.freedesktop.timedate1",
204 "/org/freedesktop/timedate1",
205 "org.freedesktop.timedate1",
209 "xbb", (int64_t)t
, relative
, interactive
);
211 log_error("Failed to set time: %s", bus_error_message(&error
, -r
));
216 static int set_timezone(sd_bus
*bus
, char **args
, unsigned n
) {
217 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
223 polkit_agent_open_if_enabled();
225 r
= sd_bus_call_method(bus
,
226 "org.freedesktop.timedate1",
227 "/org/freedesktop/timedate1",
228 "org.freedesktop.timedate1",
232 "sb", args
[1], arg_ask_password
);
234 log_error("Failed to set time zone: %s", bus_error_message(&error
, -r
));
239 static int set_local_rtc(sd_bus
*bus
, char **args
, unsigned n
) {
240 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
246 polkit_agent_open_if_enabled();
248 b
= parse_boolean(args
[1]);
250 log_error("Failed to parse local RTC setting: %s", args
[1]);
254 r
= sd_bus_call_method(bus
,
255 "org.freedesktop.timedate1",
256 "/org/freedesktop/timedate1",
257 "org.freedesktop.timedate1",
261 "bbb", b
, arg_adjust_system_clock
, arg_ask_password
);
263 log_error("Failed to set local RTC: %s", bus_error_message(&error
, -r
));
268 static int set_ntp(sd_bus
*bus
, char **args
, unsigned n
) {
269 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
275 polkit_agent_open_if_enabled();
277 b
= parse_boolean(args
[1]);
279 log_error("Failed to parse NTP setting: %s", args
[1]);
283 r
= sd_bus_call_method(bus
,
284 "org.freedesktop.timedate1",
285 "/org/freedesktop/timedate1",
286 "org.freedesktop.timedate1",
290 "bb", b
, arg_ask_password
);
292 log_error("Failed to set ntp: %s", bus_error_message(&error
, -r
));
297 static int list_timezones(sd_bus
*bus
, char **args
, unsigned n
) {
298 _cleanup_strv_free_
char **zones
= NULL
;
304 r
= get_timezones(&zones
);
306 return log_error_errno(r
, "Failed to read list of time zones: %m");
308 pager_open(arg_no_pager
, false);
314 static void help(void) {
315 printf("%s [OPTIONS...] COMMAND ...\n\n"
316 "Query or change system time and date settings.\n\n"
317 " -h --help Show this help message\n"
318 " --version Show package version\n"
319 " --no-pager Do not pipe output into a pager\n"
320 " --no-ask-password Do not prompt for password\n"
321 " -H --host=[USER@]HOST Operate on remote host\n"
322 " -M --machine=CONTAINER Operate on local container\n"
323 " --adjust-system-clock Adjust system clock when changing local RTC mode\n\n"
325 " status Show current time settings\n"
326 " set-time TIME Set system time\n"
327 " set-timezone ZONE Set system time zone\n"
328 " list-timezones Show known time zones\n"
329 " set-local-rtc BOOL Control whether RTC is in local time\n"
330 " set-ntp BOOL Enable or disable network time synchronization\n",
331 program_invocation_short_name
);
334 static int parse_argv(int argc
, char *argv
[]) {
339 ARG_ADJUST_SYSTEM_CLOCK
,
343 static const struct option options
[] = {
344 { "help", no_argument
, NULL
, 'h' },
345 { "version", no_argument
, NULL
, ARG_VERSION
},
346 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
347 { "host", required_argument
, NULL
, 'H' },
348 { "machine", required_argument
, NULL
, 'M' },
349 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
350 { "adjust-system-clock", no_argument
, NULL
, ARG_ADJUST_SYSTEM_CLOCK
},
359 while ((c
= getopt_long(argc
, argv
, "hH:M:", options
, NULL
)) >= 0)
371 arg_transport
= BUS_TRANSPORT_REMOTE
;
376 arg_transport
= BUS_TRANSPORT_MACHINE
;
380 case ARG_NO_ASK_PASSWORD
:
381 arg_ask_password
= false;
384 case ARG_ADJUST_SYSTEM_CLOCK
:
385 arg_adjust_system_clock
= true;
396 assert_not_reached("Unhandled option");
402 static int timedatectl_main(sd_bus
*bus
, int argc
, char *argv
[]) {
404 static const struct {
412 int (* const dispatch
)(sd_bus
*bus
, char **args
, unsigned n
);
414 { "status", LESS
, 1, show_status
},
415 { "set-time", EQUAL
, 2, set_time
},
416 { "set-timezone", EQUAL
, 2, set_timezone
},
417 { "list-timezones", EQUAL
, 1, list_timezones
},
418 { "set-local-rtc", EQUAL
, 2, set_local_rtc
},
419 { "set-ntp", EQUAL
, 2, set_ntp
, },
428 left
= argc
- optind
;
431 /* Special rule: no arguments means "status" */
434 if (streq(argv
[optind
], "help")) {
439 for (i
= 0; i
< ELEMENTSOF(verbs
); i
++)
440 if (streq(argv
[optind
], verbs
[i
].verb
))
443 if (i
>= ELEMENTSOF(verbs
)) {
444 log_error("Unknown operation %s", argv
[optind
]);
449 switch (verbs
[i
].argc_cmp
) {
452 if (left
!= verbs
[i
].argc
) {
453 log_error("Invalid number of arguments.");
460 if (left
< verbs
[i
].argc
) {
461 log_error("Too few arguments.");
468 if (left
> verbs
[i
].argc
) {
469 log_error("Too many arguments.");
476 assert_not_reached("Unknown comparison operator.");
479 return verbs
[i
].dispatch(bus
, argv
+ optind
, left
);
482 int main(int argc
, char *argv
[]) {
483 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
486 setlocale(LC_ALL
, "");
487 log_parse_environment();
490 r
= parse_argv(argc
, argv
);
494 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
496 log_error_errno(r
, "Failed to create bus connection: %m");
500 r
= timedatectl_main(bus
, argc
, argv
);
505 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;