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