]>
Commit | Line | Data |
---|---|---|
6d0274f1 LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2012 Lennart Poettering | |
2f6a5907 | 7 | Copyright 2013 Kay Sievers |
6d0274f1 LP |
8 | |
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. | |
13 | ||
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. | |
18 | ||
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/>. | |
21 | ***/ | |
22 | ||
23 | #include <stdlib.h> | |
24 | #include <stdbool.h> | |
6d0274f1 | 25 | #include <getopt.h> |
a9cdc94f | 26 | #include <locale.h> |
6d0274f1 | 27 | |
a281d9c7 TA |
28 | #include "sd-bus.h" |
29 | #include "bus-util.h" | |
30 | #include "bus-error.h" | |
6d0274f1 LP |
31 | #include "util.h" |
32 | #include "spawn-polkit-agent.h" | |
33 | #include "build.h" | |
6d0274f1 LP |
34 | #include "strv.h" |
35 | #include "pager.h" | |
288a74cc | 36 | #include "terminal-util.h" |
6d0274f1 | 37 | |
6d0274f1 | 38 | static bool arg_no_pager = false; |
6d0274f1 | 39 | static bool arg_ask_password = true; |
e1636421 | 40 | static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; |
7085053a | 41 | static char *arg_host = NULL; |
e1636421 | 42 | static bool arg_adjust_system_clock = false; |
6d0274f1 LP |
43 | |
44 | static void pager_open_if_enabled(void) { | |
45 | ||
46 | if (arg_no_pager) | |
47 | return; | |
48 | ||
1b12a7b5 | 49 | pager_open(false); |
6d0274f1 LP |
50 | } |
51 | ||
52 | static void polkit_agent_open_if_enabled(void) { | |
53 | ||
54 | /* Open the polkit agent as a child process if necessary */ | |
6d0274f1 LP |
55 | if (!arg_ask_password) |
56 | return; | |
57 | ||
46e65dcc LP |
58 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
59 | return; | |
60 | ||
6d0274f1 LP |
61 | polkit_agent_open(); |
62 | } | |
63 | ||
64 | typedef struct StatusInfo { | |
2f6a5907 | 65 | usec_t time; |
ffc06c35 | 66 | char *timezone; |
2f6a5907 KS |
67 | |
68 | usec_t rtc_time; | |
69 | bool rtc_local; | |
70 | ||
71 | bool ntp_enabled; | |
72 | bool ntp_capable; | |
73 | bool ntp_synced; | |
6d0274f1 LP |
74 | } StatusInfo; |
75 | ||
e7e55dbd DH |
76 | static void status_info_clear(StatusInfo *info) { |
77 | if (info) { | |
78 | free(info->timezone); | |
79 | zero(*info); | |
80 | } | |
81 | } | |
82 | ||
ffc06c35 | 83 | static void print_status_info(const StatusInfo *i) { |
f18ca9dc | 84 | char a[FORMAT_TIMESTAMP_MAX]; |
6d0274f1 LP |
85 | struct tm tm; |
86 | time_t sec; | |
9ff09bcb | 87 | bool have_time = false; |
d95a74ed | 88 | const char *old_tz = NULL, *tz; |
6d0274f1 LP |
89 | int r; |
90 | ||
59965986 LP |
91 | assert(i); |
92 | ||
d95a74ed LP |
93 | /* Save the old $TZ */ |
94 | tz = getenv("TZ"); | |
95 | if (tz) | |
96 | old_tz = strdupa(tz); | |
2311eb2f | 97 | |
d95a74ed | 98 | /* Set the new $TZ */ |
8c4fa32a | 99 | if (i->timezone && setenv("TZ", i->timezone, true) < 0) |
d95a74ed LP |
100 | log_warning_errno(errno, "Failed to set TZ environment variable, ignoring: %m"); |
101 | else | |
102 | tzset(); | |
3e5e74d5 | 103 | |
9ff09bcb SL |
104 | if (i->time != 0) { |
105 | sec = (time_t) (i->time / USEC_PER_SEC); | |
106 | have_time = true; | |
d95a74ed | 107 | } else if (IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE)) { |
9ff09bcb SL |
108 | sec = time(NULL); |
109 | have_time = true; | |
110 | } else | |
d95a74ed | 111 | log_warning("Could not get time from timedated and not operating locally, ignoring."); |
6d0274f1 | 112 | |
9ff09bcb | 113 | if (have_time) { |
5ffa8c81 ZJS |
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); | |
116 | ||
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); | |
9ff09bcb SL |
119 | } else { |
120 | printf(" Local time: %s\n", "n/a"); | |
121 | printf(" Universal time: %s\n", "n/a"); | |
122 | } | |
6d0274f1 | 123 | |
2f6a5907 KS |
124 | if (i->rtc_time > 0) { |
125 | time_t rtc_sec; | |
6d0274f1 | 126 | |
d95a74ed | 127 | rtc_sec = (time_t) (i->rtc_time / USEC_PER_SEC); |
5ffa8c81 ZJS |
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); | |
2f6a5907 | 130 | } else |
9ff09bcb | 131 | printf(" RTC time: %s\n", "n/a"); |
6d0274f1 | 132 | |
5ffa8c81 ZJS |
133 | if (have_time) |
134 | xstrftime(a, "%Z, %z", localtime_r(&sec, &tm)); | |
2667cc25 | 135 | |
d95a74ed LP |
136 | /* Restore the $TZ */ |
137 | if (old_tz) | |
138 | r = setenv("TZ", old_tz, true); | |
139 | else | |
140 | r = unsetenv("TZ"); | |
141 | if (r < 0) | |
142 | log_warning_errno(errno, "Failed to set TZ environment variable, ignoring: %m"); | |
143 | else | |
144 | tzset(); | |
145 | ||
5ffa8c81 | 146 | printf(" Time zone: %s (%.*s)\n" |
b90930c7 | 147 | " Network time on: %s\n" |
6d0274f1 LP |
148 | "NTP synchronized: %s\n" |
149 | " RTC in local TZ: %s\n", | |
5ffa8c81 | 150 | strna(i->timezone), (int) sizeof(a), have_time ? a : "n/a", |
2f6a5907 KS |
151 | i->ntp_capable ? yes_no(i->ntp_enabled) : "n/a", |
152 | yes_no(i->ntp_synced), | |
153 | yes_no(i->rtc_local)); | |
6d0274f1 | 154 | |
2f6a5907 | 155 | if (i->rtc_local) |
6d0274f1 | 156 | fputs("\n" ANSI_HIGHLIGHT_ON |
ab59f412 VM |
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_HIGHLIGHT_OFF ".\n", stdout); | |
6d0274f1 LP |
163 | } |
164 | ||
a281d9c7 | 165 | static int show_status(sd_bus *bus, char **args, unsigned n) { |
e7e55dbd | 166 | _cleanup_(status_info_clear) StatusInfo info = {}; |
9f6eb1cd KS |
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) }, | |
ffc06c35 KS |
175 | {} |
176 | }; | |
177 | int r; | |
6d0274f1 | 178 | |
a281d9c7 | 179 | assert(bus); |
6d0274f1 | 180 | |
ffc06c35 KS |
181 | r = bus_map_all_properties(bus, |
182 | "org.freedesktop.timedate1", | |
183 | "/org/freedesktop/timedate1", | |
9f6eb1cd KS |
184 | map, |
185 | &info); | |
e7e55dbd DH |
186 | if (r < 0) |
187 | return log_error_errno(r, "Failed to query server: %m"); | |
6d0274f1 LP |
188 | |
189 | print_status_info(&info); | |
ffc06c35 | 190 | |
ffc06c35 | 191 | return r; |
6d0274f1 LP |
192 | } |
193 | ||
a281d9c7 TA |
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; | |
6d0274f1 | 197 | usec_t t; |
6d0274f1 LP |
198 | int r; |
199 | ||
200 | assert(args); | |
201 | assert(n == 2); | |
202 | ||
203 | polkit_agent_open_if_enabled(); | |
204 | ||
205 | r = parse_timestamp(args[1], &t); | |
206 | if (r < 0) { | |
207 | log_error("Failed to parse time specification: %s", args[1]); | |
208 | return r; | |
209 | } | |
210 | ||
a281d9c7 TA |
211 | r = sd_bus_call_method(bus, |
212 | "org.freedesktop.timedate1", | |
213 | "/org/freedesktop/timedate1", | |
214 | "org.freedesktop.timedate1", | |
215 | "SetTime", | |
216 | &error, | |
217 | NULL, | |
218 | "xbb", (int64_t)t, relative, interactive); | |
219 | if (r < 0) | |
220 | log_error("Failed to set time: %s", bus_error_message(&error, -r)); | |
221 | ||
222 | return r; | |
6d0274f1 LP |
223 | } |
224 | ||
a281d9c7 TA |
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; | |
a281d9c7 | 227 | int r; |
6d0274f1 LP |
228 | |
229 | assert(args); | |
230 | assert(n == 2); | |
231 | ||
232 | polkit_agent_open_if_enabled(); | |
233 | ||
a281d9c7 TA |
234 | r = sd_bus_call_method(bus, |
235 | "org.freedesktop.timedate1", | |
236 | "/org/freedesktop/timedate1", | |
237 | "org.freedesktop.timedate1", | |
238 | "SetTimezone", | |
239 | &error, | |
240 | NULL, | |
e5609878 | 241 | "sb", args[1], arg_ask_password); |
a281d9c7 | 242 | if (r < 0) |
07a062a7 | 243 | log_error("Failed to set time zone: %s", bus_error_message(&error, -r)); |
a281d9c7 TA |
244 | |
245 | return r; | |
6d0274f1 LP |
246 | } |
247 | ||
a281d9c7 TA |
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; | |
e5609878 | 250 | int r, b; |
6d0274f1 LP |
251 | |
252 | assert(args); | |
253 | assert(n == 2); | |
254 | ||
255 | polkit_agent_open_if_enabled(); | |
256 | ||
e5609878 LP |
257 | b = parse_boolean(args[1]); |
258 | if (b < 0) { | |
6d0274f1 | 259 | log_error("Failed to parse local RTC setting: %s", args[1]); |
e5609878 | 260 | return b; |
6d0274f1 LP |
261 | } |
262 | ||
a281d9c7 TA |
263 | r = sd_bus_call_method(bus, |
264 | "org.freedesktop.timedate1", | |
265 | "/org/freedesktop/timedate1", | |
266 | "org.freedesktop.timedate1", | |
267 | "SetLocalRTC", | |
268 | &error, | |
269 | NULL, | |
e5609878 | 270 | "bbb", b, arg_adjust_system_clock, arg_ask_password); |
a281d9c7 TA |
271 | if (r < 0) |
272 | log_error("Failed to set local RTC: %s", bus_error_message(&error, -r)); | |
273 | ||
274 | return r; | |
6d0274f1 LP |
275 | } |
276 | ||
a281d9c7 TA |
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; | |
e5609878 | 279 | int b, r; |
6d0274f1 LP |
280 | |
281 | assert(args); | |
282 | assert(n == 2); | |
283 | ||
284 | polkit_agent_open_if_enabled(); | |
285 | ||
e5609878 LP |
286 | b = parse_boolean(args[1]); |
287 | if (b < 0) { | |
6d0274f1 | 288 | log_error("Failed to parse NTP setting: %s", args[1]); |
e5609878 | 289 | return b; |
6d0274f1 LP |
290 | } |
291 | ||
a281d9c7 TA |
292 | r = sd_bus_call_method(bus, |
293 | "org.freedesktop.timedate1", | |
294 | "/org/freedesktop/timedate1", | |
295 | "org.freedesktop.timedate1", | |
296 | "SetNTP", | |
297 | &error, | |
298 | NULL, | |
e5609878 | 299 | "bb", b, arg_ask_password); |
a281d9c7 TA |
300 | if (r < 0) |
301 | log_error("Failed to set ntp: %s", bus_error_message(&error, -r)); | |
302 | ||
303 | return r; | |
6d0274f1 LP |
304 | } |
305 | ||
a281d9c7 | 306 | static int list_timezones(sd_bus *bus, char **args, unsigned n) { |
6d0274f1 | 307 | _cleanup_strv_free_ char **zones = NULL; |
75683450 | 308 | int r; |
6d0274f1 LP |
309 | |
310 | assert(args); | |
311 | assert(n == 1); | |
312 | ||
75683450 | 313 | r = get_timezones(&zones); |
f647962d MS |
314 | if (r < 0) |
315 | return log_error_errno(r, "Failed to read list of time zones: %m"); | |
6d0274f1 | 316 | |
6d0274f1 | 317 | pager_open_if_enabled(); |
7c2d8094 | 318 | strv_print(zones); |
6d0274f1 LP |
319 | |
320 | return 0; | |
321 | } | |
322 | ||
601185b4 | 323 | static void help(void) { |
7591abd4 LP |
324 | printf("%s [OPTIONS...] COMMAND ...\n\n" |
325 | "Query or change system time and date settings.\n\n" | |
07a062a7 | 326 | " -h --help Show this help message\n" |
4f8f66cb ZJS |
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" | |
6d0274f1 | 333 | "Commands:\n" |
4f8f66cb ZJS |
334 | " status Show current time settings\n" |
335 | " set-time TIME Set system time\n" | |
07a062a7 JSJ |
336 | " set-timezone ZONE Set system time zone\n" |
337 | " list-timezones Show known time zones\n" | |
4f8f66cb | 338 | " set-local-rtc BOOL Control whether RTC is in local time\n" |
3906ab4a | 339 | " set-ntp BOOL Enable or disable network time synchronization\n", |
6d0274f1 | 340 | program_invocation_short_name); |
6d0274f1 LP |
341 | } |
342 | ||
343 | static int parse_argv(int argc, char *argv[]) { | |
344 | ||
345 | enum { | |
346 | ARG_VERSION = 0x100, | |
347 | ARG_NO_PAGER, | |
c9783430 | 348 | ARG_ADJUST_SYSTEM_CLOCK, |
6d0274f1 LP |
349 | ARG_NO_ASK_PASSWORD |
350 | }; | |
351 | ||
352 | static const struct option options[] = { | |
c9783430 LP |
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' }, | |
a281d9c7 | 357 | { "machine", required_argument, NULL, 'M' }, |
c9783430 LP |
358 | { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD }, |
359 | { "adjust-system-clock", no_argument, NULL, ARG_ADJUST_SYSTEM_CLOCK }, | |
eb9da376 | 360 | {} |
6d0274f1 LP |
361 | }; |
362 | ||
363 | int c; | |
364 | ||
365 | assert(argc >= 0); | |
366 | assert(argv); | |
367 | ||
601185b4 | 368 | while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0) |
6d0274f1 LP |
369 | |
370 | switch (c) { | |
371 | ||
372 | case 'h': | |
601185b4 ZJS |
373 | help(); |
374 | return 0; | |
6d0274f1 LP |
375 | |
376 | case ARG_VERSION: | |
377 | puts(PACKAGE_STRING); | |
6d0274f1 LP |
378 | puts(SYSTEMD_FEATURES); |
379 | return 0; | |
380 | ||
a281d9c7 TA |
381 | case 'H': |
382 | arg_transport = BUS_TRANSPORT_REMOTE; | |
383 | arg_host = optarg; | |
6d0274f1 LP |
384 | break; |
385 | ||
a281d9c7 | 386 | case 'M': |
de33fc62 | 387 | arg_transport = BUS_TRANSPORT_MACHINE; |
a281d9c7 | 388 | arg_host = optarg; |
6d0274f1 LP |
389 | break; |
390 | ||
546158bc JJ |
391 | case ARG_NO_ASK_PASSWORD: |
392 | arg_ask_password = false; | |
393 | break; | |
394 | ||
c9783430 LP |
395 | case ARG_ADJUST_SYSTEM_CLOCK: |
396 | arg_adjust_system_clock = true; | |
6d0274f1 LP |
397 | break; |
398 | ||
399 | case ARG_NO_PAGER: | |
400 | arg_no_pager = true; | |
401 | break; | |
402 | ||
403 | case '?': | |
404 | return -EINVAL; | |
405 | ||
406 | default: | |
eb9da376 | 407 | assert_not_reached("Unhandled option"); |
6d0274f1 | 408 | } |
6d0274f1 LP |
409 | |
410 | return 1; | |
411 | } | |
412 | ||
a281d9c7 | 413 | static int timedatectl_main(sd_bus *bus, int argc, char *argv[]) { |
6d0274f1 LP |
414 | |
415 | static const struct { | |
416 | const char* verb; | |
417 | const enum { | |
418 | MORE, | |
419 | LESS, | |
420 | EQUAL | |
421 | } argc_cmp; | |
422 | const int argc; | |
a281d9c7 | 423 | int (* const dispatch)(sd_bus *bus, char **args, unsigned n); |
6d0274f1 LP |
424 | } verbs[] = { |
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, }, | |
431 | }; | |
432 | ||
433 | int left; | |
434 | unsigned i; | |
435 | ||
436 | assert(argc >= 0); | |
437 | assert(argv); | |
6d0274f1 LP |
438 | |
439 | left = argc - optind; | |
440 | ||
441 | if (left <= 0) | |
442 | /* Special rule: no arguments means "status" */ | |
443 | i = 0; | |
444 | else { | |
445 | if (streq(argv[optind], "help")) { | |
446 | help(); | |
447 | return 0; | |
448 | } | |
449 | ||
450 | for (i = 0; i < ELEMENTSOF(verbs); i++) | |
451 | if (streq(argv[optind], verbs[i].verb)) | |
452 | break; | |
453 | ||
454 | if (i >= ELEMENTSOF(verbs)) { | |
455 | log_error("Unknown operation %s", argv[optind]); | |
456 | return -EINVAL; | |
457 | } | |
458 | } | |
459 | ||
460 | switch (verbs[i].argc_cmp) { | |
461 | ||
462 | case EQUAL: | |
463 | if (left != verbs[i].argc) { | |
464 | log_error("Invalid number of arguments."); | |
465 | return -EINVAL; | |
466 | } | |
467 | ||
468 | break; | |
469 | ||
470 | case MORE: | |
471 | if (left < verbs[i].argc) { | |
472 | log_error("Too few arguments."); | |
473 | return -EINVAL; | |
474 | } | |
475 | ||
476 | break; | |
477 | ||
478 | case LESS: | |
479 | if (left > verbs[i].argc) { | |
480 | log_error("Too many arguments."); | |
481 | return -EINVAL; | |
482 | } | |
483 | ||
484 | break; | |
485 | ||
486 | default: | |
487 | assert_not_reached("Unknown comparison operator."); | |
488 | } | |
489 | ||
6d0274f1 LP |
490 | return verbs[i].dispatch(bus, argv + optind, left); |
491 | } | |
492 | ||
493 | int main(int argc, char *argv[]) { | |
03976f7b | 494 | _cleanup_bus_flush_close_unref_ sd_bus *bus = NULL; |
84f6181c | 495 | int r; |
6d0274f1 | 496 | |
a9cdc94f | 497 | setlocale(LC_ALL, ""); |
6d0274f1 LP |
498 | log_parse_environment(); |
499 | log_open(); | |
500 | ||
501 | r = parse_argv(argc, argv); | |
84f6181c | 502 | if (r <= 0) |
6d0274f1 | 503 | goto finish; |
6d0274f1 | 504 | |
a281d9c7 TA |
505 | r = bus_open_transport(arg_transport, arg_host, false, &bus); |
506 | if (r < 0) { | |
da927ba9 | 507 | log_error_errno(r, "Failed to create bus connection: %m"); |
a281d9c7 | 508 | goto finish; |
6d0274f1 LP |
509 | } |
510 | ||
a281d9c7 | 511 | r = timedatectl_main(bus, argc, argv); |
6d0274f1 | 512 | |
a281d9c7 | 513 | finish: |
6d0274f1 LP |
514 | pager_close(); |
515 | ||
84f6181c | 516 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
6d0274f1 | 517 | } |