summaryrefslogtreecommitdiff
path: root/src/libsystemd-bus/bus-control.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2013-03-19 20:03:16 +0100
committerLennart Poettering <lennart@poettering.net>2013-03-20 23:00:09 +0100
commitde1c301ed165eb4d04a0c9d4babe97912b5233bb (patch)
tree7a83edfde27a959d3ee3abf2cc9c99fc6bc6bb24 /src/libsystemd-bus/bus-control.c
parent76f0199f2ce007c50b5a38f7ff6c51167c3a05a0 (diff)
bus: add basic implementation of a native bus client library
Diffstat (limited to 'src/libsystemd-bus/bus-control.c')
-rw-r--r--src/libsystemd-bus/bus-control.c322
1 files changed, 322 insertions, 0 deletions
diff --git a/src/libsystemd-bus/bus-control.c b/src/libsystemd-bus/bus-control.c
new file mode 100644
index 0000000000..e4cc251a4b
--- /dev/null
+++ b/src/libsystemd-bus/bus-control.c
@@ -0,0 +1,322 @@
+/*-*- 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/>.
+***/
+
+#include <stddef.h>
+#include <errno.h>
+
+#include "strv.h"
+
+#include "sd-bus.h"
+#include "bus-internal.h"
+#include "bus-message.h"
+
+const char *sd_bus_get_unique_name(sd_bus *bus) {
+ if (!bus)
+ return NULL;
+
+ return bus->unique_name;
+}
+
+int sd_bus_request_name(sd_bus *bus, const char *name, int flags) {
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
+ uint32_t ret;
+ int r;
+
+ if (!bus)
+ return -EINVAL;
+ if (!name)
+ return -EINVAL;
+
+ r = sd_bus_message_new_method_call(
+ bus,
+ "org.freedesktop.DBus",
+ "/",
+ "org.freedesktop.DBus",
+ "RequestName",
+ &m);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_append(m, "su", name, flags);
+ 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", &ret);
+ if (r < 0)
+ return r;
+
+ return ret;
+}
+
+int sd_bus_release_name(sd_bus *bus, const char *name) {
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
+ uint32_t ret;
+ int r;
+
+ if (!bus)
+ return -EINVAL;
+ if (!name)
+ return -EINVAL;
+
+ r = sd_bus_message_new_method_call(
+ bus,
+ "org.freedesktop.DBus",
+ "/",
+ "org.freedesktop.DBus",
+ "ReleaseName",
+ &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", &ret);
+ if (r < 0)
+ return r;
+
+ return ret;
+}
+
+int sd_bus_list_names(sd_bus *bus, char ***l) {
+ _cleanup_bus_message_unref_ sd_bus_message *m1 = NULL, *reply1 = NULL, *m2 = NULL, *reply2 = NULL;
+ _cleanup_strv_free_ char **a = NULL, **b = NULL;
+ char **x = NULL;
+ int r;
+
+ if (!bus)
+ return -EINVAL;
+ if (!l)
+ return -EINVAL;
+
+ r = sd_bus_message_new_method_call(
+ bus,
+ "org.freedesktop.DBus",
+ "/",
+ "org.freedesktop.DBus",
+ "ListNames",
+ &m1);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_new_method_call(
+ bus,
+ "org.freedesktop.DBus",
+ "/",
+ "org.freedesktop.DBus",
+ "ListActivatableNames",
+ &m2);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_send_with_reply_and_block(bus, m1, (uint64_t) -1, NULL, &reply1);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_send_with_reply_and_block(bus, m2, (uint64_t) -1, NULL, &reply2);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_read(reply1, "as", &a);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_read(reply2, "as", &b);
+ if (r < 0)
+ return r;
+
+ x = strv_merge(a, b);
+ if (!x)
+ return -ENOMEM;
+
+ *l = strv_uniq(x);
+ return 0;
+}
+
+int sd_bus_get_owner(sd_bus *bus, const char *name, char **owner) {
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
+ int r;
+
+ if (!bus)
+ return -EINVAL;
+ if (!name)
+ return -EINVAL;
+
+ r = sd_bus_message_new_method_call(
+ bus,
+ "org.freedesktop.DBus",
+ "/",
+ "org.freedesktop.DBus",
+ "GetNameOwner",
+ &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;
+
+ 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);
+}