]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/timedate/timedatectl.c
test-cpu-set-util.c: fix typo in comment (#6916)
[thirdparty/systemd.git] / src / timedate / timedatectl.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2012 Lennart Poettering
5 Copyright 2013 Kay Sievers
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
21 #include <getopt.h>
22 #include <locale.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
25
26 #include "sd-bus.h"
27
28 #include "bus-error.h"
29 #include "bus-util.h"
30 #include "pager.h"
31 #include "parse-util.h"
32 #include "spawn-polkit-agent.h"
33 #include "strv.h"
34 #include "terminal-util.h"
35 #include "util.h"
36
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;
42
43 static void polkit_agent_open_if_enabled(void) {
44
45 /* Open the polkit agent as a child process if necessary */
46 if (!arg_ask_password)
47 return;
48
49 if (arg_transport != BUS_TRANSPORT_LOCAL)
50 return;
51
52 polkit_agent_open();
53 }
54
55 typedef struct StatusInfo {
56 usec_t time;
57 char *timezone;
58
59 usec_t rtc_time;
60 int rtc_local;
61
62 int ntp_enabled;
63 int ntp_capable;
64 int ntp_synced;
65 } StatusInfo;
66
67 static void status_info_clear(StatusInfo *info) {
68 if (info) {
69 free(info->timezone);
70 zero(*info);
71 }
72 }
73
74 static void print_status_info(const StatusInfo *i) {
75 char a[FORMAT_TIMESTAMP_MAX];
76 struct tm tm;
77 time_t sec;
78 bool have_time = false;
79 const char *old_tz = NULL, *tz;
80 int r;
81
82 assert(i);
83
84 /* Save the old $TZ */
85 tz = getenv("TZ");
86 if (tz)
87 old_tz = strdupa(tz);
88
89 /* Set the new $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");
92 else
93 tzset();
94
95 if (i->time != 0) {
96 sec = (time_t) (i->time / USEC_PER_SEC);
97 have_time = true;
98 } else if (IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE)) {
99 sec = time(NULL);
100 have_time = true;
101 } else
102 log_warning("Could not get time from timedated and not operating locally, ignoring.");
103
104 if (have_time) {
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);
107
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);
110 } else {
111 printf(" Local time: %s\n", "n/a");
112 printf(" Universal time: %s\n", "n/a");
113 }
114
115 if (i->rtc_time > 0) {
116 time_t rtc_sec;
117
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);
121 } else
122 printf(" RTC time: %s\n", "n/a");
123
124 if (have_time)
125 xstrftime(a, "%Z, %z", localtime_r(&sec, &tm));
126
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
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",
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));
145
146 if (i->rtc_local)
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());
154 }
155
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) },
166 {}
167 };
168
169 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
170 int r;
171
172 assert(bus);
173
174 r = bus_map_all_properties(bus,
175 "org.freedesktop.timedate1",
176 "/org/freedesktop/timedate1",
177 map,
178 &error,
179 &info);
180 if (r < 0)
181 return log_error_errno(r, "Failed to query server: %s", bus_error_message(&error, r));
182
183 print_status_info(&info);
184
185 return r;
186 }
187
188 static int set_time(sd_bus *bus, char **args, unsigned n) {
189 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
190 bool relative = false, interactive = arg_ask_password;
191 usec_t t;
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
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;
217 }
218
219 static int set_timezone(sd_bus *bus, char **args, unsigned n) {
220 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
221 int r;
222
223 assert(args);
224 assert(n == 2);
225
226 polkit_agent_open_if_enabled();
227
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,
235 "sb", args[1], arg_ask_password);
236 if (r < 0)
237 log_error("Failed to set time zone: %s", bus_error_message(&error, -r));
238
239 return r;
240 }
241
242 static int set_local_rtc(sd_bus *bus, char **args, unsigned n) {
243 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
244 int r, b;
245
246 assert(args);
247 assert(n == 2);
248
249 polkit_agent_open_if_enabled();
250
251 b = parse_boolean(args[1]);
252 if (b < 0) {
253 log_error("Failed to parse local RTC setting: %s", args[1]);
254 return b;
255 }
256
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,
264 "bbb", b, arg_adjust_system_clock, arg_ask_password);
265 if (r < 0)
266 log_error("Failed to set local RTC: %s", bus_error_message(&error, -r));
267
268 return r;
269 }
270
271 static int set_ntp(sd_bus *bus, char **args, unsigned n) {
272 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
273 int b, r;
274
275 assert(args);
276 assert(n == 2);
277
278 polkit_agent_open_if_enabled();
279
280 b = parse_boolean(args[1]);
281 if (b < 0) {
282 log_error("Failed to parse NTP setting: %s", args[1]);
283 return b;
284 }
285
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,
293 "bb", b, arg_ask_password);
294 if (r < 0)
295 log_error("Failed to set ntp: %s", bus_error_message(&error, -r));
296
297 return r;
298 }
299
300 static int list_timezones(sd_bus *bus, char **args, unsigned n) {
301 _cleanup_strv_free_ char **zones = NULL;
302 int r;
303
304 assert(args);
305 assert(n == 1);
306
307 r = get_timezones(&zones);
308 if (r < 0)
309 return log_error_errno(r, "Failed to read list of time zones: %m");
310
311 pager_open(arg_no_pager, false);
312 strv_print(zones);
313
314 return 0;
315 }
316
317 static void help(void) {
318 printf("%s [OPTIONS...] COMMAND ...\n\n"
319 "Query or change system time and date settings.\n\n"
320 " -h --help Show this help message\n"
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"
327 "Commands:\n"
328 " status Show current time settings\n"
329 " set-time TIME Set system time\n"
330 " set-timezone ZONE Set system time zone\n"
331 " list-timezones Show known time zones\n"
332 " set-local-rtc BOOL Control whether RTC is in local time\n"
333 " set-ntp BOOL Enable or disable network time synchronization\n",
334 program_invocation_short_name);
335 }
336
337 static int parse_argv(int argc, char *argv[]) {
338
339 enum {
340 ARG_VERSION = 0x100,
341 ARG_NO_PAGER,
342 ARG_ADJUST_SYSTEM_CLOCK,
343 ARG_NO_ASK_PASSWORD
344 };
345
346 static const struct option options[] = {
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' },
351 { "machine", required_argument, NULL, 'M' },
352 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
353 { "adjust-system-clock", no_argument, NULL, ARG_ADJUST_SYSTEM_CLOCK },
354 {}
355 };
356
357 int c;
358
359 assert(argc >= 0);
360 assert(argv);
361
362 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
363
364 switch (c) {
365
366 case 'h':
367 help();
368 return 0;
369
370 case ARG_VERSION:
371 return version();
372
373 case 'H':
374 arg_transport = BUS_TRANSPORT_REMOTE;
375 arg_host = optarg;
376 break;
377
378 case 'M':
379 arg_transport = BUS_TRANSPORT_MACHINE;
380 arg_host = optarg;
381 break;
382
383 case ARG_NO_ASK_PASSWORD:
384 arg_ask_password = false;
385 break;
386
387 case ARG_ADJUST_SYSTEM_CLOCK:
388 arg_adjust_system_clock = true;
389 break;
390
391 case ARG_NO_PAGER:
392 arg_no_pager = true;
393 break;
394
395 case '?':
396 return -EINVAL;
397
398 default:
399 assert_not_reached("Unhandled option");
400 }
401
402 return 1;
403 }
404
405 static int timedatectl_main(sd_bus *bus, int argc, char *argv[]) {
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;
415 int (* const dispatch)(sd_bus *bus, char **args, unsigned n);
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);
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
482 return verbs[i].dispatch(bus, argv + optind, left);
483 }
484
485 int main(int argc, char *argv[]) {
486 sd_bus *bus = NULL;
487 int r;
488
489 setlocale(LC_ALL, "");
490 log_parse_environment();
491 log_open();
492
493 r = parse_argv(argc, argv);
494 if (r <= 0)
495 goto finish;
496
497 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
498 if (r < 0) {
499 log_error_errno(r, "Failed to create bus connection: %m");
500 goto finish;
501 }
502
503 r = timedatectl_main(bus, argc, argv);
504
505 finish:
506 sd_bus_flush_close_unref(bus);
507 pager_close();
508
509 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
510 }