bus-control.c revision de1c301ed165eb4d04a0c9d4babe97912b5233bb
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering/***
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering This file is part of systemd.
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering Copyright 2013 Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering systemd is free software; you can redistribute it and/or modify it
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering under the terms of the GNU Lesser General Public License as published by
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering (at your option) any later version.
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering systemd is distributed in the hope that it will be useful, but
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering Lesser General Public License for more details.
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering You should have received a copy of the GNU Lesser General Public License
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering***/
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include <stddef.h>
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include <errno.h>
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include "strv.h"
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include "sd-bus.h"
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include "bus-internal.h"
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include "bus-message.h"
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poetteringconst char *sd_bus_get_unique_name(sd_bus *bus) {
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering if (!bus)
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering return NULL;
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering return bus->unique_name;
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering}
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poetteringint sd_bus_request_name(sd_bus *bus, const char *name, int flags) {
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering uint32_t ret;
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering int r;
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering if (!bus)
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering return -EINVAL;
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering if (!name)
ac8cfcf56c5d7b3eaeec64e94f6c4659e82a2b10Lennart Poettering return -EINVAL;
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering r = sd_bus_message_new_method_call(
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering bus,
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering "org.freedesktop.DBus",
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering "/",
b237ef2cfac7ab0b33170809e8cb64628606207dTollef Fog Heen "org.freedesktop.DBus",
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer "RequestName",
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering &m);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering if (r < 0)
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering return r;
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering r = sd_bus_message_append(m, "su", name, flags);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering if (r < 0)
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering return r;
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering if (r < 0)
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering return r;
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer r = sd_bus_message_read(reply, "u", &ret);
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer if (r < 0)
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer return r;
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer return ret;
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer}
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremerint sd_bus_release_name(sd_bus *bus, const char *name) {
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer uint32_t ret;
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer int r;
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer if (!bus)
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer return -EINVAL;
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer if (!name)
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer return -EINVAL;
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer r = sd_bus_message_new_method_call(
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer bus,
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer "org.freedesktop.DBus",
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer "/",
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer "org.freedesktop.DBus",
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer "ReleaseName",
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer &m);
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer if (r < 0)
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer return r;
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer r = sd_bus_message_append(m, "s", name);
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer if (r < 0)
8e27452380193a5f81bfd08a59aab8b07008ba0bLennart Poettering return r;
8e27452380193a5f81bfd08a59aab8b07008ba0bLennart Poettering
8e27452380193a5f81bfd08a59aab8b07008ba0bLennart Poettering r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
5fada852803f8ffc68c6f28ee2cf91808afa906fLennart Poettering if (r < 0)
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering return r;
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
f2b4af1cd4112df6ce56f8fc1e677639935e3d0eFabian Henze r = sd_bus_message_read(reply, "u", &ret);
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering if (r < 0)
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering return r;
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering return ret;
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering}
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poetteringint sd_bus_list_names(sd_bus *bus, char ***l) {
d7c114c00030309435fc56c77c8578a25d228ebeDave Reisner _cleanup_bus_message_unref_ sd_bus_message *m1 = NULL, *reply1 = NULL, *m2 = NULL, *reply2 = NULL;
f2b4af1cd4112df6ce56f8fc1e677639935e3d0eFabian Henze _cleanup_strv_free_ char **a = NULL, **b = NULL;
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering char **x = NULL;
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering int r;
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering if (!bus)
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering return -EINVAL;
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering if (!l)
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering return -EINVAL;
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering r = sd_bus_message_new_method_call(
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering bus,
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering "org.freedesktop.DBus",
2c696a96a2bd63d2ff0d5595622124ef9270b172Lennart Poettering "/",
2c696a96a2bd63d2ff0d5595622124ef9270b172Lennart Poettering "org.freedesktop.DBus",
2c696a96a2bd63d2ff0d5595622124ef9270b172Lennart Poettering "ListNames",
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering &m1);
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering if (r < 0)
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering return r;
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering
2c696a96a2bd63d2ff0d5595622124ef9270b172Lennart Poettering r = sd_bus_message_new_method_call(
2c696a96a2bd63d2ff0d5595622124ef9270b172Lennart Poettering bus,
2c696a96a2bd63d2ff0d5595622124ef9270b172Lennart Poettering "org.freedesktop.DBus",
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering "/",
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering "org.freedesktop.DBus",
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering "ListActivatableNames",
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering &m2);
2c696a96a2bd63d2ff0d5595622124ef9270b172Lennart Poettering if (r < 0)
924b4d5bcc97ee2dc395723c3b73a53f3772e2edLennart Poettering return r;
2c696a96a2bd63d2ff0d5595622124ef9270b172Lennart Poettering
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering r = sd_bus_send_with_reply_and_block(bus, m1, (uint64_t) -1, NULL, &reply1);
d7c114c00030309435fc56c77c8578a25d228ebeDave Reisner if (r < 0)
d7c114c00030309435fc56c77c8578a25d228ebeDave Reisner return r;
d7c114c00030309435fc56c77c8578a25d228ebeDave Reisner
d7c114c00030309435fc56c77c8578a25d228ebeDave Reisner r = sd_bus_send_with_reply_and_block(bus, m2, (uint64_t) -1, NULL, &reply2);
d7c114c00030309435fc56c77c8578a25d228ebeDave Reisner if (r < 0)
d7c114c00030309435fc56c77c8578a25d228ebeDave Reisner return r;
d7c114c00030309435fc56c77c8578a25d228ebeDave Reisner
f2b4af1cd4112df6ce56f8fc1e677639935e3d0eFabian Henze r = sd_bus_message_read(reply1, "as", &a);
f2b4af1cd4112df6ce56f8fc1e677639935e3d0eFabian Henze if (r < 0)
f2b4af1cd4112df6ce56f8fc1e677639935e3d0eFabian Henze return r;
f2b4af1cd4112df6ce56f8fc1e677639935e3d0eFabian Henze
f2b4af1cd4112df6ce56f8fc1e677639935e3d0eFabian Henze r = sd_bus_message_read(reply2, "as", &b);
f2b4af1cd4112df6ce56f8fc1e677639935e3d0eFabian Henze if (r < 0)
f2b4af1cd4112df6ce56f8fc1e677639935e3d0eFabian Henze return r;
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering
2c696a96a2bd63d2ff0d5595622124ef9270b172Lennart Poettering x = strv_merge(a, b);
2c696a96a2bd63d2ff0d5595622124ef9270b172Lennart Poettering if (!x)
2c696a96a2bd63d2ff0d5595622124ef9270b172Lennart Poettering return -ENOMEM;
2c696a96a2bd63d2ff0d5595622124ef9270b172Lennart Poettering
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering *l = strv_uniq(x);
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering return 0;
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering}
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poetteringint sd_bus_get_owner(sd_bus *bus, const char *name, char **owner) {
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering int r;
d64b723a9e275ee4c6ccb947e3fd6ed6b3e4c359Lennart Poettering
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering if (!bus)
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering return -EINVAL;
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering if (!name)
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering return -EINVAL;
d7c114c00030309435fc56c77c8578a25d228ebeDave Reisner
f2b4af1cd4112df6ce56f8fc1e677639935e3d0eFabian Henze r = sd_bus_message_new_method_call(
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering bus,
2c696a96a2bd63d2ff0d5595622124ef9270b172Lennart Poettering "org.freedesktop.DBus",
2c696a96a2bd63d2ff0d5595622124ef9270b172Lennart Poettering "/",
2c696a96a2bd63d2ff0d5595622124ef9270b172Lennart Poettering "org.freedesktop.DBus",
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering "GetNameOwner",
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering &m);
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering if (r < 0)
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering return r;
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering
2c696a96a2bd63d2ff0d5595622124ef9270b172Lennart Poettering r = sd_bus_message_append(m, "s", name);
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering if (r < 0)
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering return r;
2c696a96a2bd63d2ff0d5595622124ef9270b172Lennart Poettering
2c696a96a2bd63d2ff0d5595622124ef9270b172Lennart Poettering r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer if (r < 0)
0571e0111d76cf96aa4069d9c7a6e24d97aa7e48Lennart Poettering return r;
return sd_bus_message_read(reply, "s", owner);
}
int sd_bus_get_owner_uid(sd_bus *bus, const char *name, uid_t *uid) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
uint32_t u;
int r;
if (!bus)
return -EINVAL;
if (!name)
return -EINVAL;
if (!uid)
return -EINVAL;
r = sd_bus_message_new_method_call(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"GetConnectionUnixUser",
&m);
if (r < 0)
return r;
r = sd_bus_message_append(m, "s", name);
if (r < 0)
return r;
r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
if (r < 0)
return r;
r = sd_bus_message_read(reply, "u", &u);
if (r < 0)
return r;
*uid = (uid_t) u;
return 0;
}
int sd_bus_get_owner_pid(sd_bus *bus, const char *name, pid_t *pid) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
uint32_t u;
int r;
if (!bus)
return -EINVAL;
if (!name)
return -EINVAL;
if (!pid)
return -EINVAL;
r = sd_bus_message_new_method_call(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"GetConnectionUnixUser",
&m);
if (r < 0)
return r;
r = sd_bus_message_append(m, "s", name);
if (r < 0)
return r;
r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
if (r < 0)
return r;
r = sd_bus_message_read(reply, "u", &u);
if (r < 0)
return r;
if (u == 0)
return -EIO;
*pid = (uid_t) u;
return 0;
}
int sd_bus_add_match(sd_bus *bus, const char *match) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
int r;
if (!bus)
return -EINVAL;
if (!match)
return -EINVAL;
r = sd_bus_message_new_method_call(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"AddMatch",
&m);
if (r < 0)
return r;
r = sd_bus_message_append(m, "s", match);
if (r < 0)
return r;
return sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
}
int sd_bus_remove_match(sd_bus *bus, const char *match) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
int r;
if (!bus)
return -EINVAL;
if (!match)
return -EINVAL;
r = sd_bus_message_new_method_call(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"RemoveMatch",
&m);
if (r < 0)
return r;
r = sd_bus_message_append(m, "s", match);
if (r < 0)
return r;
return sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
}