From c577fe65f36941e5ce868b2c10853be88bfd2a0b Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 30 Nov 2024 02:56:06 +0900 Subject: [PATCH] systemctl: downgrade log level of ECONNREFUSED from system dbus.service To suppress log message when 'systemctl poweroff' or friends invoked in rescue shell, which does not have dbus.service. --- src/shared/bus-util.c | 10 +++++----- src/shared/bus-util.h | 5 ++++- src/systemctl/systemctl-logind.c | 4 ++-- src/systemctl/systemctl-util.c | 5 +++-- src/systemctl/systemctl-util.h | 5 ++++- 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index ff80e580fc3..8313dfdd14e 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -51,14 +51,14 @@ int bus_log_address_error(int r, BusTransport transport) { "Failed to set bus address: %m"); } -int bus_log_connect_error(int r, BusTransport transport, RuntimeScope scope) { +int bus_log_connect_full(int log_level, int r, BusTransport transport, RuntimeScope scope) { bool hint_vars = transport == BUS_TRANSPORT_LOCAL && r == -ENOMEDIUM, hint_addr = transport == BUS_TRANSPORT_LOCAL && ERRNO_IS_PRIVILEGE(r); - return log_error_errno(r, - hint_vars ? "Failed to connect to %s scope bus via %s transport: $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR not defined (consider using --machine=@.host --user to connect to bus of other user)" : - hint_addr ? "Failed to connect to %s scope bus via %s transport: Operation not permitted (consider using --machine=@.host --user to connect to bus of other user)" : - "Failed to connect to %s scope bus via %s transport: %m", runtime_scope_to_string(scope), bus_transport_to_string(transport)); + return log_full_errno(log_level, r, + hint_vars ? "Failed to connect to %s scope bus via %s transport: $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR not defined (consider using --machine=@.host --user to connect to bus of other user)" : + hint_addr ? "Failed to connect to %s scope bus via %s transport: Operation not permitted (consider using --machine=@.host --user to connect to bus of other user)" : + "Failed to connect to %s scope bus via %s transport: %m", runtime_scope_to_string(scope), bus_transport_to_string(transport)); } int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name) { diff --git a/src/shared/bus-util.h b/src/shared/bus-util.h index fbccb24314f..e1908ecbaaf 100644 --- a/src/shared/bus-util.h +++ b/src/shared/bus-util.h @@ -48,7 +48,10 @@ int bus_connect_transport(BusTransport transport, const char *host, RuntimeScope int bus_connect_transport_systemd(BusTransport transport, const char *host, RuntimeScope runtime_scope, sd_bus **bus); int bus_log_address_error(int r, BusTransport transport); -int bus_log_connect_error(int r, BusTransport transport, RuntimeScope scope); +int bus_log_connect_full(int log_level, int r, BusTransport transport, RuntimeScope scope); +static inline int bus_log_connect_error(int r, BusTransport transport, RuntimeScope scope) { + return bus_log_connect_full(LOG_ERR, r, transport, scope); +} #define bus_log_parse_error(r) \ log_error_errno(r, "Failed to parse bus message: %m") diff --git a/src/systemctl/systemctl-logind.c b/src/systemctl/systemctl-logind.c index 1258852a016..e54067397aa 100644 --- a/src/systemctl/systemctl-logind.c +++ b/src/systemctl/systemctl-logind.c @@ -66,7 +66,7 @@ int logind_reboot(enum action a) { if (!actions[a]) return -EINVAL; - r = acquire_bus(BUS_FULL, &bus); + r = acquire_bus_full(BUS_FULL, /* graceful = */ true, &bus); if (r < 0) return r; @@ -151,7 +151,7 @@ int logind_check_inhibitors(enum action a) { if (arg_transport != BUS_TRANSPORT_LOCAL) return 0; - r = acquire_bus(BUS_FULL, &bus); + r = acquire_bus_full(BUS_FULL, /* graceful = */ true, &bus); if (r == -ECONNREFUSED && geteuid() == 0) return 0; /* When D-Bus is not running, allow root to force a shutdown. E.g. when running at * the emergency console. */ diff --git a/src/systemctl/systemctl-util.c b/src/systemctl/systemctl-util.c index 4674f0f36ff..ecff2453e62 100644 --- a/src/systemctl/systemctl-util.c +++ b/src/systemctl/systemctl-util.c @@ -32,7 +32,7 @@ static sd_bus *buses[_BUS_FOCUS_MAX] = {}; -int acquire_bus(BusFocus focus, sd_bus **ret) { +int acquire_bus_full(BusFocus focus, bool graceful, sd_bus **ret) { int r; assert(focus < _BUS_FOCUS_MAX); @@ -54,7 +54,8 @@ int acquire_bus(BusFocus focus, sd_bus **ret) { else r = bus_connect_transport(arg_transport, arg_host, arg_runtime_scope, &buses[focus]); if (r < 0) - return bus_log_connect_error(r, arg_transport, arg_runtime_scope); + return bus_log_connect_full(graceful && focus == BUS_FULL && r == -ECONNREFUSED ? LOG_DEBUG : LOG_ERR, + r, arg_transport, arg_runtime_scope); (void) sd_bus_set_allow_interactive_authorization(buses[focus], arg_ask_password); } diff --git a/src/systemctl/systemctl-util.h b/src/systemctl/systemctl-util.h index a950dde5155..96543a7b5ea 100644 --- a/src/systemctl/systemctl-util.h +++ b/src/systemctl/systemctl-util.h @@ -13,7 +13,10 @@ typedef enum BusFocus { _BUS_FOCUS_MAX } BusFocus; -int acquire_bus(BusFocus focus, sd_bus **ret); +int acquire_bus_full(BusFocus focus, bool graceful, sd_bus **ret); +static inline int acquire_bus(BusFocus focus, sd_bus **ret) { + return acquire_bus_full(focus, false, ret); +} void release_busses(void); void ask_password_agent_open_maybe(void);