[systemd-commits] src/libsystemd-bus src/systemd src/timedate

Lennart Poettering lennart at kemper.freedesktop.org
Thu Oct 31 03:02:56 CET 2013


 src/libsystemd-bus/bus-convenience.c |   36 +++++++++++++
 src/systemd/sd-bus.h                 |    1 
 src/timedate/timedatectl.c           |   94 ++++++++++++++---------------------
 3 files changed, 75 insertions(+), 56 deletions(-)

New commits:
commit e5609878d8802e2469c433be418bcbcf55fbe63b
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Oct 31 03:02:49 2013 +0100

    timedatectl: assorted simplifications

diff --git a/src/libsystemd-bus/bus-convenience.c b/src/libsystemd-bus/bus-convenience.c
index 95a7577..7c98242 100644
--- a/src/libsystemd-bus/bus-convenience.c
+++ b/src/libsystemd-bus/bus-convenience.c
@@ -23,6 +23,7 @@
 #include "bus-message.h"
 #include "bus-signature.h"
 #include "bus-util.h"
+#include "bus-type.h"
 
 int sd_bus_emit_signal(
                 sd_bus *bus,
@@ -269,6 +270,41 @@ int sd_bus_get_property(
         return 0;
 }
 
+int sd_bus_get_property_trivial(
+                sd_bus *bus,
+                const char *destination,
+                const char *path,
+                const char *interface,
+                const char *member,
+                sd_bus_error *error,
+                char type, void *ptr) {
+
+        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+        int r;
+
+        assert_return(bus, -EINVAL);
+        assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL);
+        assert_return(member_name_is_valid(member), -EINVAL);
+        assert_return(bus_type_is_trivial(type), -EINVAL);
+        assert_return(ptr, -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+
+        r = sd_bus_call_method(bus, destination, path, "org.freedesktop.DBus.Properties", "Get", error, &reply, "ss", strempty(interface), member);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_enter_container(reply, 'v', CHAR_TO_STR(type));
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_read_basic(reply, type, ptr);
+        if (r < 0)
+                return r;
+
+        return 0;
+}
+
 int sd_bus_set_property(
                 sd_bus *bus,
                 const char *destination,
diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h
index 7163b0c..affd309 100644
--- a/src/systemd/sd-bus.h
+++ b/src/systemd/sd-bus.h
@@ -207,6 +207,7 @@ int sd_bus_message_rewind(sd_bus_message *m, int complete);
 
 int sd_bus_call_method(sd_bus *bus, const char *destination, const char *path, const char *interface, const char *member, sd_bus_error *error, sd_bus_message **reply, const char *types, ...);
 int sd_bus_get_property(sd_bus *bus, const char *destination, const char *path, const char *interface, const char *member, sd_bus_error *error, sd_bus_message **reply, const char *type);
+int sd_bus_get_property_trivial(sd_bus *bus, const char *destination, const char *path, const char *interface, const char *member, sd_bus_error *error, char type, void *ptr);
 int sd_bus_set_property(sd_bus *bus, const char *destination, const char *path, const char *interface, const char *member, sd_bus_error *error, const char *type, ...);
 int sd_bus_reply_method_return(sd_bus *bus, sd_bus_message *call, const char *types, ...);
 int sd_bus_reply_method_error(sd_bus *bus, sd_bus_message *call, const sd_bus_error *e);
diff --git a/src/timedate/timedatectl.c b/src/timedate/timedatectl.c
index 31f9994..30054b6 100644
--- a/src/timedate/timedatectl.c
+++ b/src/timedate/timedatectl.c
@@ -193,29 +193,24 @@ static void print_status_info(StatusInfo *i) {
 static int get_timedate_property_bool(sd_bus *bus, const char *name, bool *target) {
         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-        int r;
+        int r, b;
 
         assert(name);
 
-        r = sd_bus_get_property(bus,
-                                "org.freedesktop.timedate1",
-                                "/org/freedesktop/timedate1",
-                                "org.freedesktop.timedate1",
-                                name,
-                                &error,
-                                &reply,
-                                "b");
+        r = sd_bus_get_property_trivial(
+                        bus,
+                        "org.freedesktop.timedate1",
+                        "/org/freedesktop/timedate1",
+                        "org.freedesktop.timedate1",
+                        name,
+                        &error,
+                        'b', &b);
         if (r < 0) {
                 log_error("Failed to get property: %s %s", name, bus_error_message(&error, -r));
                 return r;
         }
 
-        r = sd_bus_message_read(reply, "b", target);
-        if (r < 0) {
-                log_error("Failed to parse reply.");
-                return r;
-        }
-
+        *target = b;
         return 0;
 }
 
@@ -226,25 +221,19 @@ static int get_timedate_property_usec(sd_bus *bus, const char *name, usec_t *tar
 
         assert(name);
 
-        r = sd_bus_get_property(bus,
-                                "org.freedesktop.timedate1",
-                                "/org/freedesktop/timedate1",
-                                "org.freedesktop.timedate1",
-                                name,
-                                &error,
-                                &reply,
-                                "t");
+        r = sd_bus_get_property_trivial(
+                        bus,
+                        "org.freedesktop.timedate1",
+                        "/org/freedesktop/timedate1",
+                        "org.freedesktop.timedate1",
+                        name,
+                        &error,
+                        't', target);
         if (r < 0) {
                 log_error("Failed to get property: %s %s", name, bus_error_message(&error, -r));
                 return r;
         }
 
-        r = sd_bus_message_read(reply, "t", target);
-        if (r < 0) {
-                log_error("Failed to parse reply.");
-                return r;
-        }
-
         return 0;
 }
 
@@ -256,14 +245,15 @@ static int show_status(sd_bus *bus, char **args, unsigned n) {
 
         assert(bus);
 
-        r = sd_bus_get_property(bus,
-                                "org.freedesktop.timedate1",
-                                "/org/freedesktop/timedate1",
-                                "org.freedesktop.timedate1",
-                                "Timezone",
-                                &error,
-                                &reply,
-                                "s");
+        r = sd_bus_get_property(
+                        bus,
+                        "org.freedesktop.timedate1",
+                        "/org/freedesktop/timedate1",
+                        "org.freedesktop.timedate1",
+                        "Timezone",
+                        &error,
+                        &reply,
+                        "s");
         if (r < 0) {
                 log_error("Failed to get property: Timezone %s", bus_error_message(&error, -r));
                 return r;
@@ -334,7 +324,6 @@ static int set_time(sd_bus *bus, char **args, unsigned n) {
 
 static int set_timezone(sd_bus *bus, char **args, unsigned n) {
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-        bool interactive = arg_ask_password;
         int r;
 
         assert(args);
@@ -349,7 +338,7 @@ static int set_timezone(sd_bus *bus, char **args, unsigned n) {
                                "SetTimezone",
                                &error,
                                NULL,
-                               "sb", args[1], interactive);
+                               "sb", args[1], arg_ask_password);
         if (r < 0)
                 log_error("Failed to set timezone: %s", bus_error_message(&error, -r));
 
@@ -358,23 +347,19 @@ static int set_timezone(sd_bus *bus, char **args, unsigned n) {
 
 static int set_local_rtc(sd_bus *bus, char **args, unsigned n) {
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-        bool interactive = arg_ask_password, b, q;
-        int r;
+        int r, b;
 
         assert(args);
         assert(n == 2);
 
         polkit_agent_open_if_enabled();
 
-        r = parse_boolean(args[1]);
-        if (r < 0) {
+        b = parse_boolean(args[1]);
+        if (b < 0) {
                 log_error("Failed to parse local RTC setting: %s", args[1]);
-                return r;
+                return b;
         }
 
-        b = r;
-        q = arg_adjust_system_clock;
-
         r = sd_bus_call_method(bus,
                                "org.freedesktop.timedate1",
                                "/org/freedesktop/timedate1",
@@ -382,7 +367,7 @@ static int set_local_rtc(sd_bus *bus, char **args, unsigned n) {
                                "SetLocalRTC",
                                &error,
                                NULL,
-                               "bbb", b, q, interactive);
+                               "bbb", b, arg_adjust_system_clock, arg_ask_password);
         if (r < 0)
                 log_error("Failed to set local RTC: %s", bus_error_message(&error, -r));
 
@@ -391,22 +376,19 @@ static int set_local_rtc(sd_bus *bus, char **args, unsigned n) {
 
 static int set_ntp(sd_bus *bus, char **args, unsigned n) {
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-        bool interactive = arg_ask_password, b;
-        int r;
+        int b, r;
 
         assert(args);
         assert(n == 2);
 
         polkit_agent_open_if_enabled();
 
-        r = parse_boolean(args[1]);
-        if (r < 0) {
+        b = parse_boolean(args[1]);
+        if (b < 0) {
                 log_error("Failed to parse NTP setting: %s", args[1]);
-                return r;
+                return b;
         }
 
-        b = r;
-
         r = sd_bus_call_method(bus,
                                "org.freedesktop.timedate1",
                                "/org/freedesktop/timedate1",
@@ -414,7 +396,7 @@ static int set_ntp(sd_bus *bus, char **args, unsigned n) {
                                "SetNTP",
                                &error,
                                NULL,
-                               "bb", b, interactive);
+                               "bb", b, arg_ask_password);
         if (r < 0)
                 log_error("Failed to set ntp: %s", bus_error_message(&error, -r));
 



More information about the systemd-commits mailing list