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"
18 #include "conf-files.h"
21 #include "fileio-label.h"
26 #include "main-func.h"
27 #include "memory-util.h"
28 #include "missing_capability.h"
29 #include "path-util.h"
30 #include "selinux-util.h"
31 #include "signal-util.h"
32 #include "string-util.h"
35 #include "unit-name.h"
36 #include "user-util.h"
38 #define NULL_ADJTIME_UTC "0.0 0 0\n0\nUTC\n"
39 #define NULL_ADJTIME_LOCAL "0.0 0 0\n0\nLOCAL\n"
41 #define UNIT_LIST_DIRS (const char* const*) CONF_PATHS_STRV("systemd/ntp-units.d")
43 typedef struct UnitStatusInfo
{
46 char *unit_file_state
;
50 LIST_FIELDS(struct UnitStatusInfo
, units
);
53 typedef struct Context
{
56 Hashmap
*polkit_registry
;
57 sd_bus_message
*cache
;
59 sd_bus_slot
*slot_job_removed
;
61 LIST_HEAD(UnitStatusInfo
, units
);
64 static void unit_status_info_clear(UnitStatusInfo
*p
) {
67 p
->load_state
= mfree(p
->load_state
);
68 p
->unit_file_state
= mfree(p
->unit_file_state
);
69 p
->active_state
= mfree(p
->active_state
);
72 static void unit_status_info_free(UnitStatusInfo
*p
) {
75 unit_status_info_clear(p
);
81 static void context_clear(Context
*c
) {
87 bus_verify_polkit_async_registry_free(c
->polkit_registry
);
88 sd_bus_message_unref(c
->cache
);
90 sd_bus_slot_unref(c
->slot_job_removed
);
92 while ((p
= c
->units
)) {
93 LIST_REMOVE(units
, c
->units
, p
);
94 unit_status_info_free(p
);
98 static int context_add_ntp_service(Context
*c
, const char *s
) {
101 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
104 /* Do not add this if it is already listed */
105 LIST_FOREACH(units
, u
, c
->units
)
106 if (streq(u
->name
, s
))
109 u
= new0(UnitStatusInfo
, 1);
119 LIST_APPEND(units
, c
->units
, u
);
124 static int context_parse_ntp_services_from_environment(Context
*c
) {
130 env
= getenv("SYSTEMD_TIMEDATED_NTP_SERVICES");
134 log_debug("Using list of ntp services from environment variable $SYSTEMD_TIMEDATED_NTP_SERVICES.");
137 _cleanup_free_
char *word
= NULL
;
139 r
= extract_first_word(&p
, &word
, ":", 0);
145 log_error("Invalid syntax, ignoring: %s", env
);
149 r
= context_add_ntp_service(c
, word
);
151 log_warning_errno(r
, "Failed to add NTP service \"%s\", ignoring: %m", word
);
157 static int context_parse_ntp_services_from_disk(Context
*c
) {
158 _cleanup_strv_free_
char **files
= NULL
;
162 r
= conf_files_list_strv(&files
, ".list", NULL
, CONF_FILES_FILTER_MASKED
, UNIT_LIST_DIRS
);
164 return log_error_errno(r
, "Failed to enumerate .list files: %m");
166 STRV_FOREACH(f
, files
) {
167 _cleanup_fclose_
FILE *file
= NULL
;
169 log_debug("Reading file '%s'", *f
);
171 r
= fopen_unlocked(*f
, "re", &file
);
173 log_error_errno(r
, "Failed to open %s, ignoring: %m", *f
);
178 _cleanup_free_
char *line
= NULL
;
181 r
= read_line(file
, LINE_MAX
, &line
);
183 log_error_errno(r
, "Failed to read %s, ignoring: %m", *f
);
189 word
= strstrip(line
);
190 if (isempty(word
) || startswith("#", word
))
193 r
= context_add_ntp_service(c
, word
);
195 log_warning_errno(r
, "Failed to add NTP service \"%s\", ignoring: %m", word
);
202 static int context_parse_ntp_services(Context
*c
) {
205 r
= context_parse_ntp_services_from_environment(c
);
209 return context_parse_ntp_services_from_disk(c
);
212 static int context_ntp_service_is_active(Context
*c
) {
213 UnitStatusInfo
*info
;
218 /* Call context_update_ntp_status() to update UnitStatusInfo before calling this. */
220 LIST_FOREACH(units
, info
, c
->units
)
221 count
+= !STRPTR_IN_SET(info
->active_state
, "inactive", "failed");
226 static int context_ntp_service_is_enabled(Context
*c
) {
227 UnitStatusInfo
*info
;
232 /* Call context_update_ntp_status() to update UnitStatusInfo before calling this. */
234 LIST_FOREACH(units
, info
, c
->units
)
235 count
+= !STRPTR_IN_SET(info
->unit_file_state
, "masked", "masked-runtime", "disabled", "bad");
240 static int context_ntp_service_exists(Context
*c
) {
241 UnitStatusInfo
*info
;
246 /* Call context_update_ntp_status() to update UnitStatusInfo before calling this. */
248 LIST_FOREACH(units
, info
, c
->units
)
249 count
+= streq_ptr(info
->load_state
, "loaded");
254 static int context_read_data(Context
*c
) {
255 _cleanup_free_
char *t
= NULL
;
260 r
= get_timezone(&t
);
262 log_warning_errno(r
, "/etc/localtime should be a symbolic link to a time zone data file in /usr/share/zoneinfo/.");
264 log_warning_errno(r
, "Failed to get target of /etc/localtime: %m");
266 free_and_replace(c
->zone
, t
);
268 c
->local_rtc
= clock_is_localtime(NULL
) > 0;
273 static int context_write_data_timezone(Context
*c
) {
274 _cleanup_free_
char *p
= NULL
;
279 if (isempty(c
->zone
)) {
280 if (unlink("/etc/localtime") < 0 && errno
!= ENOENT
)
286 p
= path_join("../usr/share/zoneinfo", c
->zone
);
290 r
= symlink_atomic(p
, "/etc/localtime");
297 static int context_write_data_local_rtc(Context
*c
) {
299 _cleanup_free_
char *s
= NULL
, *w
= NULL
;
303 r
= read_full_file("/etc/adjtime", &s
, NULL
);
311 w
= strdup(NULL_ADJTIME_LOCAL
);
316 const char *e
= "\n"; /* default if there is less than 3 lines */
317 const char *prepend
= "";
320 p
= strchrnul(s
, '\n');
322 /* only one line, no \n terminator */
324 else if (p
[1] == '\0') {
325 /* only one line, with \n terminator */
329 p
= strchr(p
+1, '\n');
331 /* only two lines, no \n terminator */
336 /* third line might have a \n terminator or not */
338 end
= strchr(p
, '\n');
339 /* if we actually have a fourth line, use that as suffix "e", otherwise the default \n */
348 w
= new(char, a
+ (c
->local_rtc
? 5 : 3) + strlen(prepend
) + b
+ 1);
352 *(char*) mempcpy(stpcpy(stpcpy(mempcpy(w
, s
, a
), prepend
), c
->local_rtc
? "LOCAL" : "UTC"), e
, b
) = 0;
354 if (streq(w
, NULL_ADJTIME_UTC
)) {
355 if (unlink("/etc/adjtime") < 0)
364 return write_string_file_atomic_label("/etc/adjtime", w
);
367 static int context_update_ntp_status(Context
*c
, sd_bus
*bus
, sd_bus_message
*m
) {
368 static const struct bus_properties_map map
[] = {
369 { "LoadState", "s", NULL
, offsetof(UnitStatusInfo
, load_state
) },
370 { "ActiveState", "s", NULL
, offsetof(UnitStatusInfo
, active_state
) },
371 { "UnitFileState", "s", NULL
, offsetof(UnitStatusInfo
, unit_file_state
) },
380 /* Suppress calling context_update_ntp_status() multiple times within single DBus transaction. */
385 sd_bus_message_unref(c
->cache
);
386 c
->cache
= sd_bus_message_ref(m
);
389 LIST_FOREACH(units
, u
, c
->units
) {
390 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
391 _cleanup_free_
char *path
= NULL
;
393 unit_status_info_clear(u
);
395 path
= unit_dbus_path_from_name(u
->name
);
399 r
= bus_map_all_properties(
401 "org.freedesktop.systemd1",
409 return log_error_errno(r
, "Failed to get properties: %s", bus_error_message(&error
, r
));
415 static int match_job_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
416 Context
*c
= userdata
;
425 r
= sd_bus_message_read(m
, "uoss", NULL
, &path
, NULL
, NULL
);
427 bus_log_parse_error(r
);
431 LIST_FOREACH(units
, u
, c
->units
)
432 if (streq_ptr(path
, u
->path
))
433 u
->path
= mfree(u
->path
);
438 c
->slot_job_removed
= sd_bus_slot_unref(c
->slot_job_removed
);
440 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
), "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "NTP", NULL
);
446 static int unit_start_or_stop(UnitStatusInfo
*u
, sd_bus
*bus
, sd_bus_error
*error
, bool start
) {
447 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
455 r
= sd_bus_call_method(
457 "org.freedesktop.systemd1",
458 "/org/freedesktop/systemd1",
459 "org.freedesktop.systemd1.Manager",
460 start
? "StartUnit" : "StopUnit",
469 r
= sd_bus_message_read(reply
, "o", &path
);
471 return bus_log_parse_error(r
);
473 r
= free_and_strdup(&u
->path
, path
);
480 static int unit_enable_or_disable(UnitStatusInfo
*u
, sd_bus
*bus
, sd_bus_error
*error
, bool enable
) {
487 /* Call context_update_ntp_status() to update UnitStatusInfo before calling this. */
489 if (streq(u
->unit_file_state
, "enabled") == enable
)
493 r
= sd_bus_call_method(
495 "org.freedesktop.systemd1",
496 "/org/freedesktop/systemd1",
497 "org.freedesktop.systemd1.Manager",
505 r
= sd_bus_call_method(
507 "org.freedesktop.systemd1",
508 "/org/freedesktop/systemd1",
509 "org.freedesktop.systemd1.Manager",
519 r
= sd_bus_call_method(
521 "org.freedesktop.systemd1",
522 "/org/freedesktop/systemd1",
523 "org.freedesktop.systemd1.Manager",
534 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_time
, "t", now(CLOCK_REALTIME
));
535 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_ntp_sync
, "b", ntp_synced());
537 static int property_get_rtc_time(
540 const char *interface
,
541 const char *property
,
542 sd_bus_message
*reply
,
544 sd_bus_error
*error
) {
551 r
= clock_get_hwclock(&tm
);
553 log_warning("/dev/rtc is busy. Is somebody keeping it open continuously? That's not a good idea... Returning a bogus RTC timestamp.");
555 } else if (r
== -ENOENT
) {
556 log_debug("/dev/rtc not found.");
557 t
= 0; /* no RTC found */
559 return sd_bus_error_set_errnof(error
, r
, "Failed to read RTC: %m");
561 t
= (usec_t
) timegm(&tm
) * USEC_PER_SEC
;
563 return sd_bus_message_append(reply
, "t", t
);
566 static int property_get_can_ntp(
569 const char *interface
,
570 const char *property
,
571 sd_bus_message
*reply
,
573 sd_bus_error
*error
) {
575 Context
*c
= userdata
;
584 if (c
->slot_job_removed
)
585 /* When the previous request is not finished, then assume NTP is enabled. */
586 return sd_bus_message_append(reply
, "b", true);
588 r
= context_update_ntp_status(c
, bus
, reply
);
592 return sd_bus_message_append(reply
, "b", context_ntp_service_exists(c
) > 0);
595 static int property_get_ntp(
598 const char *interface
,
599 const char *property
,
600 sd_bus_message
*reply
,
602 sd_bus_error
*error
) {
604 Context
*c
= userdata
;
613 if (c
->slot_job_removed
)
614 /* When the previous request is not finished, then assume NTP is active. */
615 return sd_bus_message_append(reply
, "b", true);
617 r
= context_update_ntp_status(c
, bus
, reply
);
621 return sd_bus_message_append(reply
, "b", context_ntp_service_is_active(c
) > 0);
624 static int method_set_timezone(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
625 Context
*c
= userdata
;
632 r
= sd_bus_message_read(m
, "sb", &z
, &interactive
);
636 if (!timezone_is_valid(z
, LOG_DEBUG
))
637 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid time zone '%s'", z
);
639 if (streq_ptr(z
, c
->zone
))
640 return sd_bus_reply_method_return(m
, NULL
);
642 r
= bus_verify_polkit_async(
645 "org.freedesktop.timedate1.set-timezone",
654 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
656 r
= free_and_strdup(&c
->zone
, z
);
660 /* 1. Write new configuration file */
661 r
= context_write_data_timezone(c
);
663 log_error_errno(r
, "Failed to set time zone: %m");
664 return sd_bus_error_set_errnof(error
, r
, "Failed to set time zone: %m");
667 /* 2. Make glibc notice the new timezone */
670 /* 3. Tell the kernel our timezone */
671 r
= clock_set_timezone(NULL
);
673 log_debug_errno(r
, "Failed to tell kernel about timezone, ignoring: %m");
679 /* 4. Sync RTC from system clock, with the new delta */
680 assert_se(clock_gettime(CLOCK_REALTIME
, &ts
) == 0);
681 assert_se(localtime_r(&ts
.tv_sec
, &tm
));
683 r
= clock_set_hwclock(&tm
);
685 log_debug_errno(r
, "Failed to sync time to hardware clock, ignoring: %m");
689 "MESSAGE_ID=" SD_MESSAGE_TIMEZONE_CHANGE_STR
,
690 "TIMEZONE=%s", c
->zone
,
691 "TIMEZONE_SHORTNAME=%s", tzname
[daylight
],
692 "DAYLIGHT=%i", daylight
,
693 LOG_MESSAGE("Changed time zone to '%s' (%s).", c
->zone
, tzname
[daylight
]));
695 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
), "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "Timezone", NULL
);
697 return sd_bus_reply_method_return(m
, NULL
);
700 static int method_set_local_rtc(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
701 int lrtc
, fix_system
, interactive
;
702 Context
*c
= userdata
;
709 r
= sd_bus_message_read(m
, "bbb", &lrtc
, &fix_system
, &interactive
);
713 if (lrtc
== c
->local_rtc
)
714 return sd_bus_reply_method_return(m
, NULL
);
716 r
= bus_verify_polkit_async(
719 "org.freedesktop.timedate1.set-local-rtc",
732 /* 1. Write new configuration file */
733 r
= context_write_data_local_rtc(c
);
735 log_error_errno(r
, "Failed to set RTC to local/UTC: %m");
736 return sd_bus_error_set_errnof(error
, r
, "Failed to set RTC to local/UTC: %m");
739 /* 2. Tell the kernel our timezone */
740 r
= clock_set_timezone(NULL
);
742 log_debug_errno(r
, "Failed to tell kernel about timezone, ignoring: %m");
744 /* 3. Synchronize clocks */
745 assert_se(clock_gettime(CLOCK_REALTIME
, &ts
) == 0);
750 /* Sync system clock from RTC; first, initialize the timezone fields of struct tm. */
752 localtime_r(&ts
.tv_sec
, &tm
);
754 gmtime_r(&ts
.tv_sec
, &tm
);
756 /* Override the main fields of struct tm, but not the timezone fields */
757 r
= clock_get_hwclock(&tm
);
759 log_debug_errno(r
, "Failed to get hardware clock, ignoring: %m");
761 /* And set the system clock with this */
763 ts
.tv_sec
= mktime(&tm
);
765 ts
.tv_sec
= timegm(&tm
);
767 if (clock_settime(CLOCK_REALTIME
, &ts
) < 0)
768 log_debug_errno(errno
, "Failed to update system clock, ignoring: %m");
774 /* Sync RTC from system clock */
776 localtime_r(&ts
.tv_sec
, &tm
);
778 gmtime_r(&ts
.tv_sec
, &tm
);
780 r
= clock_set_hwclock(&tm
);
782 log_debug_errno(r
, "Failed to sync time to hardware clock, ignoring: %m");
785 log_info("RTC configured to %s time.", c
->local_rtc
? "local" : "UTC");
787 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
), "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "LocalRTC", NULL
);
789 return sd_bus_reply_method_return(m
, NULL
);
792 static int method_set_time(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
793 sd_bus
*bus
= sd_bus_message_get_bus(m
);
794 int relative
, interactive
, r
;
795 Context
*c
= userdata
;
804 if (c
->slot_job_removed
)
805 return sd_bus_error_set(error
, BUS_ERROR_AUTOMATIC_TIME_SYNC_ENABLED
, "Previous request is not finished, refusing.");
807 r
= context_update_ntp_status(c
, bus
, m
);
809 return sd_bus_error_set_errnof(error
, r
, "Failed to update context: %m");
811 if (context_ntp_service_is_active(c
) > 0)
812 return sd_bus_error_set(error
, BUS_ERROR_AUTOMATIC_TIME_SYNC_ENABLED
, "Automatic time synchronization is enabled");
814 /* this only gets used if dbus does not provide a timestamp */
815 start
= now(CLOCK_MONOTONIC
);
817 r
= sd_bus_message_read(m
, "xbb", &utc
, &relative
, &interactive
);
821 if (!relative
&& utc
<= 0)
822 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid absolute time");
824 if (relative
&& utc
== 0)
825 return sd_bus_reply_method_return(m
, NULL
);
830 n
= now(CLOCK_REALTIME
);
833 if ((utc
> 0 && x
< n
) ||
835 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Time value overflow");
837 timespec_store(&ts
, x
);
839 timespec_store(&ts
, (usec_t
) utc
);
841 r
= bus_verify_polkit_async(
844 "org.freedesktop.timedate1.set-time",
855 /* adjust ts for time spent in program */
856 r
= sd_bus_message_get_monotonic_usec(m
, &start
);
857 /* when sd_bus_message_get_monotonic_usec() returns -ENODATA it does not modify &start */
858 if (r
< 0 && r
!= -ENODATA
)
861 timespec_store(&ts
, timespec_load(&ts
) + (now(CLOCK_MONOTONIC
) - start
));
863 /* Set system clock */
864 if (clock_settime(CLOCK_REALTIME
, &ts
) < 0) {
865 log_error_errno(errno
, "Failed to set local time: %m");
866 return sd_bus_error_set_errnof(error
, errno
, "Failed to set local time: %m");
869 /* Sync down to RTC */
871 localtime_r(&ts
.tv_sec
, &tm
);
873 gmtime_r(&ts
.tv_sec
, &tm
);
875 r
= clock_set_hwclock(&tm
);
877 log_debug_errno(r
, "Failed to update hardware clock, ignoring: %m");
880 "MESSAGE_ID=" SD_MESSAGE_TIME_CHANGE_STR
,
881 "REALTIME="USEC_FMT
, timespec_load(&ts
),
882 LOG_MESSAGE("Changed local time to %s", ctime(&ts
.tv_sec
)));
884 return sd_bus_reply_method_return(m
, NULL
);
887 static int method_set_ntp(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
888 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
889 sd_bus
*bus
= sd_bus_message_get_bus(m
);
890 Context
*c
= userdata
;
892 int enable
, interactive
, q
, r
;
898 r
= sd_bus_message_read(m
, "bb", &enable
, &interactive
);
902 r
= context_update_ntp_status(c
, bus
, m
);
906 if (context_ntp_service_exists(c
) <= 0)
907 return sd_bus_error_set(error
, BUS_ERROR_NO_NTP_SUPPORT
, "NTP not supported");
909 r
= bus_verify_polkit_async(
912 "org.freedesktop.timedate1.set-ntp",
923 /* This method may be called frequently. Forget the previous job if it has not completed yet. */
924 LIST_FOREACH(units
, u
, c
->units
)
925 u
->path
= mfree(u
->path
);
927 if (!c
->slot_job_removed
) {
928 r
= sd_bus_match_signal_async(
931 "org.freedesktop.systemd1",
932 "/org/freedesktop/systemd1",
933 "org.freedesktop.systemd1.Manager",
935 match_job_removed
, NULL
, c
);
941 LIST_FOREACH(units
, u
, c
->units
) {
942 if (!streq(u
->load_state
, "loaded"))
945 q
= unit_enable_or_disable(u
, bus
, error
, enable
);
949 q
= unit_start_or_stop(u
, bus
, error
, enable
);
954 else if (context_ntp_service_is_enabled(c
) <= 0)
955 LIST_FOREACH(units
, u
, c
->units
) {
956 if (!streq(u
->load_state
, "loaded"))
959 r
= unit_enable_or_disable(u
, bus
, error
, enable
);
963 r
= unit_start_or_stop(u
, bus
, error
, enable
);
968 LIST_FOREACH(units
, u
, c
->units
) {
969 if (!streq(u
->load_state
, "loaded") ||
970 !streq(u
->unit_file_state
, "enabled"))
973 r
= unit_start_or_stop(u
, bus
, error
, enable
);
981 c
->slot_job_removed
= TAKE_PTR(slot
);
983 log_info("Set NTP to %sd", enable_disable(enable
));
985 return sd_bus_reply_method_return(m
, NULL
);
988 static int method_list_timezones(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
989 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
990 _cleanup_strv_free_
char **zones
= NULL
;
995 r
= get_timezones(&zones
);
997 return sd_bus_error_set_errnof(error
, r
, "Failed to read list of time zones: %m");
999 r
= sd_bus_message_new_method_return(m
, &reply
);
1003 r
= sd_bus_message_append_strv(reply
, zones
);
1007 return sd_bus_send(NULL
, reply
, NULL
);
1010 static const sd_bus_vtable timedate_vtable
[] = {
1011 SD_BUS_VTABLE_START(0),
1012 SD_BUS_PROPERTY("Timezone", "s", NULL
, offsetof(Context
, zone
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1013 SD_BUS_PROPERTY("LocalRTC", "b", bus_property_get_bool
, offsetof(Context
, local_rtc
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1014 SD_BUS_PROPERTY("CanNTP", "b", property_get_can_ntp
, 0, 0),
1015 SD_BUS_PROPERTY("NTP", "b", property_get_ntp
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1016 SD_BUS_PROPERTY("NTPSynchronized", "b", property_get_ntp_sync
, 0, 0),
1017 SD_BUS_PROPERTY("TimeUSec", "t", property_get_time
, 0, 0),
1018 SD_BUS_PROPERTY("RTCTimeUSec", "t", property_get_rtc_time
, 0, 0),
1019 SD_BUS_METHOD("SetTime", "xbb", NULL
, method_set_time
, SD_BUS_VTABLE_UNPRIVILEGED
),
1020 SD_BUS_METHOD("SetTimezone", "sb", NULL
, method_set_timezone
, SD_BUS_VTABLE_UNPRIVILEGED
),
1021 SD_BUS_METHOD("SetLocalRTC", "bbb", NULL
, method_set_local_rtc
, SD_BUS_VTABLE_UNPRIVILEGED
),
1022 SD_BUS_METHOD("SetNTP", "bb", NULL
, method_set_ntp
, SD_BUS_VTABLE_UNPRIVILEGED
),
1023 SD_BUS_METHOD("ListTimezones", NULL
, "as", method_list_timezones
, SD_BUS_VTABLE_UNPRIVILEGED
),
1027 static int connect_bus(Context
*c
, sd_event
*event
, sd_bus
**_bus
) {
1028 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1035 r
= sd_bus_default_system(&bus
);
1037 return log_error_errno(r
, "Failed to get system bus connection: %m");
1039 r
= sd_bus_add_object_vtable(bus
, NULL
, "/org/freedesktop/timedate1", "org.freedesktop.timedate1", timedate_vtable
, c
);
1041 return log_error_errno(r
, "Failed to register object: %m");
1043 r
= sd_bus_request_name_async(bus
, NULL
, "org.freedesktop.timedate1", 0, NULL
, NULL
);
1045 return log_error_errno(r
, "Failed to request name: %m");
1047 r
= sd_bus_attach_event(bus
, event
, 0);
1049 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1051 *_bus
= TAKE_PTR(bus
);
1056 static int run(int argc
, char *argv
[]) {
1057 _cleanup_(context_clear
) Context context
= {};
1058 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1059 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1062 log_setup_service();
1067 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "This program takes no arguments.");
1069 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
1071 r
= sd_event_default(&event
);
1073 return log_error_errno(r
, "Failed to allocate event loop: %m");
1075 (void) sd_event_set_watchdog(event
, true);
1077 r
= sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
1079 return log_error_errno(r
, "Failed to install SIGINT handler: %m");
1081 r
= sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
1083 return log_error_errno(r
, "Failed to install SIGTERM handler: %m");
1085 r
= connect_bus(&context
, event
, &bus
);
1089 (void) sd_bus_negotiate_timestamp(bus
, true);
1091 r
= context_read_data(&context
);
1093 return log_error_errno(r
, "Failed to read time zone data: %m");
1095 r
= context_parse_ntp_services(&context
);
1099 r
= bus_event_loop_with_idle(event
, bus
, "org.freedesktop.timedate1", DEFAULT_EXIT_USEC
, NULL
, NULL
);
1101 return log_error_errno(r
, "Failed to run event loop: %m");
1106 DEFINE_MAIN_FUNCTION(run
);