summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2014-11-28 15:59:05 +0100
committerLennart Poettering <lennart@poettering.net>2014-11-28 16:17:33 +0100
commit3acc1dafd14420872f7f13319260eeb5d62c2533 (patch)
tree376044a15fe091db6e6b712a462b7dfe2c91c582
parent52cfc0379a9d63f99cdb3d9f63c839bbc8889b4c (diff)
sd-bus: add new call sd_bus_get_scope() for querying whether one is connected to a system or a user bus
-rw-r--r--src/libsystemd/sd-bus/bus-kernel.c36
-rw-r--r--src/libsystemd/sd-bus/bus-kernel.h2
-rw-r--r--src/libsystemd/sd-bus/busctl.c16
-rw-r--r--src/libsystemd/sd-bus/sd-bus.c42
-rw-r--r--src/libsystemd/sd-bus/test-bus-kernel.c5
-rw-r--r--src/systemd/sd-bus.h1
6 files changed, 98 insertions, 4 deletions
diff --git a/src/libsystemd/sd-bus/bus-kernel.c b/src/libsystemd/sd-bus/bus-kernel.c
index c86ed6ae42..58f011ff86 100644
--- a/src/libsystemd/sd-bus/bus-kernel.c
+++ b/src/libsystemd/sd-bus/bus-kernel.c
@@ -1845,3 +1845,39 @@ int bus_kernel_fix_attach_mask(void) {
return 0;
}
+
+int bus_kernel_get_bus_name(sd_bus *bus, char **name) {
+ struct kdbus_cmd_info cmd = {
+ .size = sizeof(struct kdbus_cmd_info),
+ };
+ struct kdbus_info *info;
+ struct kdbus_item *item;
+ char *n = NULL;
+ int r;
+
+ assert(bus);
+ assert(name);
+ assert(bus->is_kernel);
+
+ r = ioctl(bus->input_fd, KDBUS_CMD_BUS_CREATOR_INFO, &cmd);
+ if (r < 0)
+ return -errno;
+
+ info = (struct kdbus_info*) ((uint8_t*) bus->kdbus_buffer + cmd.offset);
+
+ KDBUS_ITEM_FOREACH(item, info, items)
+ if (item->type == KDBUS_ITEM_MAKE_NAME) {
+ r = free_and_strdup(&n, item->str);
+ break;
+ }
+
+ bus_kernel_cmd_free(bus, cmd.offset);
+
+ if (r < 0)
+ return r;
+ if (!n)
+ return -EIO;
+
+ *name = n;
+ return 0;
+}
diff --git a/src/libsystemd/sd-bus/bus-kernel.h b/src/libsystemd/sd-bus/bus-kernel.h
index 0d406fbc81..2152f62d12 100644
--- a/src/libsystemd/sd-bus/bus-kernel.h
+++ b/src/libsystemd/sd-bus/bus-kernel.h
@@ -92,4 +92,6 @@ int bus_kernel_realize_attach_flags(sd_bus *bus);
int bus_kernel_fix_attach_mask(void);
+int bus_kernel_get_bus_name(sd_bus *bus, char **name);
+
int bus_kernel_cmd_free(sd_bus *bus, uint64_t offset);
diff --git a/src/libsystemd/sd-bus/busctl.c b/src/libsystemd/sd-bus/busctl.c
index b4a91dfed3..febfc98a59 100644
--- a/src/libsystemd/sd-bus/busctl.c
+++ b/src/libsystemd/sd-bus/busctl.c
@@ -1193,11 +1193,18 @@ static int status(sd_bus *bus, char *argv[]) {
&creds,
pid,
_SD_BUS_CREDS_ALL);
- } else
+ } else {
+ const char *scope;
+
+ r = sd_bus_get_scope(bus, &scope);
+ if (r >= 0)
+ printf("Scope=%s%s%s\n", ansi_highlight(), scope, ansi_highlight_off());
+
r = sd_bus_get_owner_creds(
bus,
(arg_augment_creds ? SD_BUS_CREDS_AUGMENT : 0) | _SD_BUS_CREDS_ALL,
&creds);
+ }
if (r < 0) {
log_error_errno(r, "Failed to get credentials: %m");
@@ -2006,10 +2013,13 @@ int main(int argc, char *argv[]) {
switch (arg_transport) {
case BUS_TRANSPORT_LOCAL:
- if (arg_user)
+ if (arg_user) {
+ bus->is_user = true;
r = bus_set_address_user(bus);
- else
+ } else {
+ bus->is_system = true;
r = bus_set_address_system(bus);
+ }
break;
case BUS_TRANSPORT_REMOTE:
diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c
index 8f97a582d7..9ce6bfab8f 100644
--- a/src/libsystemd/sd-bus/sd-bus.c
+++ b/src/libsystemd/sd-bus/sd-bus.c
@@ -1283,6 +1283,7 @@ _public_ int sd_bus_open_system_remote(sd_bus **ret, const char *host) {
bus->bus_client = true;
bus->trusted = false;
+ bus->is_system = true;
r = sd_bus_start(bus);
if (r < 0)
@@ -1335,6 +1336,7 @@ _public_ int sd_bus_open_system_container(sd_bus **ret, const char *machine) {
bus->bus_client = true;
bus->trusted = false;
+ bus->is_system = true;
r = sd_bus_start(bus);
if (r < 0)
@@ -3376,3 +3378,43 @@ int bus_get_root_path(sd_bus *bus) {
return r;
}
+
+_public_ int sd_bus_get_scope(sd_bus *bus, const char **scope) {
+ int r;
+
+ assert_return(bus, -EINVAL);
+ assert_return(scope, -EINVAL);
+ assert_return(!bus_pid_changed(bus), -ECHILD);
+
+ if (bus->is_kernel) {
+ _cleanup_free_ char *n = NULL;
+ const char *dash;
+
+ r = bus_kernel_get_bus_name(bus, &n);
+ if (r < 0)
+ return r;
+
+ if (streq(n, "0-system")) {
+ *scope = "system";
+ return 1;
+ }
+
+ dash = strchr(n, '-');
+ if (streq(dash, "-user")) {
+ *scope = "user";
+ return 1;
+ }
+ }
+
+ if (bus->is_user) {
+ *scope = "user";
+ return 1;
+ }
+
+ if (bus->is_system) {
+ *scope = "system";
+ return 1;
+ }
+
+ return -ENODATA;
+}
diff --git a/src/libsystemd/sd-bus/test-bus-kernel.c b/src/libsystemd/sd-bus/test-bus-kernel.c
index 485c396bdb..3aec568229 100644
--- a/src/libsystemd/sd-bus/test-bus-kernel.c
+++ b/src/libsystemd/sd-bus/test-bus-kernel.c
@@ -33,7 +33,7 @@
int main(int argc, char *argv[]) {
_cleanup_close_ int bus_ref = -1;
- _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL;
+ _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL, *bname = NULL;
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
const char *ua = NULL, *ub = NULL, *the_string = NULL;
@@ -102,6 +102,9 @@ int main(int argc, char *argv[]) {
assert_se(r >= 0);
printf("name of b: %s\n", nn);
+ assert_se(bus_kernel_get_bus_name(b, &bname) >= 0);
+ assert_se(endswith(bname, name));
+
r = sd_bus_call_method(a, "this.doesnt.exist", "/foo", "meh.mah", "muh", &error, NULL, "s", "yayayay");
assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_SERVICE_UNKNOWN));
assert_se(r == -EHOSTUNREACH);
diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h
index 47fc8df1d0..b245833672 100644
--- a/src/systemd/sd-bus.h
+++ b/src/systemd/sd-bus.h
@@ -140,6 +140,7 @@ int sd_bus_is_open(sd_bus *bus);
int sd_bus_can_send(sd_bus *bus, char type);
int sd_bus_get_owner_id(sd_bus *bus, sd_id128_t *id);
int sd_bus_get_owner_creds(sd_bus *bus, uint64_t creds_mask, sd_bus_creds **ret);
+int sd_bus_get_scope(sd_bus *bus, const char **scope);
int sd_bus_get_description(sd_bus *bus, const char **description);
int sd_bus_get_tid(sd_bus *bus, pid_t *tid);