chiark / gitweb /
sd-bus: add new call sd_bus_get_scope() for querying whether one is connected to...
authorLennart Poettering <lennart@poettering.net>
Fri, 28 Nov 2014 14:59:05 +0000 (15:59 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 28 Nov 2014 15:17:33 +0000 (16:17 +0100)
src/libsystemd/sd-bus/bus-kernel.c
src/libsystemd/sd-bus/bus-kernel.h
src/libsystemd/sd-bus/busctl.c
src/libsystemd/sd-bus/sd-bus.c
src/libsystemd/sd-bus/test-bus-kernel.c
src/systemd/sd-bus.h

index c86ed6a..58f011f 100644 (file)
@@ -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;
+}
index 0d406fb..2152f62 100644 (file)
@@ -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);
index b4a91df..febfc98 100644 (file)
@@ -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:
index 8f97a58..9ce6bfa 100644 (file)
@@ -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;
+}
index 485c396..3aec568 100644 (file)
@@ -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);
index 47fc8df..b245833 100644 (file)
@@ -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);