timedated: Add dbus method to retrieve list of time zones (#11114)
authortibbling <45659916+tibbling@users.noreply.github.com>
Wed, 12 Dec 2018 19:49:04 +0000 (20:49 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 12 Dec 2018 19:49:04 +0000 (20:49 +0100)
Move function call get_timezones from timedatectl to timedated and
create a dbus method to list timezones.

src/timedate/timedatectl.c
src/timedate/timedated.c

index 24b103b..1e7b262 100644 (file)
@@ -283,12 +283,27 @@ static int set_ntp(int argc, char **argv, void *userdata) {
 }
 
 static int list_timezones(int argc, char **argv, void *userdata) {
-        _cleanup_strv_free_ char **zones = NULL;
+        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
+        sd_bus *bus = userdata;
+        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
         int r;
+        char** zones;
+
+        r = sd_bus_call_method(bus,
+                               "org.freedesktop.timedate1",
+                               "/org/freedesktop/timedate1",
+                               "org.freedesktop.timedate1",
+                               "ListTimezones",
+                               &error,
+                               &reply,
+                               NULL);
+        if (r < 0)
+                return log_error_errno(r, "Failed to request list of time zones: %s",
+                                       bus_error_message(&error, r));
 
-        r = get_timezones(&zones);
+        r = sd_bus_message_read_strv(reply, &zones);
         if (r < 0)
-                return log_error_errno(r, "Failed to read list of time zones: %m");
+                return bus_log_parse_error(r);
 
         (void) pager_open(arg_pager_flags);
         strv_print(zones);
index 5a432fe..e168889 100644 (file)
@@ -916,6 +916,28 @@ static int method_set_ntp(sd_bus_message *m, void *userdata, sd_bus_error *error
         return sd_bus_reply_method_return(m, NULL);
 }
 
+static int method_list_timezones(sd_bus_message *m, void *userdata, sd_bus_error *error) {
+        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
+        _cleanup_strv_free_ char **zones = NULL;
+        int r;
+
+        assert(m);
+
+        r = get_timezones(&zones);
+        if (r < 0)
+                return sd_bus_error_set_errnof(error, r, "Failed to read list of time zones: %m");
+
+        r = sd_bus_message_new_method_return(m, &reply);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_append_strv(reply, zones);
+        if (r < 0)
+                return r;
+
+        return sd_bus_send(NULL, reply, NULL);
+}
+
 static const sd_bus_vtable timedate_vtable[] = {
         SD_BUS_VTABLE_START(0),
         SD_BUS_PROPERTY("Timezone", "s", NULL, offsetof(Context, zone), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
@@ -929,6 +951,7 @@ static const sd_bus_vtable timedate_vtable[] = {
         SD_BUS_METHOD("SetTimezone", "sb", NULL, method_set_timezone, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_METHOD("SetLocalRTC", "bbb", NULL, method_set_local_rtc, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_METHOD("SetNTP", "bb", NULL, method_set_ntp, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("ListTimezones", NULL, "as", method_list_timezones, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_VTABLE_END,
 };