chiark / gitweb /
bus: reenable id change subscriptions
[elogind.git] / src / libsystemd-bus / busctl.c
index 4d4f38e0a80aefe06aafff60ccdef7059514a1d4..e6c46d78a729f202ec1883b0b636c63b6fd1e291 100644 (file)
@@ -52,14 +52,20 @@ static void pager_open_if_enabled(void) {
 }
 
 static int list_bus_names(sd_bus *bus, char **argv) {
-        _cleanup_strv_free_ char **l = NULL;
+        _cleanup_strv_free_ char **acquired = NULL, **activatable = NULL;
+        _cleanup_free_ char **merged = NULL;
+        _cleanup_hashmap_free_ Hashmap *names = NULL;
         char **i;
         int r;
         size_t max_i = 0;
+        unsigned n = 0;
+        void *v;
+        char *k;
+        Iterator iterator;
 
         assert(bus);
 
-        r = sd_bus_list_names(bus, &l);
+        r = sd_bus_list_names(bus, &acquired, &activatable);
         if (r < 0) {
                 log_error("Failed to list names: %s", strerror(-r));
                 return r;
@@ -67,11 +73,37 @@ static int list_bus_names(sd_bus *bus, char **argv) {
 
         pager_open_if_enabled();
 
-        strv_sort(l);
+        names = hashmap_new(string_hash_func, string_compare_func);
+        if (!names)
+                return log_oom();
 
-        STRV_FOREACH(i, l)
+        STRV_FOREACH(i, acquired) {
                 max_i = MAX(max_i, strlen(*i));
 
+                r = hashmap_put(names, *i, INT_TO_PTR(1));
+                if (r < 0) {
+                        log_error("Failed to add to hashmap: %s", strerror(-r));
+                        return r;
+                }
+        }
+
+        STRV_FOREACH(i, activatable) {
+                max_i = MAX(max_i, strlen(*i));
+
+                r = hashmap_put(names, *i, INT_TO_PTR(2));
+                if (r < 0 && r != -EEXIST) {
+                        log_error("Failed to add to hashmap: %s", strerror(-r));
+                        return r;
+                }
+        }
+
+        merged = new(char*, hashmap_size(names) + 1);
+        HASHMAP_FOREACH_KEY(v, k, names, iterator)
+                merged[n++] = k;
+
+        merged[n] = NULL;
+        strv_sort(merged);
+
         printf("%-*s %*s %-*s %-*s %-*s",
                (int) max_i, "NAME", 10, "PID", 15, "PROCESS", 16, "USER", 20, "CONNECTION");
 
@@ -80,18 +112,31 @@ static int list_bus_names(sd_bus *bus, char **argv) {
         else
                 putchar('\n');
 
-        STRV_FOREACH(i, l) {
+        STRV_FOREACH(i, merged) {
                 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
-                _cleanup_free_ char *owner = NULL;
                 sd_id128_t mid;
 
+                if (hashmap_get(names, *i) == INT_TO_PTR(2)) {
+                        /* Activatable */
+
+                        printf("%-*s", (int) max_i, *i);
+                        printf("          - -               -                (activation)        ");
+                        if (arg_no_machine)
+                                putchar('\n');
+                        else
+                                puts(" -");
+                        continue;
+
+                }
+
                 if (arg_no_unique && (*i)[0] == ':')
                         continue;
 
                 printf("%-*s", (int) max_i, *i);
 
-                r = sd_bus_get_owner(bus, *i, SD_BUS_CREDS_UID|SD_BUS_CREDS_PID|SD_BUS_CREDS_COMM, &owner, &creds);
+                r = sd_bus_get_owner(bus, *i, SD_BUS_CREDS_UID|SD_BUS_CREDS_PID|SD_BUS_CREDS_COMM|SD_BUS_CREDS_UNIQUE_NAME, &creds);
                 if (r >= 0) {
+                        const char *unique;
                         pid_t pid;
                         uid_t uid;
 
@@ -120,8 +165,9 @@ static int list_bus_names(sd_bus *bus, char **argv) {
                         } else
                                 fputs(" -               ", stdout);
 
-                        if (owner)
-                                printf(" %-20s", owner);
+                        r = sd_bus_creds_get_unique_name(creds, &unique);
+                        if (r >= 0)
+                                printf(" %-20s", unique);
                         else
                                 fputs(" -                   ", stdout);
 
@@ -210,8 +256,33 @@ static int monitor(sd_bus *bus, char *argv[]) {
                         return r;
                 }
         }
+}
 
-        return -EINVAL;
+static int status(sd_bus *bus, char *argv[]) {
+        _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
+        pid_t pid;
+        int r;
+
+        assert(bus);
+
+        if (strv_length(argv) != 2) {
+                log_error("Expects one argument.");
+                return -EINVAL;
+        }
+
+        r = parse_pid(argv[1], &pid);
+        if (r < 0)
+                r = sd_bus_get_owner(bus, argv[1], _SD_BUS_CREDS_ALL, &creds);
+        else
+                r = sd_bus_creds_new_from_pid(pid, _SD_BUS_CREDS_ALL, &creds);
+
+        if (r < 0) {
+                log_error("Failed to get credentials: %s", strerror(-r));
+                return r;
+        }
+
+        bus_creds_dump(creds, NULL);
+        return 0;
 }
 
 static int help(void) {
@@ -227,11 +298,13 @@ static int help(void) {
                "  -M --machine=CONTAINER  Operate on local container\n"
                "     --address=ADDRESS    Connect to bus specified by address\n"
                "     --no-unique          Only show well-known names\n"
-               "     --no-machine         Don't show machine ID column in list\n\n"
-               "     --match=MATCH        Only show matching messages\n"
+               "     --no-machine         Don't show machine ID column in list\n"
+               "     --match=MATCH        Only show matching messages\n\n"
                "Commands:\n"
                "  list                    List bus names\n"
-               "  monitor [SERVICE...]    Show bus traffic\n",
+               "  monitor [SERVICE...]    Show bus traffic\n"
+               "  status ENDPOINT         Show endpoint status\n"
+               "  help                    Show this help\n",
                program_invocation_short_name);
 
         return 0;
@@ -342,6 +415,9 @@ static int busctl_main(sd_bus *bus, int argc, char *argv[]) {
         if (streq(argv[optind], "monitor"))
                 return monitor(bus, argv + optind);
 
+        if (streq(argv[optind], "status"))
+                return status(bus, argv + optind);
+
         if (streq(argv[optind], "help"))
                 return help();