#include <fcntl.h>
#include <malloc.h>
+#include <libgen.h>
#include <sys/mman.h>
#include <sys/prctl.h>
return r;
}
+static void unset_memfds(struct sd_bus_message *m) {
+ struct bus_body_part *part;
+ unsigned i;
+
+ assert(m);
+
+ /* Make sure the memfds are not freed twice */
+ MESSAGE_FOREACH_PART(part, i, m)
+ if (part->memfd >= 0)
+ part->memfd = -1;
+}
+
static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
sd_bus_message *m = NULL;
struct kdbus_item *d;
return 1;
fail:
- if (m) {
- struct bus_body_part *part;
- unsigned i;
-
- /* Make sure the memfds are not freed twice */
- MESSAGE_FOREACH_PART(part, i, m)
- if (part->memfd >= 0)
- part->memfd = -1;
-
- sd_bus_message_unref(m);
- }
+ unset_memfds(m);
+ sd_bus_message_unref(m);
return r;
}
sz += ALIGN8(offsetof(struct kdbus_item, str) + l + 1);
}
- hello = alloca0(sz);
+ hello = alloca0_align(sz, 8);
hello->size = sz;
hello->conn_flags = b->hello_flags;
hello->attach_flags = b->attach_flags;
}
static void close_kdbus_msg(sd_bus *bus, struct kdbus_msg *k) {
- uint64_t off;
+ struct kdbus_cmd_free cmd;
struct kdbus_item *d;
assert(bus);
assert(k);
- off = (uint8_t *)k - (uint8_t *)bus->kdbus_buffer;
- ioctl(bus->input_fd, KDBUS_CMD_FREE, &off);
+ cmd.flags = 0;
+ cmd.offset = (uint8_t *)k - (uint8_t *)bus->kdbus_buffer;
+
+ ioctl(bus->input_fd, KDBUS_CMD_FREE, &cmd);
KDBUS_ITEM_FOREACH(d, k, items) {
assert(k);
assert(d);
- if (d->type == KDBUS_ITEM_NAME_ADD || (d->name_change.old.flags & (KDBUS_NAME_IN_QUEUE|KDBUS_NAME_ACTIVATOR)))
+ if (d->type == KDBUS_ITEM_NAME_ADD || (d->name_change.old_id.flags & (KDBUS_NAME_IN_QUEUE|KDBUS_NAME_ACTIVATOR)))
old_owner[0] = 0;
else
- sprintf(old_owner, ":1.%llu", (unsigned long long) d->name_change.old.id);
+ sprintf(old_owner, ":1.%llu", (unsigned long long) d->name_change.old_id.id);
- if (d->type == KDBUS_ITEM_NAME_REMOVE || (d->name_change.new.flags & (KDBUS_NAME_IN_QUEUE|KDBUS_NAME_ACTIVATOR))) {
+ if (d->type == KDBUS_ITEM_NAME_REMOVE || (d->name_change.new_id.flags & (KDBUS_NAME_IN_QUEUE|KDBUS_NAME_ACTIVATOR))) {
if (isempty(old_owner))
return 0;
new_owner[0] = 0;
} else
- sprintf(new_owner, ":1.%llu", (unsigned long long) d->name_change.new.id);
+ sprintf(new_owner, ":1.%llu", (unsigned long long) d->name_change.new_id.id);
return push_name_owner_changed(bus, d->name_change.name, old_owner, new_owner);
}
if (fd < 0)
return -errno;
- make = alloca0(ALIGN8(offsetof(struct kdbus_cmd_make, items) +
- offsetof(struct kdbus_item, data64) + sizeof(uint64_t) +
- offsetof(struct kdbus_item, str) +
- DECIMAL_STR_MAX(uid_t) + 1 + strlen(name) + 1));
+ make = alloca0_align(ALIGN8(offsetof(struct kdbus_cmd_make, items) +
+ offsetof(struct kdbus_item, data64) + sizeof(uint64_t) +
+ offsetof(struct kdbus_item, str) +
+ DECIMAL_STR_MAX(uid_t) + 1 + strlen(name) + 1),
+ 8);
make->size = offsetof(struct kdbus_cmd_make, items);
return fd;
}
+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_item *n;
+ size_t size;
+ int fd;
+
+ fd = bus_kernel_open_bus_fd(bus_name, &path);
+ if (fd < 0)
+ return fd;
+
+ size = ALIGN8(offsetof(struct kdbus_cmd_make, items));
+ size += ALIGN8(offsetof(struct kdbus_item, str) + strlen(ep_name) + 1);
+
+ make = alloca0_align(size, 8);
+ make->size = size;
+ make->flags = KDBUS_MAKE_ACCESS_WORLD;
+
+ n = make->items;
+
+ n->type = KDBUS_ITEM_MAKE_NAME;
+ n->size = offsetof(struct kdbus_item, str) + strlen(ep_name) + 1;
+ strcpy(n->str, ep_name);
+
+ if (ioctl(fd, KDBUS_CMD_EP_MAKE, make) < 0) {
+ safe_close(fd);
+ return -errno;
+ }
+
+ /* The higher 32bit of the flags field are considered
+ * 'incompatible flags'. Refuse them all for now. */
+ if (make->flags > 0xFFFFFFFFULL) {
+ safe_close(fd);
+ return -ENOTSUP;
+ }
+
+ if (ep_path) {
+ char *p;
+
+ p = strjoin(dirname(path), "/", ep_name, NULL);
+ if (!p) {
+ safe_close(fd);
+ return -ENOMEM;
+ }
+
+ *ep_path = p;
+ }
+
+ return fd;
+}
+
+int bus_kernel_set_endpoint_policy(int fd, uid_t uid, BusEndpoint *ep) {
+
+ struct kdbus_cmd_update *update;
+ struct kdbus_item *n;
+ BusEndpointPolicy *po;
+ Iterator i;
+ size_t size;
+ int r;
+
+ size = ALIGN8(offsetof(struct kdbus_cmd_update, items));
+
+ HASHMAP_FOREACH(po, ep->policy_hash, i) {
+ size += ALIGN8(offsetof(struct kdbus_item, str) + strlen(po->name) + 1);
+ size += ALIGN8(offsetof(struct kdbus_item, policy_access) + sizeof(struct kdbus_policy_access));
+ }
+
+ update = alloca0_align(size, 8);
+ update->size = size;
+
+ n = update->items;
+
+ HASHMAP_FOREACH(po, ep->policy_hash, i) {
+ n->type = KDBUS_ITEM_NAME;
+ n->size = offsetof(struct kdbus_item, str) + strlen(po->name) + 1;
+ strcpy(n->str, po->name);
+ n = KDBUS_ITEM_NEXT(n);
+
+ n->type = KDBUS_ITEM_POLICY_ACCESS;
+ n->size = offsetof(struct kdbus_item, policy_access) + sizeof(struct kdbus_policy_access);
+
+ n->policy_access.type = KDBUS_POLICY_ACCESS_USER;
+ n->policy_access.access = bus_kernel_translate_access(po->access);
+ n->policy_access.id = uid;
+
+ n = KDBUS_ITEM_NEXT(n);
+ }
+
+ r = ioctl(fd, KDBUS_CMD_EP_UPDATE, update);
+ if (r < 0)
+ return -errno;
+
+ return 0;
+}
+
int bus_kernel_make_starter(
int fd,
const char *name,
ALIGN8(offsetof(struct kdbus_item, str) + strlen(name) + 1) +
policy_cnt * ALIGN8(offsetof(struct kdbus_item, policy_access) + sizeof(struct kdbus_policy_access));
- hello = alloca0(size);
+ hello = alloca0_align(size, 8);
n = hello->items;
strcpy(n->str, name);
if (fd < 0)
return -errno;
- make = alloca0(ALIGN8(offsetof(struct kdbus_cmd_make, items) +
- offsetof(struct kdbus_item, str) +
- strlen(name) + 1));
+ make = alloca0_align(ALIGN8(offsetof(struct kdbus_cmd_make, items) +
+ offsetof(struct kdbus_item, str) +
+ strlen(name) + 1),
+ 8);
n = make->items;
strcpy(n->str, name);