chiark / gitweb /
libsystemd-bus: catch up with latest kdbus changes
[elogind.git] / src / libsystemd-bus / bus-control.c
index 1d416b7dc4f05b7381fbb7f8f05c2bb3993f412f..b222175148222db02ec504d2e4d2a1ed91d576fd 100644 (file)
@@ -84,10 +84,20 @@ _public_ int sd_bus_request_name(sd_bus *bus, const char *name, int flags) {
 #endif
 
                 r = ioctl(bus->input_fd, KDBUS_CMD_NAME_ACQUIRE, n);
-                if (r < 0)
+                if (r < 0) {
+                        if (errno == -EALREADY)
+                                return SD_BUS_NAME_ALREADY_OWNER;
+
+                        if (errno == -EEXIST)
+                                return SD_BUS_NAME_EXISTS;
+
                         return -errno;
+                }
 
-                return n->flags;
+                if (n->flags & KDBUS_NAME_IN_QUEUE)
+                        return SD_BUS_NAME_IN_QUEUE;
+
+                return SD_BUS_NAME_PRIMARY_OWNER;
         } else {
                 r = sd_bus_call_method(
                                 bus,
@@ -180,43 +190,90 @@ _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;
 
-        r = sd_bus_call_method(
-                        bus,
-                        "org.freedesktop.DBus",
-                        "/",
-                        "org.freedesktop.DBus",
-                        "ListActivatableNames",
-                        NULL,
-                        &reply2,
-                        NULL);
-        if (r < 0)
-                return r;
+                /* assume 8k size first. If that doesn't suffice, kdbus will tell us
+                 * how big the buffer needs to be.  */
+                size = 8192;
 
-        r = bus_message_read_strv_extend(reply1, &x);
-        if (r < 0) {
-                strv_free(x);
-                return r;
-        }
+                for(;;) {
+                        names = realloc(names, size);
+                        if (!names)
+                                return -ENOMEM;
 
-        r = bus_message_read_strv_extend(reply2, &x);
-        if (r < 0) {
-                strv_free(x);
-                return r;
+                        names->size = size;
+                        names->flags = KDBUS_NAME_LIST_UNIQUE_NAMES;
+
+                        r = ioctl(sd_bus_get_fd(bus), KDBUS_CMD_NAME_LIST, names);
+                        if (r < 0) {
+                                if (errno == ENOBUFS && size != names->size) {
+                                        size = names->size;
+                                        continue;
+                                }
+
+                                return -errno;
+                        }
+
+                        break;
+                }
+
+                KDBUS_PART_FOREACH(name, names, names) {
+                        char *n;
+
+                        if (name->size > sizeof(*name))
+                                n = name->name;
+                        else
+                                asprintf(&n, ":1.%llu", (unsigned long long) name->id);
+
+                        r = strv_extend(&x, n);
+                        if (r < 0)
+                                return -ENOMEM;
+                }
+
+                *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;
 }
 
@@ -507,7 +564,7 @@ int bus_add_match_internal(
                         item->type = KDBUS_MATCH_BLOOM;
                         memcpy(item->data64, bloom, BLOOM_SIZE);
 
-                        item = KDBUS_ITEM_NEXT(item);
+                        item = KDBUS_PART_NEXT(item);
                 }
 
                 if (sender) {