]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/timedate/timedated.c
nspawn-expose-ports: fix a typo in error message
[thirdparty/systemd.git] / src / timedate / timedated.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
f401e48c 2
f401e48c
LP
3#include <errno.h>
4#include <string.h>
ca78ad1d
ZJS
5#include <sys/stat.h>
6#include <sys/types.h>
f401e48c
LP
7#include <unistd.h>
8
40ca29a1 9#include "sd-bus.h"
f4f15635
LP
10#include "sd-event.h"
11#include "sd-messages.h"
40ca29a1 12
b5efdb8a 13#include "alloc-util.h"
96aad8d1 14#include "bus-common-errors.h"
f4f15635
LP
15#include "bus-error.h"
16#include "bus-util.h"
17#include "clock-util.h"
18#include "def.h"
f4f15635 19#include "fileio-label.h"
e4de7287 20#include "fileio.h"
f4f15635 21#include "fs-util.h"
5d280742
YW
22#include "hashmap.h"
23#include "list.h"
1f47bc33 24#include "main-func.h"
0a970718 25#include "memory-util.h"
36dd5ffd 26#include "missing_capability.h"
f4f15635 27#include "path-util.h"
d7b8eec7 28#include "selinux-util.h"
754f0269 29#include "signal-util.h"
5d280742 30#include "string-util.h"
f4f15635 31#include "strv.h"
5d280742
YW
32#include "unit-def.h"
33#include "unit-name.h"
ee104e11 34#include "user-util.h"
f401e48c
LP
35
36#define NULL_ADJTIME_UTC "0.0 0 0\n0\nUTC\n"
37#define NULL_ADJTIME_LOCAL "0.0 0 0\n0\nLOCAL\n"
38
5d280742
YW
39typedef struct UnitStatusInfo {
40 char *name;
41 char *load_state;
42 char *unit_file_state;
43 char *active_state;
cf3872bd 44 char *path;
5d280742
YW
45
46 LIST_FIELDS(struct UnitStatusInfo, units);
47} UnitStatusInfo;
48
40ca29a1 49typedef struct Context {
d200735e
MS
50 char *zone;
51 bool local_rtc;
40ca29a1 52 Hashmap *polkit_registry;
2770af85 53 sd_bus_message *cache;
5d280742 54
3af0a96c 55 sd_bus_slot *slot_job_removed;
3af0a96c 56
5d280742 57 LIST_HEAD(UnitStatusInfo, units);
40ca29a1 58} Context;
f401e48c 59
5d280742
YW
60static void unit_status_info_clear(UnitStatusInfo *p) {
61 assert(p);
62
63 p->load_state = mfree(p->load_state);
64 p->unit_file_state = mfree(p->unit_file_state);
65 p->active_state = mfree(p->active_state);
66}
67
68static void unit_status_info_free(UnitStatusInfo *p) {
69 assert(p);
70
71 unit_status_info_clear(p);
72 free(p->name);
cf3872bd 73 free(p->path);
5d280742
YW
74 free(p);
75}
76
1f47bc33 77static void context_clear(Context *c) {
5d280742
YW
78 UnitStatusInfo *p;
79
40ca29a1 80 assert(c);
f401e48c 81
82d115d9 82 free(c->zone);
36e34057 83 bus_verify_polkit_async_registry_free(c->polkit_registry);
2770af85 84 sd_bus_message_unref(c->cache);
5d280742 85
3af0a96c 86 sd_bus_slot_unref(c->slot_job_removed);
3af0a96c 87
5d280742
YW
88 while ((p = c->units)) {
89 LIST_REMOVE(units, c->units, p);
90 unit_status_info_free(p);
91 }
92}
93
94static int context_add_ntp_service(Context *c, const char *s) {
5d280742
YW
95 UnitStatusInfo *u;
96
97 if (!unit_name_is_valid(s, UNIT_NAME_PLAIN))
98 return -EINVAL;
99
100 /* Do not add this if it is already listed */
101 LIST_FOREACH(units, u, c->units)
102 if (streq(u->name, s))
103 return 0;
104
105 u = new0(UnitStatusInfo, 1);
106 if (!u)
107 return -ENOMEM;
108
109 u->name = strdup(s);
110 if (!u->name) {
111 free(u);
112 return -ENOMEM;
113 }
114
115 LIST_APPEND(units, c->units, u);
116
117 return 0;
118}
119
120static int context_parse_ntp_services(Context *c) {
121 const char *env, *p;
122 int r;
123
124 assert(c);
125
126 env = getenv("SYSTEMD_TIMEDATED_NTP_SERVICES");
127 if (!env) {
128 r = context_add_ntp_service(c, "systemd-timesyncd.service");
129 if (r < 0)
130 log_warning_errno(r, "Failed to add NTP service \"systemd-timesyncd.service\", ignoring: %m");
131
132 return 0;
133 }
134
135 for (p = env;;) {
136 _cleanup_free_ char *word = NULL;
137
138 r = extract_first_word(&p, &word, ":", 0);
139 if (r == 0)
140 break;
141 if (r == -ENOMEM)
142 return log_oom();
143 if (r < 0) {
144 log_error("Invalid syntax, ignoring: %s", env);
145 break;
146 }
147
148 r = context_add_ntp_service(c, word);
149 if (r < 0)
150 log_warning_errno(r, "Failed to add NTP service \"%s\", ignoring: %m", word);
151 }
152
153 return 0;
154}
155
156static int context_ntp_service_is_active(Context *c) {
157 UnitStatusInfo *info;
158 int count = 0;
159
160 assert(c);
161
162 /* Call context_update_ntp_status() to update UnitStatusInfo before calling this. */
163
164 LIST_FOREACH(units, info, c->units)
84a87726 165 count += !STRPTR_IN_SET(info->active_state, "inactive", "failed");
5d280742
YW
166
167 return count;
168}
169
170static int context_ntp_service_is_enabled(Context *c) {
171 UnitStatusInfo *info;
172 int count = 0;
173
174 assert(c);
175
176 /* Call context_update_ntp_status() to update UnitStatusInfo before calling this. */
177
178 LIST_FOREACH(units, info, c->units)
84a87726 179 count += !STRPTR_IN_SET(info->unit_file_state, "masked", "masked-runtime", "disabled", "bad");
5d280742
YW
180
181 return count;
182}
183
184static int context_ntp_service_exists(Context *c) {
185 UnitStatusInfo *info;
186 int count = 0;
187
188 assert(c);
189
190 /* Call context_update_ntp_status() to update UnitStatusInfo before calling this. */
191
192 LIST_FOREACH(units, info, c->units)
193 count += streq_ptr(info->load_state, "loaded");
194
195 return count;
f401e48c
LP
196}
197
40ca29a1 198static int context_read_data(Context *c) {
424a19f8 199 _cleanup_free_ char *t = NULL;
40ca29a1
LP
200 int r;
201
202 assert(c);
f401e48c 203
5c904ba5
LP
204 r = get_timezone(&t);
205 if (r == -EINVAL)
206 log_warning_errno(r, "/etc/localtime should be a symbolic link to a time zone data file in /usr/share/zoneinfo/.");
207 else if (r < 0)
208 log_warning_errno(r, "Failed to get target of /etc/localtime: %m");
92c4ef2d 209
f9ecfd3b 210 free_and_replace(c->zone, t);
f401e48c 211
6369641d 212 c->local_rtc = clock_is_localtime(NULL) > 0;
f401e48c
LP
213
214 return 0;
215}
216
40ca29a1 217static int context_write_data_timezone(Context *c) {
424a19f8 218 _cleanup_free_ char *p = NULL;
40ca29a1
LP
219 int r = 0;
220
221 assert(c);
e19a21a8 222
40ca29a1 223 if (isempty(c->zone)) {
424a19f8 224 if (unlink("/etc/localtime") < 0 && errno != ENOENT)
f401e48c
LP
225 r = -errno;
226
f401e48c
LP
227 return r;
228 }
229
40ca29a1 230 p = strappend("../usr/share/zoneinfo/", c->zone);
0d0f0c50
SL
231 if (!p)
232 return log_oom();
f401e48c 233
424a19f8 234 r = symlink_atomic(p, "/etc/localtime");
f401e48c 235 if (r < 0)
424a19f8 236 return r;
f401e48c 237
f401e48c
LP
238 return 0;
239}
240
40ca29a1 241static int context_write_data_local_rtc(Context *c) {
f401e48c 242 int r;
7fd1b19b 243 _cleanup_free_ char *s = NULL, *w = NULL;
f401e48c 244
40ca29a1
LP
245 assert(c);
246
f401e48c
LP
247 r = read_full_file("/etc/adjtime", &s, NULL);
248 if (r < 0) {
249 if (r != -ENOENT)
250 return r;
251
40ca29a1 252 if (!c->local_rtc)
f401e48c
LP
253 return 0;
254
255 w = strdup(NULL_ADJTIME_LOCAL);
256 if (!w)
257 return -ENOMEM;
258 } else {
c9410dd4 259 char *p;
8f462d87 260 const char *e = "\n"; /* default if there is less than 3 lines */
c9410dd4 261 const char *prepend = "";
f401e48c
LP
262 size_t a, b;
263
c9410dd4 264 p = strchrnul(s, '\n');
cb971cc0 265 if (*p == '\0')
c9410dd4
MP
266 /* only one line, no \n terminator */
267 prepend = "\n0\n";
cb971cc0 268 else if (p[1] == '\0') {
c9410dd4
MP
269 /* only one line, with \n terminator */
270 ++p;
271 prepend = "0\n";
272 } else {
273 p = strchr(p+1, '\n');
274 if (!p) {
275 /* only two lines, no \n terminator */
276 prepend = "\n";
277 p = s + strlen(s);
278 } else {
279 char *end;
280 /* third line might have a \n terminator or not */
281 p++;
282 end = strchr(p, '\n');
283 /* if we actually have a fourth line, use that as suffix "e", otherwise the default \n */
284 if (end)
285 e = end;
286 }
287 }
f401e48c
LP
288
289 a = p - s;
290 b = strlen(e);
291
c9410dd4 292 w = new(char, a + (c->local_rtc ? 5 : 3) + strlen(prepend) + b + 1);
d257f05a 293 if (!w)
f401e48c 294 return -ENOMEM;
f401e48c 295
c9410dd4 296 *(char*) mempcpy(stpcpy(stpcpy(mempcpy(w, s, a), prepend), c->local_rtc ? "LOCAL" : "UTC"), e, b) = 0;
f401e48c
LP
297
298 if (streq(w, NULL_ADJTIME_UTC)) {
d257f05a 299 if (unlink("/etc/adjtime") < 0)
f401e48c
LP
300 if (errno != ENOENT)
301 return -errno;
f401e48c
LP
302
303 return 0;
304 }
305 }
40ca29a1 306
c3dacc8b 307 mac_selinux_init();
d257f05a 308 return write_string_file_atomic_label("/etc/adjtime", w);
f401e48c
LP
309}
310
5d280742
YW
311static int context_update_ntp_status(Context *c, sd_bus *bus, sd_bus_message *m) {
312 static const struct bus_properties_map map[] = {
313 { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) },
314 { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state) },
315 { "UnitFileState", "s", NULL, offsetof(UnitStatusInfo, unit_file_state) },
316 {}
317 };
5d280742 318 UnitStatusInfo *u;
c5f0532f
LP
319 int r;
320
40ca29a1 321 assert(c);
c5f0532f
LP
322 assert(bus);
323
2770af85
YW
324 /* Suppress calling context_update_ntp_status() multiple times within single DBus transaction. */
325 if (m) {
326 if (m == c->cache)
327 return 0;
2aa4c315 328
2770af85
YW
329 sd_bus_message_unref(c->cache);
330 c->cache = sd_bus_message_ref(m);
331 }
2aa4c315 332
5d280742
YW
333 LIST_FOREACH(units, u, c->units) {
334 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
5d280742 335 _cleanup_free_ char *path = NULL;
c5f0532f 336
5d280742 337 unit_status_info_clear(u);
40ca29a1 338
5d280742
YW
339 path = unit_dbus_path_from_name(u->name);
340 if (!path)
341 return -ENOMEM;
342
343 r = bus_map_all_properties(
344 bus,
345 "org.freedesktop.systemd1",
346 path,
347 map,
348 BUS_MAP_STRDUP,
349 &error,
350 NULL,
351 u);
352 if (r < 0)
353 return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r));
354 }
c5f0532f 355
40ca29a1 356 return 0;
c5f0532f
LP
357}
358
3af0a96c 359static int match_job_removed(sd_bus_message *m, void *userdata, sd_bus_error *error) {
3af0a96c 360 Context *c = userdata;
cf3872bd
YW
361 UnitStatusInfo *u;
362 const char *path;
363 unsigned n = 0;
3af0a96c
YW
364 int r;
365
366 assert(c);
367 assert(m);
368
369 r = sd_bus_message_read(m, "uoss", NULL, &path, NULL, NULL);
370 if (r < 0) {
371 bus_log_parse_error(r);
372 return 0;
373 }
374
cf3872bd
YW
375 LIST_FOREACH(units, u, c->units)
376 if (streq_ptr(path, u->path))
377 u->path = mfree(u->path);
378 else
379 n += !!u->path;
3af0a96c 380
cf3872bd 381 if (n == 0) {
cf3872bd 382 c->slot_job_removed = sd_bus_slot_unref(c->slot_job_removed);
49942d6b
YW
383
384 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m), "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "NTP", NULL);
cf3872bd 385 }
3af0a96c
YW
386
387 return 0;
388}
389
cf3872bd 390static int unit_start_or_stop(UnitStatusInfo *u, sd_bus *bus, sd_bus_error *error, bool start) {
3af0a96c 391 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3af0a96c 392 const char *path;
c5f0532f
LP
393 int r;
394
5d280742 395 assert(u);
c5f0532f
LP
396 assert(bus);
397 assert(error);
398
81b84399
ZJS
399 r = sd_bus_call_method(
400 bus,
401 "org.freedesktop.systemd1",
402 "/org/freedesktop/systemd1",
403 "org.freedesktop.systemd1.Manager",
5d280742 404 start ? "StartUnit" : "StopUnit",
81b84399 405 error,
3af0a96c 406 &reply,
81b84399 407 "ss",
5d280742 408 u->name,
81b84399 409 "replace");
5d280742 410 if (r < 0)
b72ddf0f 411 return r;
c5f0532f 412
3af0a96c
YW
413 r = sd_bus_message_read(reply, "o", &path);
414 if (r < 0)
415 return bus_log_parse_error(r);
416
cf3872bd 417 r = free_and_strdup(&u->path, path);
3af0a96c
YW
418 if (r < 0)
419 return log_oom();
420
b72ddf0f 421 return 0;
c5f0532f
LP
422}
423
5d280742 424static int unit_enable_or_disable(UnitStatusInfo *u, sd_bus *bus, sd_bus_error *error, bool enable) {
c5f0532f 425 int r;
c5f0532f 426
5d280742 427 assert(u);
c5f0532f
LP
428 assert(bus);
429 assert(error);
430
5d280742
YW
431 /* Call context_update_ntp_status() to update UnitStatusInfo before calling this. */
432
433 if (streq(u->unit_file_state, "enabled") == enable)
434 return 0;
435
436 if (enable)
40ca29a1
LP
437 r = sd_bus_call_method(
438 bus,
ac7019f3
LP
439 "org.freedesktop.systemd1",
440 "/org/freedesktop/systemd1",
441 "org.freedesktop.systemd1.Manager",
b72ddf0f 442 "EnableUnitFiles",
40ca29a1
LP
443 error,
444 NULL,
b72ddf0f 445 "asbb", 1,
5d280742 446 u->name,
b72ddf0f
KS
447 false, true);
448 else
449 r = sd_bus_call_method(
450 bus,
451 "org.freedesktop.systemd1",
452 "/org/freedesktop/systemd1",
453 "org.freedesktop.systemd1.Manager",
454 "DisableUnitFiles",
455 error,
456 NULL,
457 "asb", 1,
5d280742 458 u->name,
b72ddf0f 459 false);
5d280742 460 if (r < 0)
b72ddf0f 461 return r;
c5f0532f 462
b72ddf0f
KS
463 r = sd_bus_call_method(
464 bus,
465 "org.freedesktop.systemd1",
466 "/org/freedesktop/systemd1",
467 "org.freedesktop.systemd1.Manager",
468 "Reload",
469 error,
470 NULL,
471 NULL);
3af0a96c
YW
472 if (r < 0)
473 return r;
474
b72ddf0f 475 return 0;
40ca29a1 476}
c5f0532f 477
6cc379b5
YW
478static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_time, "t", now(CLOCK_REALTIME));
479static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_ntp_sync, "b", ntp_synced());
480
6fc60278
LP
481static int property_get_rtc_time(
482 sd_bus *bus,
483 const char *path,
484 const char *interface,
485 const char *property,
486 sd_bus_message *reply,
ebcf1f97
LP
487 void *userdata,
488 sd_bus_error *error) {
6fc60278
LP
489
490 struct tm tm;
491 usec_t t;
492 int r;
493
494 zero(tm);
60989612 495 r = clock_get_hwclock(&tm);
88e262b6 496 if (r == -EBUSY) {
07a062a7 497 log_warning("/dev/rtc is busy. Is somebody keeping it open continuously? That's not a good idea... Returning a bogus RTC timestamp.");
88e262b6 498 t = 0;
fe2b58a4 499 } else if (r == -ENOENT) {
07a062a7 500 log_debug("/dev/rtc not found.");
fe2b58a4 501 t = 0; /* no RTC found */
ebcf1f97 502 } else if (r < 0)
10a87006 503 return sd_bus_error_set_errnof(error, r, "Failed to read RTC: %m");
ebcf1f97 504 else
2f6a5907 505 t = (usec_t) timegm(&tm) * USEC_PER_SEC;
6fc60278 506
ebcf1f97 507 return sd_bus_message_append(reply, "t", t);
6fc60278
LP
508}
509
5d280742
YW
510static int property_get_can_ntp(
511 sd_bus *bus,
512 const char *path,
513 const char *interface,
514 const char *property,
515 sd_bus_message *reply,
516 void *userdata,
517 sd_bus_error *error) {
518
519 Context *c = userdata;
520 int r;
521
522 assert(c);
523 assert(bus);
524 assert(property);
525 assert(reply);
526 assert(error);
527
b4356b57
YW
528 if (c->slot_job_removed)
529 /* When the previous request is not finished, then assume NTP is enabled. */
530 return sd_bus_message_append(reply, "b", true);
531
5d280742
YW
532 r = context_update_ntp_status(c, bus, reply);
533 if (r < 0)
534 return r;
535
536 return sd_bus_message_append(reply, "b", context_ntp_service_exists(c) > 0);
537}
538
539static int property_get_ntp(
540 sd_bus *bus,
541 const char *path,
542 const char *interface,
543 const char *property,
544 sd_bus_message *reply,
545 void *userdata,
546 sd_bus_error *error) {
547
548 Context *c = userdata;
549 int r;
550
551 assert(c);
552 assert(bus);
553 assert(property);
554 assert(reply);
555 assert(error);
556
b4356b57
YW
557 if (c->slot_job_removed)
558 /* When the previous request is not finished, then assume NTP is active. */
559 return sd_bus_message_append(reply, "b", true);
560
5d280742
YW
561 r = context_update_ntp_status(c, bus, reply);
562 if (r < 0)
563 return r;
564
565 return sd_bus_message_append(reply, "b", context_ntp_service_is_active(c) > 0);
566}
567
19070062 568static int method_set_timezone(sd_bus_message *m, void *userdata, sd_bus_error *error) {
40ca29a1 569 Context *c = userdata;
2c3def62 570 int interactive, r;
40ca29a1 571 const char *z;
c5f0532f 572
7e9cf16c
LP
573 assert(m);
574 assert(c);
575
40ca29a1
LP
576 r = sd_bus_message_read(m, "sb", &z, &interactive);
577 if (r < 0)
ebcf1f97 578 return r;
c5f0532f 579
089fb865 580 if (!timezone_is_valid(z, LOG_DEBUG))
ebcf1f97 581 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid time zone '%s'", z);
ac7019f3 582
539a68e0 583 if (streq_ptr(z, c->zone))
df2d202e 584 return sd_bus_reply_method_return(m, NULL);
c5f0532f 585
c529695e
LP
586 r = bus_verify_polkit_async(
587 m,
588 CAP_SYS_TIME,
589 "org.freedesktop.timedate1.set-timezone",
403ed0e5 590 NULL,
c529695e
LP
591 interactive,
592 UID_INVALID,
593 &c->polkit_registry,
594 error);
40ca29a1 595 if (r < 0)
ebcf1f97 596 return r;
40ca29a1 597 if (r == 0)
6fc60278 598 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
6ffe5e37 599
539a68e0
YW
600 r = free_and_strdup(&c->zone, z);
601 if (r < 0)
602 return r;
603
40ca29a1
LP
604 /* 1. Write new configuration file */
605 r = context_write_data_timezone(c);
606 if (r < 0) {
da927ba9 607 log_error_errno(r, "Failed to set time zone: %m");
10a87006 608 return sd_bus_error_set_errnof(error, r, "Failed to set time zone: %m");
40ca29a1 609 }
6ffe5e37 610
8a50b96f
LP
611 /* 2. Make glibc notice the new timezone */
612 tzset();
613
614 /* 3. Tell the kernel our timezone */
2a7ff45f
LP
615 r = clock_set_timezone(NULL);
616 if (r < 0)
617 log_debug_errno(r, "Failed to tell kernel about timezone, ignoring: %m");
6ffe5e37 618
40ca29a1
LP
619 if (c->local_rtc) {
620 struct timespec ts;
e0f691e1 621 struct tm tm;
c5f0532f 622
8a50b96f 623 /* 4. Sync RTC from system clock, with the new delta */
40ca29a1 624 assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
e0f691e1 625 assert_se(localtime_r(&ts.tv_sec, &tm));
2a7ff45f 626
e0f691e1 627 r = clock_set_hwclock(&tm);
2a7ff45f
LP
628 if (r < 0)
629 log_debug_errno(r, "Failed to sync time to hardware clock, ignoring: %m");
40ca29a1 630 }
c5f0532f 631
40ca29a1 632 log_struct(LOG_INFO,
2b044526 633 "MESSAGE_ID=" SD_MESSAGE_TIMEZONE_CHANGE_STR,
40ca29a1 634 "TIMEZONE=%s", c->zone,
8a50b96f
LP
635 "TIMEZONE_SHORTNAME=%s", tzname[daylight],
636 "DAYLIGHT=%i", daylight,
a1230ff9 637 LOG_MESSAGE("Changed time zone to '%s' (%s).", c->zone, tzname[daylight]));
c5f0532f 638
19070062 639 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m), "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "Timezone", NULL);
c5f0532f 640
df2d202e 641 return sd_bus_reply_method_return(m, NULL);
c5f0532f
LP
642}
643
19070062 644static int method_set_local_rtc(sd_bus_message *m, void *userdata, sd_bus_error *error) {
102d8f81 645 int lrtc, fix_system, interactive;
40ca29a1
LP
646 Context *c = userdata;
647 struct timespec ts;
f401e48c
LP
648 int r;
649
40ca29a1
LP
650 assert(m);
651 assert(c);
f401e48c 652
40ca29a1
LP
653 r = sd_bus_message_read(m, "bbb", &lrtc, &fix_system, &interactive);
654 if (r < 0)
ebcf1f97 655 return r;
f401e48c 656
40ca29a1 657 if (lrtc == c->local_rtc)
df2d202e 658 return sd_bus_reply_method_return(m, NULL);
f401e48c 659
c529695e
LP
660 r = bus_verify_polkit_async(
661 m,
662 CAP_SYS_TIME,
663 "org.freedesktop.timedate1.set-local-rtc",
403ed0e5 664 NULL,
c529695e
LP
665 interactive,
666 UID_INVALID,
667 &c->polkit_registry,
668 error);
40ca29a1 669 if (r < 0)
ebcf1f97 670 return r;
40ca29a1
LP
671 if (r == 0)
672 return 1;
f401e48c 673
40ca29a1 674 c->local_rtc = lrtc;
f401e48c 675
40ca29a1
LP
676 /* 1. Write new configuration file */
677 r = context_write_data_local_rtc(c);
678 if (r < 0) {
da927ba9 679 log_error_errno(r, "Failed to set RTC to local/UTC: %m");
10a87006 680 return sd_bus_error_set_errnof(error, r, "Failed to set RTC to local/UTC: %m");
40ca29a1 681 }
f401e48c 682
40ca29a1 683 /* 2. Tell the kernel our timezone */
2a7ff45f
LP
684 r = clock_set_timezone(NULL);
685 if (r < 0)
686 log_debug_errno(r, "Failed to tell kernel about timezone, ignoring: %m");
f401e48c 687
40ca29a1
LP
688 /* 3. Synchronize clocks */
689 assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
f401e48c 690
40ca29a1
LP
691 if (fix_system) {
692 struct tm tm;
f401e48c 693
2a7ff45f 694 /* Sync system clock from RTC; first, initialize the timezone fields of struct tm. */
40ca29a1 695 if (c->local_rtc)
e46acb79 696 localtime_r(&ts.tv_sec, &tm);
40ca29a1 697 else
e46acb79 698 gmtime_r(&ts.tv_sec, &tm);
72edcff5 699
2a7ff45f
LP
700 /* Override the main fields of struct tm, but not the timezone fields */
701 r = clock_get_hwclock(&tm);
702 if (r < 0)
703 log_debug_errno(r, "Failed to get hardware clock, ignoring: %m");
704 else {
705 /* And set the system clock with this */
40ca29a1
LP
706 if (c->local_rtc)
707 ts.tv_sec = mktime(&tm);
708 else
709 ts.tv_sec = timegm(&tm);
2076cf88 710
2a7ff45f
LP
711 if (clock_settime(CLOCK_REALTIME, &ts) < 0)
712 log_debug_errno(errno, "Failed to update system clock, ignoring: %m");
f401e48c
LP
713 }
714
40ca29a1 715 } else {
e46acb79 716 struct tm tm;
f401e48c 717
40ca29a1
LP
718 /* Sync RTC from system clock */
719 if (c->local_rtc)
e46acb79 720 localtime_r(&ts.tv_sec, &tm);
40ca29a1 721 else
e46acb79 722 gmtime_r(&ts.tv_sec, &tm);
2076cf88 723
e46acb79 724 r = clock_set_hwclock(&tm);
2a7ff45f
LP
725 if (r < 0)
726 log_debug_errno(r, "Failed to sync time to hardware clock, ignoring: %m");
40ca29a1 727 }
2076cf88 728
40ca29a1 729 log_info("RTC configured to %s time.", c->local_rtc ? "local" : "UTC");
2076cf88 730
19070062 731 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m), "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "LocalRTC", NULL);
2076cf88 732
df2d202e 733 return sd_bus_reply_method_return(m, NULL);
40ca29a1 734}
2076cf88 735
19070062 736static int method_set_time(sd_bus_message *m, void *userdata, sd_bus_error *error) {
5d280742
YW
737 sd_bus *bus = sd_bus_message_get_bus(m);
738 int relative, interactive, r;
40ca29a1
LP
739 Context *c = userdata;
740 int64_t utc;
741 struct timespec ts;
2479df30 742 usec_t start;
e46acb79 743 struct tm tm;
2076cf88 744
40ca29a1
LP
745 assert(m);
746 assert(c);
2076cf88 747
b4356b57
YW
748 if (c->slot_job_removed)
749 return sd_bus_error_set(error, BUS_ERROR_AUTOMATIC_TIME_SYNC_ENABLED, "Previous request is not finished, refusing.");
750
5d280742
YW
751 r = context_update_ntp_status(c, bus, m);
752 if (r < 0)
753 return sd_bus_error_set_errnof(error, r, "Failed to update context: %m");
754
755 if (context_ntp_service_is_active(c) > 0)
e9e5ea88 756 return sd_bus_error_set(error, BUS_ERROR_AUTOMATIC_TIME_SYNC_ENABLED, "Automatic time synchronization is enabled");
82d115d9 757
6829cec4
SL
758 /* this only gets used if dbus does not provide a timestamp */
759 start = now(CLOCK_MONOTONIC);
760
40ca29a1
LP
761 r = sd_bus_message_read(m, "xbb", &utc, &relative, &interactive);
762 if (r < 0)
ebcf1f97 763 return r;
2076cf88 764
40ca29a1 765 if (!relative && utc <= 0)
e9e5ea88 766 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid absolute time");
2076cf88 767
40ca29a1 768 if (relative && utc == 0)
df2d202e 769 return sd_bus_reply_method_return(m, NULL);
2076cf88 770
40ca29a1
LP
771 if (relative) {
772 usec_t n, x;
f401e48c 773
40ca29a1
LP
774 n = now(CLOCK_REALTIME);
775 x = n + utc;
f401e48c 776
40ca29a1
LP
777 if ((utc > 0 && x < n) ||
778 (utc < 0 && x > n))
e9e5ea88 779 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Time value overflow");
f401e48c 780
40ca29a1
LP
781 timespec_store(&ts, x);
782 } else
783 timespec_store(&ts, (usec_t) utc);
2076cf88 784
c529695e
LP
785 r = bus_verify_polkit_async(
786 m,
787 CAP_SYS_TIME,
788 "org.freedesktop.timedate1.set-time",
403ed0e5 789 NULL,
c529695e
LP
790 interactive,
791 UID_INVALID,
792 &c->polkit_registry,
793 error);
40ca29a1 794 if (r < 0)
ebcf1f97 795 return r;
40ca29a1
LP
796 if (r == 0)
797 return 1;
798
2479df30
SL
799 /* adjust ts for time spent in program */
800 r = sd_bus_message_get_monotonic_usec(m, &start);
6829cec4 801 /* when sd_bus_message_get_monotonic_usec() returns -ENODATA it does not modify &start */
2479df30
SL
802 if (r < 0 && r != -ENODATA)
803 return r;
6829cec4
SL
804
805 timespec_store(&ts, timespec_load(&ts) + (now(CLOCK_MONOTONIC) - start));
2479df30 806
40ca29a1
LP
807 /* Set system clock */
808 if (clock_settime(CLOCK_REALTIME, &ts) < 0) {
56f64d95 809 log_error_errno(errno, "Failed to set local time: %m");
ebcf1f97 810 return sd_bus_error_set_errnof(error, errno, "Failed to set local time: %m");
40ca29a1 811 }
2076cf88 812
40ca29a1
LP
813 /* Sync down to RTC */
814 if (c->local_rtc)
e46acb79 815 localtime_r(&ts.tv_sec, &tm);
40ca29a1 816 else
e46acb79 817 gmtime_r(&ts.tv_sec, &tm);
2a7ff45f 818
e46acb79 819 r = clock_set_hwclock(&tm);
2a7ff45f
LP
820 if (r < 0)
821 log_debug_errno(r, "Failed to update hardware clock, ignoring: %m");
f401e48c 822
40ca29a1 823 log_struct(LOG_INFO,
2b044526 824 "MESSAGE_ID=" SD_MESSAGE_TIME_CHANGE_STR,
de0671ee 825 "REALTIME="USEC_FMT, timespec_load(&ts),
a1230ff9 826 LOG_MESSAGE("Changed local time to %s", ctime(&ts.tv_sec)));
f401e48c 827
df2d202e 828 return sd_bus_reply_method_return(m, NULL);
40ca29a1 829}
f401e48c 830
19070062 831static int method_set_ntp(sd_bus_message *m, void *userdata, sd_bus_error *error) {
cf3872bd 832 _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot = NULL;
5d280742 833 sd_bus *bus = sd_bus_message_get_bus(m);
40ca29a1 834 Context *c = userdata;
5d280742
YW
835 UnitStatusInfo *u;
836 int enable, interactive, q, r;
f401e48c 837
19070062 838 assert(m);
5d280742 839 assert(bus);
19070062
LP
840 assert(c);
841
5d280742 842 r = sd_bus_message_read(m, "bb", &enable, &interactive);
40ca29a1 843 if (r < 0)
ebcf1f97 844 return r;
f401e48c 845
5d280742
YW
846 r = context_update_ntp_status(c, bus, m);
847 if (r < 0)
848 return r;
849
850 if (context_ntp_service_exists(c) <= 0)
851 return sd_bus_error_set(error, BUS_ERROR_NO_NTP_SUPPORT, "NTP not supported");
f401e48c 852
c529695e
LP
853 r = bus_verify_polkit_async(
854 m,
855 CAP_SYS_TIME,
856 "org.freedesktop.timedate1.set-ntp",
403ed0e5 857 NULL,
c529695e
LP
858 interactive,
859 UID_INVALID,
860 &c->polkit_registry,
861 error);
40ca29a1 862 if (r < 0)
ebcf1f97 863 return r;
40ca29a1
LP
864 if (r == 0)
865 return 1;
f401e48c 866
cf3872bd
YW
867 /* This method may be called frequently. Forget the previous job if it has not completed yet. */
868 LIST_FOREACH(units, u, c->units)
869 u->path = mfree(u->path);
870
871 if (!c->slot_job_removed) {
872 r = sd_bus_match_signal_async(
873 bus,
874 &slot,
875 "org.freedesktop.systemd1",
876 "/org/freedesktop/systemd1",
877 "org.freedesktop.systemd1.Manager",
878 "JobRemoved",
879 match_job_removed, NULL, c);
880 if (r < 0)
881 return r;
882 }
883
5d280742
YW
884 if (!enable)
885 LIST_FOREACH(units, u, c->units) {
886 if (!streq(u->load_state, "loaded"))
887 continue;
888
889 q = unit_enable_or_disable(u, bus, error, enable);
890 if (q < 0)
891 r = q;
892
cf3872bd 893 q = unit_start_or_stop(u, bus, error, enable);
5d280742
YW
894 if (q < 0)
895 r = q;
896 }
897
898 else if (context_ntp_service_is_enabled(c) <= 0)
899 LIST_FOREACH(units, u, c->units) {
900 if (!streq(u->load_state, "loaded"))
901 continue;
902
903 r = unit_enable_or_disable(u, bus, error, enable);
904 if (r < 0)
905 continue;
906
cf3872bd 907 r = unit_start_or_stop(u, bus, error, enable);
5d280742
YW
908 break;
909 }
910
3af0a96c 911 else
5d280742
YW
912 LIST_FOREACH(units, u, c->units) {
913 if (!streq(u->load_state, "loaded") ||
914 !streq(u->unit_file_state, "enabled"))
915 continue;
916
cf3872bd 917 r = unit_start_or_stop(u, bus, error, enable);
5d280742
YW
918 break;
919 }
40ca29a1 920
40ca29a1 921 if (r < 0)
ebcf1f97 922 return r;
f401e48c 923
cf3872bd
YW
924 if (slot)
925 c->slot_job_removed = TAKE_PTR(slot);
926
5d280742 927 log_info("Set NTP to %sd", enable_disable(enable));
f401e48c 928
df2d202e 929 return sd_bus_reply_method_return(m, NULL);
f401e48c
LP
930}
931
2cf0b2fe
NT
932static int method_list_timezones(sd_bus_message *m, void *userdata, sd_bus_error *error) {
933 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
934 _cleanup_strv_free_ char **zones = NULL;
935 int r;
936
937 assert(m);
938
939 r = get_timezones(&zones);
940 if (r < 0)
941 return sd_bus_error_set_errnof(error, r, "Failed to read list of time zones: %m");
942
943 r = sd_bus_message_new_method_return(m, &reply);
944 if (r < 0)
945 return r;
946
947 r = sd_bus_message_append_strv(reply, zones);
948 if (r < 0)
949 return r;
950
951 return sd_bus_send(NULL, reply, NULL);
952}
953
40ca29a1
LP
954static const sd_bus_vtable timedate_vtable[] = {
955 SD_BUS_VTABLE_START(0),
956 SD_BUS_PROPERTY("Timezone", "s", NULL, offsetof(Context, zone), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
82d115d9 957 SD_BUS_PROPERTY("LocalRTC", "b", bus_property_get_bool, offsetof(Context, local_rtc), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
5d280742
YW
958 SD_BUS_PROPERTY("CanNTP", "b", property_get_can_ntp, 0, 0),
959 SD_BUS_PROPERTY("NTP", "b", property_get_ntp, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
03cc26dd
LP
960 SD_BUS_PROPERTY("NTPSynchronized", "b", property_get_ntp_sync, 0, 0),
961 SD_BUS_PROPERTY("TimeUSec", "t", property_get_time, 0, 0),
6fc60278 962 SD_BUS_PROPERTY("RTCTimeUSec", "t", property_get_rtc_time, 0, 0),
adacb957
LP
963 SD_BUS_METHOD("SetTime", "xbb", NULL, method_set_time, SD_BUS_VTABLE_UNPRIVILEGED),
964 SD_BUS_METHOD("SetTimezone", "sb", NULL, method_set_timezone, SD_BUS_VTABLE_UNPRIVILEGED),
965 SD_BUS_METHOD("SetLocalRTC", "bbb", NULL, method_set_local_rtc, SD_BUS_VTABLE_UNPRIVILEGED),
966 SD_BUS_METHOD("SetNTP", "bb", NULL, method_set_ntp, SD_BUS_VTABLE_UNPRIVILEGED),
2cf0b2fe 967 SD_BUS_METHOD("ListTimezones", NULL, "as", method_list_timezones, SD_BUS_VTABLE_UNPRIVILEGED),
40ca29a1
LP
968 SD_BUS_VTABLE_END,
969};
970
971static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
4afd3348 972 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
f401e48c
LP
973 int r;
974
40ca29a1
LP
975 assert(c);
976 assert(event);
f401e48c
LP
977 assert(_bus);
978
76b54375 979 r = sd_bus_default_system(&bus);
f647962d
MS
980 if (r < 0)
981 return log_error_errno(r, "Failed to get system bus connection: %m");
f401e48c 982
19befb2d 983 r = sd_bus_add_object_vtable(bus, NULL, "/org/freedesktop/timedate1", "org.freedesktop.timedate1", timedate_vtable, c);
f647962d
MS
984 if (r < 0)
985 return log_error_errno(r, "Failed to register object: %m");
f401e48c 986
0c0b9306 987 r = sd_bus_request_name_async(bus, NULL, "org.freedesktop.timedate1", 0, NULL, NULL);
f647962d 988 if (r < 0)
0c0b9306 989 return log_error_errno(r, "Failed to request name: %m");
add10b5a 990
40ca29a1 991 r = sd_bus_attach_event(bus, event, 0);
f647962d
MS
992 if (r < 0)
993 return log_error_errno(r, "Failed to attach bus to event loop: %m");
f401e48c 994
1cc6c93a 995 *_bus = TAKE_PTR(bus);
f401e48c 996
40ca29a1 997 return 0;
f401e48c
LP
998}
999
1f47bc33
YW
1000static int run(int argc, char *argv[]) {
1001 _cleanup_(context_clear) Context context = {};
4afd3348
LP
1002 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
1003 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
f401e48c 1004 int r;
f401e48c 1005
6bf3c61c 1006 log_setup_service();
f401e48c 1007
4c12626c
LP
1008 umask(0022);
1009
1f47bc33
YW
1010 if (argc != 1)
1011 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes no arguments.");
f401e48c 1012
754f0269
YW
1013 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
1014
afc6adb5 1015 r = sd_event_default(&event);
1f47bc33
YW
1016 if (r < 0)
1017 return log_error_errno(r, "Failed to allocate event loop: %m");
f401e48c 1018
754f0269
YW
1019 (void) sd_event_set_watchdog(event, true);
1020
1021 r = sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
1f47bc33
YW
1022 if (r < 0)
1023 return log_error_errno(r, "Failed to install SIGINT handler: %m");
754f0269
YW
1024
1025 r = sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
1f47bc33
YW
1026 if (r < 0)
1027 return log_error_errno(r, "Failed to install SIGTERM handler: %m");
cde93897 1028
40ca29a1 1029 r = connect_bus(&context, event, &bus);
f401e48c 1030 if (r < 0)
1f47bc33 1031 return r;
f401e48c 1032
dc751688 1033 (void) sd_bus_negotiate_timestamp(bus, true);
2479df30 1034
40ca29a1 1035 r = context_read_data(&context);
1f47bc33
YW
1036 if (r < 0)
1037 return log_error_errno(r, "Failed to read time zone data: %m");
c5f0532f 1038
5d280742
YW
1039 r = context_parse_ntp_services(&context);
1040 if (r < 0)
1f47bc33 1041 return r;
ad740100 1042
37224a5f 1043 r = bus_event_loop_with_idle(event, bus, "org.freedesktop.timedate1", DEFAULT_EXIT_USEC, NULL, NULL);
1f47bc33
YW
1044 if (r < 0)
1045 return log_error_errno(r, "Failed to run event loop: %m");
f401e48c 1046
1f47bc33 1047 return 0;
f401e48c 1048}
1f47bc33
YW
1049
1050DEFINE_MAIN_FUNCTION(run);