1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "sd-messages.h"
13 #include "alloc-util.h"
14 #include "bus-common-errors.h"
15 #include "bus-error.h"
17 #include "clock-util.h"
19 #include "fileio-label.h"
24 #include "main-func.h"
25 #include "memory-util.h"
26 #include "missing_capability.h"
27 #include "path-util.h"
28 #include "selinux-util.h"
29 #include "signal-util.h"
30 #include "string-util.h"
33 #include "unit-name.h"
34 #include "user-util.h"
36 #define NULL_ADJTIME_UTC "0.0 0 0\n0\nUTC\n"
37 #define NULL_ADJTIME_LOCAL "0.0 0 0\n0\nLOCAL\n"
39 typedef struct UnitStatusInfo
{
42 char *unit_file_state
;
46 LIST_FIELDS(struct UnitStatusInfo
, units
);
49 typedef struct Context
{
52 Hashmap
*polkit_registry
;
53 sd_bus_message
*cache
;
55 sd_bus_slot
*slot_job_removed
;
57 LIST_HEAD(UnitStatusInfo
, units
);
60 static void unit_status_info_clear(UnitStatusInfo
*p
) {
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
);
68 static void unit_status_info_free(UnitStatusInfo
*p
) {
71 unit_status_info_clear(p
);
77 static void context_clear(Context
*c
) {
83 bus_verify_polkit_async_registry_free(c
->polkit_registry
);
84 sd_bus_message_unref(c
->cache
);
86 sd_bus_slot_unref(c
->slot_job_removed
);
88 while ((p
= c
->units
)) {
89 LIST_REMOVE(units
, c
->units
, p
);
90 unit_status_info_free(p
);
94 static int context_add_ntp_service(Context
*c
, const char *s
) {
97 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
100 /* Do not add this if it is already listed */
101 LIST_FOREACH(units
, u
, c
->units
)
102 if (streq(u
->name
, s
))
105 u
= new0(UnitStatusInfo
, 1);
115 LIST_APPEND(units
, c
->units
, u
);
120 static int context_parse_ntp_services(Context
*c
) {
126 env
= getenv("SYSTEMD_TIMEDATED_NTP_SERVICES");
128 r
= context_add_ntp_service(c
, "systemd-timesyncd.service");
130 log_warning_errno(r
, "Failed to add NTP service \"systemd-timesyncd.service\", ignoring: %m");
136 _cleanup_free_
char *word
= NULL
;
138 r
= extract_first_word(&p
, &word
, ":", 0);
144 log_error("Invalid syntax, ignoring: %s", env
);
148 r
= context_add_ntp_service(c
, word
);
150 log_warning_errno(r
, "Failed to add NTP service \"%s\", ignoring: %m", word
);
156 static int context_ntp_service_is_active(Context
*c
) {
157 UnitStatusInfo
*info
;
162 /* Call context_update_ntp_status() to update UnitStatusInfo before calling this. */
164 LIST_FOREACH(units
, info
, c
->units
)
165 count
+= !STRPTR_IN_SET(info
->active_state
, "inactive", "failed");
170 static int context_ntp_service_is_enabled(Context
*c
) {
171 UnitStatusInfo
*info
;
176 /* Call context_update_ntp_status() to update UnitStatusInfo before calling this. */
178 LIST_FOREACH(units
, info
, c
->units
)
179 count
+= !STRPTR_IN_SET(info
->unit_file_state
, "masked", "masked-runtime", "disabled", "bad");
184 static int context_ntp_service_exists(Context
*c
) {
185 UnitStatusInfo
*info
;
190 /* Call context_update_ntp_status() to update UnitStatusInfo before calling this. */
192 LIST_FOREACH(units
, info
, c
->units
)
193 count
+= streq_ptr(info
->load_state
, "loaded");
198 static int context_read_data(Context
*c
) {
199 _cleanup_free_
char *t
= NULL
;
204 r
= get_timezone(&t
);
206 log_warning_errno(r
, "/etc/localtime should be a symbolic link to a time zone data file in /usr/share/zoneinfo/.");
208 log_warning_errno(r
, "Failed to get target of /etc/localtime: %m");
210 free_and_replace(c
->zone
, t
);
212 c
->local_rtc
= clock_is_localtime(NULL
) > 0;
217 static int context_write_data_timezone(Context
*c
) {
218 _cleanup_free_
char *p
= NULL
;
223 if (isempty(c
->zone
)) {
224 if (unlink("/etc/localtime") < 0 && errno
!= ENOENT
)
230 p
= path_join("../usr/share/zoneinfo", c
->zone
);
234 r
= symlink_atomic(p
, "/etc/localtime");
241 static int context_write_data_local_rtc(Context
*c
) {
243 _cleanup_free_
char *s
= NULL
, *w
= NULL
;
247 r
= read_full_file("/etc/adjtime", &s
, NULL
);
255 w
= strdup(NULL_ADJTIME_LOCAL
);
260 const char *e
= "\n"; /* default if there is less than 3 lines */
261 const char *prepend
= "";
264 p
= strchrnul(s
, '\n');
266 /* only one line, no \n terminator */
268 else if (p
[1] == '\0') {
269 /* only one line, with \n terminator */
273 p
= strchr(p
+1, '\n');
275 /* only two lines, no \n terminator */
280 /* third line might have a \n terminator or not */
282 end
= strchr(p
, '\n');
283 /* if we actually have a fourth line, use that as suffix "e", otherwise the default \n */
292 w
= new(char, a
+ (c
->local_rtc
? 5 : 3) + strlen(prepend
) + b
+ 1);
296 *(char*) mempcpy(stpcpy(stpcpy(mempcpy(w
, s
, a
), prepend
), c
->local_rtc
? "LOCAL" : "UTC"), e
, b
) = 0;
298 if (streq(w
, NULL_ADJTIME_UTC
)) {
299 if (unlink("/etc/adjtime") < 0)
308 return write_string_file_atomic_label("/etc/adjtime", w
);
311 static 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
) },
324 /* Suppress calling context_update_ntp_status() multiple times within single DBus transaction. */
329 sd_bus_message_unref(c
->cache
);
330 c
->cache
= sd_bus_message_ref(m
);
333 LIST_FOREACH(units
, u
, c
->units
) {
334 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
335 _cleanup_free_
char *path
= NULL
;
337 unit_status_info_clear(u
);
339 path
= unit_dbus_path_from_name(u
->name
);
343 r
= bus_map_all_properties(
345 "org.freedesktop.systemd1",
353 return log_error_errno(r
, "Failed to get properties: %s", bus_error_message(&error
, r
));
359 static int match_job_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
360 Context
*c
= userdata
;
369 r
= sd_bus_message_read(m
, "uoss", NULL
, &path
, NULL
, NULL
);
371 bus_log_parse_error(r
);
375 LIST_FOREACH(units
, u
, c
->units
)
376 if (streq_ptr(path
, u
->path
))
377 u
->path
= mfree(u
->path
);
382 c
->slot_job_removed
= sd_bus_slot_unref(c
->slot_job_removed
);
384 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
), "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "NTP", NULL
);
390 static int unit_start_or_stop(UnitStatusInfo
*u
, sd_bus
*bus
, sd_bus_error
*error
, bool start
) {
391 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
399 r
= sd_bus_call_method(
401 "org.freedesktop.systemd1",
402 "/org/freedesktop/systemd1",
403 "org.freedesktop.systemd1.Manager",
404 start
? "StartUnit" : "StopUnit",
413 r
= sd_bus_message_read(reply
, "o", &path
);
415 return bus_log_parse_error(r
);
417 r
= free_and_strdup(&u
->path
, path
);
424 static int unit_enable_or_disable(UnitStatusInfo
*u
, sd_bus
*bus
, sd_bus_error
*error
, bool enable
) {
431 /* Call context_update_ntp_status() to update UnitStatusInfo before calling this. */
433 if (streq(u
->unit_file_state
, "enabled") == enable
)
437 r
= sd_bus_call_method(
439 "org.freedesktop.systemd1",
440 "/org/freedesktop/systemd1",
441 "org.freedesktop.systemd1.Manager",
449 r
= sd_bus_call_method(
451 "org.freedesktop.systemd1",
452 "/org/freedesktop/systemd1",
453 "org.freedesktop.systemd1.Manager",
463 r
= sd_bus_call_method(
465 "org.freedesktop.systemd1",
466 "/org/freedesktop/systemd1",
467 "org.freedesktop.systemd1.Manager",
478 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_time
, "t", now(CLOCK_REALTIME
));
479 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_ntp_sync
, "b", ntp_synced());
481 static int property_get_rtc_time(
484 const char *interface
,
485 const char *property
,
486 sd_bus_message
*reply
,
488 sd_bus_error
*error
) {
495 r
= clock_get_hwclock(&tm
);
497 log_warning("/dev/rtc is busy. Is somebody keeping it open continuously? That's not a good idea... Returning a bogus RTC timestamp.");
499 } else if (r
== -ENOENT
) {
500 log_debug("/dev/rtc not found.");
501 t
= 0; /* no RTC found */
503 return sd_bus_error_set_errnof(error
, r
, "Failed to read RTC: %m");
505 t
= (usec_t
) timegm(&tm
) * USEC_PER_SEC
;
507 return sd_bus_message_append(reply
, "t", t
);
510 static int property_get_can_ntp(
513 const char *interface
,
514 const char *property
,
515 sd_bus_message
*reply
,
517 sd_bus_error
*error
) {
519 Context
*c
= userdata
;
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);
532 r
= context_update_ntp_status(c
, bus
, reply
);
536 return sd_bus_message_append(reply
, "b", context_ntp_service_exists(c
) > 0);
539 static int property_get_ntp(
542 const char *interface
,
543 const char *property
,
544 sd_bus_message
*reply
,
546 sd_bus_error
*error
) {
548 Context
*c
= userdata
;
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);
561 r
= context_update_ntp_status(c
, bus
, reply
);
565 return sd_bus_message_append(reply
, "b", context_ntp_service_is_active(c
) > 0);
568 static int method_set_timezone(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
569 Context
*c
= userdata
;
576 r
= sd_bus_message_read(m
, "sb", &z
, &interactive
);
580 if (!timezone_is_valid(z
, LOG_DEBUG
))
581 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid time zone '%s'", z
);
583 if (streq_ptr(z
, c
->zone
))
584 return sd_bus_reply_method_return(m
, NULL
);
586 r
= bus_verify_polkit_async(
589 "org.freedesktop.timedate1.set-timezone",
598 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
600 r
= free_and_strdup(&c
->zone
, z
);
604 /* 1. Write new configuration file */
605 r
= context_write_data_timezone(c
);
607 log_error_errno(r
, "Failed to set time zone: %m");
608 return sd_bus_error_set_errnof(error
, r
, "Failed to set time zone: %m");
611 /* 2. Make glibc notice the new timezone */
614 /* 3. Tell the kernel our timezone */
615 r
= clock_set_timezone(NULL
);
617 log_debug_errno(r
, "Failed to tell kernel about timezone, ignoring: %m");
623 /* 4. Sync RTC from system clock, with the new delta */
624 assert_se(clock_gettime(CLOCK_REALTIME
, &ts
) == 0);
625 assert_se(localtime_r(&ts
.tv_sec
, &tm
));
627 r
= clock_set_hwclock(&tm
);
629 log_debug_errno(r
, "Failed to sync time to hardware clock, ignoring: %m");
633 "MESSAGE_ID=" SD_MESSAGE_TIMEZONE_CHANGE_STR
,
634 "TIMEZONE=%s", c
->zone
,
635 "TIMEZONE_SHORTNAME=%s", tzname
[daylight
],
636 "DAYLIGHT=%i", daylight
,
637 LOG_MESSAGE("Changed time zone to '%s' (%s).", c
->zone
, tzname
[daylight
]));
639 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
), "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "Timezone", NULL
);
641 return sd_bus_reply_method_return(m
, NULL
);
644 static int method_set_local_rtc(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
645 int lrtc
, fix_system
, interactive
;
646 Context
*c
= userdata
;
653 r
= sd_bus_message_read(m
, "bbb", &lrtc
, &fix_system
, &interactive
);
657 if (lrtc
== c
->local_rtc
)
658 return sd_bus_reply_method_return(m
, NULL
);
660 r
= bus_verify_polkit_async(
663 "org.freedesktop.timedate1.set-local-rtc",
676 /* 1. Write new configuration file */
677 r
= context_write_data_local_rtc(c
);
679 log_error_errno(r
, "Failed to set RTC to local/UTC: %m");
680 return sd_bus_error_set_errnof(error
, r
, "Failed to set RTC to local/UTC: %m");
683 /* 2. Tell the kernel our timezone */
684 r
= clock_set_timezone(NULL
);
686 log_debug_errno(r
, "Failed to tell kernel about timezone, ignoring: %m");
688 /* 3. Synchronize clocks */
689 assert_se(clock_gettime(CLOCK_REALTIME
, &ts
) == 0);
694 /* Sync system clock from RTC; first, initialize the timezone fields of struct tm. */
696 localtime_r(&ts
.tv_sec
, &tm
);
698 gmtime_r(&ts
.tv_sec
, &tm
);
700 /* Override the main fields of struct tm, but not the timezone fields */
701 r
= clock_get_hwclock(&tm
);
703 log_debug_errno(r
, "Failed to get hardware clock, ignoring: %m");
705 /* And set the system clock with this */
707 ts
.tv_sec
= mktime(&tm
);
709 ts
.tv_sec
= timegm(&tm
);
711 if (clock_settime(CLOCK_REALTIME
, &ts
) < 0)
712 log_debug_errno(errno
, "Failed to update system clock, ignoring: %m");
718 /* Sync RTC from system clock */
720 localtime_r(&ts
.tv_sec
, &tm
);
722 gmtime_r(&ts
.tv_sec
, &tm
);
724 r
= clock_set_hwclock(&tm
);
726 log_debug_errno(r
, "Failed to sync time to hardware clock, ignoring: %m");
729 log_info("RTC configured to %s time.", c
->local_rtc
? "local" : "UTC");
731 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
), "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "LocalRTC", NULL
);
733 return sd_bus_reply_method_return(m
, NULL
);
736 static int method_set_time(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
737 sd_bus
*bus
= sd_bus_message_get_bus(m
);
738 int relative
, interactive
, r
;
739 Context
*c
= userdata
;
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.");
751 r
= context_update_ntp_status(c
, bus
, m
);
753 return sd_bus_error_set_errnof(error
, r
, "Failed to update context: %m");
755 if (context_ntp_service_is_active(c
) > 0)
756 return sd_bus_error_set(error
, BUS_ERROR_AUTOMATIC_TIME_SYNC_ENABLED
, "Automatic time synchronization is enabled");
758 /* this only gets used if dbus does not provide a timestamp */
759 start
= now(CLOCK_MONOTONIC
);
761 r
= sd_bus_message_read(m
, "xbb", &utc
, &relative
, &interactive
);
765 if (!relative
&& utc
<= 0)
766 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid absolute time");
768 if (relative
&& utc
== 0)
769 return sd_bus_reply_method_return(m
, NULL
);
774 n
= now(CLOCK_REALTIME
);
777 if ((utc
> 0 && x
< n
) ||
779 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Time value overflow");
781 timespec_store(&ts
, x
);
783 timespec_store(&ts
, (usec_t
) utc
);
785 r
= bus_verify_polkit_async(
788 "org.freedesktop.timedate1.set-time",
799 /* adjust ts for time spent in program */
800 r
= sd_bus_message_get_monotonic_usec(m
, &start
);
801 /* when sd_bus_message_get_monotonic_usec() returns -ENODATA it does not modify &start */
802 if (r
< 0 && r
!= -ENODATA
)
805 timespec_store(&ts
, timespec_load(&ts
) + (now(CLOCK_MONOTONIC
) - start
));
807 /* Set system clock */
808 if (clock_settime(CLOCK_REALTIME
, &ts
) < 0) {
809 log_error_errno(errno
, "Failed to set local time: %m");
810 return sd_bus_error_set_errnof(error
, errno
, "Failed to set local time: %m");
813 /* Sync down to RTC */
815 localtime_r(&ts
.tv_sec
, &tm
);
817 gmtime_r(&ts
.tv_sec
, &tm
);
819 r
= clock_set_hwclock(&tm
);
821 log_debug_errno(r
, "Failed to update hardware clock, ignoring: %m");
824 "MESSAGE_ID=" SD_MESSAGE_TIME_CHANGE_STR
,
825 "REALTIME="USEC_FMT
, timespec_load(&ts
),
826 LOG_MESSAGE("Changed local time to %s", ctime(&ts
.tv_sec
)));
828 return sd_bus_reply_method_return(m
, NULL
);
831 static int method_set_ntp(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
832 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
833 sd_bus
*bus
= sd_bus_message_get_bus(m
);
834 Context
*c
= userdata
;
836 int enable
, interactive
, q
, r
;
842 r
= sd_bus_message_read(m
, "bb", &enable
, &interactive
);
846 r
= context_update_ntp_status(c
, bus
, m
);
850 if (context_ntp_service_exists(c
) <= 0)
851 return sd_bus_error_set(error
, BUS_ERROR_NO_NTP_SUPPORT
, "NTP not supported");
853 r
= bus_verify_polkit_async(
856 "org.freedesktop.timedate1.set-ntp",
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
);
871 if (!c
->slot_job_removed
) {
872 r
= sd_bus_match_signal_async(
875 "org.freedesktop.systemd1",
876 "/org/freedesktop/systemd1",
877 "org.freedesktop.systemd1.Manager",
879 match_job_removed
, NULL
, c
);
885 LIST_FOREACH(units
, u
, c
->units
) {
886 if (!streq(u
->load_state
, "loaded"))
889 q
= unit_enable_or_disable(u
, bus
, error
, enable
);
893 q
= unit_start_or_stop(u
, bus
, error
, enable
);
898 else if (context_ntp_service_is_enabled(c
) <= 0)
899 LIST_FOREACH(units
, u
, c
->units
) {
900 if (!streq(u
->load_state
, "loaded"))
903 r
= unit_enable_or_disable(u
, bus
, error
, enable
);
907 r
= unit_start_or_stop(u
, bus
, error
, enable
);
912 LIST_FOREACH(units
, u
, c
->units
) {
913 if (!streq(u
->load_state
, "loaded") ||
914 !streq(u
->unit_file_state
, "enabled"))
917 r
= unit_start_or_stop(u
, bus
, error
, enable
);
925 c
->slot_job_removed
= TAKE_PTR(slot
);
927 log_info("Set NTP to %sd", enable_disable(enable
));
929 return sd_bus_reply_method_return(m
, NULL
);
932 static 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
;
939 r
= get_timezones(&zones
);
941 return sd_bus_error_set_errnof(error
, r
, "Failed to read list of time zones: %m");
943 r
= sd_bus_message_new_method_return(m
, &reply
);
947 r
= sd_bus_message_append_strv(reply
, zones
);
951 return sd_bus_send(NULL
, reply
, NULL
);
954 static 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
),
957 SD_BUS_PROPERTY("LocalRTC", "b", bus_property_get_bool
, offsetof(Context
, local_rtc
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
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
),
960 SD_BUS_PROPERTY("NTPSynchronized", "b", property_get_ntp_sync
, 0, 0),
961 SD_BUS_PROPERTY("TimeUSec", "t", property_get_time
, 0, 0),
962 SD_BUS_PROPERTY("RTCTimeUSec", "t", property_get_rtc_time
, 0, 0),
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
),
967 SD_BUS_METHOD("ListTimezones", NULL
, "as", method_list_timezones
, SD_BUS_VTABLE_UNPRIVILEGED
),
971 static int connect_bus(Context
*c
, sd_event
*event
, sd_bus
**_bus
) {
972 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
979 r
= sd_bus_default_system(&bus
);
981 return log_error_errno(r
, "Failed to get system bus connection: %m");
983 r
= sd_bus_add_object_vtable(bus
, NULL
, "/org/freedesktop/timedate1", "org.freedesktop.timedate1", timedate_vtable
, c
);
985 return log_error_errno(r
, "Failed to register object: %m");
987 r
= sd_bus_request_name_async(bus
, NULL
, "org.freedesktop.timedate1", 0, NULL
, NULL
);
989 return log_error_errno(r
, "Failed to request name: %m");
991 r
= sd_bus_attach_event(bus
, event
, 0);
993 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
995 *_bus
= TAKE_PTR(bus
);
1000 static int run(int argc
, char *argv
[]) {
1001 _cleanup_(context_clear
) Context context
= {};
1002 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1003 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1006 log_setup_service();
1011 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "This program takes no arguments.");
1013 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
1015 r
= sd_event_default(&event
);
1017 return log_error_errno(r
, "Failed to allocate event loop: %m");
1019 (void) sd_event_set_watchdog(event
, true);
1021 r
= sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
1023 return log_error_errno(r
, "Failed to install SIGINT handler: %m");
1025 r
= sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
1027 return log_error_errno(r
, "Failed to install SIGTERM handler: %m");
1029 r
= connect_bus(&context
, event
, &bus
);
1033 (void) sd_bus_negotiate_timestamp(bus
, true);
1035 r
= context_read_data(&context
);
1037 return log_error_errno(r
, "Failed to read time zone data: %m");
1039 r
= context_parse_ntp_services(&context
);
1043 r
= bus_event_loop_with_idle(event
, bus
, "org.freedesktop.timedate1", DEFAULT_EXIT_USEC
, NULL
, NULL
);
1045 return log_error_errno(r
, "Failed to run event loop: %m");
1050 DEFINE_MAIN_FUNCTION(run
);