Skip to content

Commit

Permalink
busctl: send BUSERROR= to caller via sd_notify() protocol
Browse files Browse the repository at this point in the history
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 systemd#6073
  • Loading branch information
poettering authored and YHNdnzj committed Jun 20, 2024
1 parent f488e97 commit 8ea9186
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/busctl/busctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 8ea9186

Please sign in to comment.