chiark / gitweb /
sd-bus: sync kdbus.h (ABI break)
[elogind.git] / src / libsystemd / sd-bus / bus-kernel.c
index be64a12ea247097a42312d4689282d9bf29d2110..bdc770600bd4de57f62625d52ef8bde89e54aeb9 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"
@@ -309,7 +314,7 @@ static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) {
                 m->kdbus->dst_id = destination ? unique : KDBUS_DST_ID_BROADCAST;
 
         m->kdbus->payload_type = KDBUS_PAYLOAD_DBUS;
-        m->kdbus->cookie = m->header->dbus2.cookie;
+        m->kdbus->cookie = (uint64_t) m->header->serial;
         m->kdbus->priority = m->priority;
 
         if (m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
@@ -411,12 +416,9 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
         struct kdbus_item *d;
         unsigned n_fds = 0;
         _cleanup_free_ int *fds = NULL;
-        struct bus_header *header = NULL;
-        void *footer = NULL;
-        size_t header_size = 0, footer_size = 0;
-        size_t n_bytes = 0, idx = 0;
+        struct bus_header *h = NULL;
+        size_t total, n_bytes = 0, idx = 0;
         const char *destination = NULL, *seclabel = NULL;
-        bool last_was_memfd = false;
         int r;
 
         assert(bus);
@@ -431,24 +433,21 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
                 switch (d->type) {
 
                 case KDBUS_ITEM_PAYLOAD_OFF:
-                        if (!header) {
-                                header = (struct bus_header *)((uint8_t *) bus->kdbus_buffer + d->vec.offset);
-                                header_size = d->vec.size;
-                        }
+                        if (!h) {
+                                h = (struct bus_header *)((uint8_t *)k + d->vec.offset);
 
-                        footer = (uint8_t*) bus->kdbus_buffer + d->vec.offset;
-                        footer_size = d->vec.size;
+                                if (!bus_header_is_complete(h, d->vec.size))
+                                        return -EBADMSG;
+                        }
 
                         n_bytes += d->vec.size;
-                        last_was_memfd = false;
                         break;
 
                 case KDBUS_ITEM_PAYLOAD_MEMFD:
-                        if (!header) /* memfd cannot be first part */
+                        if (!h)
                                 return -EBADMSG;
 
                         n_bytes += d->memfd.size;
-                        last_was_memfd = true;
                         break;
 
                 case KDBUS_ITEM_FDS: {
@@ -472,29 +471,23 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
                 }
         }
 
-        if (last_was_memfd) /* memfd cannot be last part */
+        if (!h)
                 return -EBADMSG;
 
-        if (!header)
-                return -EBADMSG;
+        r = bus_header_message_size(h, &total);
+        if (r < 0)
+                return r;
 
-        if (header_size < sizeof(struct bus_header))
+        if (n_bytes != total)
                 return -EBADMSG;
 
         /* on kdbus we only speak native endian gvariant, never dbus1
          * marshalling or reverse endian */
-        if (header->version != 2 ||
-            header->endian != BUS_NATIVE_ENDIAN)
+        if (h->version != 2 ||
+            h->endian != BUS_NATIVE_ENDIAN)
                 return -EPROTOTYPE;
 
-        r = bus_message_from_header(
-                        bus,
-                        header, header_size,
-                        footer, footer_size,
-                        n_bytes,
-                        fds, n_fds,
-                        NULL,
-                        seclabel, 0, &m);
+        r = bus_message_from_header(bus, h, sizeof(struct bus_header), fds, n_fds, NULL, seclabel, 0, &m);
         if (r < 0)
                 return r;
 
@@ -533,11 +526,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);
                                 }
 
@@ -574,11 +567,10 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
                 case KDBUS_ITEM_PIDS:
 
                         /* The PID/TID might be missing, when the data
-                         * is faked by a bus proxy and it lacks that
-                         * information about the real client (since
-                         * SO_PEERCRED is used for that). Also kernel
-                         * namespacing might make some of this data
-                         * unavailable when untranslatable. */
+                         * is faked by some data bus proxy and it
+                         * lacks that information about the real
+                         * client since SO_PEERCRED is used for
+                         * that. */
 
                         if (d->pids.pid > 0) {
                                 m->creds.pid = (pid_t) d->pids.pid;
@@ -594,8 +586,7 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
 
                 case KDBUS_ITEM_CREDS:
 
-                        /* EUID/SUID/FSUID/EGID/SGID/FSGID might be
-                         * missing too (see above). */
+                        /* EUID/SUID/FSUID/EGID/SGID/FSGID might be missing too (see above). */
 
                         if ((uid_t) d->creds.uid != UID_INVALID) {
                                 m->creds.uid = (uid_t) d->creds.uid;
@@ -673,6 +664,7 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
                                 goto fail;
 
                         m->creds.cgroup_root = bus->cgroup_root;
+
                         break;
 
                 case KDBUS_ITEM_AUDIT:
@@ -775,7 +767,7 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
                 goto fail;
 
         /* Refuse messages if kdbus and dbus1 cookie doesn't match up */
-        if ((uint64_t) m->header->dbus2.cookie != k->cookie) {
+        if ((uint64_t) m->header->serial != k->cookie) {
                 r = -EBADMSG;
                 goto fail;
         }
@@ -1534,7 +1526,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 +1539,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 +1639,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 +1648,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 +1682,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 +1708,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 +1724,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;