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