[systemd-devel] [PATCH v2 1/2] analyze: split out bus util functions

Marc-Antoine Perennou Marc-Antoine at Perennou.com
Thu Nov 7 17:34:33 PST 2013


They will soon be used in systemctl
---
 src/analyze/analyze.c         | 68 +++----------------------------------------
 src/libsystemd-bus/bus-util.c | 49 +++++++++++++++++++++++++++++++
 src/libsystemd-bus/bus-util.h | 17 +++++++++++
 3 files changed, 70 insertions(+), 64 deletions(-)

diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c
index e149b15..19e6508 100644
--- a/src/analyze/analyze.c
+++ b/src/analyze/analyze.c
@@ -89,19 +89,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;
@@ -146,31 +133,6 @@ static int bus_get_uint64_property(sd_bus *bus, const char *path, const char *in
         return 0;
 }
 
-static int bus_get_unit_property_strv(sd_bus *bus, const char *path, const char *property, char ***strv) {
-        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-        int r;
-
-        assert(bus);
-        assert(path);
-        assert(property);
-        assert(strv);
-
-        r = sd_bus_get_property_strv(
-                        bus,
-                        "org.freedesktop.systemd1",
-                        path,
-                        "org.freedesktop.systemd1.Unit",
-                        property,
-                        &error,
-                        strv);
-        if (r < 0) {
-                log_error("Failed to get unit property %s: %s", property, bus_error_message(&error, -r));
-                return r;
-        }
-
-        return 0;
-}
-
 static int compare_unit_time(const void *a, const void *b) {
         return compare(((struct unit_times *)b)->time,
                        ((struct unit_times *)a)->time);
@@ -205,34 +167,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)
-                return bus_log_parse_error(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,
@@ -253,7 +193,7 @@ static int acquire_time_data(sd_bus *bus, struct unit_times **out) {
                 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)
@@ -1027,8 +967,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,
@@ -1050,7 +990,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 ae9733d..3959972 100644
--- a/src/libsystemd-bus/bus-util.c
+++ b/src/libsystemd-bus/bus-util.c
@@ -958,3 +958,52 @@ int bus_log_parse_error(int r) {
         log_error("Failed to parse message: %s", strerror(-r));
         return r;
 }
+
+int bus_get_unit_property_strv(sd_bus *bus, const char *path, const char *property, char ***strv) {
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+        int r;
+
+        assert(bus);
+        assert(path);
+        assert(property);
+        assert(strv);
+
+        r = sd_bus_get_property_strv(
+                        bus,
+                        "org.freedesktop.systemd1",
+                        path,
+                        "org.freedesktop.systemd1.Unit",
+                        property,
+                        &error,
+                        strv);
+        if (r < 0) {
+                log_error("Failed to get unit property %s: %s", property, bus_error_message(&error, -r));
+                return r;
+        }
+
+        return 0;
+}
+
+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 5f0a0b3..9cdf3fc 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 *path, const char *property, 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);
+
 int bus_log_parse_error(int r);
 
 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus*, sd_bus_unref);
-- 
1.8.4.2



More information about the systemd-devel mailing list