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"
36 int sd_bus_get_unique_name(sd_bus *bus, const char **unique) {
44 r = bus_ensure_running(bus);
48 *unique = bus->unique_name;
52 int sd_bus_request_name(sd_bus *bus, const char *name, int flags) {
53 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
65 struct kdbus_cmd_name *n;
69 n = alloca0(offsetof(struct kdbus_cmd_name, name) + l + 1);
70 n->size = offsetof(struct kdbus_cmd_name, name) + l + 1;
71 n->name_flags = flags;
72 memcpy(n->name, name, l+1);
74 #ifdef HAVE_VALGRIND_MEMCHECK_H
75 VALGRIND_MAKE_MEM_DEFINED(n, n->size);
78 r = ioctl(bus->input_fd, KDBUS_CMD_NAME_ACQUIRE, n);
84 r = sd_bus_call_method(
86 "org.freedesktop.DBus",
88 "org.freedesktop.DBus",
98 r = sd_bus_message_read(reply, "u", &ret);
106 int sd_bus_release_name(sd_bus *bus, const char *name) {
107 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
115 if (!bus->bus_client)
118 if (bus->is_kernel) {
119 struct kdbus_cmd_name *n;
123 n = alloca0(offsetof(struct kdbus_cmd_name, name) + l + 1);
124 n->size = offsetof(struct kdbus_cmd_name, name) + l + 1;
125 memcpy(n->name, name, l+1);
127 #ifdef HAVE_VALGRIND_MEMCHECK_H
128 VALGRIND_MAKE_MEM_DEFINED(n, n->size);
130 r = ioctl(bus->input_fd, KDBUS_CMD_NAME_RELEASE, n);
134 return n->name_flags;
136 r = sd_bus_call_method(
138 "org.freedesktop.DBus",
140 "org.freedesktop.DBus",
149 r = sd_bus_message_read(reply, "u", &ret);
157 int sd_bus_list_names(sd_bus *bus, char ***l) {
158 _cleanup_bus_message_unref_ sd_bus_message *reply1 = NULL, *reply2 = NULL;
167 r = sd_bus_call_method(
169 "org.freedesktop.DBus",
171 "org.freedesktop.DBus",
179 r = sd_bus_call_method(
181 "org.freedesktop.DBus",
183 "org.freedesktop.DBus",
184 "ListActivatableNames",
191 r = bus_message_read_strv_extend(reply1, &x);
197 r = bus_message_read_strv_extend(reply2, &x);
207 int sd_bus_get_owner(sd_bus *bus, const char *name, char **owner) {
208 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
217 r = sd_bus_call_method(
219 "org.freedesktop.DBus",
221 "org.freedesktop.DBus",
230 r = sd_bus_message_read(reply, "s", &found);
247 int sd_bus_get_owner_uid(sd_bus *bus, const char *name, uid_t *uid) {
248 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
259 r = sd_bus_call_method(
261 "org.freedesktop.DBus",
263 "org.freedesktop.DBus",
264 "GetConnectionUnixUser",
272 r = sd_bus_message_read(reply, "u", &u);
280 int sd_bus_get_owner_pid(sd_bus *bus, const char *name, pid_t *pid) {
281 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
292 r = sd_bus_call_method(
294 "org.freedesktop.DBus",
296 "org.freedesktop.DBus",
297 "GetConnectionUnixProcessID",
305 r = sd_bus_message_read(reply, "u", &u);
316 int bus_add_match_internal(sd_bus *bus, const char *match) {
320 return sd_bus_call_method(
322 "org.freedesktop.DBus",
324 "org.freedesktop.DBus",
332 int bus_remove_match_internal(sd_bus *bus, const char *match) {
336 return sd_bus_call_method(
338 "org.freedesktop.DBus",
340 "org.freedesktop.DBus",
348 int sd_bus_get_owner_machine_id(sd_bus *bus, const char *name, sd_id128_t *machine) {
349 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
358 if (streq_ptr(name, bus->unique_name))
359 return sd_id128_get_machine(machine);
361 r = sd_bus_call_method(bus,
364 "org.freedesktop.DBus.Peer",
373 r = sd_bus_message_read(reply, "s", &mid);
377 return sd_id128_from_string(mid, machine);