[PATCH 0/2] remote_daemon: Silence DBus errors

*** BLURB HERE *** Michal Prívozník (2): virgdbus: Introduce virGDBusHasSessionBus() remote_daemon: Silence DBus errors src/libvirt_private.syms | 1 + src/remote/remote_daemon.c | 36 +++++++++++++++------------ src/util/virgdbus.c | 50 +++++++++++++++++++++++++++++++++++--- src/util/virgdbus.h | 3 +++ 4 files changed, 70 insertions(+), 20 deletions(-) -- 2.45.3

This is just like virGDBusHasSystemBus() except it checks for the session bus instead of the system one. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/libvirt_private.syms | 1 + src/util/virgdbus.c | 50 ++++++++++++++++++++++++++++++++++++---- src/util/virgdbus.h | 3 +++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index bd9564f2a7..30a9f806f0 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2496,6 +2496,7 @@ virGDBusCloseSystemBus; virGDBusErrorIsUnknownMethod; virGDBusGetSessionBus; virGDBusGetSystemBus; +virGDBusHasSessionBus; virGDBusHasSystemBus; virGDBusIsServiceEnabled; virGDBusIsServiceRegistered; diff --git a/src/util/virgdbus.c b/src/util/virgdbus.c index 71ca3cd43b..dd3c6d77b4 100644 --- a/src/util/virgdbus.c +++ b/src/util/virgdbus.c @@ -113,8 +113,8 @@ virGDBusSessionBusInit(void) } -GDBusConnection * -virGDBusGetSessionBus(void) +static GDBusConnection * +virGDBusGetSessionBusInternal(void) { if (virOnce(&sessionOnce, virGDBusSessionBusInit) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", @@ -122,14 +122,56 @@ virGDBusGetSessionBus(void) return NULL; } - if (!sessionBus) { + return sessionBus; +} + + +GDBusConnection * +virGDBusGetSessionBus(void) +{ + GDBusConnection *bus = virGDBusGetSessionBusInternal(); + + if (!bus) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Unable to get session bus connection: %1$s"), sessionError->message); return NULL; } - return sessionBus; + return bus; +} + + +/** + * virGDBusHasSessionBus: + * + * Check if DBus session bus is running. This does not imply that we have + * a connection. DBus might be running and refusing connections due to its + * client limit. The latter must be treated as a fatal error. + * + * Return false if dbus is not available, true if probably available. + */ +bool +virGDBusHasSessionBus(void) +{ + g_autofree char *name = NULL; + + if (virGDBusGetSessionBusInternal()) + return true; + + if (!g_dbus_error_is_remote_error(sessionError)) + return false; + + name = g_dbus_error_get_remote_error(sessionError); + + if (name && + (STREQ(name, "org.freedesktop.DBus.Error.FileNotFound") || + STREQ(name, "org.freedesktop.DBus.Error.NoServer"))) { + VIR_DEBUG("System bus not available: %s", NULLSTR(sessionError->message)); + return false; + } + + return true; } diff --git a/src/util/virgdbus.h b/src/util/virgdbus.h index ca7073e27c..dfe6138112 100644 --- a/src/util/virgdbus.h +++ b/src/util/virgdbus.h @@ -36,6 +36,9 @@ virGDBusGetSystemBus(void); GDBusConnection * virGDBusGetSessionBus(void); +bool +virGDBusHasSessionBus(void); + bool virGDBusHasSystemBus(void); -- 2.45.3

When a daemon (like libvirtd, virtqemud, etc.) is started as an unprivileged user (which is exactly how KubeVirt does it), then it tries to register on both session and system DBus-es so that it can shut itself down (e.g. when system is powering off or user logs out). It's worth noting that this is just opportunistic and if no DBus is available then no error is reported. Or at least that's what we thought. Because the way our virGDBusGetSessionBus() and virGDBusGetSystemBus() are written an error is actually reported every time the daemon starts. Use virGDBusHasSessionBus() and virGDBusHasSystemBus() to check if corresponding bus is available. Resolves: https://issues.redhat.com/browse/RHEL-79088 Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/remote/remote_daemon.c | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/remote/remote_daemon.c b/src/remote/remote_daemon.c index d44a365000..d90355c0d2 100644 --- a/src/remote/remote_daemon.c +++ b/src/remote/remote_daemon.c @@ -631,23 +631,27 @@ static void daemonRunStateInit(void *opaque) /* Tie the non-privileged daemons to the session/shutdown lifecycle */ if (!virNetDaemonIsPrivileged(dmn)) { - sessionBus = virGDBusGetSessionBus(); - if (sessionBus != NULL) - g_dbus_connection_add_filter(sessionBus, - handleSessionMessageFunc, dmn, NULL); + if (virGDBusHasSessionBus()) { + sessionBus = virGDBusGetSessionBus(); + if (sessionBus != NULL) + g_dbus_connection_add_filter(sessionBus, + handleSessionMessageFunc, dmn, NULL); + } - systemBus = virGDBusGetSystemBus(); - if (systemBus != NULL) - g_dbus_connection_signal_subscribe(systemBus, - "org.freedesktop.login1", - "org.freedesktop.login1.Manager", - "PrepareForShutdown", - NULL, - NULL, - G_DBUS_SIGNAL_FLAGS_NONE, - handleSystemMessageFunc, - dmn, - NULL); + if (virGDBusHasSystemBus()) { + systemBus = virGDBusGetSystemBus(); + if (systemBus != NULL) + g_dbus_connection_signal_subscribe(systemBus, + "org.freedesktop.login1", + "org.freedesktop.login1.Manager", + "PrepareForShutdown", + NULL, + NULL, + G_DBUS_SIGNAL_FLAGS_NONE, + handleSystemMessageFunc, + dmn, + NULL); + } } /* Only now accept clients from network */ -- 2.45.3

On Thu, Feb 13, 2025 at 09:56:35AM +0100, Michal Privoznik wrote:
Michal Prívozník (2): virgdbus: Introduce virGDBusHasSessionBus() remote_daemon: Silence DBus errors
src/libvirt_private.syms | 1 + src/remote/remote_daemon.c | 36 +++++++++++++++------------ src/util/virgdbus.c | 50 +++++++++++++++++++++++++++++++++++--- src/util/virgdbus.h | 3 +++ 4 files changed, 70 insertions(+), 20 deletions(-)
Reviewed-by: Andrea Bolognani <abologna@redhat.com> -- Andrea Bolognani / Red Hat / Virtualization
participants (2)
-
Andrea Bolognani
-
Michal Privoznik