[systemd-devel] [PATCH 3/3] analyze: split out bus util functions

Marc-Antoine Perennou Marc-Antoine at Perennou.com
Tue Nov 5 04:42:27 PST 2013


They will soon be used in systemctl

Signed-off-by: Marc-Antoine Perennou <Marc-Antoine at Perennou.com>
---
 src/analyze/analyze.c         | 80 +++----------------------------------------
 src/libsystemd-bus/bus-util.c | 46 +++++++++++++++++++++++++
 src/libsystemd-bus/bus-util.h | 17 +++++++++
 3 files changed, 67 insertions(+), 76 deletions(-)

diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c
index e278a64..ecbba95 100644
--- a/src/analyze/analyze.c
+++ b/src/analyze/analyze.c
@@ -87,19 +87,6 @@ struct boot_times {
         usec_t unitsload_finish_time;
 };
 
-struct unit_info {
-        const char *id;
-        const char *description;
-        const char *load_state;
-        const char *active_state;
-        const char *sub_state;
-        const char *following;
-        const char *unit_path;
-        uint32_t job_id;
-        const char *job_type;
-        const char *job_path;
-};
-
 struct unit_times {
         char *name;
         usec_t activating;
@@ -172,71 +159,12 @@ static void free_unit_times(struct unit_times *t, unsigned n) {
         free(t);
 }
 
-static int bus_parse_unit_info(sd_bus_message *message, struct unit_info *u) {
-        int r = 0;
-
-        assert(message);
-        assert(u);
-
-        r = sd_bus_message_read(message, "(ssssssouso)", &u->id,
-                                                         &u->description,
-                                                         &u->load_state,
-                                                         &u->active_state,
-                                                         &u->sub_state,
-                                                         &u->following,
-                                                         &u->unit_path,
-                                                         &u->job_id,
-                                                         &u->job_type,
-                                                         &u->job_path);
-        if (r < 0) {
-                log_error("Failed to parse message as unit_info.");
-                return -EIO;
-        }
-
-        return r;
-}
-
-static int bus_get_unit_property_strv(sd_bus *bus, const char *unit_path, const char *prop, char ***strv) {
-        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
-        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-        int r;
-        const char *s;
-
-        r = sd_bus_get_property(
-                        bus,
-                        "org.freedesktop.systemd1",
-                        unit_path,
-                        "org.freedesktop.systemd1.Unit",
-                        prop,
-                        &error,
-                        &reply,
-                        "as");
-        if (r < 0) {
-                log_error("Failed to get unit property: %s %s", prop, bus_error_message(&error, -r));
-                return r;
-        }
-
-        r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s");
-        if (r < 0)
-            return r;
-
-        while ((r = sd_bus_message_read(reply, "s", &s)) > 0) {
-                r = strv_extend(strv, s);
-                if (r < 0) {
-                        log_oom();
-                        return r;
-                }
-        }
-
-        return r;
-}
-
 static int acquire_time_data(sd_bus *bus, struct unit_times **out) {
         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         int r, c = 0, n_units = 0;
         struct unit_times *unit_times = NULL;
-        struct unit_info u;
+        struct unit_info u = {};
 
         r = sd_bus_call_method(
                         bus,
@@ -256,7 +184,7 @@ static int acquire_time_data(sd_bus *bus, struct unit_times **out) {
         if (r < 0)
             goto fail;
 
-        while ((r = bus_parse_unit_info(reply, &u)) > 0) {
+        while ((r = bus_message_read_unit_info(reply, &u)) > 0) {
                 struct unit_times *t;
 
                 if (r < 0)
@@ -1041,8 +969,8 @@ static int graph_one(sd_bus *bus, const struct unit_info *u, char *patterns[]) {
 static int dot(sd_bus *bus, char* patterns[]) {
         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+        struct unit_info u = {};
         int r;
-        struct unit_info u;
 
         r = sd_bus_call_method(
                         bus,
@@ -1064,7 +992,7 @@ static int dot(sd_bus *bus, char* patterns[]) {
 
         printf("digraph systemd {\n");
 
-        while ((r = bus_parse_unit_info(reply, &u)) > 0) {
+        while ((r = bus_message_read_unit_info(reply, &u)) > 0) {
                 r = graph_one(bus, &u, patterns);
                 if (r < 0)
                         return r;
diff --git a/src/libsystemd-bus/bus-util.c b/src/libsystemd-bus/bus-util.c
index eec70ed..aa57677 100644
--- a/src/libsystemd-bus/bus-util.c
+++ b/src/libsystemd-bus/bus-util.c
@@ -952,3 +952,49 @@ int bus_property_get_uid(
 
         return sd_bus_message_append_basic(reply, 'u', userdata);
 }
+
+int bus_get_unit_property_strv(sd_bus *bus, const char *unit_path, const char *prop, char ***strv) {
+        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+        int r;
+
+        r = sd_bus_get_property(
+                        bus,
+                        "org.freedesktop.systemd1",
+                        unit_path,
+                        "org.freedesktop.systemd1.Unit",
+                        prop,
+                        &error,
+                        &reply,
+                        "as");
+        if (r < 0) {
+                log_error("Failed to get unit property: %s %s", prop, bus_error_message(&error, -r));
+                return r;
+        }
+
+        return sd_bus_message_read_strv(reply, strv);
+}
+
+int bus_message_read_unit_info(sd_bus_message *m, struct unit_info *u) {
+        int r;
+
+        assert(m);
+        assert(u);
+
+        r = sd_bus_message_read(m, "(ssssssouso)", &u->id,
+                                                   &u->description,
+                                                   &u->load_state,
+                                                   &u->active_state,
+                                                   &u->sub_state,
+                                                   &u->following,
+                                                   &u->unit_path,
+                                                   &u->job_id,
+                                                   &u->job_type,
+                                                   &u->job_path);
+        if (r < 0) {
+                log_error("Failed to parse message as unit_info.");
+                return -EIO;
+        }
+
+        return r;
+}
diff --git a/src/libsystemd-bus/bus-util.h b/src/libsystemd-bus/bus-util.h
index 101a2ec..d59e66c 100644
--- a/src/libsystemd-bus/bus-util.h
+++ b/src/libsystemd-bus/bus-util.h
@@ -75,6 +75,23 @@ int bus_property_get_uid(sd_bus *bus, const char *path, const char *interface, c
 #define bus_property_get_gid bus_property_get_uid
 #define bus_property_get_pid bus_property_get_uid
 
+int bus_get_unit_property_strv(sd_bus *bus, const char *unit_path, const char *prop, char ***strv);
+
+struct unit_info {
+        const char *id;
+        const char *description;
+        const char *load_state;
+        const char *active_state;
+        const char *sub_state;
+        const char *following;
+        const char *unit_path;
+        uint32_t job_id;
+        const char *job_type;
+        const char *job_path;
+};
+
+int bus_message_read_unit_info(sd_bus_message *m, struct unit_info *u);
+
 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus*, sd_bus_unref);
 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_message*, sd_bus_message_unref);
 
-- 
1.8.4.2



More information about the systemd-devel mailing list