src/rpc/virnetdaemon: convert to use GLib DBus

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Pavel Hrdina 2020-09-16 16:01:51 +02:00
parent 5b9ad2a377
commit 35069cad0a
2 changed files with 35 additions and 81 deletions

View File

@ -98,7 +98,6 @@ virt_rpc_server_lib = static_library(
rpc_gen_headers, rpc_gen_headers,
], ],
dependencies: [ dependencies: [
dbus_dep,
sasl_dep, sasl_dep,
src_dep, src_dep,
xdr_dep, xdr_dep,

View File

@ -32,7 +32,7 @@
#include "virutil.h" #include "virutil.h"
#include "virfile.h" #include "virfile.h"
#include "virnetserver.h" #include "virnetserver.h"
#include "virdbus.h" #include "virgdbus.h"
#include "virhash.h" #include "virhash.h"
#include "virstring.h" #include "virstring.h"
#include "virsystemd.h" #include "virsystemd.h"
@ -78,7 +78,6 @@ struct _virNetDaemon {
unsigned int autoShutdownTimeout; unsigned int autoShutdownTimeout;
size_t autoShutdownInhibitions; size_t autoShutdownInhibitions;
bool autoShutdownCallingInhibit;
int autoShutdownInhibitFd; int autoShutdownInhibitFd;
}; };
@ -459,53 +458,7 @@ virNetDaemonAutoShutdown(virNetDaemonPtr dmn,
} }
#if defined(WITH_DBUS) && defined(DBUS_TYPE_UNIX_FD) #ifdef G_OS_UNIX
static void
virNetDaemonGotInhibitReplyLocked(DBusPendingCall *pending,
virNetDaemonPtr dmn)
{
DBusMessage *reply;
int fd;
dmn->autoShutdownCallingInhibit = false;
VIR_DEBUG("dmn=%p", dmn);
reply = dbus_pending_call_steal_reply(pending);
if (reply == NULL)
goto cleanup;
if (dbus_message_get_args(reply, NULL,
DBUS_TYPE_UNIX_FD, &fd,
DBUS_TYPE_INVALID)) {
if (dmn->autoShutdownInhibitions) {
dmn->autoShutdownInhibitFd = fd;
VIR_DEBUG("Got inhibit FD %d", fd);
} else {
/* We stopped the last VM since we made the inhibit call */
VIR_DEBUG("Closing inhibit FD %d", fd);
VIR_FORCE_CLOSE(fd);
}
}
virDBusMessageUnref(reply);
cleanup:
dbus_pending_call_unref(pending);
}
static void
virNetDaemonGotInhibitReply(DBusPendingCall *pending,
void *opaque)
{
virNetDaemonPtr dmn = opaque;
virObjectLock(dmn);
virNetDaemonGotInhibitReplyLocked(pending, dmn);
virObjectUnlock(dmn);
}
/* As per: https://www.freedesktop.org/wiki/Software/systemd/inhibit */ /* As per: https://www.freedesktop.org/wiki/Software/systemd/inhibit */
static void static void
virNetDaemonCallInhibit(virNetDaemonPtr dmn, virNetDaemonCallInhibit(virNetDaemonPtr dmn,
@ -514,9 +467,12 @@ virNetDaemonCallInhibit(virNetDaemonPtr dmn,
const char *why, const char *why,
const char *mode) const char *mode)
{ {
DBusMessage *message; g_autoptr(GVariant) reply = NULL;
DBusPendingCall *pendingReply = NULL; g_autoptr(GUnixFDList) replyFD = NULL;
DBusConnection *systemBus; GVariant *message = NULL;
GDBusConnection *systemBus;
int fd;
int rc;
VIR_DEBUG("dmn=%p what=%s who=%s why=%s mode=%s", VIR_DEBUG("dmn=%p what=%s who=%s why=%s mode=%s",
dmn, NULLSTR(what), NULLSTR(who), NULLSTR(why), NULLSTR(mode)); dmn, NULLSTR(what), NULLSTR(who), NULLSTR(why), NULLSTR(mode));
@ -524,41 +480,40 @@ virNetDaemonCallInhibit(virNetDaemonPtr dmn,
if (virSystemdHasLogind() < 0) if (virSystemdHasLogind() < 0)
return; return;
if (!(systemBus = virDBusGetSystemBus())) if (!(systemBus = virGDBusGetSystemBus()))
return; return;
/* Only one outstanding call at a time */ message = g_variant_new("(ssss)", what, who, why, mode);
if (dmn->autoShutdownCallingInhibit)
rc = virGDBusCallMethodWithFD(systemBus,
&reply,
&replyFD,
NULL,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"Inhibit",
message,
NULL);
if (rc < 0)
return; return;
message = dbus_message_new_method_call("org.freedesktop.login1", if (g_unix_fd_list_get_length(replyFD) <= 0)
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"Inhibit");
if (message == NULL)
return; return;
dbus_message_append_args(message, fd = g_unix_fd_list_get(replyFD, 0, NULL);
DBUS_TYPE_STRING, &what, if (fd < 0)
DBUS_TYPE_STRING, &who, return;
DBUS_TYPE_STRING, &why,
DBUS_TYPE_STRING, &mode,
DBUS_TYPE_INVALID);
if (dbus_connection_send_with_reply(systemBus, message, if (dmn->autoShutdownInhibitions) {
&pendingReply, dmn->autoShutdownInhibitFd = fd;
25 * 1000) && VIR_DEBUG("Got inhibit FD %d", fd);
pendingReply) { } else {
if (dbus_pending_call_get_completed(pendingReply)) { /* We stopped the last VM since we made the inhibit call */
virNetDaemonGotInhibitReplyLocked(pendingReply, dmn); VIR_DEBUG("Closing inhibit FD %d", fd);
} else { VIR_FORCE_CLOSE(fd);
dbus_pending_call_set_notify(pendingReply,
virNetDaemonGotInhibitReply,
dmn, NULL);
}
dmn->autoShutdownCallingInhibit = true;
} }
virDBusMessageUnref(message);
} }
#endif #endif
@ -570,7 +525,7 @@ virNetDaemonAddShutdownInhibition(virNetDaemonPtr dmn)
VIR_DEBUG("dmn=%p inhibitions=%zu", dmn, dmn->autoShutdownInhibitions); VIR_DEBUG("dmn=%p inhibitions=%zu", dmn, dmn->autoShutdownInhibitions);
#if defined(WITH_DBUS) && defined(DBUS_TYPE_UNIX_FD) #ifdef G_OS_UNIX
if (dmn->autoShutdownInhibitions == 1) if (dmn->autoShutdownInhibitions == 1)
virNetDaemonCallInhibit(dmn, virNetDaemonCallInhibit(dmn,
"shutdown", "shutdown",