diff options
author | Lennart Poettering <lennart@poettering.net> | 2013-11-29 20:07:56 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2013-11-29 20:14:11 +0100 |
commit | 777d7a6123cbb192a8ff9e4ac5c05b1da84b4217 (patch) | |
tree | ab54eb2d02f28fad0a85d86a0a851a50fa9b23ec /src/libsystemd-bus | |
parent | a9ed78b0ca1caa8cdb8caecdb5c1fe776974c434 (diff) |
kdbus: synthesize NameOwnerChange signals from kernel messages and support matches against NameOwnerChange
Diffstat (limited to 'src/libsystemd-bus')
-rw-r--r-- | src/libsystemd-bus/bus-control.c | 219 | ||||
-rw-r--r-- | src/libsystemd-bus/bus-internal.h | 2 | ||||
-rw-r--r-- | src/libsystemd-bus/bus-kernel.c | 224 | ||||
-rw-r--r-- | src/libsystemd-bus/sd-bus.c | 2 |
4 files changed, 416 insertions, 31 deletions
diff --git a/src/libsystemd-bus/bus-control.c b/src/libsystemd-bus/bus-control.c index 08c0f49360..988d4265ff 100644 --- a/src/libsystemd-bus/bus-control.c +++ b/src/libsystemd-bus/bus-control.c @@ -416,6 +416,183 @@ _public_ int sd_bus_get_owner( return 0; } +static int add_name_change_match(sd_bus *bus, + uint64_t cookie, + const char *name, + const char *old_owner, + const char *new_owner) { + + uint64_t name_id = 0, old_owner_id = 0, new_owner_id = 0; + int is_name_id = -1, r; + struct kdbus_item *item; + + assert(bus); + + /* If we encounter a match that could match against + * NameOwnerChanged messages, then we need to create + * KDBUS_MATCH_NAME_{ADD,REMOVE,CHANGE} and + * KDBUS_MATCH_ID_{ADD,REMOVE} matches for it, possibly + * multiple if the match is underspecified. + * + * The NameOwnerChanged signals take three parameters with + * unique or well-known names, but only some forms actually + * exist: + * + * WELLKNOWN, "", UNIQUE → KDBUS_MATCH_NAME_ADD + * WELLKNOWN, UNIQUE, "" → KDBUS_MATCH_NAME_REMOVE + * WELLKNOWN, UNIQUE, UNIQUE → KDBUS_MATCH_NAME_CHANGE + * UNIQUE, "", UNIQUE → KDBUS_MATCH_ID_ADD + * UNIQUE, UNIQUE, "" → KDBUS_MATCH_ID_REMOVE + * + * For the latter two the two unique names must be identical. + * + * */ + + if (name) { + is_name_id = bus_kernel_parse_unique_name(name, &name_id); + if (is_name_id < 0) + return 0; + } + + if (old_owner) { + r = bus_kernel_parse_unique_name(old_owner, &old_owner_id); + if (r < 0) + return 0; + if (r == 0) + return 0; + if (is_name_id > 0 && old_owner_id != name_id) + return 0; + } + + if (new_owner) { + r = bus_kernel_parse_unique_name(new_owner, &new_owner_id); + if (r < 0) + return r; + if (r == 0) + return 0; + if (is_name_id > 0 && new_owner_id != name_id) + return 0; + } + + if (is_name_id <= 0) { + size_t sz, l; + + /* If the name argument is missing or is a well-known + * name, then add KDBUS_MATCH_NAME_{ADD,REMOVE,CHANGE} + * matches for it */ + + l = name ? strlen(name) : 0; + + sz = ALIGN8(offsetof(struct kdbus_cmd_match, items) + + offsetof(struct kdbus_item, name_change) + + offsetof(struct kdbus_notify_name_change, name) + + l+1); + + { + union { + uint8_t buffer[sz]; + struct kdbus_cmd_match match; + } m; + + memzero(&m, sz); + + m.match.size = sz; + m.match.cookie = cookie; + m.match.src_id = KDBUS_SRC_ID_KERNEL; + + item = m.match.items; + item->size = + offsetof(struct kdbus_item, name_change) + + offsetof(struct kdbus_notify_name_change, name) + + l+1; + + item->name_change.old_id = old_owner_id; + item->name_change.new_id = new_owner_id; + + if (name) + strcpy(item->name_change.name, name); + + /* If the old name is unset or empty, then + * this can match against added names */ + if (!old_owner || old_owner[0] == 0) { + item->type = KDBUS_MATCH_NAME_ADD; + + r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m); + if (r < 0) + return -errno; + } + + /* If the new name is unset or empty, then + * this can match against removed names */ + if (!new_owner || new_owner[0] == 0) { + item->type = KDBUS_MATCH_NAME_REMOVE; + + r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m); + if (r < 0) + return -errno; + } + + /* If the neither name is explicitly set to + * the empty string, then this can match + * agains changed names */ + if (!(old_owner && old_owner[0] == 0) && + !(new_owner && new_owner[0] == 0)) { + item->type = KDBUS_MATCH_NAME_CHANGE; + + r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m); + if (r < 0) + return -errno; + } + } + } + + if (is_name_id != 0) { + uint64_t sz = + ALIGN8(offsetof(struct kdbus_cmd_match, items) + + offsetof(struct kdbus_item, id_change)); + union { + uint8_t buffer[sz]; + struct kdbus_cmd_match match; + } m; + + /* If the name argument is missing or is a unique + * name, then add KDBUS_MATCH_ID_{ADD,REMOVE} matches + * for it */ + + memzero(&m, sz); + + m.match.size = sz; + m.match.cookie = cookie; + m.match.src_id = KDBUS_SRC_ID_KERNEL; + + item = m.match.items; + item->size = offsetof(struct kdbus_item, id_change) + sizeof(struct kdbus_notify_id_change); + item->id_change.id = name_id; + + /* If the old name is unset or empty, then this can + * match against added ids */ + if (!old_owner || old_owner[0] == 0) { + item->type = KDBUS_MATCH_ID_ADD; + + r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m); + if (r < 0) + return -errno; + } + + /* If thew new name is unset or empty, then this can + match against removed ids */ + if (!new_owner || new_owner[0] == 0) { + item->type = KDBUS_MATCH_ID_REMOVE; + + r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m); + if (r < 0) + return -errno; + } + } + + return 0; +} + int bus_add_match_internal( sd_bus *bus, const char *match, @@ -438,6 +615,8 @@ int bus_add_match_internal( uint64_t src_id = KDBUS_MATCH_SRC_ID_ANY; bool using_bloom = false; unsigned i; + bool matches_name_change = true; + const char *name_change_arg[3] = {}; zero(bloom); @@ -449,6 +628,9 @@ int bus_add_match_internal( switch (c->type) { case BUS_MATCH_SENDER: + if (!streq(c->value_str, "org.freedesktop.DBus")) + matches_name_change = false; + r = bus_kernel_parse_unique_name(c->value_str, &src_id); if (r < 0) return r; @@ -462,21 +644,33 @@ int bus_add_match_internal( break; case BUS_MATCH_MESSAGE_TYPE: + if (c->value_u8 != SD_BUS_MESSAGE_SIGNAL) + matches_name_change = false; + bloom_add_pair(bloom, "message-type", bus_message_type_to_string(c->value_u8)); using_bloom = true; break; case BUS_MATCH_INTERFACE: + if (!streq(c->value_str, "org.freedesktop.DBus")) + matches_name_change = false; + bloom_add_pair(bloom, "interface", c->value_str); using_bloom = true; break; case BUS_MATCH_MEMBER: + if (!streq(c->value_str, "NameOwnerChanged")) + matches_name_change = false; + bloom_add_pair(bloom, "member", c->value_str); using_bloom = true; break; case BUS_MATCH_PATH: + if (!streq(c->value_str, "/org/freedesktop/DBus")) + matches_name_change = false; + bloom_add_pair(bloom, "path", c->value_str); using_bloom = true; break; @@ -491,6 +685,9 @@ int bus_add_match_internal( case BUS_MATCH_ARG...BUS_MATCH_ARG_LAST: { char buf[sizeof("arg")-1 + 2 + 1]; + if (c->type - BUS_MATCH_ARG < 3) + name_change_arg[c->type - BUS_MATCH_ARG] = c->value_str; + snprintf(buf, sizeof(buf), "arg%u", c->type - BUS_MATCH_ARG); bloom_add_pair(bloom, buf, c->value_str); using_bloom = true; @@ -560,7 +757,20 @@ int bus_add_match_internal( if (r < 0) return -errno; - } else { + if (matches_name_change) { + + /* If this match could theoretically match + * NameOwnerChanged messages, we need to + * install a second non-bloom filter explitly + * for it */ + + r = add_name_change_match(bus, cookie, name_change_arg[0], name_change_arg[1], name_change_arg[2]); + if (r < 0) + return r; + } + + return 0; + } else return sd_bus_call_method( bus, "org.freedesktop.DBus", @@ -571,9 +781,6 @@ int bus_add_match_internal( NULL, "s", match); - } - - return 0; } int bus_remove_match_internal( @@ -597,6 +804,8 @@ int bus_remove_match_internal( if (r < 0) return -errno; + return 0; + } else { return sd_bus_call_method( bus, @@ -609,8 +818,6 @@ int bus_remove_match_internal( "s", match); } - - return 0; } _public_ int sd_bus_get_owner_machine_id(sd_bus *bus, const char *name, sd_id128_t *machine) { diff --git a/src/libsystemd-bus/bus-internal.h b/src/libsystemd-bus/bus-internal.h index 9c9e142a5b..7a7d8f5ffb 100644 --- a/src/libsystemd-bus/bus-internal.h +++ b/src/libsystemd-bus/bus-internal.h @@ -294,6 +294,8 @@ int bus_ensure_running(sd_bus *bus); int bus_start_running(sd_bus *bus); int bus_next_address(sd_bus *bus); +int bus_seal_message(sd_bus *b, sd_bus_message *m); + bool bus_pid_changed(sd_bus *bus); char *bus_address_escape(const char *v); diff --git a/src/libsystemd-bus/bus-kernel.c b/src/libsystemd-bus/bus-kernel.c index 6962fdcdb3..1b586d81d0 100644 --- a/src/libsystemd-bus/bus-kernel.c +++ b/src/libsystemd-bus/bus-kernel.c @@ -33,6 +33,9 @@ #include "bus-message.h" #include "bus-kernel.h" #include "bus-bloom.h" +#include "bus-util.h" + +#define UNIQUE_NAME_MAX (3+DECIMAL_STR_MAX(uint64_t)) int bus_kernel_parse_unique_name(const char *s, uint64_t *id) { int r; @@ -422,6 +425,147 @@ static void close_kdbus_msg(sd_bus *bus, struct kdbus_msg *k) { } } +static int return_name_owner_changed(sd_bus *bus, const char *name, const char *old_owner, const char *new_owner, sd_bus_message **ret) { + _cleanup_bus_message_unref_ sd_bus_message *m = NULL; + int r; + + assert(bus); + assert(ret); + + r = sd_bus_message_new_signal( + bus, + "/org/freedesktop/DBus", + "org.freedesktop.DBus", + "NameOwnerChanged", + &m); + if (r < 0) + return r; + + r = sd_bus_message_append(m, "sss", name, old_owner, new_owner); + if (r < 0) + return r; + + m->sender = "org.freedesktop.DBus"; + + r = bus_seal_message(bus, m); + if (r < 0) + return r; + + *ret = m; + m = NULL; + + return 1; +} + +static int translate_name_change(sd_bus *bus, struct kdbus_msg *k, struct kdbus_item *d, sd_bus_message **ret) { + char new_owner[UNIQUE_NAME_MAX], old_owner[UNIQUE_NAME_MAX]; + + assert(bus); + assert(k); + assert(d); + assert(ret); + + if (d->name_change.flags != 0) + return 0; + + if (d->type == KDBUS_ITEM_NAME_ADD) + old_owner[0] = 0; + else + sprintf(old_owner, ":1.%llu", (unsigned long long) d->name_change.old_id); + + if (d->type == KDBUS_ITEM_NAME_REMOVE) + new_owner[0] = 0; + else + sprintf(new_owner, ":1.%llu", (unsigned long long) d->name_change.new_id); + + return return_name_owner_changed(bus, d->name_change.name, old_owner, new_owner, ret); +} + +static int translate_id_change(sd_bus *bus, struct kdbus_msg *k, struct kdbus_item *d, sd_bus_message **ret) { + char owner[UNIQUE_NAME_MAX]; + + assert(bus); + assert(k); + assert(d); + assert(ret); + + sprintf(owner, ":1.%llu", d->id_change.id); + + return return_name_owner_changed( + bus, owner, + d->type == KDBUS_ITEM_ID_ADD ? NULL : owner, + d->type == KDBUS_ITEM_ID_ADD ? owner : NULL, + ret); +} + +static int translate_reply(sd_bus *bus, struct kdbus_msg *k, struct kdbus_item *d, sd_bus_message **ret) { + _cleanup_bus_message_unref_ sd_bus_message *m = NULL; + int r; + + assert(bus); + assert(k); + assert(d); + assert(ret); + + r = bus_message_new_synthetic_error( + bus, + k->cookie_reply, + d->type == KDBUS_ITEM_REPLY_TIMEOUT ? + &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out") : + &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call peer died"), + &m); + if (r < 0) + return r; + + m->sender = "org.freedesktop.DBus"; + + r = bus_seal_message(bus, m); + if (r < 0) + return r; + + *ret = m; + m = NULL; + + return 1; +} + +static int bus_kernel_translate_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_message **ret) { + struct kdbus_item *d, *found = NULL; + + static int (* const translate[])(sd_bus *bus, struct kdbus_msg *k, struct kdbus_item *d, sd_bus_message **ret) = { + [KDBUS_ITEM_NAME_ADD - _KDBUS_ITEM_KERNEL_BASE] = translate_name_change, + [KDBUS_ITEM_NAME_REMOVE - _KDBUS_ITEM_KERNEL_BASE] = translate_name_change, + [KDBUS_ITEM_NAME_CHANGE - _KDBUS_ITEM_KERNEL_BASE] = translate_name_change, + + [KDBUS_ITEM_ID_ADD - _KDBUS_ITEM_KERNEL_BASE] = translate_id_change, + [KDBUS_ITEM_ID_REMOVE - _KDBUS_ITEM_KERNEL_BASE] = translate_id_change, + + [KDBUS_ITEM_REPLY_TIMEOUT - _KDBUS_ITEM_KERNEL_BASE] = translate_reply, + [KDBUS_ITEM_REPLY_DEAD - _KDBUS_ITEM_KERNEL_BASE] = translate_reply, + }; + + assert(bus); + assert(k); + assert(ret); + assert(k->payload_type == KDBUS_PAYLOAD_KERNEL); + + KDBUS_PART_FOREACH(d, k, items) { + if (d->type >= _KDBUS_ITEM_KERNEL_BASE && d->type < _KDBUS_ITEM_KERNEL_BASE + ELEMENTSOF(translate)) { + if (found) + return -EBADMSG; + found = d; + } else + log_debug("Got unknown field from kernel %llu", d->type); + } + + if (!found) { + log_debug("Didn't find a kernel message to translate."); + return 0; + } + + return translate[found->type](bus, k, d, ret); +} + static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_message **ret) { sd_bus_message *m = NULL; struct kdbus_item *d; @@ -435,17 +579,16 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess assert(bus); assert(k); assert(ret); - - if (k->payload_type != KDBUS_PAYLOAD_DBUS1) - return 0; + assert(k->payload_type == KDBUS_PAYLOAD_DBUS1); KDBUS_PART_FOREACH(d, k, items) { size_t l; l = d->size - offsetof(struct kdbus_item, data); - if (d->type == KDBUS_ITEM_PAYLOAD_OFF) { + switch (d->type) { + case KDBUS_ITEM_PAYLOAD_OFF: if (!h) { h = (struct bus_header *)((uint8_t *)bus->kdbus_buffer + d->vec.offset); @@ -454,15 +597,16 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess } n_bytes += d->vec.size; + break; - } else if (d->type == KDBUS_ITEM_PAYLOAD_MEMFD) { - + case KDBUS_ITEM_PAYLOAD_MEMFD: if (!h) return -EBADMSG; n_bytes += d->memfd.size; + break; - } else if (d->type == KDBUS_ITEM_FDS) { + case KDBUS_ITEM_FDS: { int *f; unsigned j; @@ -474,9 +618,13 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess fds = f; memcpy(fds + n_fds, d->fds, sizeof(int) * j); n_fds += j; + break; + } - } else if (d->type == KDBUS_ITEM_SECLABEL) + case KDBUS_ITEM_SECLABEL: seclabel = d->str; + break; + } } if (!h) @@ -498,7 +646,9 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess l = d->size - offsetof(struct kdbus_item, data); - if (d->type == KDBUS_ITEM_PAYLOAD_OFF) { + switch (d->type) { + + case KDBUS_ITEM_PAYLOAD_OFF: { size_t begin_body; begin_body = BUS_MESSAGE_BODY_BEGIN(m); @@ -531,7 +681,10 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess } idx += d->vec.size; - } else if (d->type == KDBUS_ITEM_PAYLOAD_MEMFD) { + break; + } + + case KDBUS_ITEM_PAYLOAD_MEMFD: { struct bus_body_part *part; if (idx < BUS_MESSAGE_BODY_BEGIN(m)) { @@ -550,56 +703,73 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess part->sealed = true; idx += d->memfd.size; + break; + } - } else if (d->type == KDBUS_ITEM_CREDS) { + case KDBUS_ITEM_CREDS: m->creds.pid_starttime = d->creds.starttime / NSEC_PER_USEC; m->creds.uid = d->creds.uid; m->creds.gid = d->creds.gid; m->creds.pid = d->creds.pid; m->creds.tid = d->creds.tid; m->creds.mask |= (SD_BUS_CREDS_UID|SD_BUS_CREDS_GID|SD_BUS_CREDS_PID|SD_BUS_CREDS_PID_STARTTIME|SD_BUS_CREDS_TID) & bus->creds_mask; + break; - } else if (d->type == KDBUS_ITEM_TIMESTAMP) { + case KDBUS_ITEM_TIMESTAMP: m->realtime = d->timestamp.realtime_ns / NSEC_PER_USEC; m->monotonic = d->timestamp.monotonic_ns / NSEC_PER_USEC; + break; - } else if (d->type == KDBUS_ITEM_PID_COMM) { + case KDBUS_ITEM_PID_COMM: m->creds.comm = d->str; m->creds.mask |= SD_BUS_CREDS_COMM & bus->creds_mask; + break; - } else if (d->type == KDBUS_ITEM_TID_COMM) { + case KDBUS_ITEM_TID_COMM: m->creds.tid_comm = d->str; m->creds.mask |= SD_BUS_CREDS_TID_COMM & bus->creds_mask; + break; - } else if (d->type == KDBUS_ITEM_EXE) { + case KDBUS_ITEM_EXE: m->creds.exe = d->str; m->creds.mask |= SD_BUS_CREDS_EXE & bus->creds_mask; + break; - } else if (d->type == KDBUS_ITEM_CMDLINE) { + case KDBUS_ITEM_CMDLINE: m->creds.cmdline = d->str; m->creds.cmdline_length = l; m->creds.mask |= SD_BUS_CREDS_CMDLINE & bus->creds_mask; + break; - } else if (d->type == KDBUS_ITEM_CGROUP) { + case KDBUS_ITEM_CGROUP: m->creds.cgroup = d->str; m->creds.mask |= (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID) & bus->creds_mask; + break; - } else if (d->type == KDBUS_ITEM_AUDIT) { + case KDBUS_ITEM_AUDIT: m->creds.audit_session_id = d->audit.sessionid; m->creds.audit_login_uid = d->audit.loginuid; m->creds.mask |= (SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID) & bus->creds_mask; + break; - } else if (d->type == KDBUS_ITEM_CAPS) { + case KDBUS_ITEM_CAPS: m->creds.capability = d->data; m->creds.capability_size = l; m->creds.mask |= (SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS) & bus->creds_mask; + break; - } else if (d->type == KDBUS_ITEM_DST_NAME) + case KDBUS_ITEM_DST_NAME: destination = d->str; - else if (d->type != KDBUS_ITEM_FDS && - d->type != KDBUS_ITEM_SECLABEL && - d->type != KDBUS_ITEM_NAMES) + break; + + case KDBUS_ITEM_FDS: + case KDBUS_ITEM_SECLABEL: + case KDBUS_ITEM_NAMES: + break; + + default: log_debug("Got unknown field from kernel %llu", d->type); + } } r = bus_message_parse_fields(m); @@ -666,7 +836,13 @@ int bus_kernel_read_message(sd_bus *bus, sd_bus_message **m) { } k = (struct kdbus_msg *)((uint8_t *)bus->kdbus_buffer + off); - r = bus_kernel_make_message(bus, k, m); + if (k->payload_type == KDBUS_PAYLOAD_DBUS1) + r = bus_kernel_make_message(bus, k, m); + else if (k->payload_type == KDBUS_PAYLOAD_KERNEL) + r = bus_kernel_translate_message(bus, k, m); + else + r = 0; + if (r <= 0) close_kdbus_msg(bus, k); diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c index 2fe6340e36..01176b010e 100644 --- a/src/libsystemd-bus/sd-bus.c +++ b/src/libsystemd-bus/sd-bus.c @@ -1257,7 +1257,7 @@ _public_ int sd_bus_get_server_id(sd_bus *bus, sd_id128_t *server_id) { return 0; } -static int bus_seal_message(sd_bus *b, sd_bus_message *m) { +int bus_seal_message(sd_bus *b, sd_bus_message *m) { assert(m); if (m->header->version > b->message_version) |