bus-control.c revision cccb0b2cdbd25e90ae92d2d5b107125cb1ca3433
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering This file is part of systemd.
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering Copyright 2013 Lennart Poettering
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering systemd is free software; you can redistribute it and/or modify it
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering under the terms of the GNU Lesser General Public License as published by
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering (at your option) any later version.
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering systemd is distributed in the hope that it will be useful, but
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering Lesser General Public License for more details.
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering You should have received a copy of the GNU Lesser General Public License
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering_public_ int sd_bus_get_unique_name(sd_bus *bus, const char **unique) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering assert_return(!bus_pid_changed(bus), -ECHILD);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poetteringstatic int bus_request_name_kernel(sd_bus *bus, const char *name, uint64_t flags) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering size = offsetof(struct kdbus_cmd_name, name) + l + 1;
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering kdbus_translate_request_name_flags(flags, (uint64_t *) &n->flags);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering r = ioctl(bus->input_fd, KDBUS_CMD_NAME_ACQUIRE, n);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poetteringstatic int bus_request_name_dbus1(sd_bus *bus, const char *name, uint64_t flags) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering if (flags & SD_BUS_NAME_ALLOW_REPLACEMENT)
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering if (flags & SD_BUS_NAME_REPLACE_EXISTING)
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering "org.freedesktop.DBus",
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering "org.freedesktop.DBus",
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering "RequestName",
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering r = sd_bus_message_read(reply, "u", &ret);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering_public_ int sd_bus_request_name(sd_bus *bus, const char *name, uint64_t flags) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering assert_return(!bus_pid_changed(bus), -ECHILD);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering assert_return(!(flags & ~(SD_BUS_NAME_ALLOW_REPLACEMENT|SD_BUS_NAME_REPLACE_EXISTING|SD_BUS_NAME_QUEUE)), -EINVAL);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering assert_return(service_name_is_valid(name), -EINVAL);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering return bus_request_name_kernel(bus, name, flags);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering return bus_request_name_dbus1(bus, name, flags);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poetteringstatic int bus_release_name_kernel(sd_bus *bus, const char *name) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering n = alloca0(offsetof(struct kdbus_cmd_name, name) + l + 1);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering n->size = offsetof(struct kdbus_cmd_name, name) + l + 1;
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering r = ioctl(bus->input_fd, KDBUS_CMD_NAME_RELEASE, n);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poetteringstatic int bus_release_name_dbus1(sd_bus *bus, const char *name) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering "org.freedesktop.DBus",
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering "org.freedesktop.DBus",
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering "ReleaseName",
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering r = sd_bus_message_read(reply, "u", &ret);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering_public_ int sd_bus_release_name(sd_bus *bus, const char *name) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering assert_return(!bus_pid_changed(bus), -ECHILD);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering assert_return(service_name_is_valid(name), -EINVAL);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering return bus_release_name_kernel(bus, name);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering return bus_release_name_dbus1(bus, name);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poetteringstatic int kernel_get_list(sd_bus *bus, uint64_t flags, char ***x) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering /* Caller will free half-constructed list on failure... */
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering r = ioctl(bus->input_fd, KDBUS_CMD_NAME_LIST, &cmd);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering name_list = (struct kdbus_name_list *) ((uint8_t *) bus->kdbus_buffer + cmd.offset);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering KDBUS_ITEM_FOREACH(name, name_list, names) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering if ((flags & KDBUS_NAME_LIST_UNIQUE) && name->owner_id != previous_id) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering if (asprintf(&n, ":1.%llu", (unsigned long long) name->owner_id) < 0)
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering if (name->size > sizeof(*name) && service_name_is_valid(name->name)) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering r = ioctl(bus->input_fd, KDBUS_CMD_FREE, &cmd.offset);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poetteringstatic int bus_list_names_kernel(sd_bus *bus, char ***acquired, char ***activatable) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering _cleanup_strv_free_ char **x = NULL, **y = NULL;
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering r = kernel_get_list(bus, KDBUS_NAME_LIST_UNIQUE | KDBUS_NAME_LIST_NAMES, &x);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering r = kernel_get_list(bus, KDBUS_NAME_LIST_ACTIVATORS, &y);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poetteringstatic int bus_list_names_dbus1(sd_bus *bus, char ***acquired, char ***activatable) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering _cleanup_strv_free_ char **x = NULL, **y = NULL;
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering "org.freedesktop.DBus",
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering "org.freedesktop.DBus",
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering "org.freedesktop.DBus",
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering "org.freedesktop.DBus",
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering "ListActivatableNames",
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering_public_ int sd_bus_list_names(sd_bus *bus, char ***acquired, char ***activatable) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering assert_return(acquired || activatable, -EINVAL);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering assert_return(!bus_pid_changed(bus), -ECHILD);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering return bus_list_names_kernel(bus, acquired, activatable);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering return bus_list_names_dbus1(bus, acquired, activatable);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering r = bus_kernel_parse_unique_name(name, &id);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering size = offsetof(struct kdbus_cmd_conn_info, name);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering size = offsetof(struct kdbus_cmd_conn_info, name) + strlen(name) + 1;
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering kdbus_translate_attach_flags(mask, (uint64_t*) &cmd->flags);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering r = ioctl(bus->input_fd, KDBUS_CMD_CONN_INFO, cmd);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering conn_info = (struct kdbus_conn_info *) ((uint8_t *) bus->kdbus_buffer + cmd->offset);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering /* Non-activated names are considered not available */
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering if (conn_info->flags & KDBUS_HELLO_ACTIVATOR)
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering return name[0] == ':' ? -ENXIO : -ENOENT;
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering if (asprintf(&c->unique_name, ":1.%llu", (unsigned long long) conn_info->id) < 0)
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering KDBUS_ITEM_FOREACH(item, conn_info, items) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering m = (SD_BUS_CREDS_UID | SD_BUS_CREDS_GID | SD_BUS_CREDS_PID) & mask;
case KDBUS_ITEM_PID_COMM:
if (!c->comm) {
r = -ENOMEM;
goto fail;
case KDBUS_ITEM_TID_COMM:
if (!c->tid_comm) {
r = -ENOMEM;
goto fail;
case KDBUS_ITEM_EXE:
if (!c->exe) {
r = -ENOMEM;
goto fail;
case KDBUS_ITEM_CMDLINE:
if (!c->cmdline) {
r = -ENOMEM;
goto fail;
case KDBUS_ITEM_CGROUP:
if (!c->cgroup) {
r = -ENOMEM;
goto fail;
goto fail;
if (!c->cgroup_root) {
r = -ENOMEM;
goto fail;
c->mask |= m;
case KDBUS_ITEM_CAPS:
if (!c->capability) {
r = -ENOMEM;
goto fail;
c->mask |= m;
case KDBUS_ITEM_SECLABEL:
if (!c->label) {
r = -ENOMEM;
goto fail;
case KDBUS_ITEM_AUDIT:
c->mask |= m;
case KDBUS_ITEM_NAME:
goto fail;
case KDBUS_ITEM_CONN_NAME:
if (!c->conn_name) {
r = -ENOMEM;
goto fail;
if (creds) {
*creds = c;
c = NULL;
fail:
static int bus_get_owner_dbus1(
const char *name,
r = sd_bus_call_method(
bus,
"/org/freedesktop/DBus",
NULL,
name);
if (mask != 0) {
c = bus_creds_new();
return -ENOMEM;
if (!c->unique_name)
return -ENOMEM;
SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID|
SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS|
uint32_t u;
r = sd_bus_call_method(
bus,
"/org/freedesktop/DBus",
NULL,
&reply,
pid = u;
c->pid = u;
uint32_t u;
r = sd_bus_call_method(
bus,
"/org/freedesktop/DBus",
NULL,
&reply,
c->uid = u;
r = sd_bus_call_method(
bus,
"/org/freedesktop/DBus",
NULL,
&reply,
if (!c->label)
return -ENOMEM;
if (creds) {
*creds = c;
c = NULL;
const char *name,
const char *name,
const char *old_owner,
const char *new_owner) {
if (name) {
if (is_name_id < 0)
if (is_name_id <= 0) {
struct kdbus_cmd_match *m;
if (name)
return -errno;
return -errno;
return -errno;
if (is_name_id != 0) {
struct kdbus_cmd_match *m;
sizeof(struct kdbus_notify_id_change));
sizeof(struct kdbus_notify_id_change);
return -errno;
return -errno;
unsigned n_components,
struct kdbus_cmd_match *m;
bool using_bloom = false;
bool matches_name_change = true;
for (i = 0; i < n_components; i++) {
switch (c->type) {
case BUS_MATCH_SENDER:
matches_name_change = false;
case BUS_MATCH_MESSAGE_TYPE:
matches_name_change = false;
using_bloom = true;
case BUS_MATCH_INTERFACE:
matches_name_change = false;
using_bloom = true;
case BUS_MATCH_MEMBER:
matches_name_change = false;
using_bloom = true;
case BUS_MATCH_PATH:
matches_name_change = false;
using_bloom = true;
case BUS_MATCH_PATH_NAMESPACE:
using_bloom = true;
using_bloom = true;
using_bloom = true;
using_bloom = true;
case BUS_MATCH_DESTINATION:
case BUS_MATCH_ROOT:
case BUS_MATCH_VALUE:
case BUS_MATCH_LEAF:
case _BUS_MATCH_NODE_TYPE_MAX:
if (using_bloom)
if (using_bloom) {
if (sender) {
return -errno;
if (matches_name_change) {
static int bus_add_match_internal_dbus1(
const char *match) {
return sd_bus_call_method(
bus,
"/org/freedesktop/DBus",
NULL,
NULL,
match);
const char *match,
unsigned n_components,
struct kdbus_cmd_match m;
zero(m);
return -errno;
static int bus_remove_match_internal_dbus1(
const char *match) {
return sd_bus_call_method(
bus,
"/org/freedesktop/DBus",
NULL,
NULL,
match);
const char *match,
const char *mid;
bus,
name,
r = sd_bus_message_set_no_auto_start(m, true);