]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/timedate/timedatectl.c
Merge pull request #176 from filbranden/test_cgroup_mask1
[thirdparty/systemd.git] / src / timedate / timedatectl.c
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
7 Copyright 2013 Kay Sievers
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 <getopt.h>
26 #include <locale.h>
27
28 #include "sd-bus.h"
29 #include "bus-util.h"
30 #include "bus-error.h"
31 #include "util.h"
32 #include "spawn-polkit-agent.h"
33 #include "build.h"
34 #include "strv.h"
35 #include "pager.h"
36 #include "terminal-util.h"
37
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;
43
44 static void pager_open_if_enabled(void) {
45
46 if (arg_no_pager)
47 return;
48
49 pager_open(false);
50 }
51
52 static void polkit_agent_open_if_enabled(void) {
53
54 /* Open the polkit agent as a child process if necessary */
55 if (!arg_ask_password)
56 return;
57
58 if (arg_transport != BUS_TRANSPORT_LOCAL)
59 return;
60
61 polkit_agent_open();
62 }
63
64 typedef struct StatusInfo {
65 usec_t time;
66 char *timezone;
67
68 usec_t rtc_time;
69 bool rtc_local;
70
71 bool ntp_enabled;
72 bool ntp_capable;
73 bool ntp_synced;
74 } StatusInfo;
75
76 static void status_info_clear(StatusInfo *info) {
77 if (info) {
78 free(info->timezone);
79 zero(*info);
80 }
81 }
82
83 static void print_status_info(const StatusInfo *i) {
84 char a[FORMAT_TIMESTAMP_MAX];
85 struct tm tm;
86 time_t sec;
87 bool have_time = false;
88 const char *old_tz = NULL, *tz;
89 int r;
90
91 assert(i);
92
93 /* Save the old $TZ */
94 tz = getenv("TZ");
95 if (tz)
96 old_tz = strdupa(tz);
97
98 /* Set the new $TZ */
99 if (setenv("TZ", i->timezone, true) < 0)
100 log_warning_errno(errno, "Failed to set TZ environment variable, ignoring: %m");
101 else
102 tzset();
103
104 if (i->time != 0) {
105 sec = (time_t) (i->time / USEC_PER_SEC);
106 have_time = true;
107 } else if (IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE)) {
108 sec = time(NULL);
109 have_time = true;
110 } else
111 log_warning("Could not get time from timedated and not operating locally, ignoring.");
112
113 if (have_time) {
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);
119 } else {
120 printf(" Local time: %s\n", "n/a");
121 printf(" Universal time: %s\n", "n/a");
122 }
123
124 if (i->rtc_time > 0) {
125 time_t rtc_sec;
126
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);
130 } else
131 printf(" RTC time: %s\n", "n/a");
132
133 if (have_time)
134 xstrftime(a, "%Z, %z", localtime_r(&sec, &tm));
135
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
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));
154
155 if (i->rtc_local)
156 fputs("\n" ANSI_HIGHLIGHT_ON
157 "Warning: The system is configured to read the RTC time in the local time zone. This\n"
158 " mode can not be fully supported. It will create various problems with time\n"
159 " zone changes and daylight saving time adjustments. The RTC time is never updated,\n"
160 " it relies on external facilities to maintain it. If at all possible, use\n"
161 " RTC in UTC by calling 'timedatectl set-local-rtc 0'" ANSI_HIGHLIGHT_OFF ".\n", stdout);
162 }
163
164 static int show_status(sd_bus *bus, char **args, unsigned n) {
165 _cleanup_(status_info_clear) StatusInfo info = {};
166 static const struct bus_properties_map map[] = {
167 { "Timezone", "s", NULL, offsetof(StatusInfo, timezone) },
168 { "LocalRTC", "b", NULL, offsetof(StatusInfo, rtc_local) },
169 { "NTP", "b", NULL, offsetof(StatusInfo, ntp_enabled) },
170 { "CanNTP", "b", NULL, offsetof(StatusInfo, ntp_capable) },
171 { "NTPSynchronized", "b", NULL, offsetof(StatusInfo, ntp_synced) },
172 { "TimeUSec", "t", NULL, offsetof(StatusInfo, time) },
173 { "RTCTimeUSec", "t", NULL, offsetof(StatusInfo, rtc_time) },
174 {}
175 };
176 int r;
177
178 assert(bus);
179
180 r = bus_map_all_properties(bus,
181 "org.freedesktop.timedate1",
182 "/org/freedesktop/timedate1",
183 map,
184 &info);
185 if (r < 0)
186 return log_error_errno(r, "Failed to query server: %m");
187
188 print_status_info(&info);
189
190 return r;
191 }
192
193 static int set_time(sd_bus *bus, char **args, unsigned n) {
194 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
195 bool relative = false, interactive = arg_ask_password;
196 usec_t t;
197 int r;
198
199 assert(args);
200 assert(n == 2);
201
202 polkit_agent_open_if_enabled();
203
204 r = parse_timestamp(args[1], &t);
205 if (r < 0) {
206 log_error("Failed to parse time specification: %s", args[1]);
207 return r;
208 }
209
210 r = sd_bus_call_method(bus,
211 "org.freedesktop.timedate1",
212 "/org/freedesktop/timedate1",
213 "org.freedesktop.timedate1",
214 "SetTime",
215 &error,
216 NULL,
217 "xbb", (int64_t)t, relative, interactive);
218 if (r < 0)
219 log_error("Failed to set time: %s", bus_error_message(&error, -r));
220
221 return r;
222 }
223
224 static int set_timezone(sd_bus *bus, char **args, unsigned n) {
225 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
226 int r;
227
228 assert(args);
229 assert(n == 2);
230
231 polkit_agent_open_if_enabled();
232
233 r = sd_bus_call_method(bus,
234 "org.freedesktop.timedate1",
235 "/org/freedesktop/timedate1",
236 "org.freedesktop.timedate1",
237 "SetTimezone",
238 &error,
239 NULL,
240 "sb", args[1], arg_ask_password);
241 if (r < 0)
242 log_error("Failed to set time zone: %s", bus_error_message(&error, -r));
243
244 return r;
245 }
246
247 static int set_local_rtc(sd_bus *bus, char **args, unsigned n) {
248 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
249 int r, b;
250
251 assert(args);
252 assert(n == 2);
253
254 polkit_agent_open_if_enabled();
255
256 b = parse_boolean(args[1]);
257 if (b < 0) {
258 log_error("Failed to parse local RTC setting: %s", args[1]);
259 return b;
260 }
261
262 r = sd_bus_call_method(bus,
263 "org.freedesktop.timedate1",
264 "/org/freedesktop/timedate1",
265 "org.freedesktop.timedate1",
266 "SetLocalRTC",
267 &error,
268 NULL,
269 "bbb", b, arg_adjust_system_clock, arg_ask_password);
270 if (r < 0)
271 log_error("Failed to set local RTC: %s", bus_error_message(&error, -r));
272
273 return r;
274 }
275
276 static int set_ntp(sd_bus *bus, char **args, unsigned n) {
277 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
278 int b, r;
279
280 assert(args);
281 assert(n == 2);
282
283 polkit_agent_open_if_enabled();
284
285 b = parse_boolean(args[1]);
286 if (b < 0) {
287 log_error("Failed to parse NTP setting: %s", args[1]);
288 return b;
289 }
290
291 r = sd_bus_call_method(bus,
292 "org.freedesktop.timedate1",
293 "/org/freedesktop/timedate1",
294 "org.freedesktop.timedate1",
295 "SetNTP",
296 &error,
297 NULL,
298 "bb", b, arg_ask_password);
299 if (r < 0)
300 log_error("Failed to set ntp: %s", bus_error_message(&error, -r));
301
302 return r;
303 }
304
305 static int list_timezones(sd_bus *bus, char **args, unsigned n) {
306 _cleanup_strv_free_ char **zones = NULL;
307 int r;
308
309 assert(args);
310 assert(n == 1);
311
312 r = get_timezones(&zones);
313 if (r < 0)
314 return log_error_errno(r, "Failed to read list of time zones: %m");
315
316 pager_open_if_enabled();
317 strv_print(zones);
318
319 return 0;
320 }
321
322 static void help(void) {
323 printf("%s [OPTIONS...] COMMAND ...\n\n"
324 "Query or change system time and date settings.\n\n"
325 " -h --help Show this help message\n"
326 " --version Show package version\n"
327 " --no-pager Do not pipe output into a pager\n"
328 " --no-ask-password Do not prompt for password\n"
329 " -H --host=[USER@]HOST Operate on remote host\n"
330 " -M --machine=CONTAINER Operate on local container\n"
331 " --adjust-system-clock Adjust system clock when changing local RTC mode\n\n"
332 "Commands:\n"
333 " status Show current time settings\n"
334 " set-time TIME Set system time\n"
335 " set-timezone ZONE Set system time zone\n"
336 " list-timezones Show known time zones\n"
337 " set-local-rtc BOOL Control whether RTC is in local time\n"
338 " set-ntp BOOL Enable or disable network time synchronization\n",
339 program_invocation_short_name);
340 }
341
342 static int parse_argv(int argc, char *argv[]) {
343
344 enum {
345 ARG_VERSION = 0x100,
346 ARG_NO_PAGER,
347 ARG_ADJUST_SYSTEM_CLOCK,
348 ARG_NO_ASK_PASSWORD
349 };
350
351 static const struct option options[] = {
352 { "help", no_argument, NULL, 'h' },
353 { "version", no_argument, NULL, ARG_VERSION },
354 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
355 { "host", required_argument, NULL, 'H' },
356 { "machine", required_argument, NULL, 'M' },
357 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
358 { "adjust-system-clock", no_argument, NULL, ARG_ADJUST_SYSTEM_CLOCK },
359 {}
360 };
361
362 int c;
363
364 assert(argc >= 0);
365 assert(argv);
366
367 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
368
369 switch (c) {
370
371 case 'h':
372 help();
373 return 0;
374
375 case ARG_VERSION:
376 puts(PACKAGE_STRING);
377 puts(SYSTEMD_FEATURES);
378 return 0;
379
380 case 'H':
381 arg_transport = BUS_TRANSPORT_REMOTE;
382 arg_host = optarg;
383 break;
384
385 case 'M':
386 arg_transport = BUS_TRANSPORT_MACHINE;
387 arg_host = optarg;
388 break;
389
390 case ARG_NO_ASK_PASSWORD:
391 arg_ask_password = false;
392 break;
393
394 case ARG_ADJUST_SYSTEM_CLOCK:
395 arg_adjust_system_clock = true;
396 break;
397
398 case ARG_NO_PAGER:
399 arg_no_pager = true;
400 break;
401
402 case '?':
403 return -EINVAL;
404
405 default:
406 assert_not_reached("Unhandled option");
407 }
408
409 return 1;
410 }
411
412 static int timedatectl_main(sd_bus *bus, int argc, char *argv[]) {
413
414 static const struct {
415 const char* verb;
416 const enum {
417 MORE,
418 LESS,
419 EQUAL
420 } argc_cmp;
421 const int argc;
422 int (* const dispatch)(sd_bus *bus, char **args, unsigned n);
423 } verbs[] = {
424 { "status", LESS, 1, show_status },
425 { "set-time", EQUAL, 2, set_time },
426 { "set-timezone", EQUAL, 2, set_timezone },
427 { "list-timezones", EQUAL, 1, list_timezones },
428 { "set-local-rtc", EQUAL, 2, set_local_rtc },
429 { "set-ntp", EQUAL, 2, set_ntp, },
430 };
431
432 int left;
433 unsigned i;
434
435 assert(argc >= 0);
436 assert(argv);
437
438 left = argc - optind;
439
440 if (left <= 0)
441 /* Special rule: no arguments means "status" */
442 i = 0;
443 else {
444 if (streq(argv[optind], "help")) {
445 help();
446 return 0;
447 }
448
449 for (i = 0; i < ELEMENTSOF(verbs); i++)
450 if (streq(argv[optind], verbs[i].verb))
451 break;
452
453 if (i >= ELEMENTSOF(verbs)) {
454 log_error("Unknown operation %s", argv[optind]);
455 return -EINVAL;
456 }
457 }
458
459 switch (verbs[i].argc_cmp) {
460
461 case EQUAL:
462 if (left != verbs[i].argc) {
463 log_error("Invalid number of arguments.");
464 return -EINVAL;
465 }
466
467 break;
468
469 case MORE:
470 if (left < verbs[i].argc) {
471 log_error("Too few arguments.");
472 return -EINVAL;
473 }
474
475 break;
476
477 case LESS:
478 if (left > verbs[i].argc) {
479 log_error("Too many arguments.");
480 return -EINVAL;
481 }
482
483 break;
484
485 default:
486 assert_not_reached("Unknown comparison operator.");
487 }
488
489 return verbs[i].dispatch(bus, argv + optind, left);
490 }
491
492 int main(int argc, char *argv[]) {
493 _cleanup_bus_close_unref_ sd_bus *bus = NULL;
494 int r;
495
496 setlocale(LC_ALL, "");
497 log_parse_environment();
498 log_open();
499
500 r = parse_argv(argc, argv);
501 if (r <= 0)
502 goto finish;
503
504 r = bus_open_transport(arg_transport, arg_host, false, &bus);
505 if (r < 0) {
506 log_error_errno(r, "Failed to create bus connection: %m");
507 goto finish;
508 }
509
510 r = timedatectl_main(bus, argc, argv);
511
512 finish:
513 pager_close();
514
515 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
516 }