1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "sd-messages.h"
11 #include "alloc-util.h"
12 #include "bus-common-errors.h"
13 #include "bus-error.h"
15 #include "clock-util.h"
17 #include "fileio-label.h"
22 #include "main-func.h"
23 #include "memory-util.h"
24 #include "missing_capability.h"
25 #include "path-util.h"
26 #include "selinux-util.h"
27 #include "signal-util.h"
28 #include "string-util.h"
31 #include "unit-name.h"
32 #include "user-util.h"
34 #define NULL_ADJTIME_UTC "0.0 0 0\n0\nUTC\n"
35 #define NULL_ADJTIME_LOCAL "0.0 0 0\n0\nLOCAL\n"
37 typedef struct UnitStatusInfo
{
40 char *unit_file_state
;
44 LIST_FIELDS(struct UnitStatusInfo
, units
);
47 typedef struct Context
{
50 Hashmap
*polkit_registry
;
51 sd_bus_message
*cache
;
53 sd_bus_slot
*slot_job_removed
;
55 LIST_HEAD(UnitStatusInfo
, units
);
58 static void unit_status_info_clear(UnitStatusInfo
*p
) {
61 p
->load_state
= mfree(p
->load_state
);
62 p
->unit_file_state
= mfree(p
->unit_file_state
);
63 p
->active_state
= mfree(p
->active_state
);
66 static void unit_status_info_free(UnitStatusInfo
*p
) {
69 unit_status_info_clear(p
);
75 static void context_clear(Context
*c
) {
81 bus_verify_polkit_async_registry_free(c
->polkit_registry
);
82 sd_bus_message_unref(c
->cache
);
84 sd_bus_slot_unref(c
->slot_job_removed
);
86 while ((p
= c
->units
)) {
87 LIST_REMOVE(units
, c
->units
, p
);
88 unit_status_info_free(p
);
92 static int context_add_ntp_service(Context
*c
, const char *s
) {
95 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
98 /* Do not add this if it is already listed */
99 LIST_FOREACH(units
, u
, c
->units
)
100 if (streq(u
->name
, s
))
103 u
= new0(UnitStatusInfo
, 1);
113 LIST_APPEND(units
, c
->units
, u
);
118 static int context_parse_ntp_services(Context
*c
) {
124 env
= getenv("SYSTEMD_TIMEDATED_NTP_SERVICES");
126 r
= context_add_ntp_service(c
, "systemd-timesyncd.service");
128 log_warning_errno(r
, "Failed to add NTP service \"systemd-timesyncd.service\", ignoring: %m");
134 _cleanup_free_
char *word
= NULL
;
136 r
= extract_first_word(&p
, &word
, ":", 0);
142 log_error("Invalid syntax, ignoring: %s", env
);
146 r
= context_add_ntp_service(c
, word
);
148 log_warning_errno(r
, "Failed to add NTP service \"%s\", ignoring: %m", word
);
154 static int context_ntp_service_is_active(Context
*c
) {
155 UnitStatusInfo
*info
;
160 /* Call context_update_ntp_status() to update UnitStatusInfo before calling this. */
162 LIST_FOREACH(units
, info
, c
->units
)
163 count
+= !STRPTR_IN_SET(info
->active_state
, "inactive", "failed");
168 static int context_ntp_service_is_enabled(Context
*c
) {
169 UnitStatusInfo
*info
;
174 /* Call context_update_ntp_status() to update UnitStatusInfo before calling this. */
176 LIST_FOREACH(units
, info
, c
->units
)
177 count
+= !STRPTR_IN_SET(info
->unit_file_state
, "masked", "masked-runtime", "disabled", "bad");
182 static int context_ntp_service_exists(Context
*c
) {
183 UnitStatusInfo
*info
;
188 /* Call context_update_ntp_status() to update UnitStatusInfo before calling this. */
190 LIST_FOREACH(units
, info
, c
->units
)
191 count
+= streq_ptr(info
->load_state
, "loaded");
196 static int context_read_data(Context
*c
) {
197 _cleanup_free_
char *t
= NULL
;
202 r
= get_timezone(&t
);
204 log_warning_errno(r
, "/etc/localtime should be a symbolic link to a time zone data file in /usr/share/zoneinfo/.");
206 log_warning_errno(r
, "Failed to get target of /etc/localtime: %m");
208 free_and_replace(c
->zone
, t
);
210 c
->local_rtc
= clock_is_localtime(NULL
) > 0;
215 static int context_write_data_timezone(Context
*c
) {
216 _cleanup_free_
char *p
= NULL
;
221 if (isempty(c
->zone
)) {
222 if (unlink("/etc/localtime") < 0 && errno
!= ENOENT
)
228 p
= strappend("../usr/share/zoneinfo/", c
->zone
);
232 r
= symlink_atomic(p
, "/etc/localtime");
239 static int context_write_data_local_rtc(Context
*c
) {
241 _cleanup_free_
char *s
= NULL
, *w
= NULL
;
245 r
= read_full_file("/etc/adjtime", &s
, NULL
);
253 w
= strdup(NULL_ADJTIME_LOCAL
);
258 const char *e
= "\n"; /* default if there is less than 3 lines */
259 const char *prepend
= "";
262 p
= strchrnul(s
, '\n');
264 /* only one line, no \n terminator */
266 else if (p
[1] == '\0') {
267 /* only one line, with \n terminator */
271 p
= strchr(p
+1, '\n');
273 /* only two lines, no \n terminator */
278 /* third line might have a \n terminator or not */
280 end
= strchr(p
, '\n');
281 /* if we actually have a fourth line, use that as suffix "e", otherwise the default \n */
290 w
= new(char, a
+ (c
->local_rtc
? 5 : 3) + strlen(prepend
) + b
+ 1);
294 *(char*) mempcpy(stpcpy(stpcpy(mempcpy(w
, s
, a
), prepend
), c
->local_rtc
? "LOCAL" : "UTC"), e
, b
) = 0;
296 if (streq(w
, NULL_ADJTIME_UTC
)) {
297 if (unlink("/etc/adjtime") < 0)
306 return write_string_file_atomic_label("/etc/adjtime", w
);
309 static int context_update_ntp_status(Context
*c
, sd_bus
*bus
, sd_bus_message
*m
) {
310 static const struct bus_properties_map map
[] = {
311 { "LoadState", "s", NULL
, offsetof(UnitStatusInfo
, load_state
) },
312 { "ActiveState", "s", NULL
, offsetof(UnitStatusInfo
, active_state
) },
313 { "UnitFileState", "s", NULL
, offsetof(UnitStatusInfo
, unit_file_state
) },
322 /* Suppress calling context_update_ntp_status() multiple times within single DBus transaction. */
327 sd_bus_message_unref(c
->cache
);
328 c
->cache
= sd_bus_message_ref(m
);
331 LIST_FOREACH(units
, u
, c
->units
) {
332 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
333 _cleanup_free_
char *path
= NULL
;
335 unit_status_info_clear(u
);
337 path
= unit_dbus_path_from_name(u
->name
);
341 r
= bus_map_all_properties(
343 "org.freedesktop.systemd1",
351 return log_error_errno(r
, "Failed to get properties: %s", bus_error_message(&error
, r
));
357 static int match_job_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
358 Context
*c
= userdata
;
367 r
= sd_bus_message_read(m
, "uoss", NULL
, &path
, NULL
, NULL
);
369 bus_log_parse_error(r
);
373 LIST_FOREACH(units
, u
, c
->units
)
374 if (streq_ptr(path
, u
->path
))
375 u
->path
= mfree(u
->path
);
380 c
->slot_job_removed
= sd_bus_slot_unref(c
->slot_job_removed
);
382 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
), "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "NTP", NULL
);
388 static int unit_start_or_stop(UnitStatusInfo
*u
, sd_bus
*bus
, sd_bus_error
*error
, bool start
) {
389 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
397 r
= sd_bus_call_method(
399 "org.freedesktop.systemd1",
400 "/org/freedesktop/systemd1",
401 "org.freedesktop.systemd1.Manager",
402 start
? "StartUnit" : "StopUnit",
411 r
= sd_bus_message_read(reply
, "o", &path
);
413 return bus_log_parse_error(r
);
415 r
= free_and_strdup(&u
->path
, path
);
422 static int unit_enable_or_disable(UnitStatusInfo
*u
, sd_bus
*bus
, sd_bus_error
*error
, bool enable
) {
429 /* Call context_update_ntp_status() to update UnitStatusInfo before calling this. */
431 if (streq(u
->unit_file_state
, "enabled") == enable
)
435 r
= sd_bus_call_method(
437 "org.freedesktop.systemd1",
438 "/org/freedesktop/systemd1",
439 "org.freedesktop.systemd1.Manager",
447 r
= sd_bus_call_method(
449 "org.freedesktop.systemd1",
450 "/org/freedesktop/systemd1",
451 "org.freedesktop.systemd1.Manager",
461 r
= sd_bus_call_method(
463 "org.freedesktop.systemd1",
464 "/org/freedesktop/systemd1",
465 "org.freedesktop.systemd1.Manager",
476 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_time
, "t", now(CLOCK_REALTIME
));
477 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_ntp_sync
, "b", ntp_synced());
479 static int property_get_rtc_time(
482 const char *interface
,
483 const char *property
,
484 sd_bus_message
*reply
,
486 sd_bus_error
*error
) {
493 r
= clock_get_hwclock(&tm
);
495 log_warning("/dev/rtc is busy. Is somebody keeping it open continuously? That's not a good idea... Returning a bogus RTC timestamp.");
497 } else if (r
== -ENOENT
) {
498 log_debug("/dev/rtc not found.");
499 t
= 0; /* no RTC found */
501 return sd_bus_error_set_errnof(error
, r
, "Failed to read RTC: %m");
503 t
= (usec_t
) timegm(&tm
) * USEC_PER_SEC
;
505 return sd_bus_message_append(reply
, "t", t
);
508 static int property_get_can_ntp(
511 const char *interface
,
512 const char *property
,
513 sd_bus_message
*reply
,
515 sd_bus_error
*error
) {
517 Context
*c
= userdata
;
526 if (c
->slot_job_removed
)
527 /* When the previous request is not finished, then assume NTP is enabled. */
528 return sd_bus_message_append(reply
, "b", true);
530 r
= context_update_ntp_status(c
, bus
, reply
);
534 return sd_bus_message_append(reply
, "b", context_ntp_service_exists(c
) > 0);
537 static int property_get_ntp(
540 const char *interface
,
541 const char *property
,
542 sd_bus_message
*reply
,
544 sd_bus_error
*error
) {
546 Context
*c
= userdata
;
555 if (c
->slot_job_removed
)
556 /* When the previous request is not finished, then assume NTP is active. */
557 return sd_bus_message_append(reply
, "b", true);
559 r
= context_update_ntp_status(c
, bus
, reply
);
563 return sd_bus_message_append(reply
, "b", context_ntp_service_is_active(c
) > 0);
566 static int method_set_timezone(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
567 Context
*c
= userdata
;
574 r
= sd_bus_message_read(m
, "sb", &z
, &interactive
);
578 if (!timezone_is_valid(z
, LOG_DEBUG
))
579 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid time zone '%s'", z
);
581 if (streq_ptr(z
, c
->zone
))
582 return sd_bus_reply_method_return(m
, NULL
);
584 r
= bus_verify_polkit_async(
587 "org.freedesktop.timedate1.set-timezone",
596 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
598 r
= free_and_strdup(&c
->zone
, z
);
602 /* 1. Write new configuration file */
603 r
= context_write_data_timezone(c
);
605 log_error_errno(r
, "Failed to set time zone: %m");
606 return sd_bus_error_set_errnof(error
, r
, "Failed to set time zone: %m");
609 /* 2. Make glibc notice the new timezone */
612 /* 3. Tell the kernel our timezone */
613 r
= clock_set_timezone(NULL
);
615 log_debug_errno(r
, "Failed to tell kernel about timezone, ignoring: %m");
621 /* 4. Sync RTC from system clock, with the new delta */
622 assert_se(clock_gettime(CLOCK_REALTIME
, &ts
) == 0);
623 assert_se(localtime_r(&ts
.tv_sec
, &tm
));
625 r
= clock_set_hwclock(&tm
);
627 log_debug_errno(r
, "Failed to sync time to hardware clock, ignoring: %m");
631 "MESSAGE_ID=" SD_MESSAGE_TIMEZONE_CHANGE_STR
,
632 "TIMEZONE=%s", c
->zone
,
633 "TIMEZONE_SHORTNAME=%s", tzname
[daylight
],
634 "DAYLIGHT=%i", daylight
,
635 LOG_MESSAGE("Changed time zone to '%s' (%s).", c
->zone
, tzname
[daylight
]));
637 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
), "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "Timezone", NULL
);
639 return sd_bus_reply_method_return(m
, NULL
);
642 static int method_set_local_rtc(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
643 int lrtc
, fix_system
, interactive
;
644 Context
*c
= userdata
;
651 r
= sd_bus_message_read(m
, "bbb", &lrtc
, &fix_system
, &interactive
);
655 if (lrtc
== c
->local_rtc
)
656 return sd_bus_reply_method_return(m
, NULL
);
658 r
= bus_verify_polkit_async(
661 "org.freedesktop.timedate1.set-local-rtc",
674 /* 1. Write new configuration file */
675 r
= context_write_data_local_rtc(c
);
677 log_error_errno(r
, "Failed to set RTC to local/UTC: %m");
678 return sd_bus_error_set_errnof(error
, r
, "Failed to set RTC to local/UTC: %m");
681 /* 2. Tell the kernel our timezone */
682 r
= clock_set_timezone(NULL
);
684 log_debug_errno(r
, "Failed to tell kernel about timezone, ignoring: %m");
686 /* 3. Synchronize clocks */
687 assert_se(clock_gettime(CLOCK_REALTIME
, &ts
) == 0);
692 /* Sync system clock from RTC; first, initialize the timezone fields of struct tm. */
694 localtime_r(&ts
.tv_sec
, &tm
);
696 gmtime_r(&ts
.tv_sec
, &tm
);
698 /* Override the main fields of struct tm, but not the timezone fields */
699 r
= clock_get_hwclock(&tm
);
701 log_debug_errno(r
, "Failed to get hardware clock, ignoring: %m");
703 /* And set the system clock with this */
705 ts
.tv_sec
= mktime(&tm
);
707 ts
.tv_sec
= timegm(&tm
);
709 if (clock_settime(CLOCK_REALTIME
, &ts
) < 0)
710 log_debug_errno(errno
, "Failed to update system clock, ignoring: %m");
716 /* Sync RTC from system clock */
718 localtime_r(&ts
.tv_sec
, &tm
);
720 gmtime_r(&ts
.tv_sec
, &tm
);
722 r
= clock_set_hwclock(&tm
);
724 log_debug_errno(r
, "Failed to sync time to hardware clock, ignoring: %m");
727 log_info("RTC configured to %s time.", c
->local_rtc
? "local" : "UTC");
729 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
), "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "LocalRTC", NULL
);
731 return sd_bus_reply_method_return(m
, NULL
);
734 static int method_set_time(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
735 sd_bus
*bus
= sd_bus_message_get_bus(m
);
736 int relative
, interactive
, r
;
737 Context
*c
= userdata
;
746 if (c
->slot_job_removed
)
747 return sd_bus_error_set(error
, BUS_ERROR_AUTOMATIC_TIME_SYNC_ENABLED
, "Previous request is not finished, refusing.");
749 r
= context_update_ntp_status(c
, bus
, m
);
751 return sd_bus_error_set_errnof(error
, r
, "Failed to update context: %m");
753 if (context_ntp_service_is_active(c
) > 0)
754 return sd_bus_error_set(error
, BUS_ERROR_AUTOMATIC_TIME_SYNC_ENABLED
, "Automatic time synchronization is enabled");
756 /* this only gets used if dbus does not provide a timestamp */
757 start
= now(CLOCK_MONOTONIC
);
759 r
= sd_bus_message_read(m
, "xbb", &utc
, &relative
, &interactive
);
763 if (!relative
&& utc
<= 0)
764 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid absolute time");
766 if (relative
&& utc
== 0)
767 return sd_bus_reply_method_return(m
, NULL
);
772 n
= now(CLOCK_REALTIME
);
775 if ((utc
> 0 && x
< n
) ||
777 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Time value overflow");
779 timespec_store(&ts
, x
);
781 timespec_store(&ts
, (usec_t
) utc
);
783 r
= bus_verify_polkit_async(
786 "org.freedesktop.timedate1.set-time",
797 /* adjust ts for time spent in program */
798 r
= sd_bus_message_get_monotonic_usec(m
, &start
);
799 /* when sd_bus_message_get_monotonic_usec() returns -ENODATA it does not modify &start */
800 if (r
< 0 && r
!= -ENODATA
)
803 timespec_store(&ts
, timespec_load(&ts
) + (now(CLOCK_MONOTONIC
) - start
));
805 /* Set system clock */
806 if (clock_settime(CLOCK_REALTIME
, &ts
) < 0) {
807 log_error_errno(errno
, "Failed to set local time: %m");
808 return sd_bus_error_set_errnof(error
, errno
, "Failed to set local time: %m");
811 /* Sync down to RTC */
813 localtime_r(&ts
.tv_sec
, &tm
);
815 gmtime_r(&ts
.tv_sec
, &tm
);
817 r
= clock_set_hwclock(&tm
);
819 log_debug_errno(r
, "Failed to update hardware clock, ignoring: %m");
822 "MESSAGE_ID=" SD_MESSAGE_TIME_CHANGE_STR
,
823 "REALTIME="USEC_FMT
, timespec_load(&ts
),
824 LOG_MESSAGE("Changed local time to %s", ctime(&ts
.tv_sec
)));
826 return sd_bus_reply_method_return(m
, NULL
);
829 static int method_set_ntp(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
830 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
831 sd_bus
*bus
= sd_bus_message_get_bus(m
);
832 Context
*c
= userdata
;
834 int enable
, interactive
, q
, r
;
840 r
= sd_bus_message_read(m
, "bb", &enable
, &interactive
);
844 r
= context_update_ntp_status(c
, bus
, m
);
848 if (context_ntp_service_exists(c
) <= 0)
849 return sd_bus_error_set(error
, BUS_ERROR_NO_NTP_SUPPORT
, "NTP not supported");
851 r
= bus_verify_polkit_async(
854 "org.freedesktop.timedate1.set-ntp",
865 /* This method may be called frequently. Forget the previous job if it has not completed yet. */
866 LIST_FOREACH(units
, u
, c
->units
)
867 u
->path
= mfree(u
->path
);
869 if (!c
->slot_job_removed
) {
870 r
= sd_bus_match_signal_async(
873 "org.freedesktop.systemd1",
874 "/org/freedesktop/systemd1",
875 "org.freedesktop.systemd1.Manager",
877 match_job_removed
, NULL
, c
);
883 LIST_FOREACH(units
, u
, c
->units
) {
884 if (!streq(u
->load_state
, "loaded"))
887 q
= unit_enable_or_disable(u
, bus
, error
, enable
);
891 q
= unit_start_or_stop(u
, bus
, error
, enable
);
896 else if (context_ntp_service_is_enabled(c
) <= 0)
897 LIST_FOREACH(units
, u
, c
->units
) {
898 if (!streq(u
->load_state
, "loaded"))
901 r
= unit_enable_or_disable(u
, bus
, error
, enable
);
905 r
= unit_start_or_stop(u
, bus
, error
, enable
);
910 LIST_FOREACH(units
, u
, c
->units
) {
911 if (!streq(u
->load_state
, "loaded") ||
912 !streq(u
->unit_file_state
, "enabled"))
915 r
= unit_start_or_stop(u
, bus
, error
, enable
);
923 c
->slot_job_removed
= TAKE_PTR(slot
);
925 log_info("Set NTP to %sd", enable_disable(enable
));
927 return sd_bus_reply_method_return(m
, NULL
);
930 static int method_list_timezones(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
931 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
932 _cleanup_strv_free_
char **zones
= NULL
;
937 r
= get_timezones(&zones
);
939 return sd_bus_error_set_errnof(error
, r
, "Failed to read list of time zones: %m");
941 r
= sd_bus_message_new_method_return(m
, &reply
);
945 r
= sd_bus_message_append_strv(reply
, zones
);
949 return sd_bus_send(NULL
, reply
, NULL
);
952 static const sd_bus_vtable timedate_vtable
[] = {
953 SD_BUS_VTABLE_START(0),
954 SD_BUS_PROPERTY("Timezone", "s", NULL
, offsetof(Context
, zone
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
955 SD_BUS_PROPERTY("LocalRTC", "b", bus_property_get_bool
, offsetof(Context
, local_rtc
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
956 SD_BUS_PROPERTY("CanNTP", "b", property_get_can_ntp
, 0, 0),
957 SD_BUS_PROPERTY("NTP", "b", property_get_ntp
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
958 SD_BUS_PROPERTY("NTPSynchronized", "b", property_get_ntp_sync
, 0, 0),
959 SD_BUS_PROPERTY("TimeUSec", "t", property_get_time
, 0, 0),
960 SD_BUS_PROPERTY("RTCTimeUSec", "t", property_get_rtc_time
, 0, 0),
961 SD_BUS_METHOD("SetTime", "xbb", NULL
, method_set_time
, SD_BUS_VTABLE_UNPRIVILEGED
),
962 SD_BUS_METHOD("SetTimezone", "sb", NULL
, method_set_timezone
, SD_BUS_VTABLE_UNPRIVILEGED
),
963 SD_BUS_METHOD("SetLocalRTC", "bbb", NULL
, method_set_local_rtc
, SD_BUS_VTABLE_UNPRIVILEGED
),
964 SD_BUS_METHOD("SetNTP", "bb", NULL
, method_set_ntp
, SD_BUS_VTABLE_UNPRIVILEGED
),
965 SD_BUS_METHOD("ListTimezones", NULL
, "as", method_list_timezones
, SD_BUS_VTABLE_UNPRIVILEGED
),
969 static int connect_bus(Context
*c
, sd_event
*event
, sd_bus
**_bus
) {
970 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
977 r
= sd_bus_default_system(&bus
);
979 return log_error_errno(r
, "Failed to get system bus connection: %m");
981 r
= sd_bus_add_object_vtable(bus
, NULL
, "/org/freedesktop/timedate1", "org.freedesktop.timedate1", timedate_vtable
, c
);
983 return log_error_errno(r
, "Failed to register object: %m");
985 r
= sd_bus_request_name_async(bus
, NULL
, "org.freedesktop.timedate1", 0, NULL
, NULL
);
987 return log_error_errno(r
, "Failed to request name: %m");
989 r
= sd_bus_attach_event(bus
, event
, 0);
991 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
993 *_bus
= TAKE_PTR(bus
);
998 static int run(int argc
, char *argv
[]) {
999 _cleanup_(context_clear
) Context context
= {};
1000 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1001 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1004 log_setup_service();
1009 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "This program takes no arguments.");
1011 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
1013 r
= sd_event_default(&event
);
1015 return log_error_errno(r
, "Failed to allocate event loop: %m");
1017 (void) sd_event_set_watchdog(event
, true);
1019 r
= sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
1021 return log_error_errno(r
, "Failed to install SIGINT handler: %m");
1023 r
= sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
1025 return log_error_errno(r
, "Failed to install SIGTERM handler: %m");
1027 r
= connect_bus(&context
, event
, &bus
);
1031 (void) sd_bus_negotiate_timestamp(bus
, true);
1033 r
= context_read_data(&context
);
1035 return log_error_errno(r
, "Failed to read time zone data: %m");
1037 r
= context_parse_ntp_services(&context
);
1041 r
= bus_event_loop_with_idle(event
, bus
, "org.freedesktop.timedate1", DEFAULT_EXIT_USEC
, NULL
, NULL
);
1043 return log_error_errno(r
, "Failed to run event loop: %m");
1048 DEFINE_MAIN_FUNCTION(run
);