chiark / gitweb /
bus: add new sd_bus_creds object to encapsulate process credentials
[elogind.git] / src / libsystemd-bus / busctl.c
index 088111c..24db48a 100644 (file)
@@ -31,6 +31,7 @@
 #include "bus-message.h"
 #include "bus-internal.h"
 #include "bus-util.h"
+#include "bus-dump.h"
 
 static bool arg_no_pager = false;
 static char *arg_address = NULL;
@@ -70,49 +71,63 @@ static int list_bus_names(sd_bus *bus, char **argv) {
         STRV_FOREACH(i, l)
                 max_i = MAX(max_i, strlen(*i));
 
-        printf("%-*s %*s %-*s %-*s CONNECTION\n",
-               (int) max_i, "NAME", 10, "PID", 15, "PROCESS", 16, "USER");
+        printf("%-*s %*s %-*s %-*s %-*s MACHINE\n",
+               (int) max_i, "NAME", 10, "PID", 15, "PROCESS", 16, "USER", 20, "CONNECTION");
 
         STRV_FOREACH(i, l) {
+                _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
                 _cleanup_free_ char *owner = NULL;
-                pid_t pid;
-                uid_t uid;
+                sd_id128_t mid;
 
                 if (arg_no_unique && (*i)[0] == ':')
                         continue;
 
                 printf("%-*s", (int) max_i, *i);
 
-                r = sd_bus_get_owner_pid(bus, *i, &pid);
+                r = sd_bus_get_owner_creds(bus, *i, SD_BUS_CREDS_UID|SD_BUS_CREDS_PID|SD_BUS_CREDS_COMM, &creds);
                 if (r >= 0) {
-                        _cleanup_free_ char *comm = NULL;
+                        pid_t pid;
+                        uid_t uid;
 
-                        printf(" %10lu", (unsigned long) pid);
+                        r = sd_bus_creds_get_pid(creds, &pid);
+                        if (r >= 0) {
+                                const char *comm = NULL;
 
-                        get_process_comm(pid, &comm);
-                        printf(" %-15s", strna(comm));
-                } else
-                        printf("          - -              ");
+                                sd_bus_creds_get_comm(creds, &comm);
 
-                r = sd_bus_get_owner_uid(bus, *i, &uid);
-                if (r >= 0) {
-                        _cleanup_free_ char *u = NULL;
+                                printf(" %10lu %-15s", (unsigned long) pid, strna(comm));
+                        } else
+                                printf("          - -              ");
 
-                        u = uid_to_name(uid);
-                        if (!u)
-                                return log_oom();
+                        r = sd_bus_creds_get_uid(creds, &uid);
+                        if (r >= 0) {
+                                _cleanup_free_ char *u = NULL;
+
+                                u = uid_to_name(uid);
+                                if (!u)
+                                        return log_oom();
 
-                        if (strlen(u) > 16)
-                                u[16] = 0;
+                                if (strlen(u) > 16)
+                                        u[16] = 0;
 
-                        printf(" %-16s", u);
+                                printf(" %-16s", u);
+                        } else
+                                printf(" -               ");
                 } else
-                        printf(" -               ");
+                        printf("          - -               -               ");
+
 
                 r = sd_bus_get_owner(bus, *i, &owner);
                 if (r >= 0)
-                        printf(" %s\n", owner);
+                        printf(" %-20s", owner);
                 else
+                        printf(" -                   ");
+
+                r = sd_bus_get_owner_machine_id(bus, *i, &mid);
+                if (r >= 0) {
+                        char m[SD_ID128_STRING_MAX];
+                        printf(" %s\n", sd_id128_to_string(mid, m));
+                } else
                         printf(" -\n");
         }
 
@@ -120,6 +135,7 @@ static int list_bus_names(sd_bus *bus, char **argv) {
 }
 
 static int monitor(sd_bus *bus, char *argv[]) {
+        bool added_something = false;
         char **i;
         int r;
 
@@ -140,6 +156,8 @@ static int monitor(sd_bus *bus, char *argv[]) {
                         log_error("Failed to add match: %s", strerror(-r));
                         return r;
                 }
+
+                added_something = true;
         }
 
         STRV_FOREACH(i, arg_matches) {
@@ -148,6 +166,16 @@ static int monitor(sd_bus *bus, char *argv[]) {
                         log_error("Failed to add match: %s", strerror(-r));
                         return r;
                 }
+
+                added_something = true;
+        }
+
+        if (!added_something) {
+                r = sd_bus_add_match(bus, "", NULL, NULL);
+                if (r < 0) {
+                        log_error("Failed to add match: %s", strerror(-r));
+                        return r;
+                }
         }
 
         for (;;) {