[libvirt] [PATCH 5/6] qemu: Use storage driver APIs in qemuDomainBlockPeek

Peter Krempa posted 6 patches 8 years, 7 months ago
[libvirt] [PATCH 5/6] qemu: Use storage driver APIs in qemuDomainBlockPeek
Posted by Peter Krempa 8 years, 7 months ago
Refactor the access to storage driver usage along with
qemuDomainStorageFileInit which ensures that we access the file with
correct DAC uid/gid.
---
 src/qemu/qemu_driver.c | 25 ++++++++-----------------
 1 file changed, 8 insertions(+), 17 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 4c4a6a036..115368d1d 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -11309,9 +11309,9 @@ qemuDomainBlockPeek(virDomainPtr dom,
                     unsigned int flags)
 {
     virQEMUDriverPtr driver = dom->conn->privateData;
+    virDomainDiskDefPtr disk = NULL;
     virDomainObjPtr vm;
-    int fd = -1, ret = -1;
-    const char *actual;
+    int ret = -1;

     virCheckFlags(0, -1);

@@ -11322,32 +11322,23 @@ qemuDomainBlockPeek(virDomainPtr dom,
         goto cleanup;

     /* Check the path belongs to this domain.  */
-    if (!(actual = virDomainDiskPathByName(vm->def, path))) {
+    if (!(disk = virDomainDiskByName(vm->def, path, true))) {
         virReportError(VIR_ERR_INVALID_ARG,
-                       _("invalid path '%s'"), path);
+                       _("invalid disk or path '%s'"), path);
         goto cleanup;
     }
-    path = actual;

-    fd = qemuOpenFile(driver, vm, path, O_RDONLY, NULL, NULL);
-    if (fd < 0)
+    if (qemuDomainStorageFileInit(driver, vm, disk->src) < 0)
         goto cleanup;

-    /* Seek and read. */
-    /* NB. Because we configure with AC_SYS_LARGEFILE, off_t should
-     * be 64 bits on all platforms.
-     */
-    if (lseek(fd, offset, SEEK_SET) == (off_t) -1 ||
-        saferead(fd, buffer, size) == (ssize_t) -1) {
-        virReportSystemError(errno,
-                             _("%s: failed to seek or read"), path);
+    if (virStorageFileRead(disk->src, offset, size, buffer) < 0)
         goto cleanup;
-    }

     ret = 0;

  cleanup:
-    VIR_FORCE_CLOSE(fd);
+    if (disk)
+        virStorageFileDeinit(disk->src);
     virDomainObjEndAPI(&vm);
     return ret;
 }
-- 
2.12.2

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 5/6] qemu: Use storage driver APIs in qemuDomainBlockPeek
Posted by John Ferlan 8 years, 7 months ago

On 06/23/2017 09:33 AM, Peter Krempa wrote:
> Refactor the access to storage driver usage along with
> qemuDomainStorageFileInit which ensures that we access the file with
> correct DAC uid/gid.
> ---
>  src/qemu/qemu_driver.c | 25 ++++++++-----------------
>  1 file changed, 8 insertions(+), 17 deletions(-)
> 

Irony is the comment at the end of qemuOpenFile:

 * This function should not be used on storage sources. Use
 * qemuDomainStorageFileInit and storage driver APIs if possible.

Reviewed-by: John Ferlan <jferlan@redhat.com>

John

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 5/6] qemu: Use storage driver APIs in qemuDomainBlockPeek
Posted by Peter Krempa 8 years, 7 months ago
On Sat, Jul 01, 2017 at 10:43:23 -0400, John Ferlan wrote:
> 
> 
> On 06/23/2017 09:33 AM, Peter Krempa wrote:
> > Refactor the access to storage driver usage along with
> > qemuDomainStorageFileInit which ensures that we access the file with
> > correct DAC uid/gid.
> > ---
> >  src/qemu/qemu_driver.c | 25 ++++++++-----------------
> >  1 file changed, 8 insertions(+), 17 deletions(-)
> > 
> 
> Irony is the comment at the end of qemuOpenFile:
> 
>  * This function should not be used on storage sources. Use
>  * qemuDomainStorageFileInit and storage driver APIs if possible.

This message was added by:

commit f7105d0e4a485bf7d9e878fd17e675d7f9d29f9f
Author: Peter Krempa <pkrempa@redhat.com>
Date:   Wed May 10 12:28:38 2017 +0200

the usage of qemuOpenFile (prior to that, open() was used directly) was
added in:

commit b4a40dd92dc7e6f110b13f2353cb5343d1147227
Author: Martin Kletzander <mkletzan@redhat.com>
Date:   Fri May 24 18:26:26 2013 +0200

I was just lazy and did not bother fixing all places right away and
added the warning that it shouldn't be reused more.
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list