]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/timedate/timedatectl.c
Correct references to ProtectSystem and ProtectHome in documentation
[thirdparty/systemd.git] / src / timedate / timedatectl.c
CommitLineData
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>
25#include <unistd.h>
26#include <getopt.h>
a9cdc94f 27#include <locale.h>
6d0274f1
LP
28#include <string.h>
29#include <sys/timex.h>
30
a281d9c7
TA
31#include "sd-bus.h"
32#include "bus-util.h"
33#include "bus-error.h"
6d0274f1
LP
34#include "util.h"
35#include "spawn-polkit-agent.h"
36#include "build.h"
6d0274f1
LP
37#include "strv.h"
38#include "pager.h"
f18ca9dc 39#include "time-dst.h"
6d0274f1 40
6d0274f1 41static bool arg_no_pager = false;
6d0274f1 42static bool arg_ask_password = true;
e1636421 43static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
7085053a 44static char *arg_host = NULL;
e1636421 45static bool arg_adjust_system_clock = false;
6d0274f1
LP
46
47static void pager_open_if_enabled(void) {
48
49 if (arg_no_pager)
50 return;
51
1b12a7b5 52 pager_open(false);
6d0274f1
LP
53}
54
55static void polkit_agent_open_if_enabled(void) {
56
57 /* Open the polkit agent as a child process if necessary */
6d0274f1
LP
58 if (!arg_ask_password)
59 return;
60
46e65dcc
LP
61 if (arg_transport != BUS_TRANSPORT_LOCAL)
62 return;
63
6d0274f1
LP
64 polkit_agent_open();
65}
66
67typedef struct StatusInfo {
2f6a5907 68 usec_t time;
ffc06c35 69 char *timezone;
2f6a5907
KS
70
71 usec_t rtc_time;
72 bool rtc_local;
73
74 bool ntp_enabled;
75 bool ntp_capable;
76 bool ntp_synced;
6d0274f1
LP
77} StatusInfo;
78
2311eb2f
KS
79static 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);
86 return s;
87 }
88 if (delta_minutes > 0) {
89 snprintf(s, size, "%i minutes forward", delta_minutes);
90 return s;
91 }
92 return "";
93}
94
ffc06c35 95static void print_status_info(const StatusInfo *i) {
f18ca9dc 96 char a[FORMAT_TIMESTAMP_MAX];
6d0274f1 97 char b[FORMAT_TIMESTAMP_MAX];
2311eb2f 98 char s[32];
6d0274f1
LP
99 struct tm tm;
100 time_t sec;
9ff09bcb 101 bool have_time = false;
480a61ae 102 _cleanup_free_ char *zc = NULL, *zn = NULL;
f18ca9dc 103 time_t t, tc, tn;
480a61ae
LP
104 int dn = 0;
105 bool is_dstc = false, is_dstn = false;
6d0274f1
LP
106 int r;
107
59965986
LP
108 assert(i);
109
2f6a5907 110 /* Enforce the values of /etc/localtime */
2311eb2f 111 if (getenv("TZ")) {
07a062a7 112 fprintf(stderr, "Warning: Ignoring the TZ variable. Reading the system's time zone setting only.\n\n");
2311eb2f
KS
113 unsetenv("TZ");
114 }
115
9ff09bcb
SL
116 if (i->time != 0) {
117 sec = (time_t) (i->time / USEC_PER_SEC);
118 have_time = true;
119 } else if (arg_transport == BUS_TRANSPORT_LOCAL) {
120 sec = time(NULL);
121 have_time = true;
122 } else
07a062a7 123 fprintf(stderr, "Warning: Could not get time from timedated and not operating locally.\n\n");
6d0274f1 124
9ff09bcb
SL
125 if (have_time) {
126 zero(tm);
127 assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm)) > 0);
128 char_array_0(a);
129 printf(" Local time: %s\n", a);
6d0274f1 130
9ff09bcb
SL
131 zero(tm);
132 assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm)) > 0);
133 char_array_0(a);
134 printf(" Universal time: %s\n", a);
135 } else {
136 printf(" Local time: %s\n", "n/a");
137 printf(" Universal time: %s\n", "n/a");
138 }
6d0274f1 139
2f6a5907
KS
140 if (i->rtc_time > 0) {
141 time_t rtc_sec;
6d0274f1 142
2f6a5907
KS
143 rtc_sec = (time_t)(i->rtc_time / USEC_PER_SEC);
144 zero(tm);
7f35b7bc 145 assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec, &tm)) > 0);
f18ca9dc
KS
146 char_array_0(a);
147 printf(" RTC time: %s\n", a);
2f6a5907 148 } else
9ff09bcb 149 printf(" RTC time: %s\n", "n/a");
6d0274f1 150
2667cc25
TA
151 if (have_time) {
152 zero(tm);
153 assert_se(strftime(a, sizeof(a), "%Z, %z", localtime_r(&sec, &tm)) > 0);
154 char_array_0(a);
155 }
156
07a062a7 157 printf(" Time zone: %s (%s)\n"
6d0274f1
LP
158 " NTP enabled: %s\n"
159 "NTP synchronized: %s\n"
160 " RTC in local TZ: %s\n",
2667cc25 161 strna(i->timezone), have_time ? a : "n/a",
2f6a5907
KS
162 i->ntp_capable ? yes_no(i->ntp_enabled) : "n/a",
163 yes_no(i->ntp_synced),
164 yes_no(i->rtc_local));
6d0274f1 165
2667cc25
TA
166 if (have_time) {
167 r = time_get_dst(sec, "/etc/localtime",
168 &tc, &zc, &is_dstc,
169 &tn, &dn, &zn, &is_dstn);
170 if (r < 0)
171 printf(" DST active: %s\n", "n/a");
172 else {
173 printf(" DST active: %s\n", yes_no(is_dstc));
174
175 t = tc - 1;
176 zero(tm);
177 assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm)) > 0);
178 char_array_0(a);
179
180 zero(tm);
181 assert_se(strftime(b, sizeof(b), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tc, &tm)) > 0);
182 char_array_0(b);
183 printf(" Last DST change: DST %s at\n"
184 " %s\n"
185 " %s\n",
186 is_dstc ? "began" : "ended", a, b);
187
188 t = tn - 1;
189 zero(tm);
190 assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm)) > 0);
191 char_array_0(a);
192
193 zero(tm);
194 assert_se(strftime(b, sizeof(b), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tn, &tm)) > 0);
195 char_array_0(b);
196 printf(" Next DST change: DST %s (the clock jumps %s) at\n"
197 " %s\n"
198 " %s\n",
199 is_dstn ? "begins" : "ends", jump_str(dn, s, sizeof(s)), a, b);
200 }
9ff09bcb
SL
201 } else
202 printf(" DST active: %s\n", yes_no(is_dstc));
f18ca9dc 203
2f6a5907 204 if (i->rtc_local)
6d0274f1 205 fputs("\n" ANSI_HIGHLIGHT_ON
c264aeab
KS
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"
07a062a7 210 " RTC in UTC by calling 'timedatectl set-local-rtc 0'" ANSI_HIGHLIGHT_OFF ".\n", stdout);
6d0274f1
LP
211}
212
a281d9c7 213static int show_status(sd_bus *bus, char **args, unsigned n) {
b92bea5d 214 StatusInfo info = {};
9f6eb1cd
KS
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) },
ffc06c35
KS
223 {}
224 };
225 int r;
6d0274f1 226
a281d9c7 227 assert(bus);
6d0274f1 228
ffc06c35
KS
229 r = bus_map_all_properties(bus,
230 "org.freedesktop.timedate1",
231 "/org/freedesktop/timedate1",
9f6eb1cd
KS
232 map,
233 &info);
adacb957
LP
234 if (r < 0) {
235 log_error("Failed to query server: %s", strerror(-r));
ffc06c35 236 goto fail;
adacb957 237 }
6d0274f1
LP
238
239 print_status_info(&info);
ffc06c35
KS
240
241fail:
242 free(info.timezone);
243 return r;
6d0274f1
LP
244}
245
a281d9c7
TA
246static 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;
6d0274f1 249 usec_t t;
6d0274f1
LP
250 int r;
251
252 assert(args);
253 assert(n == 2);
254
255 polkit_agent_open_if_enabled();
256
257 r = parse_timestamp(args[1], &t);
258 if (r < 0) {
259 log_error("Failed to parse time specification: %s", args[1]);
260 return r;
261 }
262
a281d9c7
TA
263 r = sd_bus_call_method(bus,
264 "org.freedesktop.timedate1",
265 "/org/freedesktop/timedate1",
266 "org.freedesktop.timedate1",
267 "SetTime",
268 &error,
269 NULL,
270 "xbb", (int64_t)t, relative, interactive);
271 if (r < 0)
272 log_error("Failed to set time: %s", bus_error_message(&error, -r));
273
274 return r;
6d0274f1
LP
275}
276
a281d9c7
TA
277static int set_timezone(sd_bus *bus, char **args, unsigned n) {
278 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
a281d9c7 279 int r;
6d0274f1
LP
280
281 assert(args);
282 assert(n == 2);
283
284 polkit_agent_open_if_enabled();
285
a281d9c7
TA
286 r = sd_bus_call_method(bus,
287 "org.freedesktop.timedate1",
288 "/org/freedesktop/timedate1",
289 "org.freedesktop.timedate1",
290 "SetTimezone",
291 &error,
292 NULL,
e5609878 293 "sb", args[1], arg_ask_password);
a281d9c7 294 if (r < 0)
07a062a7 295 log_error("Failed to set time zone: %s", bus_error_message(&error, -r));
a281d9c7
TA
296
297 return r;
6d0274f1
LP
298}
299
a281d9c7
TA
300static int set_local_rtc(sd_bus *bus, char **args, unsigned n) {
301 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
e5609878 302 int r, b;
6d0274f1
LP
303
304 assert(args);
305 assert(n == 2);
306
307 polkit_agent_open_if_enabled();
308
e5609878
LP
309 b = parse_boolean(args[1]);
310 if (b < 0) {
6d0274f1 311 log_error("Failed to parse local RTC setting: %s", args[1]);
e5609878 312 return b;
6d0274f1
LP
313 }
314
a281d9c7
TA
315 r = sd_bus_call_method(bus,
316 "org.freedesktop.timedate1",
317 "/org/freedesktop/timedate1",
318 "org.freedesktop.timedate1",
319 "SetLocalRTC",
320 &error,
321 NULL,
e5609878 322 "bbb", b, arg_adjust_system_clock, arg_ask_password);
a281d9c7
TA
323 if (r < 0)
324 log_error("Failed to set local RTC: %s", bus_error_message(&error, -r));
325
326 return r;
6d0274f1
LP
327}
328
a281d9c7
TA
329static int set_ntp(sd_bus *bus, char **args, unsigned n) {
330 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
e5609878 331 int b, r;
6d0274f1
LP
332
333 assert(args);
334 assert(n == 2);
335
336 polkit_agent_open_if_enabled();
337
e5609878
LP
338 b = parse_boolean(args[1]);
339 if (b < 0) {
6d0274f1 340 log_error("Failed to parse NTP setting: %s", args[1]);
e5609878 341 return b;
6d0274f1
LP
342 }
343
a281d9c7
TA
344 r = sd_bus_call_method(bus,
345 "org.freedesktop.timedate1",
346 "/org/freedesktop/timedate1",
347 "org.freedesktop.timedate1",
348 "SetNTP",
349 &error,
350 NULL,
e5609878 351 "bb", b, arg_ask_password);
a281d9c7
TA
352 if (r < 0)
353 log_error("Failed to set ntp: %s", bus_error_message(&error, -r));
354
355 return r;
6d0274f1
LP
356}
357
a281d9c7 358static int list_timezones(sd_bus *bus, char **args, unsigned n) {
6d0274f1 359 _cleanup_strv_free_ char **zones = NULL;
75683450 360 int r;
6d0274f1
LP
361
362 assert(args);
363 assert(n == 1);
364
75683450
LP
365 r = get_timezones(&zones);
366 if (r < 0) {
367 log_error("Failed to read list of time zones: %s", strerror(-r));
368 return r;
6d0274f1
LP
369 }
370
6d0274f1 371 pager_open_if_enabled();
7c2d8094 372 strv_print(zones);
6d0274f1
LP
373
374 return 0;
375}
376
601185b4 377static void help(void) {
7591abd4
LP
378 printf("%s [OPTIONS...] COMMAND ...\n\n"
379 "Query or change system time and date settings.\n\n"
07a062a7 380 " -h --help Show this help message\n"
4f8f66cb
ZJS
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"
6d0274f1 387 "Commands:\n"
4f8f66cb
ZJS
388 " status Show current time settings\n"
389 " set-time TIME Set system time\n"
07a062a7
JSJ
390 " set-timezone ZONE Set system time zone\n"
391 " list-timezones Show known time zones\n"
4f8f66cb
ZJS
392 " set-local-rtc BOOL Control whether RTC is in local time\n"
393 " set-ntp BOOL Control whether NTP is enabled\n",
6d0274f1 394 program_invocation_short_name);
6d0274f1
LP
395}
396
397static int parse_argv(int argc, char *argv[]) {
398
399 enum {
400 ARG_VERSION = 0x100,
401 ARG_NO_PAGER,
c9783430 402 ARG_ADJUST_SYSTEM_CLOCK,
6d0274f1
LP
403 ARG_NO_ASK_PASSWORD
404 };
405
406 static const struct option options[] = {
c9783430
LP
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' },
a281d9c7 411 { "machine", required_argument, NULL, 'M' },
c9783430
LP
412 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
413 { "adjust-system-clock", no_argument, NULL, ARG_ADJUST_SYSTEM_CLOCK },
eb9da376 414 {}
6d0274f1
LP
415 };
416
417 int c;
418
419 assert(argc >= 0);
420 assert(argv);
421
601185b4 422 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
6d0274f1
LP
423
424 switch (c) {
425
426 case 'h':
601185b4
ZJS
427 help();
428 return 0;
6d0274f1
LP
429
430 case ARG_VERSION:
431 puts(PACKAGE_STRING);
6d0274f1
LP
432 puts(SYSTEMD_FEATURES);
433 return 0;
434
a281d9c7
TA
435 case 'H':
436 arg_transport = BUS_TRANSPORT_REMOTE;
437 arg_host = optarg;
6d0274f1
LP
438 break;
439
a281d9c7
TA
440 case 'M':
441 arg_transport = BUS_TRANSPORT_CONTAINER;
442 arg_host = optarg;
6d0274f1
LP
443 break;
444
546158bc
JJ
445 case ARG_NO_ASK_PASSWORD:
446 arg_ask_password = false;
447 break;
448
c9783430
LP
449 case ARG_ADJUST_SYSTEM_CLOCK:
450 arg_adjust_system_clock = true;
6d0274f1
LP
451 break;
452
453 case ARG_NO_PAGER:
454 arg_no_pager = true;
455 break;
456
457 case '?':
458 return -EINVAL;
459
460 default:
eb9da376 461 assert_not_reached("Unhandled option");
6d0274f1 462 }
6d0274f1
LP
463
464 return 1;
465}
466
a281d9c7 467static int timedatectl_main(sd_bus *bus, int argc, char *argv[]) {
6d0274f1
LP
468
469 static const struct {
470 const char* verb;
471 const enum {
472 MORE,
473 LESS,
474 EQUAL
475 } argc_cmp;
476 const int argc;
a281d9c7 477 int (* const dispatch)(sd_bus *bus, char **args, unsigned n);
6d0274f1
LP
478 } verbs[] = {
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, },
485 };
486
487 int left;
488 unsigned i;
489
490 assert(argc >= 0);
491 assert(argv);
6d0274f1
LP
492
493 left = argc - optind;
494
495 if (left <= 0)
496 /* Special rule: no arguments means "status" */
497 i = 0;
498 else {
499 if (streq(argv[optind], "help")) {
500 help();
501 return 0;
502 }
503
504 for (i = 0; i < ELEMENTSOF(verbs); i++)
505 if (streq(argv[optind], verbs[i].verb))
506 break;
507
508 if (i >= ELEMENTSOF(verbs)) {
509 log_error("Unknown operation %s", argv[optind]);
510 return -EINVAL;
511 }
512 }
513
514 switch (verbs[i].argc_cmp) {
515
516 case EQUAL:
517 if (left != verbs[i].argc) {
518 log_error("Invalid number of arguments.");
519 return -EINVAL;
520 }
521
522 break;
523
524 case MORE:
525 if (left < verbs[i].argc) {
526 log_error("Too few arguments.");
527 return -EINVAL;
528 }
529
530 break;
531
532 case LESS:
533 if (left > verbs[i].argc) {
534 log_error("Too many arguments.");
535 return -EINVAL;
536 }
537
538 break;
539
540 default:
541 assert_not_reached("Unknown comparison operator.");
542 }
543
6d0274f1
LP
544 return verbs[i].dispatch(bus, argv + optind, left);
545}
546
547int main(int argc, char *argv[]) {
a281d9c7 548 _cleanup_bus_unref_ sd_bus *bus = NULL;
84f6181c 549 int r;
6d0274f1 550
a9cdc94f 551 setlocale(LC_ALL, "");
6d0274f1
LP
552 log_parse_environment();
553 log_open();
554
555 r = parse_argv(argc, argv);
84f6181c 556 if (r <= 0)
6d0274f1 557 goto finish;
6d0274f1 558
a281d9c7
TA
559 r = bus_open_transport(arg_transport, arg_host, false, &bus);
560 if (r < 0) {
561 log_error("Failed to create bus connection: %s", strerror(-r));
a281d9c7 562 goto finish;
6d0274f1
LP
563 }
564
a281d9c7 565 r = timedatectl_main(bus, argc, argv);
6d0274f1 566
a281d9c7 567finish:
6d0274f1
LP
568 pager_close();
569
84f6181c 570 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
6d0274f1 571}