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;
78 n->name_flags = flags;
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);
145 return n->name_flags;
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_DESTINATION:
389 /* The bloom filter does not include
390 the destination, since it is only
391 available for broadcast messages
392 which do not carry a destination
393 since they are undirected. */
396 case BUS_MATCH_INTERFACE:
397 bloom_add_pair(bloom, "interface", c->value_str);
401 case BUS_MATCH_MEMBER:
402 bloom_add_pair(bloom, "member", c->value_str);
407 bloom_add_pair(bloom, "path", c->value_str);
411 case BUS_MATCH_PATH_NAMESPACE:
412 bloom_add_pair(bloom, "path-slash-prefix", c->value_str);
416 case BUS_MATCH_ARG...BUS_MATCH_ARG_LAST:
417 case BUS_MATCH_ARG_PATH...BUS_MATCH_ARG_PATH_LAST:
418 case BUS_MATCH_ARG_NAMESPACE...BUS_MATCH_ARG_NAMESPACE_LAST:
419 case BUS_MATCH_MESSAGE_TYPE:
420 assert_not_reached("FIXME!");
424 case BUS_MATCH_VALUE:
426 case _BUS_MATCH_NODE_TYPE_MAX:
427 case _BUS_MATCH_NODE_TYPE_INVALID:
428 assert_not_reached("Invalid match type?");
433 sz += ALIGN8(offsetof(struct kdbus_item, data64) + BLOOM_SIZE);
443 item->size = offsetof(struct kdbus_item, data64) + BLOOM_SIZE;
444 item->type = KDBUS_MATCH_BLOOM;
445 memcpy(item->data64, bloom, BLOOM_SIZE);
447 item = KDBUS_ITEM_NEXT(item);
451 item->size = offsetof(struct kdbus_item, str) + sender_length + 1;
452 item->type = KDBUS_MATCH_SRC_NAME;
453 memcpy(item->str, sender, sender_length + 1);
456 r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m);
461 return sd_bus_call_method(
463 "org.freedesktop.DBus",
465 "org.freedesktop.DBus",
476 int bus_remove_match_internal(
486 if (bus->is_kernel) {
487 struct kdbus_cmd_match m;
490 m.size = offsetof(struct kdbus_cmd_match, items);
493 r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_REMOVE, &m);
498 return sd_bus_call_method(
500 "org.freedesktop.DBus",
502 "org.freedesktop.DBus",
513 int sd_bus_get_owner_machine_id(sd_bus *bus, const char *name, sd_id128_t *machine) {
514 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
522 if (!BUS_IS_OPEN(bus->state))
524 if (bus_pid_changed(bus))
527 if (streq_ptr(name, bus->unique_name))
528 return sd_id128_get_machine(machine);
530 r = sd_bus_call_method(bus,
533 "org.freedesktop.DBus.Peer",
542 r = sd_bus_message_read(reply, "s", &mid);
546 return sd_id128_from_string(mid, machine);