2013-03-19 20:03:16 +01:00
|
|
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
|
|
|
|
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2013 Lennart Poettering
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
2013-04-14 03:19:51 +02:00
|
|
|
#ifdef HAVE_VALGRIND_MEMCHECK_H
|
|
|
|
#include <valgrind/memcheck.h>
|
|
|
|
#endif
|
|
|
|
|
2013-03-19 20:03:16 +01:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "strv.h"
|
|
|
|
|
|
|
|
#include "sd-bus.h"
|
|
|
|
#include "bus-internal.h"
|
|
|
|
#include "bus-message.h"
|
2013-03-31 16:16:37 +02:00
|
|
|
#include "bus-control.h"
|
2013-05-19 18:39:08 +02:00
|
|
|
#include "bus-bloom.h"
|
2013-10-16 06:10:04 +02:00
|
|
|
#include "bus-util.h"
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-11-06 08:33:42 +01:00
|
|
|
_public_ int sd_bus_get_unique_name(sd_bus *bus, const char **unique) {
|
2013-03-22 03:34:29 +01:00
|
|
|
int r;
|
|
|
|
|
2013-11-29 17:57:00 +01:00
|
|
|
assert_return(bus, -EINVAL);
|
|
|
|
assert_return(unique, -EINVAL);
|
|
|
|
assert_return(!bus_pid_changed(bus), -ECHILD);
|
2013-03-22 03:34:29 +01:00
|
|
|
|
|
|
|
r = bus_ensure_running(bus);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-03-22 03:34:29 +01:00
|
|
|
*unique = bus->unique_name;
|
|
|
|
return 0;
|
2013-03-19 20:03:16 +01:00
|
|
|
}
|
|
|
|
|
2013-11-06 08:33:42 +01:00
|
|
|
_public_ int sd_bus_request_name(sd_bus *bus, const char *name, int flags) {
|
2013-04-05 14:49:45 +02:00
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
|
2013-03-19 20:03:16 +01:00
|
|
|
uint32_t ret;
|
|
|
|
int r;
|
|
|
|
|
2013-11-29 17:57:00 +01:00
|
|
|
assert_return(bus, -EINVAL);
|
|
|
|
assert_return(name, -EINVAL);
|
|
|
|
assert_return(bus->bus_client, -EINVAL);
|
|
|
|
assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
|
|
|
|
assert_return(!bus_pid_changed(bus), -ECHILD);
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-04-12 03:08:14 +02:00
|
|
|
if (bus->is_kernel) {
|
|
|
|
struct kdbus_cmd_name *n;
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
l = strlen(name);
|
2013-04-14 17:43:59 +02:00
|
|
|
n = alloca0(offsetof(struct kdbus_cmd_name, name) + l + 1);
|
2013-04-12 03:08:14 +02:00
|
|
|
n->size = offsetof(struct kdbus_cmd_name, name) + l + 1;
|
2013-05-24 03:08:48 +02:00
|
|
|
n->flags = flags;
|
2013-04-12 03:08:14 +02:00
|
|
|
memcpy(n->name, name, l+1);
|
|
|
|
|
2013-04-14 03:19:51 +02:00
|
|
|
#ifdef HAVE_VALGRIND_MEMCHECK_H
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED(n, n->size);
|
|
|
|
#endif
|
|
|
|
|
2013-04-12 03:08:14 +02:00
|
|
|
r = ioctl(bus->input_fd, KDBUS_CMD_NAME_ACQUIRE, n);
|
2013-11-26 17:24:18 +01:00
|
|
|
if (r < 0) {
|
|
|
|
if (errno == -EALREADY)
|
|
|
|
return SD_BUS_NAME_ALREADY_OWNER;
|
|
|
|
|
|
|
|
if (errno == -EEXIST)
|
|
|
|
return SD_BUS_NAME_EXISTS;
|
|
|
|
|
2013-04-12 03:08:14 +02:00
|
|
|
return -errno;
|
2013-11-26 17:24:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n->flags & KDBUS_NAME_IN_QUEUE)
|
|
|
|
return SD_BUS_NAME_IN_QUEUE;
|
2013-04-12 03:08:14 +02:00
|
|
|
|
2013-11-26 17:24:18 +01:00
|
|
|
return SD_BUS_NAME_PRIMARY_OWNER;
|
2013-04-12 03:08:14 +02:00
|
|
|
} else {
|
|
|
|
r = sd_bus_call_method(
|
|
|
|
bus,
|
|
|
|
"org.freedesktop.DBus",
|
|
|
|
"/",
|
|
|
|
"org.freedesktop.DBus",
|
|
|
|
"RequestName",
|
|
|
|
NULL,
|
|
|
|
&reply,
|
|
|
|
"su",
|
|
|
|
name,
|
|
|
|
flags);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_message_read(reply, "u", &ret);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2013-03-19 20:03:16 +01:00
|
|
|
}
|
|
|
|
|
2013-11-06 08:33:42 +01:00
|
|
|
_public_ int sd_bus_release_name(sd_bus *bus, const char *name) {
|
2013-04-05 14:49:45 +02:00
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
|
2013-03-19 20:03:16 +01:00
|
|
|
uint32_t ret;
|
|
|
|
int r;
|
|
|
|
|
2013-11-29 17:57:00 +01:00
|
|
|
assert_return(bus, -EINVAL);
|
|
|
|
assert_return(name, -EINVAL);
|
|
|
|
assert_return(bus->bus_client, -EINVAL);
|
|
|
|
assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
|
|
|
|
assert_return(!bus_pid_changed(bus), -ECHILD);
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-04-12 03:08:14 +02:00
|
|
|
if (bus->is_kernel) {
|
|
|
|
struct kdbus_cmd_name *n;
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
l = strlen(name);
|
2013-04-14 17:43:59 +02:00
|
|
|
n = alloca0(offsetof(struct kdbus_cmd_name, name) + l + 1);
|
2013-04-12 03:08:14 +02:00
|
|
|
n->size = offsetof(struct kdbus_cmd_name, name) + l + 1;
|
|
|
|
memcpy(n->name, name, l+1);
|
|
|
|
|
2013-04-14 03:19:51 +02:00
|
|
|
#ifdef HAVE_VALGRIND_MEMCHECK_H
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED(n, n->size);
|
|
|
|
#endif
|
2013-04-12 03:08:14 +02:00
|
|
|
r = ioctl(bus->input_fd, KDBUS_CMD_NAME_RELEASE, n);
|
|
|
|
if (r < 0)
|
|
|
|
return -errno;
|
|
|
|
|
2013-05-24 03:08:48 +02:00
|
|
|
return n->flags;
|
2013-04-12 03:08:14 +02:00
|
|
|
} else {
|
|
|
|
r = sd_bus_call_method(
|
|
|
|
bus,
|
|
|
|
"org.freedesktop.DBus",
|
|
|
|
"/",
|
|
|
|
"org.freedesktop.DBus",
|
|
|
|
"ReleaseName",
|
|
|
|
NULL,
|
|
|
|
&reply,
|
|
|
|
"s",
|
|
|
|
name);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_message_read(reply, "u", &ret);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
2013-03-19 20:03:16 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-11-06 08:33:42 +01:00
|
|
|
_public_ int sd_bus_list_names(sd_bus *bus, char ***l) {
|
2013-04-05 14:49:45 +02:00
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *reply1 = NULL, *reply2 = NULL;
|
2013-03-19 20:03:16 +01:00
|
|
|
char **x = NULL;
|
|
|
|
int r;
|
|
|
|
|
2013-11-29 17:57:00 +01:00
|
|
|
assert_return(bus, -EINVAL);
|
|
|
|
assert_return(l, -EINVAL);
|
|
|
|
assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
|
|
|
|
assert_return(!bus_pid_changed(bus), -ECHILD);
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-11-15 16:04:32 +01:00
|
|
|
if (bus->is_kernel) {
|
|
|
|
_cleanup_free_ struct kdbus_cmd_names *names = NULL;
|
|
|
|
struct kdbus_cmd_name *name;
|
|
|
|
size_t size;
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-11-15 16:04:32 +01:00
|
|
|
/* assume 8k size first. If that doesn't suffice, kdbus will tell us
|
|
|
|
* how big the buffer needs to be. */
|
|
|
|
size = 8192;
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-11-15 16:04:32 +01:00
|
|
|
for(;;) {
|
|
|
|
names = realloc(names, size);
|
|
|
|
if (!names)
|
|
|
|
return -ENOMEM;
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-11-15 16:04:32 +01:00
|
|
|
names->size = size;
|
2013-11-19 16:33:05 +01:00
|
|
|
names->flags = KDBUS_NAME_LIST_UNIQUE_NAMES;
|
2013-11-15 16:04:32 +01:00
|
|
|
|
|
|
|
r = ioctl(sd_bus_get_fd(bus), KDBUS_CMD_NAME_LIST, names);
|
|
|
|
if (r < 0) {
|
|
|
|
if (errno == ENOBUFS && size != names->size) {
|
|
|
|
size = names->size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
KDBUS_PART_FOREACH(name, names, names) {
|
2013-11-19 16:33:05 +01:00
|
|
|
char *n;
|
|
|
|
|
|
|
|
if (name->size > sizeof(*name))
|
|
|
|
n = name->name;
|
|
|
|
else
|
|
|
|
asprintf(&n, ":1.%llu", (unsigned long long) name->id);
|
|
|
|
|
|
|
|
r = strv_extend(&x, n);
|
2013-11-15 16:04:32 +01:00
|
|
|
if (r < 0)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
*l = x;
|
|
|
|
} else {
|
|
|
|
r = sd_bus_call_method(
|
|
|
|
bus,
|
|
|
|
"org.freedesktop.DBus",
|
|
|
|
"/",
|
|
|
|
"org.freedesktop.DBus",
|
|
|
|
"ListNames",
|
|
|
|
NULL,
|
|
|
|
&reply1,
|
|
|
|
NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_call_method(
|
|
|
|
bus,
|
|
|
|
"org.freedesktop.DBus",
|
|
|
|
"/",
|
|
|
|
"org.freedesktop.DBus",
|
|
|
|
"ListActivatableNames",
|
|
|
|
NULL,
|
|
|
|
&reply2,
|
|
|
|
NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = bus_message_read_strv_extend(reply1, &x);
|
|
|
|
if (r < 0) {
|
|
|
|
strv_free(x);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = bus_message_read_strv_extend(reply2, &x);
|
|
|
|
if (r < 0) {
|
|
|
|
strv_free(x);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
*l = strv_uniq(x);
|
2013-03-20 22:56:25 +01:00
|
|
|
}
|
2013-03-19 20:03:16 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-28 20:41:55 +01:00
|
|
|
_public_ int sd_bus_get_owner(
|
|
|
|
sd_bus *bus,
|
|
|
|
const char *name,
|
|
|
|
uint64_t mask,
|
|
|
|
char **owner,
|
|
|
|
sd_bus_creds **creds) {
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-04-05 14:49:45 +02:00
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
|
2013-11-28 17:50:02 +01:00
|
|
|
_cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
|
2013-11-28 20:41:55 +01:00
|
|
|
_cleanup_free_ char *unique = NULL;
|
2013-11-28 17:50:02 +01:00
|
|
|
pid_t pid = 0;
|
2013-03-19 20:03:16 +01:00
|
|
|
int r;
|
|
|
|
|
2013-11-28 17:50:02 +01:00
|
|
|
assert_return(bus, -EINVAL);
|
|
|
|
assert_return(name, -EINVAL);
|
|
|
|
assert_return(mask <= _SD_BUS_CREDS_MAX, -ENOTSUP);
|
2013-11-28 20:41:55 +01:00
|
|
|
assert_return(mask == 0 || creds, -EINVAL);
|
2013-11-28 17:50:02 +01:00
|
|
|
assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
|
|
|
|
assert_return(!bus_pid_changed(bus), -ECHILD);
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-11-28 20:41:55 +01:00
|
|
|
/* Only query the owner if the caller wants to know it or if
|
|
|
|
* the caller just wants to check whether a name exists */
|
|
|
|
if (owner || mask == 0) {
|
|
|
|
const char *found;
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-11-28 17:50:02 +01:00
|
|
|
r = sd_bus_call_method(
|
|
|
|
bus,
|
|
|
|
"org.freedesktop.DBus",
|
|
|
|
"/",
|
|
|
|
"org.freedesktop.DBus",
|
2013-11-28 20:41:55 +01:00
|
|
|
"GetNameOwner",
|
2013-11-28 17:50:02 +01:00
|
|
|
NULL,
|
|
|
|
&reply,
|
|
|
|
"s",
|
|
|
|
name);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-11-28 20:41:55 +01:00
|
|
|
r = sd_bus_message_read(reply, "s", &found);
|
2013-11-28 17:50:02 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-11-28 20:41:55 +01:00
|
|
|
unique = strdup(found);
|
|
|
|
if (!unique)
|
|
|
|
return -ENOMEM;
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-11-28 17:50:02 +01:00
|
|
|
reply = sd_bus_message_unref(reply);
|
|
|
|
}
|
|
|
|
|
2013-11-28 20:41:55 +01:00
|
|
|
if (mask != 0) {
|
|
|
|
c = bus_creds_new();
|
|
|
|
if (!c)
|
|
|
|
return -ENOMEM;
|
2013-11-28 17:50:02 +01:00
|
|
|
|
2013-11-28 20:41:55 +01:00
|
|
|
if ((mask & SD_BUS_CREDS_PID) ||
|
|
|
|
mask & ~(SD_BUS_CREDS_PID|SD_BUS_CREDS_UID|SD_BUS_CREDS_SELINUX_CONTEXT)) {
|
|
|
|
uint32_t u;
|
|
|
|
|
|
|
|
r = sd_bus_call_method(
|
|
|
|
bus,
|
|
|
|
"org.freedesktop.DBus",
|
|
|
|
"/",
|
|
|
|
"org.freedesktop.DBus",
|
|
|
|
"GetConnectionUnixProcessID",
|
|
|
|
NULL,
|
|
|
|
&reply,
|
|
|
|
"s",
|
|
|
|
name);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_message_read(reply, "u", &u);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
pid = u;
|
|
|
|
if (mask & SD_BUS_CREDS_PID) {
|
|
|
|
c->pid = u;
|
|
|
|
c->mask |= SD_BUS_CREDS_PID;
|
|
|
|
}
|
|
|
|
|
|
|
|
reply = sd_bus_message_unref(reply);
|
|
|
|
}
|
2013-11-28 17:50:02 +01:00
|
|
|
|
2013-11-28 20:41:55 +01:00
|
|
|
if (mask & SD_BUS_CREDS_UID) {
|
|
|
|
uint32_t u;
|
|
|
|
|
|
|
|
r = sd_bus_call_method(
|
|
|
|
bus,
|
|
|
|
"org.freedesktop.DBus",
|
|
|
|
"/",
|
|
|
|
"org.freedesktop.DBus",
|
|
|
|
"GetConnectionUnixUser",
|
|
|
|
NULL,
|
|
|
|
&reply,
|
|
|
|
"s",
|
|
|
|
name);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_message_read(reply, "u", &u);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
c->uid = u;
|
|
|
|
c->mask |= SD_BUS_CREDS_UID;
|
|
|
|
|
|
|
|
reply = sd_bus_message_unref(reply);
|
|
|
|
}
|
2013-11-28 17:50:02 +01:00
|
|
|
|
2013-11-28 20:41:55 +01:00
|
|
|
if (mask & SD_BUS_CREDS_SELINUX_CONTEXT) {
|
|
|
|
const void *p;
|
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
r = sd_bus_call_method(
|
|
|
|
bus,
|
|
|
|
"org.freedesktop.DBus",
|
|
|
|
"/",
|
|
|
|
"org.freedesktop.DBus",
|
|
|
|
"GetConnectionSELinuxSecurityContext",
|
|
|
|
NULL,
|
|
|
|
&reply,
|
|
|
|
"s",
|
|
|
|
name);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_message_read_array(reply, 'y', &p, &sz);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
c->label = strndup(p, sz);
|
|
|
|
if (!c->label)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
|
|
|
|
}
|
2013-11-28 17:50:02 +01:00
|
|
|
|
2013-11-28 20:41:55 +01:00
|
|
|
r = bus_creds_add_more(c, mask, pid, 0);
|
2013-11-28 17:50:02 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-11-28 20:41:55 +01:00
|
|
|
if (creds) {
|
|
|
|
*creds = c;
|
|
|
|
c = NULL;
|
|
|
|
}
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-11-28 20:41:55 +01:00
|
|
|
if (owner) {
|
|
|
|
*owner = unique;
|
|
|
|
unique = NULL;
|
|
|
|
}
|
2013-03-19 20:03:16 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-19 18:39:08 +02:00
|
|
|
int bus_add_match_internal(
|
|
|
|
sd_bus *bus,
|
|
|
|
const char *match,
|
|
|
|
struct bus_match_component *components,
|
|
|
|
unsigned n_components,
|
|
|
|
uint64_t cookie) {
|
|
|
|
|
|
|
|
int r;
|
|
|
|
|
2013-03-31 16:16:37 +02:00
|
|
|
assert(bus);
|
|
|
|
assert(match);
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-05-19 18:39:08 +02:00
|
|
|
if (bus->is_kernel) {
|
|
|
|
struct kdbus_cmd_match *m;
|
|
|
|
struct kdbus_item *item;
|
|
|
|
uint64_t bloom[BLOOM_SIZE/8];
|
|
|
|
size_t sz;
|
|
|
|
const char *sender = NULL;
|
|
|
|
size_t sender_length = 0;
|
|
|
|
uint64_t src_id = KDBUS_MATCH_SRC_ID_ANY;
|
|
|
|
bool using_bloom = false;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
zero(bloom);
|
|
|
|
|
|
|
|
sz = offsetof(struct kdbus_cmd_match, items);
|
|
|
|
|
|
|
|
for (i = 0; i < n_components; i++) {
|
|
|
|
struct bus_match_component *c = &components[i];
|
|
|
|
|
|
|
|
switch (c->type) {
|
|
|
|
|
|
|
|
case BUS_MATCH_SENDER:
|
|
|
|
r = bus_kernel_parse_unique_name(c->value_str, &src_id);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (r > 0) {
|
|
|
|
sender = c->value_str;
|
|
|
|
sender_length = strlen(sender);
|
|
|
|
sz += ALIGN8(offsetof(struct kdbus_item, str) + sender_length + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2013-05-20 00:21:56 +02:00
|
|
|
case BUS_MATCH_MESSAGE_TYPE:
|
|
|
|
bloom_add_pair(bloom, "message-type", bus_message_type_to_string(c->value_u8));
|
|
|
|
using_bloom = true;
|
2013-05-19 18:39:08 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BUS_MATCH_INTERFACE:
|
|
|
|
bloom_add_pair(bloom, "interface", c->value_str);
|
|
|
|
using_bloom = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BUS_MATCH_MEMBER:
|
|
|
|
bloom_add_pair(bloom, "member", c->value_str);
|
|
|
|
using_bloom = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BUS_MATCH_PATH:
|
|
|
|
bloom_add_pair(bloom, "path", c->value_str);
|
|
|
|
using_bloom = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BUS_MATCH_PATH_NAMESPACE:
|
2013-05-20 00:36:50 +02:00
|
|
|
if (!streq(c->value_str, "/")) {
|
|
|
|
bloom_add_pair(bloom, "path-slash-prefix", c->value_str);
|
|
|
|
using_bloom = true;
|
|
|
|
}
|
2013-05-19 18:39:08 +02:00
|
|
|
break;
|
|
|
|
|
2013-05-20 00:21:56 +02:00
|
|
|
case BUS_MATCH_ARG...BUS_MATCH_ARG_LAST: {
|
|
|
|
char buf[sizeof("arg")-1 + 2 + 1];
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "arg%u", c->type - BUS_MATCH_ARG);
|
|
|
|
bloom_add_pair(bloom, buf, c->value_str);
|
|
|
|
using_bloom = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case BUS_MATCH_ARG_PATH...BUS_MATCH_ARG_PATH_LAST: {
|
|
|
|
char buf[sizeof("arg")-1 + 2 + sizeof("-slash-prefix")];
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "arg%u-slash-prefix", c->type - BUS_MATCH_ARG_PATH);
|
|
|
|
bloom_add_pair(bloom, buf, c->value_str);
|
|
|
|
using_bloom = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case BUS_MATCH_ARG_NAMESPACE...BUS_MATCH_ARG_NAMESPACE_LAST: {
|
|
|
|
char buf[sizeof("arg")-1 + 2 + sizeof("-dot-prefix")];
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "arg%u-dot-prefix", c->type - BUS_MATCH_ARG_NAMESPACE);
|
|
|
|
bloom_add_pair(bloom, buf, c->value_str);
|
|
|
|
using_bloom = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case BUS_MATCH_DESTINATION:
|
|
|
|
/* The bloom filter does not include
|
|
|
|
the destination, since it is only
|
|
|
|
available for broadcast messages
|
|
|
|
which do not carry a destination
|
|
|
|
since they are undirected. */
|
2013-05-19 18:39:08 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BUS_MATCH_ROOT:
|
|
|
|
case BUS_MATCH_VALUE:
|
|
|
|
case BUS_MATCH_LEAF:
|
|
|
|
case _BUS_MATCH_NODE_TYPE_MAX:
|
|
|
|
case _BUS_MATCH_NODE_TYPE_INVALID:
|
|
|
|
assert_not_reached("Invalid match type?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (using_bloom)
|
|
|
|
sz += ALIGN8(offsetof(struct kdbus_item, data64) + BLOOM_SIZE);
|
|
|
|
|
|
|
|
m = alloca0(sz);
|
|
|
|
m->size = sz;
|
|
|
|
m->cookie = cookie;
|
|
|
|
m->src_id = src_id;
|
|
|
|
|
|
|
|
item = m->items;
|
|
|
|
|
|
|
|
if (using_bloom) {
|
|
|
|
item->size = offsetof(struct kdbus_item, data64) + BLOOM_SIZE;
|
|
|
|
item->type = KDBUS_MATCH_BLOOM;
|
|
|
|
memcpy(item->data64, bloom, BLOOM_SIZE);
|
|
|
|
|
2013-11-15 15:55:27 +01:00
|
|
|
item = KDBUS_PART_NEXT(item);
|
2013-05-19 18:39:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sender) {
|
|
|
|
item->size = offsetof(struct kdbus_item, str) + sender_length + 1;
|
|
|
|
item->type = KDBUS_MATCH_SRC_NAME;
|
|
|
|
memcpy(item->str, sender, sender_length + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m);
|
|
|
|
if (r < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return sd_bus_call_method(
|
|
|
|
bus,
|
|
|
|
"org.freedesktop.DBus",
|
|
|
|
"/",
|
|
|
|
"org.freedesktop.DBus",
|
|
|
|
"AddMatch",
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
"s",
|
|
|
|
match);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2013-03-19 20:03:16 +01:00
|
|
|
}
|
|
|
|
|
2013-05-19 18:39:08 +02:00
|
|
|
int bus_remove_match_internal(
|
|
|
|
sd_bus *bus,
|
|
|
|
const char *match,
|
|
|
|
uint64_t cookie) {
|
|
|
|
|
|
|
|
int r;
|
|
|
|
|
2013-03-31 16:16:37 +02:00
|
|
|
assert(bus);
|
|
|
|
assert(match);
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-05-19 18:39:08 +02:00
|
|
|
if (bus->is_kernel) {
|
|
|
|
struct kdbus_cmd_match m;
|
|
|
|
|
|
|
|
zero(m);
|
|
|
|
m.size = offsetof(struct kdbus_cmd_match, items);
|
|
|
|
m.cookie = cookie;
|
|
|
|
|
|
|
|
r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_REMOVE, &m);
|
|
|
|
if (r < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return sd_bus_call_method(
|
|
|
|
bus,
|
|
|
|
"org.freedesktop.DBus",
|
|
|
|
"/",
|
|
|
|
"org.freedesktop.DBus",
|
|
|
|
"RemoveMatch",
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
"s",
|
|
|
|
match);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2013-03-19 20:03:16 +01:00
|
|
|
}
|
2013-04-23 16:18:17 +02:00
|
|
|
|
2013-11-06 08:33:42 +01:00
|
|
|
_public_ int sd_bus_get_owner_machine_id(sd_bus *bus, const char *name, sd_id128_t *machine) {
|
2013-11-11 23:44:00 +01:00
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL, *m = NULL;
|
2013-04-23 16:18:17 +02:00
|
|
|
const char *mid;
|
|
|
|
int r;
|
|
|
|
|
2013-11-11 23:44:00 +01:00
|
|
|
assert_return(bus, -EINVAL);
|
|
|
|
assert_return(name, -EINVAL);
|
|
|
|
assert_return(machine, -EINVAL);
|
|
|
|
assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
|
|
|
|
assert_return(!bus_pid_changed(bus), -ECHILD);
|
2013-04-23 16:18:17 +02:00
|
|
|
|
|
|
|
if (streq_ptr(name, bus->unique_name))
|
|
|
|
return sd_id128_get_machine(machine);
|
|
|
|
|
2013-11-11 23:44:00 +01:00
|
|
|
r = sd_bus_message_new_method_call(
|
|
|
|
bus,
|
|
|
|
name,
|
|
|
|
"/",
|
|
|
|
"org.freedesktop.DBus.Peer",
|
|
|
|
"GetMachineId", &m);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_message_set_no_auto_start(m, true);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-04-23 16:18:17 +02:00
|
|
|
|
2013-11-11 23:44:00 +01:00
|
|
|
r = sd_bus_call(bus, m, 0, NULL, &reply);
|
2013-04-23 16:18:17 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_message_read(reply, "s", &mid);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return sd_id128_from_string(mid, machine);
|
|
|
|
}
|