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"
36 #include "terminal-util.h"
38 static bool arg_no_pager
= false;
39 static bool arg_ask_password
= true;
40 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
41 static char *arg_host
= NULL
;
42 static bool arg_adjust_system_clock
= false;
44 static void pager_open_if_enabled(void) {
52 static void polkit_agent_open_if_enabled(void) {
54 /* Open the polkit agent as a child process if necessary */
55 if (!arg_ask_password
)
58 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
64 typedef struct StatusInfo
{
76 static void status_info_clear(StatusInfo
*info
) {
83 static void print_status_info(const StatusInfo
*i
) {
84 char a
[FORMAT_TIMESTAMP_MAX
];
87 bool have_time
= false;
88 const char *old_tz
= NULL
, *tz
;
93 /* Save the old $TZ */
99 if (setenv("TZ", isempty(i
->timezone
) ? "UTC" : i
->timezone
, true) < 0)
100 log_warning_errno(errno
, "Failed to set TZ environment variable, ignoring: %m");
105 sec
= (time_t) (i
->time
/ USEC_PER_SEC
);
107 } else if (IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
)) {
111 log_warning("Could not get time from timedated and not operating locally, ignoring.");
114 xstrftime(a
, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec
, &tm
));
115 printf(" Local time: %.*s\n", (int) sizeof(a
), a
);
117 xstrftime(a
, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec
, &tm
));
118 printf(" Universal time: %.*s\n", (int) sizeof(a
), a
);
120 printf(" Local time: %s\n", "n/a");
121 printf(" Universal time: %s\n", "n/a");
124 if (i
->rtc_time
> 0) {
127 rtc_sec
= (time_t) (i
->rtc_time
/ USEC_PER_SEC
);
128 xstrftime(a
, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec
, &tm
));
129 printf(" RTC time: %.*s\n", (int) sizeof(a
), a
);
131 printf(" RTC time: %s\n", "n/a");
134 xstrftime(a
, "%Z, %z", localtime_r(&sec
, &tm
));
136 /* Restore the $TZ */
138 r
= setenv("TZ", old_tz
, true);
142 log_warning_errno(errno
, "Failed to set TZ environment variable, ignoring: %m");
146 printf(" Time zone: %s (%.*s)\n"
147 " Network time on: %s\n"
148 "NTP synchronized: %s\n"
149 " RTC in local TZ: %s\n",
150 strna(i
->timezone
), (int) sizeof(a
), have_time
? a
: "n/a",
151 i
->ntp_capable
? yes_no(i
->ntp_enabled
) : "n/a",
152 yes_no(i
->ntp_synced
),
153 yes_no(i
->rtc_local
));
156 fputs("\n" ANSI_HIGHLIGHT
157 "Warning: The system is configured to read the RTC time in the local time zone.\n"
158 " This mode can not be fully supported. It will create various problems\n"
159 " with time zone changes and daylight saving time adjustments. The RTC\n"
160 " time is never updated, it relies on external facilities to maintain it.\n"
161 " If at all possible, use RTC in UTC by calling\n"
162 " 'timedatectl set-local-rtc 0'." ANSI_NORMAL
"\n", stdout
);
165 static int show_status(sd_bus
*bus
, char **args
, unsigned n
) {
166 _cleanup_(status_info_clear
) StatusInfo info
= {};
167 static const struct bus_properties_map map
[] = {
168 { "Timezone", "s", NULL
, offsetof(StatusInfo
, timezone
) },
169 { "LocalRTC", "b", NULL
, offsetof(StatusInfo
, rtc_local
) },
170 { "NTP", "b", NULL
, offsetof(StatusInfo
, ntp_enabled
) },
171 { "CanNTP", "b", NULL
, offsetof(StatusInfo
, ntp_capable
) },
172 { "NTPSynchronized", "b", NULL
, offsetof(StatusInfo
, ntp_synced
) },
173 { "TimeUSec", "t", NULL
, offsetof(StatusInfo
, time
) },
174 { "RTCTimeUSec", "t", NULL
, offsetof(StatusInfo
, rtc_time
) },
181 r
= bus_map_all_properties(bus
,
182 "org.freedesktop.timedate1",
183 "/org/freedesktop/timedate1",
187 return log_error_errno(r
, "Failed to query server: %m");
189 print_status_info(&info
);
194 static int set_time(sd_bus
*bus
, char **args
, unsigned n
) {
195 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
196 bool relative
= false, interactive
= arg_ask_password
;
203 polkit_agent_open_if_enabled();
205 r
= parse_timestamp(args
[1], &t
);
207 log_error("Failed to parse time specification: %s", args
[1]);
211 r
= sd_bus_call_method(bus
,
212 "org.freedesktop.timedate1",
213 "/org/freedesktop/timedate1",
214 "org.freedesktop.timedate1",
218 "xbb", (int64_t)t
, relative
, interactive
);
220 log_error("Failed to set time: %s", bus_error_message(&error
, -r
));
225 static int set_timezone(sd_bus
*bus
, char **args
, unsigned n
) {
226 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
232 polkit_agent_open_if_enabled();
234 r
= sd_bus_call_method(bus
,
235 "org.freedesktop.timedate1",
236 "/org/freedesktop/timedate1",
237 "org.freedesktop.timedate1",
241 "sb", args
[1], arg_ask_password
);
243 log_error("Failed to set time zone: %s", bus_error_message(&error
, -r
));
248 static int set_local_rtc(sd_bus
*bus
, char **args
, unsigned n
) {
249 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
255 polkit_agent_open_if_enabled();
257 b
= parse_boolean(args
[1]);
259 log_error("Failed to parse local RTC setting: %s", args
[1]);
263 r
= sd_bus_call_method(bus
,
264 "org.freedesktop.timedate1",
265 "/org/freedesktop/timedate1",
266 "org.freedesktop.timedate1",
270 "bbb", b
, arg_adjust_system_clock
, arg_ask_password
);
272 log_error("Failed to set local RTC: %s", bus_error_message(&error
, -r
));
277 static int set_ntp(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 b
= parse_boolean(args
[1]);
288 log_error("Failed to parse NTP setting: %s", args
[1]);
292 r
= sd_bus_call_method(bus
,
293 "org.freedesktop.timedate1",
294 "/org/freedesktop/timedate1",
295 "org.freedesktop.timedate1",
299 "bb", b
, arg_ask_password
);
301 log_error("Failed to set ntp: %s", bus_error_message(&error
, -r
));
306 static int list_timezones(sd_bus
*bus
, char **args
, unsigned n
) {
307 _cleanup_strv_free_
char **zones
= NULL
;
313 r
= get_timezones(&zones
);
315 return log_error_errno(r
, "Failed to read list of time zones: %m");
317 pager_open_if_enabled();
323 static void help(void) {
324 printf("%s [OPTIONS...] COMMAND ...\n\n"
325 "Query or change system time and date settings.\n\n"
326 " -h --help Show this help message\n"
327 " --version Show package version\n"
328 " --no-pager Do not pipe output into a pager\n"
329 " --no-ask-password Do not prompt for password\n"
330 " -H --host=[USER@]HOST Operate on remote host\n"
331 " -M --machine=CONTAINER Operate on local container\n"
332 " --adjust-system-clock Adjust system clock when changing local RTC mode\n\n"
334 " status Show current time settings\n"
335 " set-time TIME Set system time\n"
336 " set-timezone ZONE Set system time zone\n"
337 " list-timezones Show known time zones\n"
338 " set-local-rtc BOOL Control whether RTC is in local time\n"
339 " set-ntp BOOL Enable or disable network time synchronization\n",
340 program_invocation_short_name
);
343 static int parse_argv(int argc
, char *argv
[]) {
348 ARG_ADJUST_SYSTEM_CLOCK
,
352 static const struct option options
[] = {
353 { "help", no_argument
, NULL
, 'h' },
354 { "version", no_argument
, NULL
, ARG_VERSION
},
355 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
356 { "host", required_argument
, NULL
, 'H' },
357 { "machine", required_argument
, NULL
, 'M' },
358 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
359 { "adjust-system-clock", no_argument
, NULL
, ARG_ADJUST_SYSTEM_CLOCK
},
368 while ((c
= getopt_long(argc
, argv
, "hH:M:", options
, NULL
)) >= 0)
377 puts(PACKAGE_STRING
);
378 puts(SYSTEMD_FEATURES
);
382 arg_transport
= BUS_TRANSPORT_REMOTE
;
387 arg_transport
= BUS_TRANSPORT_MACHINE
;
391 case ARG_NO_ASK_PASSWORD
:
392 arg_ask_password
= false;
395 case ARG_ADJUST_SYSTEM_CLOCK
:
396 arg_adjust_system_clock
= true;
407 assert_not_reached("Unhandled option");
413 static int timedatectl_main(sd_bus
*bus
, int argc
, char *argv
[]) {
415 static const struct {
423 int (* const dispatch
)(sd_bus
*bus
, char **args
, unsigned n
);
425 { "status", LESS
, 1, show_status
},
426 { "set-time", EQUAL
, 2, set_time
},
427 { "set-timezone", EQUAL
, 2, set_timezone
},
428 { "list-timezones", EQUAL
, 1, list_timezones
},
429 { "set-local-rtc", EQUAL
, 2, set_local_rtc
},
430 { "set-ntp", EQUAL
, 2, set_ntp
, },
439 left
= argc
- optind
;
442 /* Special rule: no arguments means "status" */
445 if (streq(argv
[optind
], "help")) {
450 for (i
= 0; i
< ELEMENTSOF(verbs
); i
++)
451 if (streq(argv
[optind
], verbs
[i
].verb
))
454 if (i
>= ELEMENTSOF(verbs
)) {
455 log_error("Unknown operation %s", argv
[optind
]);
460 switch (verbs
[i
].argc_cmp
) {
463 if (left
!= verbs
[i
].argc
) {
464 log_error("Invalid number of arguments.");
471 if (left
< verbs
[i
].argc
) {
472 log_error("Too few arguments.");
479 if (left
> verbs
[i
].argc
) {
480 log_error("Too many arguments.");
487 assert_not_reached("Unknown comparison operator.");
490 return verbs
[i
].dispatch(bus
, argv
+ optind
, left
);
493 int main(int argc
, char *argv
[]) {
494 _cleanup_bus_flush_close_unref_ sd_bus
*bus
= NULL
;
497 setlocale(LC_ALL
, "");
498 log_parse_environment();
501 r
= parse_argv(argc
, argv
);
505 r
= bus_open_transport(arg_transport
, arg_host
, false, &bus
);
507 log_error_errno(r
, "Failed to create bus connection: %m");
511 r
= timedatectl_main(bus
, argc
, argv
);
516 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;