chiark / gitweb /
bus: add new sd_bus_creds object to encapsulate process credentials
[elogind.git] / src / libsystemd-bus / busctl.c
index 220c1eb..24db48a 100644 (file)
 #include "sd-bus.h"
 #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;
-static bool arg_user = false;
 static bool arg_no_unique = false;
 static char **arg_matches = NULL;
+static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
+static char *arg_host = NULL;
+static bool arg_user = false;
 
 static void pager_open_if_enabled(void) {
 
@@ -67,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");
         }
 
@@ -117,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;
 
@@ -137,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) {
@@ -145,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 (;;) {
@@ -157,7 +188,7 @@ static int monitor(sd_bus *bus, char *argv[]) {
                 }
 
                 if (m) {
-                        bus_message_dump(m);
+                        bus_message_dump(m, stdout, true);
                         continue;
                 }
 
@@ -178,17 +209,19 @@ static int help(void) {
 
         printf("%s [OPTIONS...] {COMMAND} ...\n\n"
                "Introspect the bus.\n\n"
-               "  -h --help              Show this help\n"
-               "     --version           Show package version\n"
-               "     --system            Connect to system bus\n"
-               "     --user              Connect to user bus\n"
-               "     --address=ADDRESS   Connect to bus specified by address\n"
-               "     --no-unique         Only show well-known names\n"
-               "     --match=MATCH       Only show matching messages\n"
-               "     --no-pager          Do not pipe output into a pager\n\n"
+               "  -h --help               Show this help\n"
+               "     --version            Show package version\n"
+               "     --no-pager           Do not pipe output into a pager\n"
+               "     --system             Connect to system bus\n"
+               "     --user               Connect to user bus\n"
+               "  -H --host=[USER@]HOST   Operate on remote host\n"
+               "  -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"
+               "     --match=MATCH        Only show matching messages\n\n"
                "Commands:\n"
-               "  list                   List bus names\n"
-               "  monitor [SERVICE...]   Show bus traffic\n",
+               "  list                    List bus names\n"
+               "  monitor [SERVICE...]    Show bus traffic\n",
                program_invocation_short_name);
 
         return 0;
@@ -215,7 +248,9 @@ static int parse_argv(int argc, char *argv[]) {
                 { "address",   required_argument, NULL, ARG_ADDRESS   },
                 { "no-unique", no_argument,       NULL, ARG_NO_UNIQUE },
                 { "match",     required_argument, NULL, ARG_MATCH     },
-                { NULL,        0,                 NULL, 0             },
+                { "host",      required_argument, NULL, 'H'           },
+                { "machine",   required_argument, NULL, 'M'           },
+                {},
         };
 
         int c;
@@ -223,7 +258,7 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 0);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
+        while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0) {
 
                 switch (c) {
 
@@ -260,12 +295,21 @@ static int parse_argv(int argc, char *argv[]) {
                                 return log_oom();
                         break;
 
+                case 'H':
+                        arg_transport = BUS_TRANSPORT_REMOTE;
+                        arg_host = optarg;
+                        break;
+
+                case 'M':
+                        arg_transport = BUS_TRANSPORT_CONTAINER;
+                        arg_host = optarg;
+                        break;
+
                 case '?':
                         return -EINVAL;
 
                 default:
-                        log_error("Unknown option code %c", c);
-                        return -EINVAL;
+                        assert_not_reached("Unhandled option");
                 }
         }
 
@@ -320,10 +364,8 @@ int main(int argc, char *argv[]) {
                 }
 
                 r = sd_bus_start(bus);
-        } else if (arg_user)
-                r = sd_bus_open_user(&bus);
-        else
-                r = sd_bus_open_system(&bus);
+        } else
+                r = bus_open_transport(arg_transport, arg_host, arg_user, &bus);
 
         if (r < 0) {
                 log_error("Failed to connect to bus: %s", strerror(-r));
@@ -334,6 +376,7 @@ int main(int argc, char *argv[]) {
 
 finish:
         pager_close();
+
         strv_free(arg_matches);
 
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;