[PATCH 10/23] qemu: add a DBus daemon helper unit

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Marc-André Lureau <marcandre.lureau@xxxxxxxxxx>

Add a unit to start & stop a private dbus-daemon.

The daemon is meant to be started on demand, and associated with a
QEMU process. It is stopped when the QEMU process is stopped.

Signed-off-by: Marc-André Lureau <marcandre.lureau@xxxxxxxxxx>
---
 src/qemu/Makefile.inc.am |   4 +
 src/qemu/qemu_dbus.c     | 370 +++++++++++++++++++++++++++++++++++++++
 src/qemu/qemu_dbus.h     |  40 +++++
 src/qemu/qemu_domain.c   |  10 ++
 src/qemu/qemu_domain.h   |   5 +
 src/qemu/qemu_process.c  |   6 +
 tests/Makefile.am        |   1 +
 7 files changed, 436 insertions(+)
 create mode 100644 src/qemu/qemu_dbus.c
 create mode 100644 src/qemu/qemu_dbus.h

diff --git a/src/qemu/Makefile.inc.am b/src/qemu/Makefile.inc.am
index 254ba07dc0..248f3970c9 100644
--- a/src/qemu/Makefile.inc.am
+++ b/src/qemu/Makefile.inc.am
@@ -58,6 +58,8 @@ QEMU_DRIVER_SOURCES = \
 	qemu/qemu_qapi.h \
 	qemu/qemu_tpm.c \
 	qemu/qemu_tpm.h \
+	qemu/qemu_dbus.c \
+	qemu/qemu_dbus.h \
 	$(NULL)
 
 
@@ -81,6 +83,7 @@ libvirt_driver_qemu_impl_la_CFLAGS = \
 	$(LIBNL_CFLAGS) \
 	$(SELINUX_CFLAGS) \
 	$(XDR_CFLAGS) \
+	$(DBUS_CFLAGS) \
 	-I$(srcdir)/access \
 	-I$(srcdir)/conf \
 	-I$(srcdir)/secret \
@@ -92,6 +95,7 @@ libvirt_driver_qemu_impl_la_LIBADD = \
 	$(GNUTLS_LIBS) \
 	$(LIBNL_LIBS) \
 	$(SELINUX_LIBS) \
+	$(DBUS_LIBS) \
 	$(LIBXML_LIBS) \
 	$(NULL)
 libvirt_driver_qemu_impl_la_SOURCES = $(QEMU_DRIVER_SOURCES)
diff --git a/src/qemu/qemu_dbus.c b/src/qemu/qemu_dbus.c
new file mode 100644
index 0000000000..bf24c85910
--- /dev/null
+++ b/src/qemu/qemu_dbus.c
@@ -0,0 +1,370 @@
+/*
+ * qemu_dbus.c: QEMU dbus daemon
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+
+#include "qemu_extdevice.h"
+#include "qemu_dbus.h"
+#include "qemu_security.h"
+
+#include "viralloc.h"
+#include "virlog.h"
+#include "virstring.h"
+#include "virtime.h"
+#include "virpidfile.h"
+
+#define VIR_FROM_THIS VIR_FROM_QEMU
+
+VIR_LOG_INIT("qemu.dbus");
+
+
+int
+qemuDBusPrepareHost(virQEMUDriverPtr driver)
+{
+    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
+
+    return virDirCreate(cfg->dbusStateDir, 0770, cfg->user, cfg->group,
+                        VIR_DIR_CREATE_ALLOW_EXIST);
+}
+
+
+static char *
+qemuDBusCreatePidFilename(const char *stateDir,
+                          const char *shortName)
+{
+    VIR_AUTOFREE(char *) name = NULL;
+
+    if (virAsprintf(&name, "%s-dbus", shortName) < 0)
+        return NULL;
+
+    return virPidFileBuildPath(stateDir, name);
+}
+
+
+static char *
+qemuDBusCreateFilename(const char *stateDir,
+                       const char *shortName,
+                       const char *ext)
+{
+    VIR_AUTOFREE(char *) name = NULL;
+
+    if (virAsprintf(&name, "%s-dbus", shortName) < 0)
+        return NULL;
+
+    return virFileBuildPath(stateDir, name,  ext);
+}
+
+
+static char *
+qemuDBusCreateSocketPath(virQEMUDriverConfigPtr cfg,
+                         const char *shortName)
+{
+    return qemuDBusCreateFilename(cfg->dbusStateDir, shortName, ".sock");
+}
+
+
+char *
+qemuDBusGetAddress(virQEMUDriverPtr driver,
+                   virDomainObjPtr vm)
+{
+    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
+    VIR_AUTOFREE(char *) shortName = virDomainDefGetShortName(vm->def);
+    VIR_AUTOFREE(char *) path = qemuDBusCreateSocketPath(cfg, shortName);
+    char *ret;
+
+    ignore_value(virAsprintf(&ret, "unix:path=%s", path));
+
+    return ret;
+}
+
+
+static int
+qemuDBusGetPid(const char *binPath,
+               const char *stateDir,
+               const char *shortName,
+               pid_t *pid)
+{
+    VIR_AUTOFREE(char *) pidfile = qemuDBusCreatePidFilename(stateDir, shortName);
+
+    if (!pidfile)
+        return -ENOMEM;
+
+    return virPidFileReadPathIfAlive(pidfile, pid, binPath);
+}
+
+
+static int
+qemuDBusWriteConfig(const char *filename, const char *path)
+{
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
+    VIR_AUTOFREE(char *) config = NULL;
+
+    virBufferAddLit(&buf, "<!DOCTYPE busconfig PUBLIC \"-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN\"\n");
+    virBufferAddLit(&buf, "  \"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd\";>\n");
+    virBufferAddLit(&buf, "<busconfig>\n");
+    virBufferAdjustIndent(&buf, 2);
+
+    virBufferAddLit(&buf, "<type>org.libvirt.qemu</type>\n");
+
+    /* This may become useful: */
+    /* virBufferAddLit(&buf, "<servicedir>" DATADIR "/dbus-1/libvirt-qemu-services</servicedir>\n"); */
+
+    virBufferAsprintf(&buf, "<listen>unix:path=%s</listen>\n", path);
+
+    virBufferAddLit(&buf, "<auth>EXTERNAL</auth>\n");
+
+    virBufferAddLit(&buf, "<policy context='default'>\n");
+    virBufferAdjustIndent(&buf, 2);
+    virBufferAddLit(&buf, "<!-- Allow everything to be sent -->\n");
+    virBufferAddLit(&buf, "<allow send_destination='*' eavesdrop='true'/>\n");
+    virBufferAddLit(&buf, "<!-- Allow everything to be received -->\n");
+    virBufferAddLit(&buf, "<allow eavesdrop='true'/>\n");
+    virBufferAddLit(&buf, "<!-- Allow anyone to own anything -->\n");
+    virBufferAddLit(&buf, "<allow own='*'/>\n");
+    virBufferAdjustIndent(&buf, -2);
+    virBufferAddLit(&buf, "</policy>\n");
+
+    virBufferAddLit(&buf, "<include if_selinux_enabled='yes' selinux_root_relative='yes'>contexts/dbus_contexts</include>\n");
+
+    virBufferAdjustIndent(&buf, -2);
+    virBufferAddLit(&buf, "</busconfig>\n");
+
+    if (virBufferCheckError(&buf) < 0)
+        return -1;
+
+    config = virBufferContentAndReset(&buf);
+
+    return virFileWriteStr(filename, config, 0600);
+}
+
+
+void
+qemuDBusStop(virQEMUDriverPtr driver,
+             virDomainObjPtr vm)
+{
+    qemuDomainObjPrivatePtr priv = vm->privateData;
+    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
+    VIR_AUTOFREE(char *) shortName = NULL;
+    VIR_AUTOFREE(char *) pidfile = NULL;
+    VIR_AUTOFREE(char *) configfile = NULL;
+    virErrorPtr orig_err;
+    int rc;
+    pid_t pid;
+
+    shortName = virDomainDefGetShortName(vm->def);
+    pidfile = qemuDBusCreatePidFilename(cfg->dbusStateDir, shortName);
+    configfile = qemuDBusCreateFilename(cfg->dbusStateDir, shortName, ".conf");
+
+    if (!shortName || !pidfile || !configfile)
+        return;
+
+    rc = qemuDBusGetPid(cfg->dbusDaemonName, cfg->dbusStateDir, shortName, &pid);
+    if (rc == 0 && pid != (pid_t)-1) {
+        char ebuf[1024];
+
+        VIR_DEBUG("Killing dbus-daemon process %lld", (long long)pid);
+        if (virProcessKill(pid, SIGTERM) < 0 && errno != ESRCH)
+            VIR_ERROR(_("Failed to kill process %lld: %s"),
+                      (long long)pid,
+                      virStrerror(errno, ebuf, sizeof(ebuf)));
+    }
+
+    virErrorPreserveLast(&orig_err);
+    if (virPidFileForceCleanupPath(pidfile) < 0) {
+        VIR_WARN("Unable to kill dbus-daemon process");
+    } else {
+        if (unlink(pidfile) < 0 &&
+            errno != ENOENT) {
+            virReportSystemError(errno,
+                                 _("Unable to remove stale pidfile %s"),
+                                 pidfile);
+        }
+    }
+    if (unlink(configfile) < 0 &&
+        errno != ENOENT) {
+        virReportSystemError(errno,
+                             _("Unable to remove stale configfile %s"),
+                             pidfile);
+    }
+    virErrorRestore(&orig_err);
+
+    VIR_FREE(pidfile);
+
+    priv->dbusDaemonRunning = false;
+}
+
+
+int
+qemuDBusStart(virQEMUDriverPtr driver,
+              virDomainObjPtr vm)
+{
+    qemuDomainObjPrivatePtr priv = vm->privateData;
+    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
+    VIR_AUTOPTR(virCommand) cmd = NULL;
+    VIR_AUTOFREE(char *) shortName = NULL;
+    VIR_AUTOFREE(char *) pidfile = NULL;
+    VIR_AUTOFREE(char *) configfile = NULL;
+    VIR_AUTOFREE(char *) sockpath = NULL;
+    virTimeBackOffVar timebackoff;
+    const unsigned long long timeout = 500 * 1000; /* ms */
+    int errfd = -1;
+    int cmdret = 0;
+    int exitstatus = 0;
+
+    if (priv->dbusDaemonRunning)
+        return 0;
+
+    /* cleanup */
+    qemuDBusStop(driver, vm);
+
+    cmd = virCommandNew(cfg->dbusDaemonName);
+    shortName = virDomainDefGetShortName(vm->def);
+    pidfile = qemuDBusCreatePidFilename(cfg->dbusStateDir, shortName);
+    configfile = qemuDBusCreateFilename(cfg->dbusStateDir, shortName, ".conf");
+    sockpath = qemuDBusCreateSocketPath(cfg, shortName);
+
+    if (!cmd || !pidfile || !configfile || !sockpath)
+        return -1;
+
+    if (qemuDBusWriteConfig(configfile, sockpath) < 0) {
+        virReportSystemError(errno, _("Failed to write '%s'"), configfile);
+        return -1;
+    }
+
+    if (qemuSecurityDomainSetPathLabel(driver, vm, configfile, true) < 0)
+        return -1;
+
+    virCommandClearCaps(cmd);
+    virCommandSetPidFile(cmd, pidfile);
+    virCommandSetErrorFD(cmd, &errfd);
+    virCommandDaemonize(cmd);
+    virCommandAddArgFormat(cmd, "--config-file=%s", configfile);
+
+    if (qemuExtDeviceLogCommand(driver, vm, cmd, "DBus") < 0)
+        return -1;
+
+    if (qemuSecurityCommandRun(driver, vm, cmd,
+                               &exitstatus, &cmdret) < 0)
+        return -1;
+
+    if (cmdret < 0 || exitstatus != 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Could not start 'vhost-user-gpu'. exitstatus: %d"),
+                       exitstatus);
+        return -1;
+    }
+
+    if (virTimeBackOffStart(&timebackoff, 1, timeout) < 0)
+        return -1;
+    while (virTimeBackOffWait(&timebackoff)) {
+        pid_t pid;
+
+        if (qemuDBusGetPid(cfg->dbusDaemonName, cfg->dbusStateDir, shortName, &pid) < 0)
+            continue;
+
+        if (pid == (pid_t)-1)
+            break;
+
+        if (virFileExists(sockpath))
+            break;
+    }
+
+    if (!virFileExists(sockpath)) {
+        char errbuf[1024] = { 0 };
+
+        if (saferead(errfd, errbuf, sizeof(errbuf) - 1) < 0) {
+            virReportSystemError(errno, "%s", _("dbus-daemon died unexpectedly"));
+        } else {
+            virReportError(VIR_ERR_OPERATION_FAILED,
+                           _("dbus-daemon died and reported: %s"), errbuf);
+        }
+
+        return -1;
+    }
+
+    if (qemuSecurityDomainSetPathLabel(driver, vm, sockpath, true) < 0)
+        return -1;
+
+    priv->dbusDaemonRunning = true;
+
+    return 0;
+}
+
+
+int
+qemuDBusSetupCgroup(virQEMUDriverPtr driver,
+                    virDomainDefPtr def,
+                    virCgroupPtr cgroup)
+{
+    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
+    VIR_AUTOFREE(char *) shortName = virDomainDefGetShortName(def);
+    pid_t pid;
+    int rc;
+
+    rc = qemuDBusGetPid(cfg->dbusDaemonName, cfg->dbusStateDir, shortName, &pid);
+    if (rc < 0 || (rc == 0 && pid == (pid_t)-1)) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("Could not get process id of dbus-daemon"));
+        return -1;
+    }
+
+    if (virCgroupAddProcess(cgroup, pid) < 0)
+        return -1;
+
+    return 0;
+}
+
+
+int
+qemuDBusConnect(virQEMUDriverPtr driver,
+                virDomainObjPtr vm)
+{
+    qemuDomainObjPrivatePtr priv = vm->privateData;
+    VIR_AUTOFREE(char *) addr = qemuDBusGetAddress(driver, vm);
+    DBusConnection *conn = NULL;
+    DBusError error;
+    int ret = -1;
+
+    if (!priv->dbusDaemonRunning || priv->dbusConn)
+        return 0;
+
+    dbus_error_init(&error);
+    conn = dbus_connection_open(addr, &error);
+    if (!conn) {
+        virReportError(VIR_ERR_DBUS_SERVICE, _("failed to connect to DBus: %s"),
+                       error.message ? : _("unknown error"));
+        goto end;
+    }
+
+    if (!dbus_bus_register(conn, &error)) {
+        virReportError(VIR_ERR_DBUS_SERVICE, _("failed to register to DBus: %s"),
+                       error.message ? : _("unknown error"));
+        goto end;
+    }
+
+    priv->dbusConn = conn;
+    conn = NULL;
+    ret = 0;
+
+end:
+    if (conn) {
+        dbus_connection_unref(conn);
+    }
+    dbus_error_free(&error);
+    return ret;
+}
diff --git a/src/qemu/qemu_dbus.h b/src/qemu/qemu_dbus.h
new file mode 100644
index 0000000000..8728824bd7
--- /dev/null
+++ b/src/qemu/qemu_dbus.h
@@ -0,0 +1,40 @@
+/*
+ * qemu_dbus.h: QEMU dbus daemon
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include "qemu_conf.h"
+#include "qemu_domain.h"
+
+int qemuDBusPrepareHost(virQEMUDriverPtr driver);
+
+char *qemuDBusGetAddress(virQEMUDriverPtr driver,
+                         virDomainObjPtr vm);
+
+int qemuDBusConnect(virQEMUDriverPtr driver,
+                    virDomainObjPtr vm);
+
+int qemuDBusStart(virQEMUDriverPtr driver,
+                  virDomainObjPtr vm);
+
+void qemuDBusStop(virQEMUDriverPtr driver,
+                  virDomainObjPtr vm);
+
+int qemuDBusSetupCgroup(virQEMUDriverPtr driver,
+                        virDomainDefPtr def,
+                        virCgroupPtr cgroup);
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 6225ac23e2..192aceb605 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -2051,6 +2051,11 @@ qemuDomainObjPrivateDataClear(qemuDomainObjPrivatePtr priv)
 
     qemuDomainObjResetJob(priv);
     qemuDomainObjResetAsyncJob(priv);
+
+    if (priv->dbusConn) {
+        dbus_connection_unref(priv->dbusConn);
+        priv->dbusConn = NULL;
+    }
 }
 
 
@@ -2479,6 +2484,9 @@ qemuDomainObjPrivateXMLFormat(virBufferPtr buf,
                           virDomainChrTypeToString(priv->monConfig->type));
     }
 
+    if (priv->dbusDaemonRunning)
+        virBufferAddLit(buf, "<dbusDaemon/>\n");
+
     if (priv->namespaces) {
         ssize_t ns = -1;
 
@@ -2910,6 +2918,8 @@ qemuDomainObjPrivateXMLParse(xmlXPathContextPtr ctxt,
         goto error;
     }
 
+    priv->dbusDaemonRunning = virXPathBoolean("boolean(./dbusDaemon)", ctxt) > 0;
+
     if ((node = virXPathNode("./namespaces", ctxt))) {
         xmlNodePtr next;
 
diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
index 3eea8b0f96..9d14163c21 100644
--- a/src/qemu/qemu_domain.h
+++ b/src/qemu/qemu_domain.h
@@ -39,6 +39,7 @@
 #include "logging/log_manager.h"
 #include "virdomainmomentobjlist.h"
 #include "virenum.h"
+#include "virdbus.h"
 
 #define QEMU_DOMAIN_FORMAT_LIVE_FLAGS \
     (VIR_DOMAIN_XML_SECURE)
@@ -386,6 +387,10 @@ struct _qemuDomainObjPrivate {
 
     /* true if global -mem-prealloc appears on cmd line */
     bool memPrealloc;
+
+    /* true if dbus-daemon is running */
+    bool dbusDaemonRunning;
+    DBusConnection *dbusConn;
 };
 
 #define QEMU_DOMAIN_PRIVATE(vm) \
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 5156b4906c..5e8184b0e2 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -57,6 +57,7 @@
 #include "qemu_security.h"
 #include "qemu_extdevice.h"
 #include "qemu_firmware.h"
+#include "qemu_dbus.h"
 
 #include "cpu/cpu.h"
 #include "cpu/cpu_x86.h"
@@ -6452,6 +6453,9 @@ qemuProcessPrepareHost(virQEMUDriverPtr driver,
     qemuDomainObjPrivatePtr priv = vm->privateData;
     virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
 
+    if (qemuDBusPrepareHost(driver) < 0)
+        goto cleanup;
+
     if (qemuPrepareNVRAM(cfg, vm) < 0)
         goto cleanup;
 
@@ -7399,6 +7403,8 @@ void qemuProcessStop(virQEMUDriverPtr driver,
 
     qemuExtDevicesStop(driver, vm);
 
+    qemuDBusStop(driver, vm);
+
     vm->def->id = -1;
 
     /* Stop autodestroy in case guest is restarted */
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 115afa1c1a..85d0d8f614 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -42,6 +42,7 @@ AM_CFLAGS = \
 	$(APPARMOR_CFLAGS) \
 	$(YAJL_CFLAGS) \
 	$(XDR_CFLAGS) \
+	$(DBUS_CFLAGS) \
 	$(WARN_CFLAGS)
 
 AM_LDFLAGS = \
-- 
2.22.0.214.g8dca754b1e

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux