]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
busctl: send BUSERROR= to caller via sd_notify() protocol
authorLennart Poettering <lennart@poettering.net>
Thu, 20 Jun 2024 14:22:32 +0000 (16:22 +0200)
committerMike Yuan <me@yhndnzj.com>
Thu, 20 Jun 2024 17:03:44 +0000 (19:03 +0200)
varlinkctl has this nice feature that it sends the varlink error it gets
via sd_notify() to the caller. With previous commits this information
is collected and exposed in "systemctl status".

Let's make sure we can provide the same in busctl: also propagate errors
the same way.

With this we can comprehensively close #6073

src/busctl/busctl.c

index 5061afe6c928bb2c4da8ca8dce7e096121c36263..4a5fac76062fe4270f7f0157f601a9294558eb6e 100644 (file)
@@ -159,6 +159,14 @@ static int acquire_bus(bool set_monitor, sd_bus **ret) {
         return 0;
 }
 
+static void notify_bus_error(const sd_bus_error *error) {
+
+        if (!sd_bus_error_is_set(error))
+                return;
+
+        (void) sd_notifyf(/* unset_environment= */ false, "BUSERROR=%s", error->name);
+}
+
 static int list_bus_names(int argc, char **argv, void *userdata) {
         _cleanup_strv_free_ char **acquired = NULL, **activatable = NULL;
         _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
@@ -459,6 +467,7 @@ static int find_nodes(sd_bus *bus, const char *service, const char *path, Set *p
                                "org.freedesktop.DBus.Introspectable", "Introspect",
                                &error, &reply, NULL);
         if (r < 0) {
+                notify_bus_error(&error);
                 printf("%sFailed to introspect object %s of service %s: %s%s\n",
                        ansi_highlight_red(),
                        path, service, bus_error_message(&error, r),
@@ -996,9 +1005,11 @@ static int introspect(int argc, char **argv, void *userdata) {
         r = sd_bus_call_method(bus, argv[1], argv[2],
                                "org.freedesktop.DBus.Introspectable", "Introspect",
                                &error, &reply_xml, NULL);
-        if (r < 0)
+        if (r < 0) {
+                notify_bus_error(&error);
                 return log_error_errno(r, "Failed to introspect object %s of service %s: %s",
                                        argv[2], argv[1], bus_error_message(&error, r));
+        }
 
         r = sd_bus_message_read(reply_xml, "s", &xml);
         if (r < 0)
@@ -1032,9 +1043,11 @@ static int introspect(int argc, char **argv, void *userdata) {
                 r = sd_bus_call_method(bus, argv[1], argv[2],
                                        "org.freedesktop.DBus.Properties", "GetAll",
                                        &error, &reply, "s", m->interface);
-                if (r < 0)
+                if (r < 0) {
+                        notify_bus_error(&error);
                         return log_error_errno(r, "Failed to get all properties on interface %s: %s",
                                                m->interface, bus_error_message(&error, r));
+                }
 
                 r = sd_bus_message_enter_container(reply, 'a', "{sv}");
                 if (r < 0)
@@ -1305,9 +1318,11 @@ static int monitor(int argc, char **argv, int (*dump)(sd_bus_message *m, FILE *f
                 return bus_log_create_error(r);
 
         r = sd_bus_call(bus, message, arg_timeout, &error, NULL);
-        if (r < 0)
+        if (r < 0) {
+                notify_bus_error(&error);
                 return log_error_errno(r, "Call to org.freedesktop.DBus.Monitoring.BecomeMonitor failed: %s",
                                        bus_error_message(&error, r));
+        }
 
         r = sd_bus_get_unique_name(bus, &unique_name);
         if (r < 0)
@@ -2076,8 +2091,10 @@ static int call(int argc, char **argv, void *userdata) {
         }
 
         r = sd_bus_call(bus, m, arg_timeout, &error, &reply);
-        if (r < 0)
+        if (r < 0) {
+                notify_bus_error(&error);
                 return log_error_errno(r, "Call failed: %s", bus_error_message(&error, r));
+        }
 
         r = sd_bus_message_is_empty(reply);
         if (r < 0)
@@ -2180,10 +2197,12 @@ static int get_property(int argc, char **argv, void *userdata) {
                 r = sd_bus_call_method(bus, argv[1], argv[2],
                                        "org.freedesktop.DBus.Properties", "Get",
                                        &error, &reply, "ss", argv[3], *i);
-                if (r < 0)
+                if (r < 0) {
+                        notify_bus_error(&error);
                         return log_error_errno(r, "Failed to get property %s on interface %s: %s",
                                                *i, argv[3],
                                                bus_error_message(&error, r));
+                }
 
                 r = sd_bus_message_peek_type(reply, &type, &contents);
                 if (r < 0)
@@ -2267,10 +2286,12 @@ static int set_property(int argc, char **argv, void *userdata) {
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too many parameters for signature.");
 
         r = sd_bus_call(bus, m, arg_timeout, &error, NULL);
-        if (r < 0)
+        if (r < 0) {
+                notify_bus_error(&error);
                 return log_error_errno(r, "Failed to set property %s on interface %s: %s",
                                        argv[4], argv[3],
                                        bus_error_message(&error, r));
+        }
 
         return 0;
 }