]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemuOpenFile: Move to qemu_domain.c
authorPeter Krempa <pkrempa@redhat.com>
Thu, 16 Jul 2020 09:17:47 +0000 (11:17 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 24 Aug 2020 14:41:08 +0000 (16:41 +0200)
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@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/qemu/qemu_domain.c
src/qemu/qemu_domain.h
src/qemu/qemu_driver.c

index c27abe2f61930fde90a677d870687e5d83e9c445..210af9e2f13446d98339753ede0dc0abff42fb95 100644 (file)
@@ -64,6 +64,7 @@
 #include "virdomaincheckpointobjlist.h"
 #include "backup_conf.h"
 #include "virutil.h"
+#include "virqemu.h"
 
 #include <sys/time.h>
 #include <fcntl.h>
@@ -10825,3 +10826,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);
+}
index 2f64086fbab1f9f68e37bc34825203d2a8fea633..25af26be9902fb5c0effa576042f084a8cd0f9a6 100644 (file)
@@ -1023,3 +1023,10 @@ void qemuDomainRemoveInactiveJob(virQEMUDriverPtr driver,
 
 void qemuDomainRemoveInactiveJobLocked(virQEMUDriverPtr driver,
                                        virDomainObjPtr vm);
+
+int
+qemuDomainOpenFile(virQEMUDriverPtr driver,
+                   virDomainObjPtr vm,
+                   const char *path,
+                   int oflags,
+                   bool *needUnlink);
index 8bb0ad4e09d300c3c4ff3b067458ad0afe5c6793..2fa975794d2d3721c65ad064cdf09e39eeb975ec 100644 (file)
@@ -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) {