1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #ifdef HAVE_VALGRIND_MEMCHECK_H
23 #include <valgrind/memcheck.h>
32 #include "bus-internal.h"
33 #include "bus-message.h"
34 #include "bus-control.h"
35 #include "bus-bloom.h"
37 int sd_bus_get_unique_name(sd_bus *bus, const char **unique) {
44 if (bus_pid_changed(bus))
47 r = bus_ensure_running(bus);
51 *unique = bus->unique_name;
55 int sd_bus_request_name(sd_bus *bus, const char *name, int flags) {
56 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
66 if (!BUS_IS_OPEN(bus->state))
68 if (bus_pid_changed(bus))
72 struct kdbus_cmd_name *n;
76 n = alloca0(offsetof(struct kdbus_cmd_name, name) + l + 1);
77 n->size = offsetof(struct kdbus_cmd_name, name) + l + 1;
79 memcpy(n->name, name, l+1);
81 #ifdef HAVE_VALGRIND_MEMCHECK_H
82 VALGRIND_MAKE_MEM_DEFINED(n, n->size);
85 r = ioctl(bus->input_fd, KDBUS_CMD_NAME_ACQUIRE, n);
91 r = sd_bus_call_method(
93 "org.freedesktop.DBus",
95 "org.freedesktop.DBus",
105 r = sd_bus_message_read(reply, "u", &ret);
113 int sd_bus_release_name(sd_bus *bus, const char *name) {
114 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
122 if (!bus->bus_client)
124 if (!BUS_IS_OPEN(bus->state))
126 if (bus_pid_changed(bus))
129 if (bus->is_kernel) {
130 struct kdbus_cmd_name *n;
134 n = alloca0(offsetof(struct kdbus_cmd_name, name) + l + 1);
135 n->size = offsetof(struct kdbus_cmd_name, name) + l + 1;
136 memcpy(n->name, name, l+1);
138 #ifdef HAVE_VALGRIND_MEMCHECK_H
139 VALGRIND_MAKE_MEM_DEFINED(n, n->size);
141 r = ioctl(bus->input_fd, KDBUS_CMD_NAME_RELEASE, n);
147 r = sd_bus_call_method(
149 "org.freedesktop.DBus",
151 "org.freedesktop.DBus",
160 r = sd_bus_message_read(reply, "u", &ret);
168 int sd_bus_list_names(sd_bus *bus, char ***l) {
169 _cleanup_bus_message_unref_ sd_bus_message *reply1 = NULL, *reply2 = NULL;
177 if (!BUS_IS_OPEN(bus->state))
179 if (bus_pid_changed(bus))
182 r = sd_bus_call_method(
184 "org.freedesktop.DBus",
186 "org.freedesktop.DBus",
194 r = sd_bus_call_method(
196 "org.freedesktop.DBus",
198 "org.freedesktop.DBus",
199 "ListActivatableNames",
206 r = bus_message_read_strv_extend(reply1, &x);
212 r = bus_message_read_strv_extend(reply2, &x);
222 int sd_bus_get_owner(sd_bus *bus, const char *name, char **owner) {
223 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
231 if (!BUS_IS_OPEN(bus->state))
233 if (bus_pid_changed(bus))
236 r = sd_bus_call_method(
238 "org.freedesktop.DBus",
240 "org.freedesktop.DBus",
249 r = sd_bus_message_read(reply, "s", &found);
266 int sd_bus_get_owner_uid(sd_bus *bus, const char *name, uid_t *uid) {
267 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
277 if (!BUS_IS_OPEN(bus->state))
279 if (bus_pid_changed(bus))
282 r = sd_bus_call_method(
284 "org.freedesktop.DBus",
286 "org.freedesktop.DBus",
287 "GetConnectionUnixUser",
295 r = sd_bus_message_read(reply, "u", &u);
303 int sd_bus_get_owner_pid(sd_bus *bus, const char *name, pid_t *pid) {
304 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
314 if (!BUS_IS_OPEN(bus->state))
316 if (bus_pid_changed(bus))
319 r = sd_bus_call_method(
321 "org.freedesktop.DBus",
323 "org.freedesktop.DBus",
324 "GetConnectionUnixProcessID",
332 r = sd_bus_message_read(reply, "u", &u);
343 int bus_add_match_internal(
346 struct bus_match_component *components,
347 unsigned n_components,
355 if (bus->is_kernel) {
356 struct kdbus_cmd_match *m;
357 struct kdbus_item *item;
358 uint64_t bloom[BLOOM_SIZE/8];
360 const char *sender = NULL;
361 size_t sender_length = 0;
362 uint64_t src_id = KDBUS_MATCH_SRC_ID_ANY;
363 bool using_bloom = false;
368 sz = offsetof(struct kdbus_cmd_match, items);
370 for (i = 0; i < n_components; i++) {
371 struct bus_match_component *c = &components[i];
375 case BUS_MATCH_SENDER:
376 r = bus_kernel_parse_unique_name(c->value_str, &src_id);
381 sender = c->value_str;
382 sender_length = strlen(sender);
383 sz += ALIGN8(offsetof(struct kdbus_item, str) + sender_length + 1);
388 case BUS_MATCH_MESSAGE_TYPE:
389 bloom_add_pair(bloom, "message-type", bus_message_type_to_string(c->value_u8));
393 case BUS_MATCH_INTERFACE:
394 bloom_add_pair(bloom, "interface", c->value_str);
398 case BUS_MATCH_MEMBER:
399 bloom_add_pair(bloom, "member", c->value_str);
404 bloom_add_pair(bloom, "path", c->value_str);
408 case BUS_MATCH_PATH_NAMESPACE:
409 if (!streq(c->value_str, "/")) {
410 bloom_add_pair(bloom, "path-slash-prefix", c->value_str);
415 case BUS_MATCH_ARG...BUS_MATCH_ARG_LAST: {
416 char buf[sizeof("arg")-1 + 2 + 1];
418 snprintf(buf, sizeof(buf), "arg%u", c->type - BUS_MATCH_ARG);
419 bloom_add_pair(bloom, buf, c->value_str);
424 case BUS_MATCH_ARG_PATH...BUS_MATCH_ARG_PATH_LAST: {
425 char buf[sizeof("arg")-1 + 2 + sizeof("-slash-prefix")];
427 snprintf(buf, sizeof(buf), "arg%u-slash-prefix", c->type - BUS_MATCH_ARG_PATH);
428 bloom_add_pair(bloom, buf, c->value_str);
433 case BUS_MATCH_ARG_NAMESPACE...BUS_MATCH_ARG_NAMESPACE_LAST: {
434 char buf[sizeof("arg")-1 + 2 + sizeof("-dot-prefix")];
436 snprintf(buf, sizeof(buf), "arg%u-dot-prefix", c->type - BUS_MATCH_ARG_NAMESPACE);
437 bloom_add_pair(bloom, buf, c->value_str);
442 case BUS_MATCH_DESTINATION:
443 /* The bloom filter does not include
444 the destination, since it is only
445 available for broadcast messages
446 which do not carry a destination
447 since they are undirected. */
451 case BUS_MATCH_VALUE:
453 case _BUS_MATCH_NODE_TYPE_MAX:
454 case _BUS_MATCH_NODE_TYPE_INVALID:
455 assert_not_reached("Invalid match type?");
460 sz += ALIGN8(offsetof(struct kdbus_item, data64) + BLOOM_SIZE);
470 item->size = offsetof(struct kdbus_item, data64) + BLOOM_SIZE;
471 item->type = KDBUS_MATCH_BLOOM;
472 memcpy(item->data64, bloom, BLOOM_SIZE);
474 item = KDBUS_ITEM_NEXT(item);
478 item->size = offsetof(struct kdbus_item, str) + sender_length + 1;
479 item->type = KDBUS_MATCH_SRC_NAME;
480 memcpy(item->str, sender, sender_length + 1);
483 r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m);
488 return sd_bus_call_method(
490 "org.freedesktop.DBus",
492 "org.freedesktop.DBus",
503 int bus_remove_match_internal(
513 if (bus->is_kernel) {
514 struct kdbus_cmd_match m;
517 m.size = offsetof(struct kdbus_cmd_match, items);
520 r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_REMOVE, &m);
525 return sd_bus_call_method(
527 "org.freedesktop.DBus",
529 "org.freedesktop.DBus",
540 int sd_bus_get_owner_machine_id(sd_bus *bus, const char *name, sd_id128_t *machine) {
541 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
549 if (!BUS_IS_OPEN(bus->state))
551 if (bus_pid_changed(bus))
554 if (streq_ptr(name, bus->unique_name))
555 return sd_id128_get_machine(machine);
557 r = sd_bus_call_method(bus,
560 "org.freedesktop.DBus.Peer",
569 r = sd_bus_message_read(reply, "s", &mid);
573 return sd_id128_from_string(mid, machine);