[systemd-devel] [PATCH 3/7] libsystemd-bus: add kdbus support for sd_bus_list_names()

Daniel Mack zonque at gmail.com
Fri Nov 15 10:32:19 PST 2013


kdbus will tell us the minimum buffer size it needs in case the default
8kb buffer doesn't suffice.
---
 src/libsystemd-bus/bus-control.c | 100 ++++++++++++++++++++++++++-------------
 1 file changed, 68 insertions(+), 32 deletions(-)

diff --git a/src/libsystemd-bus/bus-control.c b/src/libsystemd-bus/bus-control.c
index 5c9e746..562513b 100644
--- a/src/libsystemd-bus/bus-control.c
+++ b/src/libsystemd-bus/bus-control.c
@@ -180,43 +180,79 @@ _public_ int sd_bus_list_names(sd_bus *bus, char ***l) {
         if (bus_pid_changed(bus))
                 return -ECHILD;
 
-        r = sd_bus_call_method(
-                        bus,
-                        "org.freedesktop.DBus",
-                        "/",
-                        "org.freedesktop.DBus",
-                        "ListNames",
-                        NULL,
-                        &reply1,
-                        NULL);
-        if (r < 0)
-                return r;
+        if (bus->is_kernel) {
+                _cleanup_free_ struct kdbus_cmd_names *names = NULL;
+                struct kdbus_cmd_name *name;
+                size_t size;
+
+                /* assume 8k size first. If that doesn't suffice, kdbus will tell us
+                 * how big the buffer needs to be.  */
+                size = 8192;
+
+retry:
+                names = realloc(names, size);
+                if (!names)
+                        return log_oom();
+
+                names->size = size;
+
+                r = ioctl(sd_bus_get_fd(bus), KDBUS_CMD_NAME_LIST, names);
+                if (r < 0) {
+                        if (errno == ENOBUFS && size != names->size) {
+                                size = names->size;
+                                goto retry;
+                        }
 
-        r = sd_bus_call_method(
-                        bus,
-                        "org.freedesktop.DBus",
-                        "/",
-                        "org.freedesktop.DBus",
-                        "ListActivatableNames",
-                        NULL,
-                        &reply2,
-                        NULL);
-        if (r < 0)
-                return r;
+                        return -errno;
+                }
 
-        r = bus_message_read_strv_extend(reply1, &x);
-        if (r < 0) {
-                strv_free(x);
-                return r;
-        }
+                KDBUS_PART_FOREACH(name, names, names) {
+                        r = strv_extend(&x, name->name);
+                        if (r < 0)
+                                return log_oom();
+                }
 
-        r = bus_message_read_strv_extend(reply2, &x);
-        if (r < 0) {
-                strv_free(x);
-                return r;
+                *l = x;
+        } else {
+                r = sd_bus_call_method(
+                                bus,
+                                "org.freedesktop.DBus",
+                                "/",
+                                "org.freedesktop.DBus",
+                                "ListNames",
+                                NULL,
+                                &reply1,
+                                NULL);
+                if (r < 0)
+                        return r;
+
+                r = sd_bus_call_method(
+                                bus,
+                                "org.freedesktop.DBus",
+                                "/",
+                                "org.freedesktop.DBus",
+                                "ListActivatableNames",
+                                NULL,
+                                &reply2,
+                                NULL);
+                if (r < 0)
+                        return r;
+
+                r = bus_message_read_strv_extend(reply1, &x);
+                if (r < 0) {
+                        strv_free(x);
+                        return r;
+                }
+
+                r = bus_message_read_strv_extend(reply2, &x);
+                if (r < 0) {
+                        strv_free(x);
+                        return r;
+                }
+
+                *l = strv_uniq(x);
         }
 
-        *l = strv_uniq(x);
         return 0;
 }
 
-- 
1.8.4.2



More information about the systemd-devel mailing list