From 3ee423c363b69588e82e522ad1634193284c09a7 Mon Sep 17 00:00:00 2001
From: Bihong Yu <yubihong@huawei.com>
Date: Tue, 14 Jul 2020 15:44:05 +0800
Subject: [PATCH] qemu: pre-create the dbus directory in qemuStateInitialize
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

There are races condiction to make '/run/libvirt/qemu/dbus' directory in
virDirCreateNoFork() while concurrent start VMs, and get "failed to create
directory '/run/libvirt/qemu/dbus': File exists" error message. pre-create the
dbus directory in qemuStateInitialize.

Signed-off-by: Bihong Yu <yubihong@huawei.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Ján Tomko <jtomko@redhat.com>
---
 src/qemu/qemu_dbus.c    | 10 ----------
 src/qemu/qemu_dbus.h    |  2 --
 src/qemu/qemu_driver.c  |  7 +++++++
 src/qemu/qemu_process.c |  3 ---
 4 files changed, 7 insertions(+), 15 deletions(-)

diff --git a/src/qemu/qemu_dbus.c b/src/qemu/qemu_dbus.c
index 51f6c94e3e..81042876fe 100644
--- a/src/qemu/qemu_dbus.c
+++ b/src/qemu/qemu_dbus.c
@@ -33,16 +33,6 @@
 VIR_LOG_INIT("qemu.dbus");
 
 
-int
-qemuDBusPrepareHost(virQEMUDriverPtr driver)
-{
-    g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
-
-    return virDirCreate(cfg->dbusStateDir, 0770, cfg->user, cfg->group,
-                        VIR_DIR_CREATE_ALLOW_EXIST);
-}
-
-
 static char *
 qemuDBusCreatePidFilename(virQEMUDriverConfigPtr cfg,
                           const char *shortName)
diff --git a/src/qemu/qemu_dbus.h b/src/qemu/qemu_dbus.h
index 474eb1058b..3c2145a223 100644
--- a/src/qemu/qemu_dbus.h
+++ b/src/qemu/qemu_dbus.h
@@ -21,8 +21,6 @@
 #include "qemu_conf.h"
 #include "qemu_domain.h"
 
-int qemuDBusPrepareHost(virQEMUDriverPtr driver);
-
 char *qemuDBusGetAddress(virQEMUDriverPtr driver,
                          virDomainObjPtr vm);
 
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 8e81c30a93..53980d4d78 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -743,6 +743,13 @@ qemuStateInitialize(bool privileged,
         goto error;
     }
 
+    if (virDirCreate(cfg->dbusStateDir, 0770, cfg->user, cfg->group,
+                     VIR_DIR_CREATE_ALLOW_EXIST) < 0) {
+        virReportSystemError(errno, _("Failed to create dbus state dir %s"),
+                             cfg->dbusStateDir);
+        goto error;
+    }
+
     if ((qemu_driver->lockFD =
          virPidFileAcquire(cfg->stateDir, "driver", false, getpid())) < 0)
         goto error;
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index e8b15ee3ca..1006f41614 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -6466,9 +6466,6 @@ qemuProcessPrepareHost(virQEMUDriverPtr driver,
     qemuDomainObjPrivatePtr priv = vm->privateData;
     g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
 
-    if (qemuDBusPrepareHost(driver) < 0)
-        return -1;
-
     if (qemuPrepareNVRAM(cfg, vm) < 0)
         return -1;
 
-- 
2.25.0.windows.1

