chiark / gitweb /
bus: switch to multiple KDBUS_ITEM_NAME including the flags
[elogind.git] / src / libsystemd-bus / bus-kernel.c
index b63fe56c41a2a80fbdec17138ec2812846bf84ad..2c87f22fd7f5e7509d9d27f52055f4e946746e61 100644 (file)
@@ -28,6 +28,7 @@
 #include <sys/mman.h>
 
 #include "util.h"
+#include "strv.h"
 
 #include "bus-internal.h"
 #include "bus-message.h"
@@ -248,8 +249,8 @@ static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) {
         memset(m->kdbus, 0, sz);
 
         m->kdbus->flags =
-                ((m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) ? 0 : KDBUS_MSG_FLAGS_EXPECT_REPLY) |
-                ((m->header->flags & SD_BUS_MESSAGE_NO_AUTO_START) ? KDBUS_MSG_FLAGS_NO_AUTO_START : 0);
+                ((m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED) ? 0 : KDBUS_MSG_FLAGS_EXPECT_REPLY) |
+                ((m->header->flags & BUS_MESSAGE_NO_AUTO_START) ? KDBUS_MSG_FLAGS_NO_AUTO_START : 0);
         m->kdbus->dst_id =
                 well_known ? 0 :
                 m->destination ? unique : KDBUS_DST_ID_BROADCAST;
@@ -775,7 +776,7 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
 
                 case KDBUS_ITEM_CMDLINE:
                         m->creds.cmdline = d->str;
-                        m->creds.cmdline_length = l;
+                        m->creds.cmdline_size = l;
                         m->creds.mask |= SD_BUS_CREDS_CMDLINE & bus->creds_mask;
                         break;
 
@@ -800,9 +801,15 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
                         destination = d->str;
                         break;
 
+                case KDBUS_ITEM_NAME:
+                        r = strv_extend(&m->creds.well_known_names, d->name.name);
+                        if (r < 0)
+                                goto fail;
+                        m->creds.mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES & bus->creds_mask;
+                        break;
+
                 case KDBUS_ITEM_FDS:
                 case KDBUS_ITEM_SECLABEL:
-                case KDBUS_ITEM_NAMES:
                         break;
 
                 default:
@@ -818,13 +825,14 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
                 m->sender = "org.freedesktop.DBus";
         else {
                 snprintf(m->sender_buffer, sizeof(m->sender_buffer), ":1.%llu", (unsigned long long) k->src_id);
-                m->sender = m->sender_buffer;
+                m->sender = m->creds.unique_name = m->sender_buffer;
+                m->creds.mask |= SD_BUS_CREDS_UNIQUE_NAME & bus->creds_mask;
         }
 
         if (!m->destination) {
                 if (destination)
                         m->destination = destination;
-                else if (k->dst_id != KDBUS_DST_ID_WELL_KNOWN_NAME &&
+                else if (k->dst_id != KDBUS_DST_ID_NAME &&
                          k->dst_id != KDBUS_DST_ID_BROADCAST) {
                         snprintf(m->destination_buffer, sizeof(m->destination_buffer), ":1.%llu", (unsigned long long) k->dst_id);
                         m->destination = m->destination_buffer;
@@ -1029,11 +1037,14 @@ int kdbus_translate_attach_flags(uint64_t mask, uint64_t *kdbus_mask) {
         if (mask & (SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID))
                 m |= KDBUS_ATTACH_AUDIT;
 
+        if (mask & SD_BUS_CREDS_WELL_KNOWN_NAMES)
+                m |= KDBUS_ATTACH_NAMES;
+
         *kdbus_mask = m;
         return 0;
 }
 
-int bus_kernel_create(const char *name, char **s) {
+int bus_kernel_create_bus(const char *name, char **s) {
         struct kdbus_cmd_bus_make *make;
         struct kdbus_item *n;
         int fd;
@@ -1065,6 +1076,13 @@ int bus_kernel_create(const char *name, char **s) {
                 return -errno;
         }
 
+        /* The higher 32bit of the flags field are considered
+         * 'incompatible flags'. Refuse them all for now. */
+        if (make->flags > 0xFFFFFFFFULL) {
+                close_nointr_nofail(fd);
+                return -ENOTSUP;
+        }
+
         if (s) {
                 char *p;
 
@@ -1079,3 +1097,104 @@ int bus_kernel_create(const char *name, char **s) {
 
         return fd;
 }
+
+int bus_kernel_create_starter(const char *bus, const char *name) {
+        struct kdbus_cmd_hello *hello;
+        struct kdbus_item *n;
+        char *p;
+        int fd;
+
+        assert(bus);
+        assert(name);
+
+        p = alloca(sizeof("/dev/kdbus/") - 1 + DECIMAL_STR_MAX(uid_t) + 1 + strlen(bus) + sizeof("/bus"));
+        sprintf(p, "/dev/kdbus/%lu-%s/bus", (unsigned long) getuid(), bus);
+
+        fd = open(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
+        if (fd < 0)
+                return -errno;
+
+        hello = alloca0(ALIGN8(offsetof(struct kdbus_cmd_hello, items) +
+                               offsetof(struct kdbus_item, str) +
+                               strlen(name) + 1));
+
+        n = hello->items;
+        strcpy(n->str, name);
+        n->size = offsetof(struct kdbus_item, str) + strlen(n->str) + 1;
+        n->type = KDBUS_ITEM_STARTER_NAME;
+
+        hello->size = ALIGN8(offsetof(struct kdbus_cmd_hello, items) + n->size);
+        hello->conn_flags = KDBUS_HELLO_STARTER;
+        hello->pool_size = KDBUS_POOL_SIZE;
+
+        if (ioctl(fd, KDBUS_CMD_HELLO, hello) < 0) {
+                close_nointr_nofail(fd);
+                return -errno;
+        }
+
+        /* The higher 32bit of both flags fields are considered
+         * 'incompatible flags'. Refuse them all for now. */
+        if (hello->bus_flags > 0xFFFFFFFFULL ||
+            hello->conn_flags > 0xFFFFFFFFULL) {
+                close_nointr_nofail(fd);
+                return -ENOTSUP;
+        }
+
+        if (hello->bloom_size != BLOOM_SIZE) {
+                close_nointr_nofail(fd);
+                return -ENOTSUP;
+        }
+
+        return fd;
+}
+
+int bus_kernel_create_namespace(const char *name, char **s) {
+        struct kdbus_cmd_ns_make *make;
+        struct kdbus_item *n;
+        int fd;
+
+        assert(name);
+        assert(s);
+
+        fd = open("/dev/kdbus/control", O_RDWR|O_NOCTTY|O_CLOEXEC);
+        if (fd < 0)
+                return -errno;
+
+        make = alloca0(ALIGN8(offsetof(struct kdbus_cmd_ns_make, items) +
+                              offsetof(struct kdbus_item, str) +
+                              strlen(name) + 1));
+
+        n = make->items;
+        strcpy(n->str, name);
+        n->size = offsetof(struct kdbus_item, str) + strlen(n->str) + 1;
+        n->type = KDBUS_MAKE_NAME;
+
+        make->size = ALIGN8(offsetof(struct kdbus_cmd_ns_make, items) + n->size);
+        make->flags = KDBUS_MAKE_POLICY_OPEN | KDBUS_MAKE_ACCESS_WORLD;
+
+        if (ioctl(fd, KDBUS_CMD_NS_MAKE, make) < 0) {
+                close_nointr_nofail(fd);
+                return -errno;
+        }
+
+        /* The higher 32bit of the flags field are considered
+         * 'incompatible flags'. Refuse them all for now. */
+        if (make->flags > 0xFFFFFFFFULL) {
+                close_nointr_nofail(fd);
+                return -ENOTSUP;
+        }
+
+        if (s) {
+                char *p;
+
+                p = strappend("/dev/kdbus/ns/", name);
+                if (!p) {
+                        close_nointr_nofail(fd);
+                        return -ENOMEM;
+                }
+
+                *s = p;
+        }
+
+        return fd;
+}