chiark / gitweb /
bus: be nice to LLVM and don't embedd a VLA in a union
[elogind.git] / src / libsystemd-bus / bus-control.c
index a0eeb305da7d6d57c5f32ccbc73f1367f6014f1b..4f8c6236329510bf5da099e2408f1a015947bcd3 100644 (file)
@@ -174,36 +174,23 @@ _public_ int sd_bus_list_names(sd_bus *bus, char ***l) {
         assert_return(!bus_pid_changed(bus), -ECHILD);
 
         if (bus->is_kernel) {
-                _cleanup_free_ struct kdbus_cmd_names *names = NULL;
+                _cleanup_free_ struct kdbus_cmd_name_list *cmd = NULL;
+                struct kdbus_name_list *name_list;
                 struct kdbus_cmd_name *name;
-                size_t size;
 
-                /* assume 8k size first. If that doesn't suffice, kdbus will tell us
-                 * how big the buffer needs to be.  */
-                size = 8192;
-
-                for(;;) {
-                        names = realloc(names, size);
-                        if (!names)
-                                return -ENOMEM;
-
-                        names->size = size;
-                        names->flags = KDBUS_NAME_LIST_UNIQUE_NAMES;
+                cmd = malloc0(sizeof(struct kdbus_cmd_name_list *));
+                if (!cmd)
+                        return -ENOMEM;
 
-                        r = ioctl(sd_bus_get_fd(bus), KDBUS_CMD_NAME_LIST, names);
-                        if (r < 0) {
-                                if (errno == ENOBUFS && size != names->size) {
-                                        size = names->size;
-                                        continue;
-                                }
+                cmd->flags = KDBUS_NAME_LIST_UNIQUE_NAMES;
 
-                                return -errno;
-                        }
+                r = ioctl(sd_bus_get_fd(bus), KDBUS_CMD_NAME_LIST, cmd);
+                if (r < 0)
+                        return -errno;
 
-                        break;
-                }
+                name_list = (struct kdbus_name_list *) ((uint8_t *) bus->kdbus_buffer + cmd->offset);
 
-                KDBUS_PART_FOREACH(name, names, names) {
+                KDBUS_PART_FOREACH(name, name_list, names) {
                         char *n;
 
                         if (name->size > sizeof(*name))
@@ -216,6 +203,10 @@ _public_ int sd_bus_list_names(sd_bus *bus, char ***l) {
                                 return -ENOMEM;
                 }
 
+                r = ioctl(sd_bus_get_fd(bus), KDBUS_CMD_FREE, &cmd->offset);
+                if (r < 0)
+                        return -errno;
+
                 *l = x;
         } else {
                 r = sd_bus_call_method(
@@ -260,24 +251,21 @@ _public_ int sd_bus_list_names(sd_bus *bus, char ***l) {
         return 0;
 }
 
-static int sd_bus_get_owner_dbus(
+static int bus_get_owner_dbus(
                 sd_bus *bus,
                 const char *name,
                 uint64_t mask,
-                char **owner,
                 sd_bus_creds **creds) {
 
-        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+        _cleanup_bus_message_unref_ sd_bus_message *reply_unique = NULL, *reply = NULL;
         _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
-        _cleanup_free_ char *unique = NULL;
+        const char *unique = NULL;
         pid_t pid = 0;
         int r;
 
         /* Only query the owner if the caller wants to know it or if
          * the caller just wants to check whether a name exists */
-        if (owner || mask == 0) {
-                const char *found;
-
+        if ((mask & SD_BUS_CREDS_UNIQUE_NAME) || mask == 0) {
                 r = sd_bus_call_method(
                                 bus,
                                 "org.freedesktop.DBus",
@@ -285,21 +273,15 @@ static int sd_bus_get_owner_dbus(
                                 "org.freedesktop.DBus",
                                 "GetNameOwner",
                                 NULL,
-                                &reply,
+                                &reply_unique,
                                 "s",
                                 name);
                 if (r < 0)
                         return r;
 
-                r = sd_bus_message_read(reply, "s", &found);
+                r = sd_bus_message_read(reply_unique, "s", &unique);
                 if (r < 0)
                         return r;
-
-                unique = strdup(found);
-                if (!unique)
-                        return -ENOMEM;
-
-                reply = sd_bus_message_unref(reply);
         }
 
         if (mask != 0) {
@@ -307,8 +289,19 @@ static int sd_bus_get_owner_dbus(
                 if (!c)
                         return -ENOMEM;
 
-                if ((mask & SD_BUS_CREDS_PID) ||
-                    mask & ~(SD_BUS_CREDS_PID|SD_BUS_CREDS_UID|SD_BUS_CREDS_SELINUX_CONTEXT)) {
+                if ((mask & SD_BUS_CREDS_UNIQUE_NAME) && unique) {
+                        c->unique_name = strdup(unique);
+                        if (!c->unique_name)
+                                return -ENOMEM;
+
+                        c->mask |= SD_BUS_CREDS_UNIQUE_NAME;
+                }
+
+                if (mask & (SD_BUS_CREDS_PID|SD_BUS_CREDS_PID_STARTTIME|SD_BUS_CREDS_GID|
+                            SD_BUS_CREDS_COMM|SD_BUS_CREDS_EXE|SD_BUS_CREDS_CMDLINE|
+                            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|
+                            SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS|
+                            SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID)) {
                         uint32_t u;
 
                         r = sd_bus_call_method(
@@ -320,7 +313,7 @@ static int sd_bus_get_owner_dbus(
                                         NULL,
                                         &reply,
                                         "s",
-                                        name);
+                                        unique ? unique : name);
                         if (r < 0)
                                 return r;
 
@@ -349,7 +342,7 @@ static int sd_bus_get_owner_dbus(
                                         NULL,
                                         &reply,
                                         "s",
-                                        name);
+                                        unique ? unique : name);
                         if (r < 0)
                                 return r;
 
@@ -376,7 +369,7 @@ static int sd_bus_get_owner_dbus(
                                         NULL,
                                         &reply,
                                         "s",
-                                        name);
+                                        unique ? unique : name);
                         if (r < 0)
                                 return r;
 
@@ -401,256 +394,58 @@ static int sd_bus_get_owner_dbus(
                 c = NULL;
         }
 
-        if (owner) {
-                *owner = unique;
-                unique = NULL;
-        }
-
         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) +
-                               sizeof(struct kdbus_notify_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;
-}
-
-static int kdbus_name_query(
+static int bus_get_owner_kdbus(
                 sd_bus *bus,
                 const char *name,
                 uint64_t mask,
-                char **owner,
                 sd_bus_creds **creds) {
 
-        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
         _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
-        _cleanup_free_ char *unique = NULL;
-        struct kdbus_cmd_name_info *name_info = NULL;
+        struct kdbus_cmd_name_info *cmd;
+        struct kdbus_name_info *name_info;
         struct kdbus_item *item;
-        uint64_t attach_flags, m;
-        size_t slen, size;
+        size_t size;
+        uint64_t m, id;
         int r;
 
-        r = kdbus_translate_attach_flags(mask, &attach_flags);
+        r = bus_kernel_parse_unique_name(name, &id);
         if (r < 0)
                 return r;
-
-        slen = strlen(name) + 1;
-
-        /*
-         * The structure is used for both directions. Start with 8k buffer size and
-         * expand to the size kdbus reports in case we fail.
-         */
-        size = slen + 8192;
-
-        for(;;) {
-                name_info = realloc(name_info, size);
-                if (!name_info)
-                        return -ENOMEM;
-
-                memset(name_info, 0, size);
-
-                name_info->size = size;
-                name_info->attach_flags = attach_flags;
-
-                item = name_info->items;
-                item->type = KDBUS_NAME_INFO_ITEM_NAME;
-                item->size = KDBUS_ITEM_SIZE(slen);
-                strcpy(item->str, name);
-
-                r = ioctl(sd_bus_get_fd(bus), KDBUS_CMD_NAME_QUERY, name_info);
-                if (r < 0) {
-                        if (errno == ENOBUFS && size != name_info->size) {
-                                size = name_info->size;
-                                continue;
-                        }
-
-                        return -errno;
-                }
-
-                break;
+        if (r > 0) {
+                size = offsetof(struct kdbus_cmd_name_info, name);
+                cmd = alloca0(size);
+                cmd->id = id;
+        } else {
+                size = offsetof(struct kdbus_cmd_name_info, name) + strlen(name) + 1;
+                cmd = alloca0(size);
+                strcpy(cmd->name, name);
         }
 
-        asprintf(&unique, ":1.%llu", (unsigned long long) name_info->id);
+        cmd->size = size;
+        r = ioctl(bus->input_fd, KDBUS_CMD_NAME_INFO, cmd);
+        if (r < 0)
+                return -errno;
+
+        name_info = (struct kdbus_name_info *) ((uint8_t *) bus->kdbus_buffer + cmd->offset);
 
         c = bus_creds_new();
         if (!c)
                 return -ENOMEM;
 
+        if (mask & SD_BUS_CREDS_UNIQUE_NAME) {
+                if (asprintf(&c->unique_name, ":1.%llu", (unsigned long long) name_info->id) < 0)
+                        return -ENOMEM;
+
+                c->mask |= SD_BUS_CREDS_UNIQUE_NAME;
+        }
+
         KDBUS_PART_FOREACH(item, name_info, items) {
+
                 switch (item->type) {
+
                 case KDBUS_ITEM_CREDS:
                         m = (SD_BUS_CREDS_UID | SD_BUS_CREDS_GID | SD_BUS_CREDS_PID |
                              SD_BUS_CREDS_TID | SD_BUS_CREDS_PID_STARTTIME) & mask;
@@ -668,8 +463,10 @@ static int kdbus_name_query(
                 case KDBUS_ITEM_PID_COMM:
                         if (mask & SD_BUS_CREDS_COMM) {
                                 c->comm = strdup(item->str);
-                                if (!c->comm)
-                                        return -ENOMEM;
+                                if (!c->comm) {
+                                        r = -ENOMEM;
+                                        goto fail;
+                                }
 
                                 c->mask |= SD_BUS_CREDS_COMM;
                         }
@@ -678,8 +475,10 @@ static int kdbus_name_query(
                 case KDBUS_ITEM_TID_COMM:
                         if (mask & SD_BUS_CREDS_TID_COMM) {
                                 c->tid_comm = strdup(item->str);
-                                if (!c->tid_comm)
-                                        return -ENOMEM;
+                                if (!c->tid_comm) {
+                                        r = -ENOMEM;
+                                        goto fail;
+                                }
 
                                 c->mask |= SD_BUS_CREDS_TID_COMM;
                         }
@@ -688,8 +487,10 @@ static int kdbus_name_query(
                 case KDBUS_ITEM_EXE:
                         if (mask & SD_BUS_CREDS_EXE) {
                                 c->exe = strdup(item->str);
-                                if (!c->exe)
-                                        return -ENOMEM;
+                                if (!c->exe) {
+                                        r = -ENOMEM;
+                                        goto fail;
+                                }
 
                                 c->mask |= SD_BUS_CREDS_EXE;
                         }
@@ -697,10 +498,12 @@ static int kdbus_name_query(
 
                 case KDBUS_ITEM_CMDLINE:
                         if (mask & SD_BUS_CREDS_CMDLINE) {
-                                c->cmdline_length = item->size - KDBUS_PART_HEADER_SIZE;
-                                c->cmdline = memdup(item->data, c->cmdline_length);
-                                if (!c->cmdline)
-                                        return -ENOMEM;
+                                c->cmdline_size = item->size - KDBUS_PART_HEADER_SIZE;
+                                c->cmdline = memdup(item->data, c->cmdline_size);
+                                if (!c->cmdline) {
+                                        r = -ENOMEM;
+                                        goto fail;
+                                }
 
                                 c->mask |= SD_BUS_CREDS_CMDLINE;
                         }
@@ -713,8 +516,10 @@ static int kdbus_name_query(
 
                         if (m) {
                                 c->cgroup = strdup(item->str);
-                                if (!c->cgroup)
-                                        return -ENOMEM;
+                                if (!c->cgroup) {
+                                        r = -ENOMEM;
+                                        goto fail;
+                                }
 
                                 c->mask |= m;
                         }
@@ -727,8 +532,10 @@ static int kdbus_name_query(
                         if (m) {
                                 c->capability_size = item->size - KDBUS_PART_HEADER_SIZE;
                                 c->capability = memdup(item->data, c->capability_size);
-                                if (!c->capability)
-                                        return -ENOMEM;
+                                if (!c->capability) {
+                                        r = -ENOMEM;
+                                        goto fail;
+                                }
 
                                 c->mask |= m;
                         }
@@ -737,8 +544,10 @@ static int kdbus_name_query(
                 case KDBUS_ITEM_SECLABEL:
                         if (mask & SD_BUS_CREDS_SELINUX_CONTEXT) {
                                 c->label = strdup(item->str);
-                                if (!c->label)
-                                        return -ENOMEM;
+                                if (!c->label) {
+                                        r = -ENOMEM;
+                                        goto fail;
+                                }
 
                                 c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
                         }
@@ -753,6 +562,19 @@ static int kdbus_name_query(
                                 c->mask |= m;
                         }
                         break;
+
+                case KDBUS_ITEM_NAMES:
+                        if (mask & SD_BUS_CREDS_WELL_KNOWN_NAMES) {
+                                c->well_known_names_size = item->size - KDBUS_PART_HEADER_SIZE;
+                                c->well_known_names = memdup(item->data, c->well_known_names_size);
+                                if (!c->well_known_names) {
+                                        r = -ENOMEM;
+                                        goto fail;
+                                }
+
+                                c->mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES;
+                        }
+                        break;
                 }
         }
 
@@ -761,19 +583,17 @@ static int kdbus_name_query(
                 c = NULL;
         }
 
-        if (owner) {
-                *owner = unique;
-                unique = NULL;
-        }
+        r = 0;
 
-        return 0;
+fail:
+        ioctl(bus->input_fd, KDBUS_CMD_FREE, &cmd->offset);
+        return r;
 }
 
 _public_ int sd_bus_get_owner(
                 sd_bus *bus,
                 const char *name,
                 uint64_t mask,
-                char **owner,
                 sd_bus_creds **creds) {
 
         assert_return(bus, -EINVAL);
@@ -784,9 +604,177 @@ _public_ int sd_bus_get_owner(
         assert_return(!bus_pid_changed(bus), -ECHILD);
 
         if (bus->is_kernel)
-                return kdbus_name_query(bus, name, mask, owner, creds);
+                return bus_get_owner_kdbus(bus, name, mask, creds);
+        else
+                return bus_get_owner_dbus(bus, name, mask, creds);
+}
+
+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;
+        }
 
-        return sd_bus_get_owner_dbus(bus, name, mask, owner, creds);
+        if (is_name_id <= 0) {
+                struct kdbus_cmd_match *m;
+                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);
+
+                m = alloca0(sz);
+                m->size = sz;
+                m->cookie = cookie;
+                m->src_id = KDBUS_SRC_ID_KERNEL;
+
+                item = m->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) {
+                struct kdbus_cmd_match *m;
+                uint64_t sz;
+
+                /* If the name argument is missing or is a unique
+                 * name, then add KDBUS_MATCH_ID_{ADD,REMOVE} matches
+                 * for it */
+
+                sz = ALIGN8(offsetof(struct kdbus_cmd_match, items) +
+                            offsetof(struct kdbus_item, id_change) +
+                            sizeof(struct kdbus_notify_id_change));
+
+                m = alloca0(sz);
+                m->size = sz;
+                m->cookie = cookie;
+                m->src_id = KDBUS_SRC_ID_KERNEL;
+
+                item = m->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(