1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "sd-messages.h"
12 #include "alloc-util.h"
13 #include "bus-common-errors.h"
14 #include "bus-error.h"
16 #include "clock-util.h"
17 #include "conf-files.h"
20 #include "fileio-label.h"
25 #include "main-func.h"
26 #include "memory-util.h"
27 #include "missing_capability.h"
28 #include "path-util.h"
29 #include "selinux-util.h"
30 #include "signal-util.h"
31 #include "string-util.h"
34 #include "unit-name.h"
35 #include "user-util.h"
37 #define NULL_ADJTIME_UTC "0.0 0 0\n0\nUTC\n"
38 #define NULL_ADJTIME_LOCAL "0.0 0 0\n0\nLOCAL\n"
40 #define UNIT_LIST_DIRS (const char* const*) CONF_PATHS_STRV("systemd/ntp-units.d")
42 typedef struct UnitStatusInfo
{
45 char *unit_file_state
;
49 LIST_FIELDS(struct UnitStatusInfo
, units
);
52 typedef struct Context
{
55 Hashmap
*polkit_registry
;
56 sd_bus_message
*cache
;
58 sd_bus_slot
*slot_job_removed
;
60 LIST_HEAD(UnitStatusInfo
, units
);
63 #define log_unit_full(unit, level, error, ...) \
65 const UnitStatusInfo *_u = (unit); \
66 log_object_internal(level, error, PROJECT_FILE, __LINE__, __func__, \
67 "UNIT=", _u->name, NULL, NULL, ##__VA_ARGS__); \
70 #define log_unit_debug(unit, ...) log_unit_full(unit, LOG_DEBUG, 0, ##__VA_ARGS__)
71 #define log_unit_info(unit, ...) log_unit_full(unit, LOG_INFO, 0, ##__VA_ARGS__)
72 #define log_unit_notice(unit, ...) log_unit_full(unit, LOG_NOTICE, 0, ##__VA_ARGS__)
73 #define log_unit_warning(unit, ...) log_unit_full(unit, LOG_WARNING, 0, ##__VA_ARGS__)
74 #define log_unit_error(unit, ...) log_unit_full(unit, LOG_ERR, 0, ##__VA_ARGS__)
76 #define log_unit_debug_errno(unit, error, ...) log_unit_full(unit, LOG_DEBUG, error, ##__VA_ARGS__)
77 #define log_unit_info_errno(unit, error, ...) log_unit_full(unit, LOG_INFO, error, ##__VA_ARGS__)
78 #define log_unit_notice_errno(unit, error, ...) log_unit_full(unit, LOG_NOTICE, error, ##__VA_ARGS__)
79 #define log_unit_warning_errno(unit, error, ...) log_unit_full(unit, LOG_WARNING, error, ##__VA_ARGS__)
80 #define log_unit_error_errno(unit, error, ...) log_unit_full(unit, LOG_ERR, error, ##__VA_ARGS__)
82 static void unit_status_info_clear(UnitStatusInfo
*p
) {
85 p
->load_state
= mfree(p
->load_state
);
86 p
->unit_file_state
= mfree(p
->unit_file_state
);
87 p
->active_state
= mfree(p
->active_state
);
90 static void unit_status_info_free(UnitStatusInfo
*p
) {
93 unit_status_info_clear(p
);
99 static void context_clear(Context
*c
) {
105 bus_verify_polkit_async_registry_free(c
->polkit_registry
);
106 sd_bus_message_unref(c
->cache
);
108 sd_bus_slot_unref(c
->slot_job_removed
);
110 while ((p
= c
->units
)) {
111 LIST_REMOVE(units
, c
->units
, p
);
112 unit_status_info_free(p
);
116 static int context_add_ntp_service(Context
*c
, const char *s
, const char *source
) {
119 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
122 /* Do not add this if it is already listed */
123 LIST_FOREACH(units
, u
, c
->units
)
124 if (streq(u
->name
, s
))
127 u
= new0(UnitStatusInfo
, 1);
137 LIST_APPEND(units
, c
->units
, u
);
138 log_unit_debug(u
, "added from %s.", source
);
143 static int context_parse_ntp_services_from_environment(Context
*c
) {
149 env
= getenv("SYSTEMD_TIMEDATED_NTP_SERVICES");
153 log_debug("Using list of ntp services from environment variable $SYSTEMD_TIMEDATED_NTP_SERVICES=%s.", env
);
156 _cleanup_free_
char *word
= NULL
;
158 r
= extract_first_word(&p
, &word
, ":", 0);
164 log_error("Invalid syntax, ignoring: %s", env
);
168 r
= context_add_ntp_service(c
, word
, "$SYSTEMD_TIMEDATED_NTP_SERVICES");
170 log_warning_errno(r
, "Failed to add NTP service \"%s\", ignoring: %m", word
);
176 static int context_parse_ntp_services_from_disk(Context
*c
) {
177 _cleanup_strv_free_
char **files
= NULL
;
181 r
= conf_files_list_strv(&files
, ".list", NULL
, CONF_FILES_FILTER_MASKED
, UNIT_LIST_DIRS
);
183 return log_error_errno(r
, "Failed to enumerate .list files: %m");
185 STRV_FOREACH(f
, files
) {
186 _cleanup_fclose_
FILE *file
= NULL
;
188 log_debug("Reading file '%s'", *f
);
190 r
= fopen_unlocked(*f
, "re", &file
);
192 log_error_errno(r
, "Failed to open %s, ignoring: %m", *f
);
197 _cleanup_free_
char *line
= NULL
;
200 r
= read_line(file
, LINE_MAX
, &line
);
202 log_error_errno(r
, "Failed to read %s, ignoring: %m", *f
);
208 word
= strstrip(line
);
209 if (isempty(word
) || startswith("#", word
))
212 r
= context_add_ntp_service(c
, word
, *f
);
214 log_warning_errno(r
, "Failed to add NTP service \"%s\", ignoring: %m", word
);
221 static int context_parse_ntp_services(Context
*c
) {
224 r
= context_parse_ntp_services_from_environment(c
);
228 return context_parse_ntp_services_from_disk(c
);
231 static int context_ntp_service_is_active(Context
*c
) {
232 UnitStatusInfo
*info
;
237 /* Call context_update_ntp_status() to update UnitStatusInfo before calling this. */
239 LIST_FOREACH(units
, info
, c
->units
)
240 count
+= !STRPTR_IN_SET(info
->active_state
, "inactive", "failed");
245 static int context_ntp_service_exists(Context
*c
) {
246 UnitStatusInfo
*info
;
251 /* Call context_update_ntp_status() to update UnitStatusInfo before calling this. */
253 LIST_FOREACH(units
, info
, c
->units
)
254 count
+= streq_ptr(info
->load_state
, "loaded");
259 static int context_read_data(Context
*c
) {
260 _cleanup_free_
char *t
= NULL
;
265 r
= get_timezone(&t
);
267 log_warning_errno(r
, "/etc/localtime should be a symbolic link to a time zone data file in /usr/share/zoneinfo/.");
269 log_warning_errno(r
, "Failed to get target of /etc/localtime: %m");
271 free_and_replace(c
->zone
, t
);
273 c
->local_rtc
= clock_is_localtime(NULL
) > 0;
278 static int context_write_data_timezone(Context
*c
) {
279 _cleanup_free_
char *p
= NULL
;
284 /* No timezone is very similar to UTC. Hence in either of these cases link the UTC file in. Except if
285 * it isn't installed, in which case we remove the symlink altogether. Since glibc defaults to an
286 * internal version of UTC in that case behaviour is mostly equivalent. We still prefer creating the
287 * symlink though, since things are more self explanatory then. */
289 if (isempty(c
->zone
) || streq(c
->zone
, "UTC")) {
291 if (access("/usr/share/zoneinfo/UTC", F_OK
) < 0) {
293 if (unlink("/etc/localtime") < 0 && errno
!= ENOENT
)
299 source
= "../usr/share/zoneinfo/UTC";
301 p
= path_join("../usr/share/zoneinfo", c
->zone
);
308 return symlink_atomic(source
, "/etc/localtime");
311 static int context_write_data_local_rtc(Context
*c
) {
312 _cleanup_free_
char *s
= NULL
, *w
= NULL
;
317 r
= read_full_file("/etc/adjtime", &s
, NULL
);
325 w
= strdup(NULL_ADJTIME_LOCAL
);
330 const char *e
= "\n"; /* default if there is less than 3 lines */
331 const char *prepend
= "";
334 p
= strchrnul(s
, '\n');
336 /* only one line, no \n terminator */
338 else if (p
[1] == '\0') {
339 /* only one line, with \n terminator */
343 p
= strchr(p
+1, '\n');
345 /* only two lines, no \n terminator */
350 /* third line might have a \n terminator or not */
352 end
= strchr(p
, '\n');
353 /* if we actually have a fourth line, use that as suffix "e", otherwise the default \n */
362 w
= new(char, a
+ (c
->local_rtc
? 5 : 3) + strlen(prepend
) + b
+ 1);
366 *(char*) mempcpy(stpcpy(stpcpy(mempcpy(w
, s
, a
), prepend
), c
->local_rtc
? "LOCAL" : "UTC"), e
, b
) = 0;
368 if (streq(w
, NULL_ADJTIME_UTC
)) {
369 if (unlink("/etc/adjtime") < 0)
378 return write_string_file_atomic_label("/etc/adjtime", w
);
381 static int context_update_ntp_status(Context
*c
, sd_bus
*bus
, sd_bus_message
*m
) {
382 static const struct bus_properties_map map
[] = {
383 { "LoadState", "s", NULL
, offsetof(UnitStatusInfo
, load_state
) },
384 { "ActiveState", "s", NULL
, offsetof(UnitStatusInfo
, active_state
) },
385 { "UnitFileState", "s", NULL
, offsetof(UnitStatusInfo
, unit_file_state
) },
394 /* Suppress calling context_update_ntp_status() multiple times within single DBus transaction. */
399 sd_bus_message_unref(c
->cache
);
400 c
->cache
= sd_bus_message_ref(m
);
403 LIST_FOREACH(units
, u
, c
->units
) {
404 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
405 _cleanup_free_
char *path
= NULL
;
407 unit_status_info_clear(u
);
409 path
= unit_dbus_path_from_name(u
->name
);
413 r
= bus_map_all_properties(
415 "org.freedesktop.systemd1",
423 return log_unit_error_errno(u
, r
, "Failed to get properties: %s", bus_error_message(&error
, r
));
429 static int match_job_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
430 Context
*c
= userdata
;
439 r
= sd_bus_message_read(m
, "uoss", NULL
, &path
, NULL
, NULL
);
441 bus_log_parse_error(r
);
445 LIST_FOREACH(units
, u
, c
->units
)
446 if (streq_ptr(path
, u
->path
))
447 u
->path
= mfree(u
->path
);
452 c
->slot_job_removed
= sd_bus_slot_unref(c
->slot_job_removed
);
454 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
),
455 "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "NTP",
462 static int unit_start_or_stop(UnitStatusInfo
*u
, sd_bus
*bus
, sd_bus_error
*error
, bool start
) {
463 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
471 r
= sd_bus_call_method(
473 "org.freedesktop.systemd1",
474 "/org/freedesktop/systemd1",
475 "org.freedesktop.systemd1.Manager",
476 start
? "StartUnit" : "StopUnit",
482 log_unit_full(u
, r
< 0 ? LOG_WARNING
: LOG_DEBUG
, r
,
483 "%s unit: %m", start
? "Starting" : "Stopping");
487 r
= sd_bus_message_read(reply
, "o", &path
);
489 return bus_log_parse_error(r
);
491 r
= free_and_strdup(&u
->path
, path
);
498 static int unit_enable_or_disable(UnitStatusInfo
*u
, sd_bus
*bus
, sd_bus_error
*error
, bool enable
) {
505 /* Call context_update_ntp_status() to update UnitStatusInfo before calling this. */
507 if (streq(u
->unit_file_state
, "enabled") == enable
) {
508 log_unit_debug(u
, "already %sd.", enable_disable(enable
));
512 log_unit_info(u
, "%s unit.", enable
? "Enabling" : "Disabling");
515 r
= sd_bus_call_method(
517 "org.freedesktop.systemd1",
518 "/org/freedesktop/systemd1",
519 "org.freedesktop.systemd1.Manager",
527 r
= sd_bus_call_method(
529 "org.freedesktop.systemd1",
530 "/org/freedesktop/systemd1",
531 "org.freedesktop.systemd1.Manager",
541 r
= sd_bus_call_method(
543 "org.freedesktop.systemd1",
544 "/org/freedesktop/systemd1",
545 "org.freedesktop.systemd1.Manager",
556 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_time
, "t", now(CLOCK_REALTIME
));
557 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_ntp_sync
, "b", ntp_synced());
559 static int property_get_rtc_time(
562 const char *interface
,
563 const char *property
,
564 sd_bus_message
*reply
,
566 sd_bus_error
*error
) {
572 r
= clock_get_hwclock(&tm
);
574 log_warning("/dev/rtc is busy. Is somebody keeping it open continuously? That's not a good idea... Returning a bogus RTC timestamp.");
575 else if (r
== -ENOENT
)
576 log_debug("/dev/rtc not found.");
578 return sd_bus_error_set_errnof(error
, r
, "Failed to read RTC: %m");
580 t
= (usec_t
) timegm(&tm
) * USEC_PER_SEC
;
582 return sd_bus_message_append(reply
, "t", t
);
585 static int property_get_can_ntp(
588 const char *interface
,
589 const char *property
,
590 sd_bus_message
*reply
,
592 sd_bus_error
*error
) {
594 Context
*c
= userdata
;
603 if (c
->slot_job_removed
)
604 /* When the previous request is not finished, then assume NTP is enabled. */
605 return sd_bus_message_append(reply
, "b", true);
607 r
= context_update_ntp_status(c
, bus
, reply
);
611 return sd_bus_message_append(reply
, "b", context_ntp_service_exists(c
) > 0);
614 static int property_get_ntp(
617 const char *interface
,
618 const char *property
,
619 sd_bus_message
*reply
,
621 sd_bus_error
*error
) {
623 Context
*c
= userdata
;
632 if (c
->slot_job_removed
)
633 /* When the previous request is not finished, then assume NTP is active. */
634 return sd_bus_message_append(reply
, "b", true);
636 r
= context_update_ntp_status(c
, bus
, reply
);
640 return sd_bus_message_append(reply
, "b", context_ntp_service_is_active(c
) > 0);
643 static int method_set_timezone(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
644 Context
*c
= userdata
;
651 r
= sd_bus_message_read(m
, "sb", &z
, &interactive
);
655 if (!timezone_is_valid(z
, LOG_DEBUG
))
656 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid or not installed time zone '%s'", z
);
658 if (streq_ptr(z
, c
->zone
))
659 return sd_bus_reply_method_return(m
, NULL
);
661 r
= bus_verify_polkit_async(
664 "org.freedesktop.timedate1.set-timezone",
673 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
675 r
= free_and_strdup(&c
->zone
, z
);
679 /* 1. Write new configuration file */
680 r
= context_write_data_timezone(c
);
682 log_error_errno(r
, "Failed to set time zone: %m");
683 return sd_bus_error_set_errnof(error
, r
, "Failed to set time zone: %m");
686 /* 2. Make glibc notice the new timezone */
689 /* 3. Tell the kernel our timezone */
690 r
= clock_set_timezone(NULL
);
692 log_debug_errno(r
, "Failed to tell kernel about timezone, ignoring: %m");
698 /* 4. Sync RTC from system clock, with the new delta */
699 assert_se(clock_gettime(CLOCK_REALTIME
, &ts
) == 0);
700 assert_se(localtime_r(&ts
.tv_sec
, &tm
));
702 r
= clock_set_hwclock(&tm
);
704 log_debug_errno(r
, "Failed to sync time to hardware clock, ignoring: %m");
708 "MESSAGE_ID=" SD_MESSAGE_TIMEZONE_CHANGE_STR
,
709 "TIMEZONE=%s", c
->zone
,
710 "TIMEZONE_SHORTNAME=%s", tzname
[daylight
],
711 "DAYLIGHT=%i", daylight
,
712 LOG_MESSAGE("Changed time zone to '%s' (%s).", c
->zone
, tzname
[daylight
]));
714 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
),
715 "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "Timezone",
718 return sd_bus_reply_method_return(m
, NULL
);
721 static int method_set_local_rtc(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
722 int lrtc
, fix_system
, interactive
;
723 Context
*c
= userdata
;
730 r
= sd_bus_message_read(m
, "bbb", &lrtc
, &fix_system
, &interactive
);
734 if (lrtc
== c
->local_rtc
)
735 return sd_bus_reply_method_return(m
, NULL
);
737 r
= bus_verify_polkit_async(
740 "org.freedesktop.timedate1.set-local-rtc",
753 /* 1. Write new configuration file */
754 r
= context_write_data_local_rtc(c
);
756 log_error_errno(r
, "Failed to set RTC to %s: %m", lrtc
? "local" : "UTC");
757 return sd_bus_error_set_errnof(error
, r
, "Failed to set RTC to %s: %m", lrtc
? "local" : "UTC");
760 /* 2. Tell the kernel our timezone */
761 r
= clock_set_timezone(NULL
);
763 log_debug_errno(r
, "Failed to tell kernel about timezone, ignoring: %m");
765 /* 3. Synchronize clocks */
766 assert_se(clock_gettime(CLOCK_REALTIME
, &ts
) == 0);
771 /* Sync system clock from RTC; first, initialize the timezone fields of struct tm. */
773 localtime_r(&ts
.tv_sec
, &tm
);
775 gmtime_r(&ts
.tv_sec
, &tm
);
777 /* Override the main fields of struct tm, but not the timezone fields */
778 r
= clock_get_hwclock(&tm
);
780 log_debug_errno(r
, "Failed to get hardware clock, ignoring: %m");
782 /* And set the system clock with this */
784 ts
.tv_sec
= mktime(&tm
);
786 ts
.tv_sec
= timegm(&tm
);
788 if (clock_settime(CLOCK_REALTIME
, &ts
) < 0)
789 log_debug_errno(errno
, "Failed to update system clock, ignoring: %m");
795 /* Sync RTC from system clock */
797 localtime_r(&ts
.tv_sec
, &tm
);
799 gmtime_r(&ts
.tv_sec
, &tm
);
801 r
= clock_set_hwclock(&tm
);
803 log_debug_errno(r
, "Failed to sync time to hardware clock, ignoring: %m");
806 log_info("RTC configured to %s time.", c
->local_rtc
? "local" : "UTC");
808 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
),
809 "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "LocalRTC",
812 return sd_bus_reply_method_return(m
, NULL
);
815 static int method_set_time(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
816 sd_bus
*bus
= sd_bus_message_get_bus(m
);
817 int relative
, interactive
, r
;
818 Context
*c
= userdata
;
827 if (c
->slot_job_removed
)
828 return sd_bus_error_set(error
, BUS_ERROR_AUTOMATIC_TIME_SYNC_ENABLED
, "Previous request is not finished, refusing.");
830 r
= context_update_ntp_status(c
, bus
, m
);
832 return sd_bus_error_set_errnof(error
, r
, "Failed to update context: %m");
834 if (context_ntp_service_is_active(c
) > 0)
835 return sd_bus_error_set(error
, BUS_ERROR_AUTOMATIC_TIME_SYNC_ENABLED
, "Automatic time synchronization is enabled");
837 /* this only gets used if dbus does not provide a timestamp */
838 start
= now(CLOCK_MONOTONIC
);
840 r
= sd_bus_message_read(m
, "xbb", &utc
, &relative
, &interactive
);
844 if (!relative
&& utc
<= 0)
845 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid absolute time");
847 if (relative
&& utc
== 0)
848 return sd_bus_reply_method_return(m
, NULL
);
853 n
= now(CLOCK_REALTIME
);
856 if ((utc
> 0 && x
< n
) ||
858 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Time value overflow");
860 timespec_store(&ts
, x
);
862 timespec_store(&ts
, (usec_t
) utc
);
864 r
= bus_verify_polkit_async(
867 "org.freedesktop.timedate1.set-time",
878 /* adjust ts for time spent in program */
879 r
= sd_bus_message_get_monotonic_usec(m
, &start
);
880 /* when sd_bus_message_get_monotonic_usec() returns -ENODATA it does not modify &start */
881 if (r
< 0 && r
!= -ENODATA
)
884 timespec_store(&ts
, timespec_load(&ts
) + (now(CLOCK_MONOTONIC
) - start
));
886 /* Set system clock */
887 if (clock_settime(CLOCK_REALTIME
, &ts
) < 0) {
888 log_error_errno(errno
, "Failed to set local time: %m");
889 return sd_bus_error_set_errnof(error
, errno
, "Failed to set local time: %m");
892 /* Sync down to RTC */
894 localtime_r(&ts
.tv_sec
, &tm
);
896 gmtime_r(&ts
.tv_sec
, &tm
);
898 r
= clock_set_hwclock(&tm
);
900 log_debug_errno(r
, "Failed to update hardware clock, ignoring: %m");
903 "MESSAGE_ID=" SD_MESSAGE_TIME_CHANGE_STR
,
904 "REALTIME="USEC_FMT
, timespec_load(&ts
),
905 LOG_MESSAGE("Changed local time to %s", ctime(&ts
.tv_sec
)));
907 return sd_bus_reply_method_return(m
, NULL
);
910 static int method_set_ntp(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
911 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
912 sd_bus
*bus
= sd_bus_message_get_bus(m
);
913 Context
*c
= userdata
;
915 const UnitStatusInfo
*selected
= NULL
;
916 int enable
, interactive
, q
, r
;
922 r
= sd_bus_message_read(m
, "bb", &enable
, &interactive
);
926 r
= context_update_ntp_status(c
, bus
, m
);
930 if (context_ntp_service_exists(c
) <= 0)
931 return sd_bus_error_set(error
, BUS_ERROR_NO_NTP_SUPPORT
, "NTP not supported");
933 r
= bus_verify_polkit_async(
936 "org.freedesktop.timedate1.set-ntp",
947 /* This method may be called frequently. Forget the previous job if it has not completed yet. */
948 LIST_FOREACH(units
, u
, c
->units
)
949 u
->path
= mfree(u
->path
);
951 if (!c
->slot_job_removed
) {
952 r
= sd_bus_match_signal_async(
955 "org.freedesktop.systemd1",
956 "/org/freedesktop/systemd1",
957 "org.freedesktop.systemd1.Manager",
959 match_job_removed
, NULL
, c
);
965 LIST_FOREACH(units
, u
, c
->units
) {
966 bool enable_this_one
= !selected
;
968 if (!streq(u
->load_state
, "loaded"))
971 r
= unit_enable_or_disable(u
, bus
, error
, enable_this_one
);
973 /* If enablement failed, don't start this unit. */
974 enable_this_one
= false;
976 r
= unit_start_or_stop(u
, bus
, error
, enable_this_one
);
978 log_unit_warning_errno(u
, r
, "Failed to %s %sd NTP unit, ignoring: %m",
979 enable_this_one
? "start" : "stop",
980 enable_disable(enable_this_one
));
985 LIST_FOREACH(units
, u
, c
->units
) {
986 if (!streq(u
->load_state
, "loaded"))
989 q
= unit_enable_or_disable(u
, bus
, error
, false);
993 q
= unit_start_or_stop(u
, bus
, error
, false);
1000 if (enable
&& !selected
)
1001 return log_error_errno(SYNTHETIC_ERRNO(ENOENT
), "No NTP service found to enable.");
1004 c
->slot_job_removed
= TAKE_PTR(slot
);
1007 log_info("Set NTP to enabled (%s).", selected
->name
);
1009 log_info("Set NTP to disabled.");
1011 return sd_bus_reply_method_return(m
, NULL
);
1014 static int method_list_timezones(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1015 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1016 _cleanup_strv_free_
char **zones
= NULL
;
1021 r
= get_timezones(&zones
);
1023 return sd_bus_error_set_errnof(error
, r
, "Failed to read list of time zones: %m");
1025 r
= sd_bus_message_new_method_return(m
, &reply
);
1029 r
= sd_bus_message_append_strv(reply
, zones
);
1033 return sd_bus_send(NULL
, reply
, NULL
);
1036 static const sd_bus_vtable timedate_vtable
[] = {
1037 SD_BUS_VTABLE_START(0),
1038 SD_BUS_PROPERTY("Timezone", "s", NULL
, offsetof(Context
, zone
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1039 SD_BUS_PROPERTY("LocalRTC", "b", bus_property_get_bool
, offsetof(Context
, local_rtc
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1040 SD_BUS_PROPERTY("CanNTP", "b", property_get_can_ntp
, 0, 0),
1041 SD_BUS_PROPERTY("NTP", "b", property_get_ntp
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1042 SD_BUS_PROPERTY("NTPSynchronized", "b", property_get_ntp_sync
, 0, 0),
1043 SD_BUS_PROPERTY("TimeUSec", "t", property_get_time
, 0, 0),
1044 SD_BUS_PROPERTY("RTCTimeUSec", "t", property_get_rtc_time
, 0, 0),
1045 SD_BUS_METHOD("SetTime", "xbb", NULL
, method_set_time
, SD_BUS_VTABLE_UNPRIVILEGED
),
1046 SD_BUS_METHOD("SetTimezone", "sb", NULL
, method_set_timezone
, SD_BUS_VTABLE_UNPRIVILEGED
),
1047 SD_BUS_METHOD("SetLocalRTC", "bbb", NULL
, method_set_local_rtc
, SD_BUS_VTABLE_UNPRIVILEGED
),
1048 SD_BUS_METHOD("SetNTP", "bb", NULL
, method_set_ntp
, SD_BUS_VTABLE_UNPRIVILEGED
),
1049 SD_BUS_METHOD("ListTimezones", NULL
, "as", method_list_timezones
, SD_BUS_VTABLE_UNPRIVILEGED
),
1053 static int connect_bus(Context
*c
, sd_event
*event
, sd_bus
**_bus
) {
1054 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1061 r
= sd_bus_default_system(&bus
);
1063 return log_error_errno(r
, "Failed to get system bus connection: %m");
1065 r
= sd_bus_add_object_vtable(bus
, NULL
, "/org/freedesktop/timedate1", "org.freedesktop.timedate1", timedate_vtable
, c
);
1067 return log_error_errno(r
, "Failed to register object: %m");
1069 r
= sd_bus_request_name_async(bus
, NULL
, "org.freedesktop.timedate1", 0, NULL
, NULL
);
1071 return log_error_errno(r
, "Failed to request name: %m");
1073 r
= sd_bus_attach_event(bus
, event
, 0);
1075 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1077 *_bus
= TAKE_PTR(bus
);
1082 static int run(int argc
, char *argv
[]) {
1083 _cleanup_(context_clear
) Context context
= {};
1084 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1085 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1088 log_setup_service();
1093 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "This program takes no arguments.");
1095 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
1097 r
= sd_event_default(&event
);
1099 return log_error_errno(r
, "Failed to allocate event loop: %m");
1101 (void) sd_event_set_watchdog(event
, true);
1103 r
= sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
1105 return log_error_errno(r
, "Failed to install SIGINT handler: %m");
1107 r
= sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
1109 return log_error_errno(r
, "Failed to install SIGTERM handler: %m");
1111 r
= connect_bus(&context
, event
, &bus
);
1115 (void) sd_bus_negotiate_timestamp(bus
, true);
1117 r
= context_read_data(&context
);
1119 return log_error_errno(r
, "Failed to read time zone data: %m");
1121 r
= context_parse_ntp_services(&context
);
1125 r
= bus_event_loop_with_idle(event
, bus
, "org.freedesktop.timedate1", DEFAULT_EXIT_USEC
, NULL
, NULL
);
1127 return log_error_errno(r
, "Failed to run event loop: %m");
1132 DEFINE_MAIN_FUNCTION(run
);