[PATCH 2/5] qemuOpenFile: Move to qemu_domain.c

Peter Krempa pkrempa at redhat.com
Thu Aug 6 09:55:13 UTC 2020


Move the code to qemu_domain.c so that it can be reused in other parts
of the qemu driver. 'qemu_domain' was chosen as the permissions are
based on the domain configuration.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/qemu/qemu_domain.c | 42 +++++++++++++++++++++++++++++++++++
 src/qemu/qemu_domain.h |  7 ++++++
 src/qemu/qemu_driver.c | 50 +++++-------------------------------------
 3 files changed, 54 insertions(+), 45 deletions(-)

diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index c440c79e1d..670db6ebfb 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -64,6 +64,7 @@
 #include "virdomaincheckpointobjlist.h"
 #include "backup_conf.h"
 #include "virutil.h"
+#include "virqemu.h"

 #include <sys/time.h>
 #include <fcntl.h>
@@ -10679,3 +10680,44 @@ qemuDomainDiskBlockJobIsSupported(virDomainObjPtr vm,

     return true;
 }
+
+
+/**
+ * qemuDomainOpenFile:
+ * @driver: driver object
+ * @vm: domain object
+ * @path: path to file to open
+ * @oflags: flags for opening/creation of the file
+ * @needUnlink: set to true if file was created by this function
+ *
+ * Internal function to properly create or open existing files, with
+ * ownership affected by qemu driver setup and domain DAC label.
+ *
+ * Returns the file descriptor on success and negative errno on failure.
+ *
+ * This function should not be used on storage sources. Use
+ * qemuDomainStorageFileInit and storage driver APIs if possible.
+ **/
+int
+qemuDomainOpenFile(virQEMUDriverPtr driver,
+                   virDomainObjPtr vm,
+                   const char *path,
+                   int oflags,
+                   bool *needUnlink)
+{
+    g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
+    uid_t user = cfg->user;
+    gid_t group = cfg->group;
+    bool dynamicOwnership = cfg->dynamicOwnership;
+    virSecurityLabelDefPtr seclabel;
+
+    /* TODO: Take imagelabel into account? */
+    if (vm &&
+        (seclabel = virDomainDefGetSecurityLabelDef(vm->def, "dac")) != NULL &&
+        seclabel->label != NULL &&
+        (virParseOwnershipIds(seclabel->label, &user, &group) < 0))
+        return -1;
+
+    return virQEMUFileOpenAs(user, group, dynamicOwnership,
+                             path, oflags, needUnlink);
+}
diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
index 3a1bcbbfa3..ef03702fa1 100644
--- a/src/qemu/qemu_domain.h
+++ b/src/qemu/qemu_domain.h
@@ -1017,3 +1017,10 @@ qemuDomainDiskBlockJobIsSupported(virDomainObjPtr vm,
 int
 qemuDomainDefNumaCPUsRectify(virDomainDefPtr def,
                              virQEMUCapsPtr qemuCaps);
+
+int
+qemuDomainOpenFile(virQEMUDriverPtr driver,
+                   virDomainObjPtr vm,
+                   const char *path,
+                   int oflags,
+                   bool *needUnlink);
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index a667eb21bf..0bc7eebe9a 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -3021,46 +3021,6 @@ qemuCompressGetCommand(virQEMUSaveFormat compression)
     return ret;
 }

-/**
- * qemuOpenFile:
- * @driver: driver object
- * @vm: domain object
- * @path: path to file to open
- * @oflags: flags for opening/creation of the file
- * @needUnlink: set to true if file was created by this function
- *
- * Internal function to properly create or open existing files, with
- * ownership affected by qemu driver setup and domain DAC label.
- *
- * Returns the file descriptor on success and negative errno on failure.
- *
- * This function should not be used on storage sources. Use
- * qemuDomainStorageFileInit and storage driver APIs if possible.
- **/
-static int
-qemuOpenFile(virQEMUDriverPtr driver,
-             virDomainObjPtr vm,
-             const char *path,
-             int oflags,
-             bool *needUnlink)
-{
-    g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
-    uid_t user = cfg->user;
-    gid_t group = cfg->group;
-    bool dynamicOwnership = cfg->dynamicOwnership;
-    virSecurityLabelDefPtr seclabel;
-
-    /* TODO: Take imagelabel into account? */
-    if (vm &&
-        (seclabel = virDomainDefGetSecurityLabelDef(vm->def, "dac")) != NULL &&
-        seclabel->label != NULL &&
-        (virParseOwnershipIds(seclabel->label, &user, &group) < 0))
-        return -1;
-
-    return virQEMUFileOpenAs(user, group, dynamicOwnership,
-                             path, oflags, needUnlink);
-}
-

 static int
 qemuFileWrapperFDClose(virDomainObjPtr vm,
@@ -3154,7 +3114,7 @@ qemuDomainSaveMemory(virQEMUDriverPtr driver,
     if (qemuFileWrapperFDClose(vm, wrapperFd) < 0)
         goto cleanup;

-    if ((fd = qemuOpenFile(driver, vm, path, O_WRONLY, NULL)) < 0 ||
+    if ((fd = qemuDomainOpenFile(driver, vm, path, O_WRONLY, NULL)) < 0 ||
         virQEMUSaveDataFinish(data, &fd, path) < 0)
         goto cleanup;

@@ -6593,7 +6553,7 @@ qemuDomainSaveImageOpen(virQEMUDriverPtr driver,
         oflags |= directFlag;
     }

-    if ((fd = qemuOpenFile(driver, NULL, path, oflags, NULL)) < 0)
+    if ((fd = qemuDomainOpenFile(driver, NULL, path, oflags, NULL)) < 0)
         return -1;

     if (bypass_cache &&
@@ -11593,7 +11553,7 @@ qemuDomainMemoryPeek(virDomainPtr dom,
  * @ret_sb: pointer to return stat buffer (local or remote)
  * @skipInaccessible: Don't report error if files are not accessible
  *
- * For local storage, open the file using qemuOpenFile and then use
+ * For local storage, open the file using qemuDomainOpenFile and then use
  * fstat() to grab the stat struct data for the caller.
  *
  * For remote storage, attempt to access the file and grab the stat
@@ -11616,8 +11576,8 @@ qemuDomainStorageOpenStat(virQEMUDriverPtr driver,
         if (skipInaccessible && !virFileExists(src->path))
             return 0;

-        if ((*ret_fd = qemuOpenFile(driver, vm, src->path, O_RDONLY,
-                                    NULL)) < 0)
+        if ((*ret_fd = qemuDomainOpenFile(driver, vm, src->path, O_RDONLY,
+                                          NULL)) < 0)
             return -1;

         if (fstat(*ret_fd, ret_sb) < 0) {
-- 
2.26.2




More information about the libvir-list mailing list