bus-control.c revision 0aa72be6a02e8bbcb364526b8e1904a94b81a547
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/***
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2013 Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering***/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#ifdef HAVE_VALGRIND_MEMCHECK_H
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <valgrind/memcheck.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#endif
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering#include <stddef.h>
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering#include <errno.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "strv.h"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering#include "sd-bus.h"
96aad8d15a324d0e956a4e5653a11a67b209b41aLennart Poettering#include "bus-internal.h"
23c80348e656a4e6fd9ba8f17523a65b6fa349a0Kay Sievers#include "bus-message.h"
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering#include "bus-control.h"
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering#include "bus-bloom.h"
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering#include "bus-util.h"
25300b5a1fcf54674a69d0f4ab08925be00b0227Lennart Poettering#include "cgroup-util.h"
003dffde2c1b93afbc9aff24b277276f65424406Lennart Poettering
4cee5eede280b7fd48c18a1942616c4ac896a554Lennart Poettering_public_ int sd_bus_get_unique_name(sd_bus *bus, const char **unique) {
003dffde2c1b93afbc9aff24b277276f65424406Lennart Poettering int r;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
003dffde2c1b93afbc9aff24b277276f65424406Lennart Poettering assert_return(bus, -EINVAL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_return(unique, -EINVAL);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering assert_return(!bus_pid_changed(bus), -ECHILD);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering r = bus_ensure_running(bus);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (r < 0)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering return r;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering *unique = bus->unique_name;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering return 0;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering}
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic int bus_request_name_kernel(sd_bus *bus, const char *name, uint64_t flags) {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering struct kdbus_cmd_name *n;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering size_t size, l;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering int r;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering assert(bus);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering assert(name);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering l = strlen(name) + 1;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering size = offsetof(struct kdbus_cmd_name, items) + KDBUS_ITEM_SIZE(l);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering n = alloca0_align(size, 8);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering n->size = size;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering n->flags = request_name_flags_to_kdbus(flags);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering n->items[0].size = KDBUS_ITEM_HEADER_SIZE + l;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering n->items[0].type = KDBUS_ITEM_NAME;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering memcpy(n->items[0].str, name, l);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering#ifdef HAVE_VALGRIND_MEMCHECK_H
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering VALGRIND_MAKE_MEM_DEFINED(n, n->size);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering#endif
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering r = ioctl(bus->input_fd, KDBUS_CMD_NAME_ACQUIRE, n);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (r < 0)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering return -errno;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (n->flags & KDBUS_NAME_IN_QUEUE)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering return 0;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering return 1;
cb81cd8073392936882643af0129934bf67e96c4Lennart Poettering}
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic int bus_request_name_dbus1(sd_bus *bus, const char *name, uint64_t flags) {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering uint32_t ret, param = 0;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering int r;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering assert(bus);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering assert(name);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (flags & SD_BUS_NAME_ALLOW_REPLACEMENT)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering param |= BUS_NAME_ALLOW_REPLACEMENT;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (flags & SD_BUS_NAME_REPLACE_EXISTING)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering param |= BUS_NAME_REPLACE_EXISTING;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (!(flags & SD_BUS_NAME_QUEUE))
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering param |= BUS_NAME_DO_NOT_QUEUE;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering r = sd_bus_call_method(
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering bus,
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering "org.freedesktop.DBus",
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering "/org/freedesktop/DBus",
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering "org.freedesktop.DBus",
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering "RequestName",
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering NULL,
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering &reply,
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering "su",
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering name,
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering param);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (r < 0)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering return r;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering r = sd_bus_message_read(reply, "u", &ret);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (r < 0)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering return r;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (ret == BUS_NAME_ALREADY_OWNER)
cb81cd8073392936882643af0129934bf67e96c4Lennart Poettering return -EALREADY;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering else if (ret == BUS_NAME_EXISTS)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering return -EEXIST;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering else if (ret == BUS_NAME_IN_QUEUE)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering return 0;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering else if (ret == BUS_NAME_PRIMARY_OWNER)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering return 1;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering return -EIO;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering}
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering_public_ int sd_bus_request_name(sd_bus *bus, const char *name, uint64_t flags) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_return(bus, -EINVAL);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_return(name, -EINVAL);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_return(bus->bus_client, -EINVAL);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_return(!bus_pid_changed(bus), -ECHILD);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_return(!(flags & ~(SD_BUS_NAME_ALLOW_REPLACEMENT|SD_BUS_NAME_REPLACE_EXISTING|SD_BUS_NAME_QUEUE)), -EINVAL);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_return(service_name_is_valid(name), -EINVAL);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_return(name[0] != ':', -EINVAL);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (!BUS_IS_OPEN(bus->state))
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return -ENOTCONN;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering if (bus->is_kernel)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return bus_request_name_kernel(bus, name, flags);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering else
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return bus_request_name_dbus1(bus, name, flags);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering}
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic int bus_release_name_kernel(sd_bus *bus, const char *name) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering struct kdbus_cmd_name *n;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering size_t size, l;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering int r;
df2d202e6ed4001a21c6512c244acad5d4706c87Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert(bus);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert(name);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering l = strlen(name) + 1;
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering size = offsetof(struct kdbus_cmd_name, items) + KDBUS_ITEM_SIZE(l);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering n = alloca0_align(size, 8);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering n->size = size;
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering n->items[0].size = KDBUS_ITEM_HEADER_SIZE + l;
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering n->items[0].type = KDBUS_ITEM_NAME;
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering memcpy(n->items[0].str, name, l);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering#ifdef HAVE_VALGRIND_MEMCHECK_H
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering VALGRIND_MAKE_MEM_DEFINED(n, n->size);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering#endif
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering r = ioctl(bus->input_fd, KDBUS_CMD_NAME_RELEASE, n);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering if (r < 0)
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering return -errno;
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering return 0;
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering}
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poetteringstatic int bus_release_name_dbus1(sd_bus *bus, const char *name) {
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering uint32_t ret;
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering int r;
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering assert(bus);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering assert(name);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = sd_bus_call_method(
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering bus,
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering "org.freedesktop.DBus",
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "/org/freedesktop/DBus",
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "org.freedesktop.DBus",
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "ReleaseName",
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering NULL,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering &reply,
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering "s",
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering name);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (r < 0)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return r;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = sd_bus_message_read(reply, "u", &ret);
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering if (r < 0)
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering return r;
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering if (ret == BUS_NAME_NON_EXISTENT)
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering return -ESRCH;
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering if (ret == BUS_NAME_NOT_OWNER)
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering return -EADDRINUSE;
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering if (ret == BUS_NAME_RELEASED)
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering return 0;
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering return -EINVAL;
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering}
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering_public_ int sd_bus_release_name(sd_bus *bus, const char *name) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_return(bus, -EINVAL);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering assert_return(name, -EINVAL);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_return(bus->bus_client, -EINVAL);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek assert_return(!bus_pid_changed(bus), -ECHILD);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_return(service_name_is_valid(name), -EINVAL);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_return(name[0] != ':', -EINVAL);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering if (!BUS_IS_OPEN(bus->state))
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return -ENOTCONN;
df2d202e6ed4001a21c6512c244acad5d4706c87Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (bus->is_kernel)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return bus_release_name_kernel(bus, name);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering else
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return bus_release_name_dbus1(bus, name);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering}
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic int kernel_get_list(sd_bus *bus, uint64_t flags, char ***x) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering struct kdbus_cmd_name_list cmd = {};
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering struct kdbus_name_list *name_list;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering struct kdbus_name_info *name;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering uint64_t previous_id = 0;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering int r;
df2d202e6ed4001a21c6512c244acad5d4706c87Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering /* Caller will free half-constructed list on failure... */
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering cmd.flags = flags;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = ioctl(bus->input_fd, KDBUS_CMD_NAME_LIST, &cmd);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering if (r < 0)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return -errno;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering name_list = (struct kdbus_name_list *) ((uint8_t *) bus->kdbus_buffer + cmd.offset);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering KDBUS_ITEM_FOREACH(name, name_list, names) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering struct kdbus_item *item;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering const char *entry_name = NULL;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if ((flags & KDBUS_NAME_LIST_UNIQUE) && name->owner_id != previous_id) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering char *n;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (asprintf(&n, ":1.%llu", (unsigned long long) name->owner_id) < 0) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = -ENOMEM;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering goto fail;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = strv_consume(x, n);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (r < 0)
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering goto fail;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
9030ca462bd13cd6536299814e4a71d5c5e85be9Lennart Poettering previous_id = name->owner_id;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering KDBUS_ITEM_FOREACH(item, name, items)
8aec412ff697bc14995746953912ca6fdf2c9ba8Lennart Poettering if (item->type == KDBUS_ITEM_OWNED_NAME)
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering entry_name = item->name.name;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (entry_name && service_name_is_valid(entry_name)) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = strv_extend(x, entry_name);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (r < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = -ENOMEM;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering goto fail;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering r = 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringfail:
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering bus_kernel_cmd_free(bus, cmd.offset);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering return r;
7f0d207d2c816e0a8cb2742b0a789911f7c99356Lennart Poettering}
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int bus_list_names_kernel(sd_bus *bus, char ***acquired, char ***activatable) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering _cleanup_strv_free_ char **x = NULL, **y = NULL;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering int r;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (acquired) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = kernel_get_list(bus, KDBUS_NAME_LIST_UNIQUE | KDBUS_NAME_LIST_NAMES, &x);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (activatable) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = kernel_get_list(bus, KDBUS_NAME_LIST_ACTIVATORS, &y);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (r < 0)
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering *activatable = y;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering y = NULL;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering }
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering if (acquired) {
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering *acquired = x;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering x = NULL;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering }
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering return 0;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering}
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poetteringstatic int bus_list_names_dbus1(sd_bus *bus, char ***acquired, char ***activatable) {
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering _cleanup_strv_free_ char **x = NULL, **y = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (acquired) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_call_method(
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering bus,
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering "org.freedesktop.DBus",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "/org/freedesktop/DBus",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "org.freedesktop.DBus",
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "ListNames",
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering NULL,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering &reply,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering NULL);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering r = sd_bus_message_read_strv(reply, &x);
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering if (r < 0)
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering return r;
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering reply = sd_bus_message_unref(reply);
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
554604b3073467af75dc94fac9e2343148603289Lennart Poettering if (activatable) {
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering r = sd_bus_call_method(
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering bus,
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering "org.freedesktop.DBus",
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "/org/freedesktop/DBus",
554604b3073467af75dc94fac9e2343148603289Lennart Poettering "org.freedesktop.DBus",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "ListActivatableNames",
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering NULL,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering &reply,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_message_read_strv(reply, &y);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *activatable = y;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering y = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (acquired) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *acquired = x;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering x = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering_public_ int sd_bus_list_names(sd_bus *bus, char ***acquired, char ***activatable) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_return(bus, -EINVAL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_return(acquired || activatable, -EINVAL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_return(!bus_pid_changed(bus), -ECHILD);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering if (!BUS_IS_OPEN(bus->state))
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering return -ENOTCONN;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering if (bus->is_kernel)
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering return bus_list_names_kernel(bus, acquired, activatable);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering else
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering return bus_list_names_dbus1(bus, acquired, activatable);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering}
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poetteringstatic int bus_populate_creds_from_items(
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering sd_bus *bus,
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering struct kdbus_info *info,
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering uint64_t mask,
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering sd_bus_creds *c) {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering struct kdbus_item *item;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering uint64_t m;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering int r;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering assert(bus);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering assert(info);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering assert(c);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering KDBUS_ITEM_FOREACH(item, info, items) {
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering switch (item->type) {
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering case KDBUS_ITEM_PIDS:
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering if (mask & SD_BUS_CREDS_PID && item->pids.pid > 0) {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering c->pid = (pid_t) item->pids.pid;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering c->mask |= SD_BUS_CREDS_PID;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering }
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering if (mask & SD_BUS_CREDS_TID && item->pids.tid > 0) {
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering c->tid = (pid_t) item->pids.tid;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering c->mask |= SD_BUS_CREDS_TID;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (mask & SD_BUS_CREDS_PID_STARTTIME && item->pids.starttime > 0) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering c->pid_starttime = item->pids.starttime;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering c->mask |= SD_BUS_CREDS_PID_STARTTIME;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
a3e7f417d72ba3251fd6b3a228a2721a4b725a03Zbigniew Jędrzejewski-Szmek
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering break;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case KDBUS_ITEM_CREDS:
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering if (mask & SD_BUS_CREDS_UID && (uid_t) item->creds.uid != UID_INVALID) {
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering c->uid = (uid_t) item->creds.uid;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering c->mask |= SD_BUS_CREDS_UID;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering }
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering if (mask & SD_BUS_CREDS_EUID && (uid_t) item->creds.euid != UID_INVALID) {
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering c->euid = (uid_t) item->creds.euid;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering c->mask |= SD_BUS_CREDS_EUID;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering }
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering if (mask & SD_BUS_CREDS_SUID && (uid_t) item->creds.suid != UID_INVALID) {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering c->suid = (uid_t) item->creds.suid;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering c->mask |= SD_BUS_CREDS_SUID;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering }
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering if (mask & SD_BUS_CREDS_FSUID && (uid_t) item->creds.fsuid != UID_INVALID) {
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering c->fsuid = (uid_t) item->creds.fsuid;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering c->mask |= SD_BUS_CREDS_FSUID;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering }
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering if (mask & SD_BUS_CREDS_GID && (gid_t) item->creds.gid != GID_INVALID) {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering c->gid = (gid_t) item->creds.gid;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering c->mask |= SD_BUS_CREDS_GID;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering }
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering if (mask & SD_BUS_CREDS_EGID && (gid_t) item->creds.egid != GID_INVALID) {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering c->egid = (gid_t) item->creds.egid;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering c->mask |= SD_BUS_CREDS_EGID;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering }
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering if (mask & SD_BUS_CREDS_SGID && (gid_t) item->creds.sgid != GID_INVALID) {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering c->sgid = (gid_t) item->creds.sgid;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering c->mask |= SD_BUS_CREDS_SGID;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering }
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering if (mask & SD_BUS_CREDS_FSGID && (gid_t) item->creds.fsgid != GID_INVALID) {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering c->fsgid = (gid_t) item->creds.fsgid;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering c->mask |= SD_BUS_CREDS_FSGID;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering }
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering break;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case KDBUS_ITEM_PID_COMM:
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering if (mask & SD_BUS_CREDS_COMM) {
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering r = free_and_strdup(&c->comm, item->str);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering if (r < 0)
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering return r;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering c->mask |= SD_BUS_CREDS_COMM;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering }
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering break;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case KDBUS_ITEM_TID_COMM:
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (mask & SD_BUS_CREDS_TID_COMM) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = free_and_strdup(&c->tid_comm, item->str);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering c->mask |= SD_BUS_CREDS_TID_COMM;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering break;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering case KDBUS_ITEM_EXE:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (mask & SD_BUS_CREDS_EXE) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = free_and_strdup(&c->exe, item->str);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (r < 0)
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering c->mask |= SD_BUS_CREDS_EXE;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering break;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering case KDBUS_ITEM_CMDLINE:
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (mask & SD_BUS_CREDS_CMDLINE) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering c->cmdline_size = item->size - offsetof(struct kdbus_item, data);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering c->cmdline = memdup(item->data, c->cmdline_size);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!c->cmdline)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return -ENOMEM;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering c->mask |= SD_BUS_CREDS_CMDLINE;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering break;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case KDBUS_ITEM_CGROUP:
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering m = (SD_BUS_CREDS_CGROUP | SD_BUS_CREDS_UNIT |
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering SD_BUS_CREDS_USER_UNIT | SD_BUS_CREDS_SLICE |
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering SD_BUS_CREDS_SESSION | SD_BUS_CREDS_OWNER_UID) & mask;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering if (m) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = free_and_strdup(&c->cgroup, item->str);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (r < 0)
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering return r;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = bus_get_root_path(bus);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (r < 0)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return r;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = free_and_strdup(&c->cgroup_root, bus->cgroup_root);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (r < 0)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return r;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering c->mask |= m;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering break;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering case KDBUS_ITEM_CAPS:
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering m = (SD_BUS_CREDS_EFFECTIVE_CAPS | SD_BUS_CREDS_PERMITTED_CAPS |
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering SD_BUS_CREDS_INHERITABLE_CAPS | SD_BUS_CREDS_BOUNDING_CAPS) & mask;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (m) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering c->capability_size = item->size - offsetof(struct kdbus_item, caps.caps);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering c->capability = memdup(item->caps.caps, c->capability_size);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (!c->capability)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return -ENOMEM;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering c->mask |= m;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering }
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering break;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering case KDBUS_ITEM_SECLABEL:
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (mask & SD_BUS_CREDS_SELINUX_CONTEXT) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering r = free_and_strdup(&c->label, item->str);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (r < 0)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return r;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering }
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering break;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering case KDBUS_ITEM_AUDIT:
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (mask & SD_BUS_CREDS_AUDIT_SESSION_ID && (uint32_t) item->audit.sessionid != (uint32_t) -1) {
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering c->audit_session_id = (uint32_t) item->audit.sessionid;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering c->mask |= SD_BUS_CREDS_AUDIT_SESSION_ID;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (mask & SD_BUS_CREDS_AUDIT_LOGIN_UID && (uid_t) item->audit.loginuid != UID_INVALID) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering c->audit_login_uid = (uid_t) item->audit.loginuid;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering c->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering break;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering case KDBUS_ITEM_OWNED_NAME:
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if ((mask & SD_BUS_CREDS_WELL_KNOWN_NAMES) && service_name_is_valid(item->name.name)) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = strv_extend(&c->well_known_names, item->name.name);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (r < 0)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return r;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering c->mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering break;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering case KDBUS_ITEM_CONN_DESCRIPTION:
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (mask & SD_BUS_CREDS_DESCRIPTION) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = free_and_strdup(&c->description, item->str);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering if (r < 0)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return r;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering c->mask |= SD_BUS_CREDS_DESCRIPTION;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering break;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering case KDBUS_ITEM_AUXGROUPS:
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering size_t n;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering uid_t *g;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert_cc(sizeof(gid_t) == sizeof(uint32_t));
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering n = (item->size - offsetof(struct kdbus_item, data32)) / sizeof(uint32_t);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering g = newdup(gid_t, item->data32, n);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (!g)
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering return -ENOMEM;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering free(c->supplementary_gids);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering c->supplementary_gids = g;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering c->n_supplementary_gids = n;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering c->mask |= SD_BUS_CREDS_SUPPLEMENTARY_GIDS;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering break;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
9030ca462bd13cd6536299814e4a71d5c5e85be9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return 0;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering}
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering
40205d706e1210763ff4c98a317556375bd04bcdLennart Poetteringint bus_get_name_creds_kdbus(
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering sd_bus *bus,
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering const char *name,
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering uint64_t mask,
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering bool allow_activator,
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering sd_bus_creds **creds) {
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering struct kdbus_cmd_info *cmd;
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering struct kdbus_info *conn_info;
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering size_t size, l;
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering uint64_t id;
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering int r;
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering r = bus_kernel_parse_unique_name(name, &id);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering if (r < 0)
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering return r;
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering if (r > 0) {
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering size = offsetof(struct kdbus_cmd_info, items);
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering cmd = alloca0_align(size, 8);
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering cmd->id = id;
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering } else {
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering l = strlen(name) + 1;
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering size = offsetof(struct kdbus_cmd_info, items) + KDBUS_ITEM_SIZE(l);
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering cmd = alloca0_align(size, 8);
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering cmd->items[0].size = KDBUS_ITEM_HEADER_SIZE + l;
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering cmd->items[0].type = KDBUS_ITEM_NAME;
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering memcpy(cmd->items[0].str, name, l);
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering }
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering cmd->size = size;
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering cmd->flags = attach_flags_to_kdbus(mask);
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering /* If augmentation is on, and the bus doesn't didn't allow us
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering * to get the bits we want, then ask for the PID/TID so that we
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering * can read the rest from /proc. */
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering if ((mask & SD_BUS_CREDS_AUGMENT) &&
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering (mask & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID|
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID|
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering SD_BUS_CREDS_COMM|SD_BUS_CREDS_TID_COMM|SD_BUS_CREDS_EXE|SD_BUS_CREDS_CMDLINE|
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering 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|
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS|
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering SD_BUS_CREDS_SELINUX_CONTEXT|
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID)))
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering cmd->flags |= KDBUS_ATTACH_PIDS;
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering r = ioctl(bus->input_fd, KDBUS_CMD_CONN_INFO, cmd);
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering if (r < 0)
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering return -errno;
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering conn_info = (struct kdbus_info *) ((uint8_t *) bus->kdbus_buffer + cmd->offset);
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering /* Non-activated names are considered not available */
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering if (!allow_activator && (conn_info->flags & KDBUS_HELLO_ACTIVATOR)) {
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering if (name[0] == ':')
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering r = -ENXIO;
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering else
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering r = -ESRCH;
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering goto fail;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering }
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering c = bus_creds_new();
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering if (!c) {
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering r = -ENOMEM;
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering goto fail;
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering }
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering if (mask & SD_BUS_CREDS_UNIQUE_NAME) {
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering if (asprintf(&c->unique_name, ":1.%llu", (unsigned long long) conn_info->id) < 0) {
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering r = -ENOMEM;
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering goto fail;
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering }
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering c->mask |= SD_BUS_CREDS_UNIQUE_NAME;
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering }
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering /* If KDBUS_ITEM_OWNED_NAME is requested then we'll get 0 of
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering them in case the service has no names. This does not mean
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering however that the list of owned names could not be
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering acquired. Hence, let's explicitly clarify that the data is
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering complete. */
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering c->mask |= mask & SD_BUS_CREDS_WELL_KNOWN_NAMES;
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering r = bus_populate_creds_from_items(bus, conn_info, mask, c);
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering if (r < 0)
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering goto fail;
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering r = bus_creds_add_more(c, mask, 0, 0);
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering if (r < 0)
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering goto fail;
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering if (creds) {
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering *creds = c;
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering c = NULL;
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering }
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering r = 0;
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poetteringfail:
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering bus_kernel_cmd_free(bus, cmd->offset);
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering return r;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering}
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poetteringstatic int bus_get_name_creds_dbus1(
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering sd_bus *bus,
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering const char *name,
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering uint64_t mask,
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering sd_bus_creds **creds) {
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply_unique = NULL, *reply = NULL;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering const char *unique = NULL;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering pid_t pid = 0;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering int r;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering /* Only query the owner if the caller wants to know it or if
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering * the caller just wants to check whether a name exists */
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering if ((mask & SD_BUS_CREDS_UNIQUE_NAME) || mask == 0) {
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering r = sd_bus_call_method(
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering bus,
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering "org.freedesktop.DBus",
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering "/org/freedesktop/DBus",
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering "org.freedesktop.DBus",
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering "GetNameOwner",
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering NULL,
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering &reply_unique,
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "s",
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering name);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (r < 0)
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return r;
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering r = sd_bus_message_read(reply_unique, "s", &unique);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (r < 0)
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return r;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering }
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (mask != 0) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering c = bus_creds_new();
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (!c)
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return -ENOMEM;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if ((mask & SD_BUS_CREDS_UNIQUE_NAME) && unique) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering c->unique_name = strdup(unique);
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering if (!c->unique_name)
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering return -ENOMEM;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering c->mask |= SD_BUS_CREDS_UNIQUE_NAME;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering }
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering if ((mask & SD_BUS_CREDS_PID) ||
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering ((mask & SD_BUS_CREDS_AUGMENT) &&
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering (mask & (SD_BUS_CREDS_PID_STARTTIME|
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID|
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID|
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering SD_BUS_CREDS_COMM|SD_BUS_CREDS_EXE|SD_BUS_CREDS_CMDLINE|
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering 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|
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS|
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering SD_BUS_CREDS_SELINUX_CONTEXT|
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID)))) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering uint32_t u;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering r = sd_bus_call_method(
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering bus,
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "org.freedesktop.DBus",
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "/org/freedesktop/DBus",
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "org.freedesktop.DBus",
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering "GetConnectionUnixProcessID",
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering NULL,
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering &reply,
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "s",
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering unique ? unique : name);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (r < 0)
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return r;
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering r = sd_bus_message_read(reply, "u", &u);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering if (r < 0)
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering return r;
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering pid = u;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (mask & SD_BUS_CREDS_PID) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering c->pid = u;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering c->mask |= SD_BUS_CREDS_PID;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering }
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering reply = sd_bus_message_unref(reply);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering }
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (mask & SD_BUS_CREDS_UID) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering uint32_t u;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering r = sd_bus_call_method(
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering bus,
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "org.freedesktop.DBus",
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "/org/freedesktop/DBus",
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering "org.freedesktop.DBus",
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering "GetConnectionUnixUser",
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering NULL,
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering &reply,
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering "s",
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering unique ? unique : name);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering if (r < 0)
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering return r;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering r = sd_bus_message_read(reply, "u", &u);
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering if (r < 0)
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering return r;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering c->uid = u;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering c->mask |= SD_BUS_CREDS_UID;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering reply = sd_bus_message_unref(reply);
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering }
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering if (mask & SD_BUS_CREDS_SELINUX_CONTEXT) {
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering const void *p = NULL;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering size_t sz = 0;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
4cee5eede280b7fd48c18a1942616c4ac896a554Lennart Poettering r = sd_bus_call_method(
4cee5eede280b7fd48c18a1942616c4ac896a554Lennart Poettering bus,
4cee5eede280b7fd48c18a1942616c4ac896a554Lennart Poettering "org.freedesktop.DBus",
4cee5eede280b7fd48c18a1942616c4ac896a554Lennart Poettering "/org/freedesktop/DBus",
4cee5eede280b7fd48c18a1942616c4ac896a554Lennart Poettering "org.freedesktop.DBus",
26166c88e0b47b83972f32b5057ecbffe06bf904Lennart Poettering "GetConnectionSELinuxSecurityContext",
26166c88e0b47b83972f32b5057ecbffe06bf904Lennart Poettering &error,
26166c88e0b47b83972f32b5057ecbffe06bf904Lennart Poettering &reply,
26166c88e0b47b83972f32b5057ecbffe06bf904Lennart Poettering "s",
efe0286285a7432f738fafae840fa4eda51c2986Lennart Poettering unique ? unique : name);
efe0286285a7432f738fafae840fa4eda51c2986Lennart Poettering if (r < 0) {
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering if (!sd_bus_error_has_name(&error, "org.freedesktop.DBus.Error.SELinuxSecurityContextUnknown"))
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering return r;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering } else {
26166c88e0b47b83972f32b5057ecbffe06bf904Lennart Poettering r = sd_bus_message_read_array(reply, 'y', &p, &sz);
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering if (r < 0)
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering return r;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering c->label = strndup(p, sz);
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering if (!c->label)
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering return -ENOMEM;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering }
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering }
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering r = bus_creds_add_more(c, mask, pid, 0);
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering if (r < 0)
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering return r;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering }
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering if (creds) {
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering *creds = c;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering c = NULL;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering }
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering return 0;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering}
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering_public_ int sd_bus_get_name_creds(
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering sd_bus *bus,
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering const char *name,
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering uint64_t mask,
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering sd_bus_creds **creds) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering assert_return(bus, -EINVAL);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering assert_return(name, -EINVAL);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering assert_return((mask & ~SD_BUS_CREDS_AUGMENT) <= _SD_BUS_CREDS_ALL, -ENOTSUP);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering assert_return(mask == 0 || creds, -EINVAL);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering assert_return(!bus_pid_changed(bus), -ECHILD);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering assert_return(service_name_is_valid(name), -EINVAL);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering assert_return(bus->bus_client, -ENODATA);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!BUS_IS_OPEN(bus->state))
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return -ENOTCONN;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (bus->is_kernel)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return bus_get_name_creds_kdbus(bus, name, mask, false, creds);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering else
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return bus_get_name_creds_dbus1(bus, name, mask, creds);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering}
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringstatic int bus_get_owner_creds_kdbus(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering struct kdbus_cmd_info cmd = {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering .size = sizeof(struct kdbus_cmd_info)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering };
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering struct kdbus_info *creator_info;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pid_t pid = 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering int r;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering c = bus_creds_new();
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!c)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return -ENOMEM;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering cmd.flags = attach_flags_to_kdbus(mask);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering /* If augmentation is on, and the bus doesn't didn't allow us
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * to get the bits we want, then ask for the PID/TID so that we
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * can read the rest from /proc. */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if ((mask & SD_BUS_CREDS_AUGMENT) &&
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering (mask & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID|
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID|
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering SD_BUS_CREDS_COMM|SD_BUS_CREDS_TID_COMM|SD_BUS_CREDS_EXE|SD_BUS_CREDS_CMDLINE|
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering 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|
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS|
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering SD_BUS_CREDS_SELINUX_CONTEXT|
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID)))
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering cmd.flags |= KDBUS_ATTACH_PIDS;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = ioctl(bus->input_fd, KDBUS_CMD_BUS_CREATOR_INFO, &cmd);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return -errno;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering creator_info = (struct kdbus_info *) ((uint8_t *) bus->kdbus_buffer + cmd.offset);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = bus_populate_creds_from_items(bus, creator_info, mask, c);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering bus_kernel_cmd_free(bus, cmd.offset);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return r;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = bus_creds_add_more(c, mask, pid, 0);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return r;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *ret = c;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering c = NULL;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering}
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringstatic int bus_get_owner_creds_dbus1(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pid_t pid = 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering int r;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!bus->ucred_valid && !isempty(bus->label))
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return -ENODATA;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering c = bus_creds_new();
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!c)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return -ENOMEM;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (bus->ucred_valid) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (bus->ucred.pid > 0) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pid = c->pid = bus->ucred.pid;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering c->mask |= SD_BUS_CREDS_PID & mask;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (bus->ucred.uid != UID_INVALID) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering c->uid = bus->ucred.uid;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering c->mask |= SD_BUS_CREDS_UID & mask;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (bus->ucred.gid != GID_INVALID) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering c->gid = bus->ucred.gid;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering c->mask |= SD_BUS_CREDS_GID & mask;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!isempty(bus->label) && (mask & SD_BUS_CREDS_SELINUX_CONTEXT)) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering c->label = strdup(bus->label);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!c->label)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return -ENOMEM;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = bus_creds_add_more(c, mask, pid, 0);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return r;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *ret = c;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering c = NULL;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering}
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering_public_ int sd_bus_get_owner_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering assert_return(bus, -EINVAL);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering assert_return((mask & ~SD_BUS_CREDS_AUGMENT) <= _SD_BUS_CREDS_ALL, -ENOTSUP);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering assert_return(ret, -EINVAL);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering assert_return(!bus_pid_changed(bus), -ECHILD);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!BUS_IS_OPEN(bus->state))
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return -ENOTCONN;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (bus->is_kernel)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return bus_get_owner_creds_kdbus(bus, mask, ret);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering else
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return bus_get_owner_creds_dbus1(bus, mask, ret);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering}
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringstatic int add_name_change_match(sd_bus *bus,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering uint64_t cookie,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering const char *name,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering const char *old_owner,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering const char *new_owner) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering uint64_t name_id = KDBUS_MATCH_ID_ANY, old_owner_id = 0, new_owner_id = 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering int is_name_id = -1, r;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering struct kdbus_item *item;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering assert(bus);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering /* If we encounter a match that could match against
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * NameOwnerChanged messages, then we need to create
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * KDBUS_ITEM_NAME_{ADD,REMOVE,CHANGE} and
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * KDBUS_ITEM_ID_{ADD,REMOVE} matches for it, possibly
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * multiple if the match is underspecified.
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * The NameOwnerChanged signals take three parameters with
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * unique or well-known names, but only some forms actually
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * exist:
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * WELLKNOWN, "", UNIQUE → KDBUS_ITEM_NAME_ADD
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * WELLKNOWN, UNIQUE, "" → KDBUS_ITEM_NAME_REMOVE
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * WELLKNOWN, UNIQUE, UNIQUE → KDBUS_ITEM_NAME_CHANGE
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * UNIQUE, "", UNIQUE → KDBUS_ITEM_ID_ADD
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * UNIQUE, UNIQUE, "" → KDBUS_ITEM_ID_REMOVE
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * For the latter two the two unique names must be identical.
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (name) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering is_name_id = bus_kernel_parse_unique_name(name, &name_id);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (is_name_id < 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!isempty(old_owner)) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = bus_kernel_parse_unique_name(old_owner, &old_owner_id);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r == 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (is_name_id > 0 && old_owner_id != name_id)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering } else
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering old_owner_id = KDBUS_MATCH_ID_ANY;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!isempty(new_owner)) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = bus_kernel_parse_unique_name(new_owner, &new_owner_id);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return r;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r == 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (is_name_id > 0 && new_owner_id != name_id)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering } else
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering new_owner_id = KDBUS_MATCH_ID_ANY;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (is_name_id <= 0) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering struct kdbus_cmd_match *m;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering size_t sz, l;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering /* If the name argument is missing or is a well-known
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * name, then add KDBUS_ITEM_NAME_{ADD,REMOVE,CHANGE}
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * matches for it */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering l = name ? strlen(name) + 1 : 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering sz = ALIGN8(offsetof(struct kdbus_cmd_match, items) +
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering offsetof(struct kdbus_item, name_change) +
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering offsetof(struct kdbus_notify_name_change, name) +
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering l);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering m = alloca0_align(sz, 8);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering m->size = sz;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering m->cookie = cookie;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering item = m->items;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering item->size =
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering offsetof(struct kdbus_item, name_change) +
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering offsetof(struct kdbus_notify_name_change, name) +
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering l;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering item->name_change.old_id.id = old_owner_id;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering item->name_change.new_id.id = new_owner_id;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (name)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering memcpy(item->name_change.name, name, l);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering /* If the old name is unset or empty, then
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * this can match against added names */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!old_owner || old_owner[0] == 0) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering item->type = KDBUS_ITEM_NAME_ADD;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (r < 0)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return -errno;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering }
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering /* If the new name is unset or empty, then
adacb9575a09981fcf11279f2f661e3fc21e58ffLennart Poettering * this can match against removed names */
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering if (!new_owner || new_owner[0] == 0) {
adacb9575a09981fcf11279f2f661e3fc21e58ffLennart Poettering item->type = KDBUS_ITEM_NAME_REMOVE;
adacb9575a09981fcf11279f2f661e3fc21e58ffLennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (r < 0)
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering return -errno;
8d07a7c47159ebc2bcc775e2c1997cb44bd1ac95Lennart Poettering }
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering /* The CHANGE match we need in either case, because
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering * what is reported as a name change by the kernel
3a6fb33c54bc64398e0af1c9d7c74a6b614a849dLennart Poettering * might just be an owner change between starter and
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering * normal clients. For userspace such a change should
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering * be considered a removal/addition, hence let's
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering * subscribe to this unconditionally. */
49af9e1368571f4e423cde0fd45ee284451434d1Lennart Poettering item->type = KDBUS_ITEM_NAME_CHANGE;
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m);
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering if (r < 0)
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering return -errno;
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering }
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering if (is_name_id != 0) {
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering struct kdbus_cmd_match *m;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering uint64_t sz;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering /* If the name argument is missing or is a unique
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * name, then add KDBUS_ITEM_ID_{ADD,REMOVE} matches
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * for it */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering sz = ALIGN8(offsetof(struct kdbus_cmd_match, items) +
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering offsetof(struct kdbus_item, id_change) +
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering sizeof(struct kdbus_notify_id_change));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering m = alloca0_align(sz, 8);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering m->size = sz;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering m->cookie = cookie;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering item = m->items;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering item->size =
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering offsetof(struct kdbus_item, id_change) +
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sizeof(struct kdbus_notify_id_change);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering item->id_change.id = name_id;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* If the old name is unset or empty, then this can
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering * match against added ids */
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (!old_owner || old_owner[0] == 0) {
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering item->type = KDBUS_ITEM_ID_ADD;
65d73cf042ba7de11d254f5c4714f467db64b7c3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m);
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering if (r < 0)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return -errno;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering /* If thew new name is unset or empty, then this can
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering * match against removed ids */
491ac9f2c4aeda8c40edde35112404b737e38b60Lennart Poettering if (!new_owner || new_owner[0] == 0) {
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering item->type = KDBUS_ITEM_ID_REMOVE;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (r < 0)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return -errno;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering }
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering }
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering return 0;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering}
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poetteringint bus_add_match_internal_kernel(
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering sd_bus *bus,
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering struct bus_match_component *components,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering unsigned n_components,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering uint64_t cookie) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering struct kdbus_cmd_match *m;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct kdbus_item *item;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering uint64_t *bloom;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering size_t sz;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering const char *sender = NULL;
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering size_t sender_length = 0;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering uint64_t src_id = KDBUS_MATCH_ID_ANY;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering bool using_bloom = false;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering unsigned i;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering bool matches_name_change = true;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering const char *name_change_arg[3] = {};
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering int r;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert(bus);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
554604b3073467af75dc94fac9e2343148603289Lennart Poettering bloom = alloca0(bus->bloom_size);
554604b3073467af75dc94fac9e2343148603289Lennart Poettering
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering sz = ALIGN8(offsetof(struct kdbus_cmd_match, items));
e5f5b5b9c92d59ef3cb5169982cb6f436c9206faLennart Poettering
e5f5b5b9c92d59ef3cb5169982cb6f436c9206faLennart Poettering for (i = 0; i < n_components; i++) {
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering struct bus_match_component *c = &components[i];
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering switch (c->type) {
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering
554604b3073467af75dc94fac9e2343148603289Lennart Poettering case BUS_MATCH_SENDER:
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (!streq(c->value_str, "org.freedesktop.DBus"))
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering matches_name_change = false;
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering r = bus_kernel_parse_unique_name(c->value_str, &src_id);
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering if (r < 0)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return r;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering else if (r > 0)
554604b3073467af75dc94fac9e2343148603289Lennart Poettering sz += ALIGN8(offsetof(struct kdbus_item, id) + sizeof(uint64_t));
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering else {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering sender = c->value_str;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering sender_length = strlen(sender);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering sz += ALIGN8(offsetof(struct kdbus_item, str) + sender_length + 1);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
554604b3073467af75dc94fac9e2343148603289Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering break;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
554604b3073467af75dc94fac9e2343148603289Lennart Poettering case BUS_MATCH_MESSAGE_TYPE:
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (c->value_u8 != SD_BUS_MESSAGE_SIGNAL)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering matches_name_change = false;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, "message-type", bus_message_type_to_string(c->value_u8));
554604b3073467af75dc94fac9e2343148603289Lennart Poettering using_bloom = true;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering break;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering case BUS_MATCH_INTERFACE:
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering if (!streq(c->value_str, "org.freedesktop.DBus"))
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering matches_name_change = false;
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, "interface", c->value_str);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering using_bloom = true;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering break;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering case BUS_MATCH_MEMBER:
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering if (!streq(c->value_str, "NameOwnerChanged"))
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering matches_name_change = false;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
554604b3073467af75dc94fac9e2343148603289Lennart Poettering bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, "member", c->value_str);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering using_bloom = true;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering break;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering case BUS_MATCH_PATH:
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (!streq(c->value_str, "/org/freedesktop/DBus"))
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering matches_name_change = false;
65d73cf042ba7de11d254f5c4714f467db64b7c3Lennart Poettering
554604b3073467af75dc94fac9e2343148603289Lennart Poettering bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, "path", c->value_str);
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering using_bloom = true;
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering break;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering case BUS_MATCH_PATH_NAMESPACE:
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering if (!streq(c->value_str, "/")) {
554604b3073467af75dc94fac9e2343148603289Lennart Poettering bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, "path-slash-prefix", c->value_str);
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering using_bloom = true;
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering }
554604b3073467af75dc94fac9e2343148603289Lennart Poettering break;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering
554604b3073467af75dc94fac9e2343148603289Lennart Poettering case BUS_MATCH_ARG...BUS_MATCH_ARG_LAST: {
554604b3073467af75dc94fac9e2343148603289Lennart Poettering char buf[sizeof("arg")-1 + 2 + 1];
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (c->type - BUS_MATCH_ARG < 3)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering name_change_arg[c->type - BUS_MATCH_ARG] = c->value_str;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering snprintf(buf, sizeof(buf), "arg%u", c->type - BUS_MATCH_ARG);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, buf, c->value_str);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering using_bloom = true;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering break;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering case BUS_MATCH_ARG_PATH...BUS_MATCH_ARG_PATH_LAST: {
554604b3073467af75dc94fac9e2343148603289Lennart Poettering char buf[sizeof("arg")-1 + 2 + sizeof("-slash-prefix")];
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering snprintf(buf, sizeof(buf), "arg%u-slash-prefix", c->type - BUS_MATCH_ARG_PATH);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, buf, c->value_str);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering using_bloom = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering break;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering case BUS_MATCH_ARG_NAMESPACE...BUS_MATCH_ARG_NAMESPACE_LAST: {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char buf[sizeof("arg")-1 + 2 + sizeof("-dot-prefix")];
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering snprintf(buf, sizeof(buf), "arg%u-dot-prefix", c->type - BUS_MATCH_ARG_NAMESPACE);
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, buf, c->value_str);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering using_bloom = true;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering break;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering case BUS_MATCH_DESTINATION:
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering /* The bloom filter does not include
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the destination, since it is only
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering available for broadcast messages
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering which do not carry a destination
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering since they are undirected. */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering break;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering case BUS_MATCH_ROOT:
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering case BUS_MATCH_VALUE:
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering case BUS_MATCH_LEAF:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case _BUS_MATCH_NODE_TYPE_MAX:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case _BUS_MATCH_NODE_TYPE_INVALID:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_not_reached("Invalid match type?");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (using_bloom)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sz += ALIGN8(offsetof(struct kdbus_item, data64) + bus->bloom_size);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering m = alloca0_align(sz, 8);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering m->size = sz;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering m->cookie = cookie;
a931ad47a8623163a29d898224d8a8c1177ffdafLennart Poettering
a931ad47a8623163a29d898224d8a8c1177ffdafLennart Poettering item = m->items;
a931ad47a8623163a29d898224d8a8c1177ffdafLennart Poettering
a931ad47a8623163a29d898224d8a8c1177ffdafLennart Poettering if (src_id != KDBUS_MATCH_ID_ANY) {
554604b3073467af75dc94fac9e2343148603289Lennart Poettering item->size = offsetof(struct kdbus_item, id) + sizeof(uint64_t);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering item->type = KDBUS_ITEM_ID;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering item->id = src_id;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering item = KDBUS_ITEM_NEXT(item);
554604b3073467af75dc94fac9e2343148603289Lennart Poettering }
554604b3073467af75dc94fac9e2343148603289Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (using_bloom) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering item->size = offsetof(struct kdbus_item, data64) + bus->bloom_size;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering item->type = KDBUS_ITEM_BLOOM_MASK;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering memcpy(item->data64, bloom, bus->bloom_size);
86b8d289717bad2800342efca0a5023aa8374e9cLennart Poettering item = KDBUS_ITEM_NEXT(item);
86b8d289717bad2800342efca0a5023aa8374e9cLennart Poettering }
86b8d289717bad2800342efca0a5023aa8374e9cLennart Poettering
86b8d289717bad2800342efca0a5023aa8374e9cLennart Poettering if (sender) {
c49b30a23583ff39daaa26696bcab478d2fee0bbLennart Poettering item->size = offsetof(struct kdbus_item, str) + sender_length + 1;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering item->type = KDBUS_ITEM_NAME;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering memcpy(item->str, sender, sender_length + 1);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -errno;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (matches_name_change) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* If this match could theoretically match
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * NameOwnerChanged messages, we need to
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * install a second non-bloom filter explitly
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * for it */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = add_name_change_match(bus, cookie, name_change_arg[0], name_change_arg[1], name_change_arg[2]);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 0;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering}
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define internal_match(bus, m) \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering ((bus)->hello_flags & KDBUS_HELLO_MONITOR \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering ? (isempty(m) ? "eavesdrop='true'" : strappenda((m), ",eavesdrop='true'")) \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering : (m))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic int bus_add_match_internal_dbus1(
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus *bus,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *match) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *e;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(bus);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert(match);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering e = internal_match(bus, match);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return sd_bus_call_method(
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering bus,
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering "org.freedesktop.DBus",
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering "/org/freedesktop/DBus",
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering "org.freedesktop.DBus",
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "AddMatch",
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering NULL,
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering NULL,
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering "s",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering e);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint bus_add_match_internal(
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus *bus,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *match,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct bus_match_component *components,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering unsigned n_components,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering uint64_t cookie) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(bus);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (bus->is_kernel)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return bus_add_match_internal_kernel(bus, components, n_components, cookie);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering else
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return bus_add_match_internal_dbus1(bus, match);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poetteringint bus_remove_match_internal_kernel(
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus *bus,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering uint64_t cookie) {
de58a50e24a0d55e3bbcc77f8f6170a7322acf52Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct kdbus_cmd_match m;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering assert(bus);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering zero(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering m.size = offsetof(struct kdbus_cmd_match, items);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering m.cookie = cookie;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_REMOVE, &m);
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering if (r < 0)
de58a50e24a0d55e3bbcc77f8f6170a7322acf52Lennart Poettering return -errno;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic int bus_remove_match_internal_dbus1(
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus *bus,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *match) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *e;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(bus);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(match);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering e = internal_match(bus, match);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return sd_bus_call_method(
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering bus,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "org.freedesktop.DBus",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "/org/freedesktop/DBus",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "org.freedesktop.DBus",
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "RemoveMatch",
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering NULL,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering NULL,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "s",
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering e);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringint bus_remove_match_internal(
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering sd_bus *bus,
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering const char *match,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering uint64_t cookie) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering assert(bus);
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (bus->is_kernel)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return bus_remove_match_internal_kernel(bus, cookie);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering else
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return bus_remove_match_internal_dbus1(bus, match);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering_public_ int sd_bus_get_name_machine_id(sd_bus *bus, const char *name, sd_id128_t *machine) {
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL, *m = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *mid;
bd16acf35e13a19cd2ded0a0c2ef774a98f73808Zbigniew Jędrzejewski-Szmek int r;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_return(bus, -EINVAL);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_return(name, -EINVAL);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_return(machine, -EINVAL);
bd16acf35e13a19cd2ded0a0c2ef774a98f73808Zbigniew Jędrzejewski-Szmek assert_return(!bus_pid_changed(bus), -ECHILD);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_return(service_name_is_valid(name), -EINVAL);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
bd16acf35e13a19cd2ded0a0c2ef774a98f73808Zbigniew Jędrzejewski-Szmek if (!BUS_IS_OPEN(bus->state))
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return -ENOTCONN;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (streq_ptr(name, bus->unique_name))
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return sd_id128_get_machine(machine);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = sd_bus_message_new_method_call(
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering bus,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering &m,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering name,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "/",
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "org.freedesktop.DBus.Peer",
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "GetMachineId");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (r < 0)
bd16acf35e13a19cd2ded0a0c2ef774a98f73808Zbigniew Jędrzejewski-Szmek return r;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = sd_bus_message_set_auto_start(m, false);
bd16acf35e13a19cd2ded0a0c2ef774a98f73808Zbigniew Jędrzejewski-Szmek if (r < 0)
bd16acf35e13a19cd2ded0a0c2ef774a98f73808Zbigniew Jędrzejewski-Szmek return r;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
bd16acf35e13a19cd2ded0a0c2ef774a98f73808Zbigniew Jędrzejewski-Szmek r = sd_bus_call(bus, m, 0, NULL, &reply);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (r < 0)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return r;
bd16acf35e13a19cd2ded0a0c2ef774a98f73808Zbigniew Jędrzejewski-Szmek
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = sd_bus_message_read(reply, "s", &mid);
bd16acf35e13a19cd2ded0a0c2ef774a98f73808Zbigniew Jędrzejewski-Szmek if (r < 0)
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers return r;
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers return sd_id128_from_string(mid, machine);
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers}
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers