chiark / gitweb /
sd-bus: sync kdbus.h (ABI break)
[elogind.git] / src / libsystemd / sd-bus / bus-kernel.c
index be64a12ea247097a42312d4689282d9bf29d2110..2294eac97eb6582427e2af18219039a5603f532a 100644 (file)
 
 #include <fcntl.h>
 #include <malloc.h>
-#include <libgen.h>
 #include <sys/mman.h>
 #include <sys/prctl.h>
 
+/* When we include libgen.h because we need dirname() we immediately
+ * undefine basename() since libgen.h defines it as a macro to the XDG
+ * version which is really broken. */
+#include <libgen.h>
+#undef basename
+
 #include "util.h"
 #include "strv.h"
 #include "memfd-util.h"
 #include "capability.h"
-#include "cgroup-util.h"
 #include "fileio.h"
 
 #include "bus-internal.h"
@@ -432,11 +436,11 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
 
                 case KDBUS_ITEM_PAYLOAD_OFF:
                         if (!header) {
-                                header = (struct bus_header *)((uint8_t *) bus->kdbus_buffer + d->vec.offset);
+                                header = (struct bus_header*)((uint8_t*) k + d->vec.offset);
                                 header_size = d->vec.size;
                         }
 
-                        footer = (uint8_t*) bus->kdbus_buffer + d->vec.offset;
+                        footer = (uint8_t*) k + d->vec.offset;
                         footer_size = d->vec.size;
 
                         n_bytes += d->vec.size;
@@ -533,11 +537,11 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
 
                                 if (idx >= begin_body) {
                                         if (!part->is_zero)
-                                                part->data = (uint8_t *)bus->kdbus_buffer + d->vec.offset;
+                                                part->data = (uint8_t* )k + d->vec.offset;
                                         part->size = d->vec.size;
                                 } else {
                                         if (!part->is_zero)
-                                                part->data = (uint8_t *)bus->kdbus_buffer + d->vec.offset + (begin_body - idx);
+                                                part->data = (uint8_t*) k + d->vec.offset + (begin_body - idx);
                                         part->size = d->vec.size - (begin_body - idx);
                                 }
 
@@ -744,10 +748,21 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
                 case KDBUS_ITEM_AUXGROUPS:
 
                         if (bus->creds_mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS) {
-                                assert_cc(sizeof(gid_t) == sizeof(uint32_t));
+                                size_t i, n;
+                                gid_t *g;
+
+                                n = (d->size - offsetof(struct kdbus_item, data64)) / sizeof(uint64_t);
+                                g = new(gid_t, n);
+                                if (!g) {
+                                        r = -ENOMEM;
+                                        goto fail;
+                                }
 
-                                m->creds.n_supplementary_gids = (d->size - offsetof(struct kdbus_item, data32)) / sizeof(uint32_t);
-                                m->creds.supplementary_gids = (gid_t*) d->data32;
+                                for (i = 0; i < n; i++)
+                                        g[i] = d->data64[i];
+
+                                m->creds.supplementary_gids = g;
+                                m->creds.n_supplementary_gids = n;
                                 m->creds.mask |= SD_BUS_CREDS_SUPPLEMENTARY_GIDS;
                         }
 
@@ -836,9 +851,8 @@ fail:
 
 int bus_kernel_take_fd(sd_bus *b) {
         struct kdbus_bloom_parameter *bloom = NULL;
+        struct kdbus_item *items, *item;
         struct kdbus_cmd_hello *hello;
-        struct kdbus_item_list *items;
-        struct kdbus_item *item;
         _cleanup_free_ char *g = NULL;
         const char *name;
         size_t l = 0, m = 0, sz;
@@ -964,7 +978,7 @@ int bus_kernel_take_fd(sd_bus *b) {
 
         /* extract bloom parameters from items */
         items = (void*)((uint8_t*)b->kdbus_buffer + hello->offset);
-        KDBUS_ITEM_FOREACH(item, items, items) {
+        KDBUS_FOREACH(item, items, hello->items_size) {
                 switch (item->type) {
                 case KDBUS_ITEM_BLOOM_PARAMETER:
                         bloom = &item->bloom_parameter;
@@ -1147,7 +1161,7 @@ int bus_kernel_write_message(sd_bus *bus, sd_bus_message *m, bool hint_sync_call
 
                                 /* Anybody can send us invalid messages, let's just drop them. */
                                 if (r == -EBADMSG || r == -EPROTOTYPE)
-                                        log_debug_errno(r, "Ignoring invalid message: %m");
+                                        log_debug_errno(r, "Ignoring invalid synchronous reply: %m");
                                 else
                                         return r;
                         }
@@ -1340,15 +1354,12 @@ int bus_kernel_read_message(sd_bus *bus, bool hint_priority, int64_t priority) {
         }
 
         r = ioctl(bus->input_fd, KDBUS_CMD_RECV, &recv);
+        if (recv.return_flags & KDBUS_RECV_RETURN_DROPPED_MSGS)
+                log_debug("%s: kdbus reports %" PRIu64 " dropped broadcast messages, ignoring.", strna(bus->description), (uint64_t) recv.dropped_msgs);
         if (r < 0) {
                 if (errno == EAGAIN)
                         return 0;
 
-                if (errno == EOVERFLOW) {
-                        log_debug("%s: kdbus reports %" PRIu64 " dropped broadcast messages, ignoring.", strna(bus->description), (uint64_t) recv.dropped_msgs);
-                        return 0;
-                }
-
                 return -errno;
         }
 
@@ -1534,7 +1545,7 @@ uint64_t attach_flags_to_kdbus(uint64_t mask) {
 }
 
 int bus_kernel_create_bus(const char *name, bool world, char **s) {
-        struct kdbus_cmd_make *make;
+        struct kdbus_cmd *make;
         struct kdbus_item *n;
         size_t l;
         int fd;
@@ -1547,14 +1558,14 @@ int bus_kernel_create_bus(const char *name, bool world, char **s) {
                 return -errno;
 
         l = strlen(name);
-        make = alloca0_align(offsetof(struct kdbus_cmd_make, items) +
+        make = alloca0_align(offsetof(struct kdbus_cmd, items) +
                              ALIGN8(offsetof(struct kdbus_item, bloom_parameter) + sizeof(struct kdbus_bloom_parameter)) +
                              ALIGN8(offsetof(struct kdbus_item, data64) + sizeof(uint64_t)) +
                              ALIGN8(offsetof(struct kdbus_item, data64) + sizeof(uint64_t)) +
                              ALIGN8(offsetof(struct kdbus_item, str) + DECIMAL_STR_MAX(uid_t) + 1 + l + 1),
                              8);
 
-        make->size = offsetof(struct kdbus_cmd_make, items);
+        make->size = offsetof(struct kdbus_cmd, items);
 
         /* Set the bloom parameters */
         n = make->items;
@@ -1647,7 +1658,7 @@ int bus_kernel_open_bus_fd(const char *bus, char **path) {
 
 int bus_kernel_create_endpoint(const char *bus_name, const char *ep_name, char **ep_path) {
         _cleanup_free_ char *path = NULL;
-        struct kdbus_cmd_make *make;
+        struct kdbus_cmd *make;
         struct kdbus_item *n;
         const char *name;
         int fd;
@@ -1656,10 +1667,10 @@ int bus_kernel_create_endpoint(const char *bus_name, const char *ep_name, char *
         if (fd < 0)
                 return fd;
 
-        make = alloca0_align(ALIGN8(offsetof(struct kdbus_cmd_make, items)) +
+        make = alloca0_align(ALIGN8(offsetof(struct kdbus_cmd, items)) +
                              ALIGN8(offsetof(struct kdbus_item, str) + DECIMAL_STR_MAX(uid_t) + 1 + strlen(ep_name) + 1),
                              8);
-        make->size = ALIGN8(offsetof(struct kdbus_cmd_make, items));
+        make->size = ALIGN8(offsetof(struct kdbus_cmd, items));
         make->flags = KDBUS_MAKE_ACCESS_WORLD;
 
         n = make->items;
@@ -1690,10 +1701,12 @@ int bus_kernel_create_endpoint(const char *bus_name, const char *ep_name, char *
 }
 
 int bus_kernel_try_close(sd_bus *bus) {
+        struct kdbus_cmd byebye = { .size = sizeof(byebye) };
+
         assert(bus);
         assert(bus->is_kernel);
 
-        if (ioctl(bus->input_fd, KDBUS_CMD_BYEBYE) < 0)
+        if (ioctl(bus->input_fd, KDBUS_CMD_BYEBYE, &byebye) < 0)
                 return -errno;
 
         return 0;
@@ -1714,13 +1727,13 @@ int bus_kernel_drop_one(int fd) {
 }
 
 int bus_kernel_realize_attach_flags(sd_bus *bus) {
-        struct kdbus_cmd_update *update;
+        struct kdbus_cmd *update;
         struct kdbus_item *n;
 
         assert(bus);
         assert(bus->is_kernel);
 
-        update = alloca0_align(offsetof(struct kdbus_cmd_update, items) +
+        update = alloca0_align(offsetof(struct kdbus_cmd, items) +
                                ALIGN8(offsetof(struct kdbus_item, data64) + sizeof(uint64_t)),
                                8);
 
@@ -1730,10 +1743,10 @@ int bus_kernel_realize_attach_flags(sd_bus *bus) {
         n->data64[0] = bus->attach_flags;
 
         update->size =
-                offsetof(struct kdbus_cmd_update, items) +
+                offsetof(struct kdbus_cmd, items) +
                 ALIGN8(n->size);
 
-        if (ioctl(bus->input_fd, KDBUS_CMD_CONN_UPDATE, update) < 0)
+        if (ioctl(bus->input_fd, KDBUS_CMD_UPDATE, update) < 0)
                 return -errno;
 
         return 0;