chiark / gitweb /
sd-bus: make sure %m resolves to the specified error in bus_error_set_errnofv()
[elogind.git] / src / libsystemd / sd-bus / busctl.c
index b2c7be20ae58fd2b3a6c2d05e7ad3e55967afefe..febfc98a59f9834b55101669ead109b6360fb7d2 100644 (file)
@@ -56,6 +56,7 @@ static bool arg_verbose = false;
 static bool arg_expect_reply = true;
 static bool arg_auto_start = true;
 static bool arg_allow_interactive_authorization = true;
+static bool arg_augment_creds = true;
 static usec_t arg_timeout = 0;
 
 static void pager_open_if_enabled(void) {
@@ -89,7 +90,7 @@ static int list_bus_names(sd_bus *bus, char **argv) {
 
         r = sd_bus_list_names(bus, (arg_acquired || arg_unique) ? &acquired : NULL, arg_activatable ? &activatable : NULL);
         if (r < 0) {
-                log_error("Failed to list names: %s", strerror(-r));
+                log_error_errno(r, "Failed to list names: %m");
                 return r;
         }
 
@@ -104,7 +105,7 @@ static int list_bus_names(sd_bus *bus, char **argv) {
 
                 r = hashmap_put(names, *i, NAME_IS_ACQUIRED);
                 if (r < 0) {
-                        log_error("Failed to add to hashmap: %s", strerror(-r));
+                        log_error_errno(r, "Failed to add to hashmap: %m");
                         return r;
                 }
         }
@@ -114,7 +115,7 @@ static int list_bus_names(sd_bus *bus, char **argv) {
 
                 r = hashmap_put(names, *i, NAME_IS_ACTIVATABLE);
                 if (r < 0 && r != -EEXIST) {
-                        log_error("Failed to add to hashmap: %s", strerror(-r));
+                        log_error_errno(r, "Failed to add to hashmap: %m");
                         return r;
                 }
         }
@@ -161,10 +162,12 @@ static int list_bus_names(sd_bus *bus, char **argv) {
 
                 printf("%-*s", (int) max_i, *i);
 
-                r = sd_bus_get_name_creds(bus, *i,
-                                     SD_BUS_CREDS_UID|SD_BUS_CREDS_PID|SD_BUS_CREDS_COMM|
-                                     SD_BUS_CREDS_UNIQUE_NAME|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_SESSION|
-                                     SD_BUS_CREDS_DESCRIPTION, &creds);
+                r = sd_bus_get_name_creds(
+                                bus, *i,
+                                (arg_augment_creds ? SD_BUS_CREDS_AUGMENT : 0) |
+                                SD_BUS_CREDS_UID|SD_BUS_CREDS_PID|SD_BUS_CREDS_COMM|
+                                SD_BUS_CREDS_UNIQUE_NAME|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_SESSION|
+                                SD_BUS_CREDS_DESCRIPTION, &creds);
                 if (r >= 0) {
                         const char *unique, *session, *unit, *cn;
                         pid_t pid;
@@ -436,7 +439,7 @@ static int tree(sd_bus *bus, char **argv) {
 
                 r = sd_bus_list_names(bus, &names, NULL);
                 if (r < 0) {
-                        log_error("Failed to get name list: %s", strerror(-r));
+                        log_error_errno(r, "Failed to get name list: %m");
                         return r;
                 }
 
@@ -1093,7 +1096,7 @@ static int monitor(sd_bus *bus, char *argv[], int (*dump)(sd_bus_message *m, FIL
 
                 r = sd_bus_add_match(bus, NULL, m, NULL, NULL);
                 if (r < 0) {
-                        log_error("Failed to add match: %s", strerror(-r));
+                        log_error_errno(r, "Failed to add match: %m");
                         return r;
                 }
 
@@ -1103,7 +1106,7 @@ static int monitor(sd_bus *bus, char *argv[], int (*dump)(sd_bus_message *m, FIL
         STRV_FOREACH(i, arg_matches) {
                 r = sd_bus_add_match(bus, NULL, *i, NULL, NULL);
                 if (r < 0) {
-                        log_error("Failed to add match: %s", strerror(-r));
+                        log_error_errno(r, "Failed to add match: %m");
                         return r;
                 }
 
@@ -1113,7 +1116,7 @@ static int monitor(sd_bus *bus, char *argv[], int (*dump)(sd_bus_message *m, FIL
         if (!added_something) {
                 r = sd_bus_add_match(bus, NULL, "", NULL, NULL);
                 if (r < 0) {
-                        log_error("Failed to add match: %s", strerror(-r));
+                        log_error_errno(r, "Failed to add match: %m");
                         return r;
                 }
         }
@@ -1123,7 +1126,7 @@ static int monitor(sd_bus *bus, char *argv[], int (*dump)(sd_bus_message *m, FIL
 
                 r = sd_bus_process(bus, &m);
                 if (r < 0) {
-                        log_error("Failed to process bus: %s", strerror(-r));
+                        log_error_errno(r, "Failed to process bus: %m");
                         return r;
                 }
 
@@ -1137,7 +1140,7 @@ static int monitor(sd_bus *bus, char *argv[], int (*dump)(sd_bus_message *m, FIL
 
                 r = sd_bus_wait(bus, (uint64_t) -1);
                 if (r < 0) {
-                        log_error("Failed to wait for bus: %s", strerror(-r));
+                        log_error_errno(r, "Failed to wait for bus: %m");
                         return r;
                 }
         }
@@ -1172,19 +1175,39 @@ static int status(sd_bus *bus, char *argv[]) {
 
         assert(bus);
 
-        if (strv_length(argv) != 2) {
-                log_error("Expects one argument.");
+        if (strv_length(argv) > 2) {
+                log_error("Expects no or one argument.");
                 return -EINVAL;
         }
 
-        r = parse_pid(argv[1], &pid);
-        if (r < 0)
-                r = sd_bus_get_name_creds(bus, argv[1], _SD_BUS_CREDS_ALL, &creds);
-        else
-                r = sd_bus_creds_new_from_pid(&creds, pid, _SD_BUS_CREDS_ALL);
+        if (argv[1]) {
+                r = parse_pid(argv[1], &pid);
+                if (r < 0)
+                        r = sd_bus_get_name_creds(
+                                        bus,
+                                        argv[1],
+                                        (arg_augment_creds ? SD_BUS_CREDS_AUGMENT : 0) | _SD_BUS_CREDS_ALL,
+                                        &creds);
+                else
+                        r = sd_bus_creds_new_from_pid(
+                                        &creds,
+                                        pid,
+                                        _SD_BUS_CREDS_ALL);
+        } 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("Failed to get credentials: %s", strerror(-r));
+                log_error_errno(r, "Failed to get credentials: %m");
                 return r;
         }
 
@@ -1664,10 +1687,11 @@ static int help(void) {
                "     --auto-start=BOOL    Auto-start destination service\n"
                "     --allow-interactive-authorization=BOOL\n"
                "                          Allow interactive authorization for operation\n"
-               "     --timeout=SECS       Maximum time to wait for method call completion\n\n"
+               "     --timeout=SECS       Maximum time to wait for method call completion\n"
+               "     --augment-creds=BOOL Extend credential data with data read from /proc/$PID\n\n"
                "Commands:\n"
                "  list                    List bus names\n"
-               "  status SERVICE          Show service name status\n"
+               "  status [SERVICE]        Show bus service, process or bus owner credentials\n"
                "  monitor [SERVICE...]    Show bus traffic\n"
                "  capture [SERVICE...]    Capture bus traffic as pcap\n"
                "  tree [SERVICE...]       Show object tree of service\n"
@@ -1705,6 +1729,7 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_AUTO_START,
                 ARG_ALLOW_INTERACTIVE_AUTHORIZATION,
                 ARG_TIMEOUT,
+                ARG_AUGMENT_CREDS,
         };
 
         static const struct option options[] = {
@@ -1730,6 +1755,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "auto-start",   required_argument, NULL, ARG_AUTO_START   },
                 { "allow-interactive-authorization", required_argument, NULL, ARG_ALLOW_INTERACTIVE_AUTHORIZATION },
                 { "timeout",      required_argument, NULL, ARG_TIMEOUT      },
+                { "augment-creds",required_argument, NULL, ARG_AUGMENT_CREDS},
                 {},
         };
 
@@ -1872,6 +1898,16 @@ static int parse_argv(int argc, char *argv[]) {
 
                         break;
 
+                case ARG_AUGMENT_CREDS:
+                        r = parse_boolean(optarg);
+                        if (r < 0) {
+                                log_error("Failed to parse --augment-creds= parameter.");
+                                return r;
+                        }
+
+                        arg_augment_creds = !!r;
+                        break;
+
                 case '?':
                         return -EINVAL;
 
@@ -1933,7 +1969,7 @@ int main(int argc, char *argv[]) {
 
         r = sd_bus_new(&bus);
         if (r < 0) {
-                log_error("Failed to allocate bus: %s", strerror(-r));
+                log_error_errno(r, "Failed to allocate bus: %m");
                 goto finish;
         }
 
@@ -1942,25 +1978,25 @@ int main(int argc, char *argv[]) {
 
                 r = sd_bus_set_monitor(bus, true);
                 if (r < 0) {
-                        log_error("Failed to set monitor mode: %s", strerror(-r));
+                        log_error_errno(r, "Failed to set monitor mode: %m");
                         goto finish;
                 }
 
-                r = sd_bus_negotiate_creds(bus, _SD_BUS_CREDS_ALL);
+                r = sd_bus_negotiate_creds(bus, true, _SD_BUS_CREDS_ALL);
                 if (r < 0) {
-                        log_error("Failed to enable credentials: %s", strerror(-r));
+                        log_error_errno(r, "Failed to enable credentials: %m");
                         goto finish;
                 }
 
                 r = sd_bus_negotiate_timestamp(bus, true);
                 if (r < 0) {
-                        log_error("Failed to enable timestamps: %s", strerror(-r));
+                        log_error_errno(r, "Failed to enable timestamps: %m");
                         goto finish;
                 }
 
                 r = sd_bus_negotiate_fds(bus, true);
                 if (r < 0) {
-                        log_error("Failed to enable fds: %s", strerror(-r));
+                        log_error_errno(r, "Failed to enable fds: %m");
                         goto finish;
                 }
         }
@@ -1970,17 +2006,20 @@ int main(int argc, char *argv[]) {
         else {
                 r = sd_bus_set_bus_client(bus, true);
                 if (r < 0) {
-                        log_error("Failed to set bus client: %s", strerror(-r));
+                        log_error_errno(r, "Failed to set bus client: %m");
                         goto finish;
                 }
 
                 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:
@@ -1996,13 +2035,13 @@ int main(int argc, char *argv[]) {
                 }
         }
         if (r < 0) {
-                log_error("Failed to set address: %s", strerror(-r));
+                log_error_errno(r, "Failed to set address: %m");
                 goto finish;
         }
 
         r = sd_bus_start(bus);
         if (r < 0) {
-                log_error("Failed to connect to bus: %s", strerror(-r));
+                log_error_errno(r, "Failed to connect to bus: %m");
                 goto finish;
         }