Skip to contentSkip to conversation

QIOChannelFile bug fixes

PATCH In review Ross Lagerwall 4 participants 16 messages Nov 1, 2017 – Feb 6, 2018

lore
Share & tools
lore
#0 lore raw

v2 cover QIOChannelFile bug fixes

Hi,

Here is a bug fix with the use of QIOChannelFile and 2 bug fixes and an improvement to implementation of QIOChannelFile.

Regards,
Ross Lagerwall

Ross Lagerwall (4):
migration: Don't leak IO channels
io: Fix QIOChannelFile when creating and opening read-write
io: Don't call close multiple times in QIOChannelFile
io: Add /dev/fdset/ support to QIOChannelFile

 include/io/channel-file.h    |  2 +-
 io/channel-file.c            | 11 ++++-------
 migration/savevm.c           |  2 ++
 tests/test-io-channel-file.c | 29 +++++++++++++++++++++++++----
 4 files changed, 32 insertions(+), 12 deletions(-)
Signature · 1 line
2.9.5
#1 lore raw

PATCH v2 1/4 migration: Don't leak IO channels

Since qemu_fopen_channel_{in,out}put take references on the underlying IO channels, make sure to release our references to them.

---
New in v2.

 migration/savevm.c | 2 ++
 1 file changed, 2 insertions(+)
migration/savevm.c +2 -0
index 4a88228..87557dd 100644
@@ -2259,6 +2259,7 @@ void qmp_xen_save_devices_state(const char *filename, Error **errp)
     }
     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-save-state");
     f = qemu_fopen_channel_output(QIO_CHANNEL(ioc));
+    object_unref(OBJECT(ioc));
     ret = qemu_save_device_state(f);
     qemu_fclose(f);
     if (ret < 0) {
@@ -2292,6 +2293,7 @@ void qmp_xen_load_devices_state(const char *filename, Error **errp)
     }
     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-load-state");
     f = qemu_fopen_channel_input(QIO_CHANNEL(ioc));
+    object_unref(OBJECT(ioc));
 
     ret = qemu_loadvm_state(f);
     qemu_fclose(f);
Signature · 1 line
2.9.5
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/4] migration: Don't leak IO channels

Quoted 24 lines · Ross Lagerwall

Since qemu_fopen_channel_{in,out}put take references on the underlying IO channels, make sure to release our references to them.

---
New in v2.

  migration/savevm.c | 2 ++
  1 file changed, 2 insertions(+)
migration/savevm.c +1 -0
index 4a88228..87557dd 100644
@@ -2259,6 +2259,7 @@ void qmp_xen_save_devices_state(const char *filename, Error **errp)
      }
      qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-save-state");
      f = qemu_fopen_channel_output(QIO_CHANNEL(ioc));
+    object_unref(OBJECT(ioc));
      ret = qemu_save_device_state(f);
      qemu_fclose(f);
      if (ret < 0) {

migration/savevm.c void qmp_xen_load_devices_state(const char *filename, Error **errp) · line 2299

Quoted 7 lines
migration/savevm.c +1 -0
@@ -2292,6 +2293,7 @@ void qmp_xen_load_devices_state(const char *filename, Error **errp)
      }
      qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-load-state");
      f = qemu_fopen_channel_input(QIO_CHANNEL(ioc));
+    object_unref(OBJECT(ioc));
 
      ret = qemu_loadvm_state(f);
      qemu_fclose(f);

Ping for review...

Thanks,

Signature · 1 line
Ross Lagerwall
Reviewed-by on v2 1/4
#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/4] migration: Don't leak IO channels

In reply to Ross Lagerwall
Quoted 9 lines · Ross Lagerwall

Since qemu_fopen_channel_{in,out}put take references on the underlying IO channels, make sure to release our references to them.

---
New in v2.

 migration/savevm.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 25 lines
migration/savevm.c +2 -0
index 4a88228..87557dd 100644
@@ -2259,6 +2259,7 @@ void qmp_xen_save_devices_state(const char *filename, Error **errp)
     }
     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-save-state");
     f = qemu_fopen_channel_output(QIO_CHANNEL(ioc));
+    object_unref(OBJECT(ioc));
     ret = qemu_save_device_state(f);
     qemu_fclose(f);
     if (ret < 0) {
@@ -2292,6 +2293,7 @@ void qmp_xen_load_devices_state(const char *filename, Error **errp)
     }
     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-load-state");
     f = qemu_fopen_channel_input(QIO_CHANNEL(ioc));
+    object_unref(OBJECT(ioc));
 
     ret = qemu_loadvm_state(f);
     qemu_fclose(f);
Signature · 1 line
2.9.5

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#4 lore raw

Re: [Qemu-devel] [PATCH v2 1/4] migration: Don't leak IO channels

In reply to Ross Lagerwall
Quoted 4 lines · * Ross Lagerwall ([email protected]) wrote:

Since qemu_fopen_channel_{in,out}put take references on the underlying IO channels, make sure to release our references to them.

Queued for migration.

Dave

Quoted 30 lines

---
New in v2.

 migration/savevm.c | 2 ++
 1 file changed, 2 insertions(+)
migration/savevm.c +2 -0
index 4a88228..87557dd 100644
@@ -2259,6 +2259,7 @@ void qmp_xen_save_devices_state(const char *filename, Error **errp)
     }
     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-save-state");
     f = qemu_fopen_channel_output(QIO_CHANNEL(ioc));
+    object_unref(OBJECT(ioc));
     ret = qemu_save_device_state(f);
     qemu_fclose(f);
     if (ret < 0) {
@@ -2292,6 +2293,7 @@ void qmp_xen_load_devices_state(const char *filename, Error **errp)
     }
     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-load-state");
     f = qemu_fopen_channel_input(QIO_CHANNEL(ioc));
+    object_unref(OBJECT(ioc));
 
     ret = qemu_loadvm_state(f);
     qemu_fclose(f);
Signature · 1 line
2.9.5

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#5 lore raw

PATCH v2 2/4 io: Fix QIOChannelFile when creating and opening read-write

In reply to Ross Lagerwall

The code wrongly passes the mode to open() only if O_WRONLY is set. Instead, the mode should be passed when O_CREAT is set (or O_TMPFILE on Linux). Fix this by always passing the mode since open() will correctly ignore the mode if it is not needed. Add a testcase which exercises this bug and also change the existing testcase to check that the mode of the created file is correct.

---
Changed in v2:
* Separated from qemu_open() change.

 include/io/channel-file.h    |  2 +-
 io/channel-file.c            |  6 +-----
 tests/test-io-channel-file.c | 29 +++++++++++++++++++++++++----
 3 files changed, 27 insertions(+), 10 deletions(-)
include/io/channel-file.h +1 -1
index 79245f1..ebfe54e 100644
@@ -73,7 +73,7 @@ qio_channel_file_new_fd(int fd);
  * qio_channel_file_new_path:
  * @path: the file path
  * @flags: the open flags (O_RDONLY|O_WRONLY|O_RDWR, etc)
- * @mode: the file creation mode if O_WRONLY is set in @flags
+ * @mode: the file creation mode if O_CREAT is set in @flags
  * @errp: pointer to initialized error object
  *
  * Create a new IO channel object for a file represented
io/channel-file.c +1 -5
index b383273..16bf7ed 100644
@@ -50,11 +50,7 @@ qio_channel_file_new_path(const char *path,
 
     ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE));
 
-    if (flags & O_WRONLY) {
-        ioc->fd = open(path, flags, mode);
-    } else {
-        ioc->fd = open(path, flags);
-    }
+    ioc->fd = open(path, flags, mode);
     if (ioc->fd < 0) {
         object_unref(OBJECT(ioc));
         error_setg_errno(errp, errno,
tests/test-io-channel-file.c +25 -4
index 6bfede6..2e94f63 100644
@@ -24,16 +24,21 @@
 #include "io-channel-helpers.h"
 #include "qapi/error.h"
 
-static void test_io_channel_file(void)
+#define TEST_FILE "tests/test-io-channel-file.txt"
+#define TEST_MASK 0600
+
+static void test_io_channel_file_helper(int flags)
 {
     QIOChannel *src, *dst;
     QIOChannelTest *test;
+    struct stat st;
+    mode_t mask;
+    int ret;
 
-#define TEST_FILE "tests/test-io-channel-file.txt"
     unlink(TEST_FILE);
     src = QIO_CHANNEL(qio_channel_file_new_path(
                           TEST_FILE,
-                          O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600,
+                          flags, TEST_MASK,
                           &error_abort));
     dst = QIO_CHANNEL(qio_channel_file_new_path(
                           TEST_FILE,
@@ -45,18 +50,33 @@ static void test_io_channel_file(void)
     qio_channel_test_run_reader(test, dst);
     qio_channel_test_validate(test);
 
+    /* Check that the requested mode took effect. */
+    mask = umask(0);
+    umask(mask);
+    ret = stat(TEST_FILE, &st);
+    g_assert_cmpint(ret, >, -1);
+    g_assert_cmpuint(TEST_MASK & ~mask, ==, st.st_mode & 0777);
+
     unlink(TEST_FILE);
     object_unref(OBJECT(src));
     object_unref(OBJECT(dst));
 }
 
+static void test_io_channel_file(void)
+{
+    test_io_channel_file_helper(O_WRONLY | O_CREAT | O_TRUNC | O_BINARY);
+}
+
+static void test_io_channel_file_rdwr(void)
+{
+    test_io_channel_file_helper(O_RDWR | O_CREAT | O_TRUNC | O_BINARY);
+}
 
 static void test_io_channel_fd(void)
 {
     QIOChannel *ioc;
     int fd = -1;
 
-#define TEST_FILE "tests/test-io-channel-file.txt"
     fd = open(TEST_FILE, O_CREAT | O_TRUNC | O_WRONLY, 0600);
     g_assert_cmpint(fd, >, -1);
 
@@ -114,6 +134,7 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
 
     g_test_add_func("/io/channel/file", test_io_channel_file);
+    g_test_add_func("/io/channel/file/rdwr", test_io_channel_file_rdwr);
     g_test_add_func("/io/channel/file/fd", test_io_channel_fd);
 #ifndef _WIN32
     g_test_add_func("/io/channel/pipe/sync", test_io_channel_pipe_sync);
Signature · 1 line
2.9.5
Reviewed-by on v2 2/4
#6 lore raw

Re: [Qemu-devel] [PATCH v2 2/4] io: Fix QIOChannelFile when creating and opening read-write

Quoted 8 lines · Ross Lagerwall

The code wrongly passes the mode to open() only if O_WRONLY is set. Instead, the mode should be passed when O_CREAT is set (or O_TMPFILE on Linux). Fix this by always passing the mode since open() will correctly ignore the mode if it is not needed. Add a testcase which exercises this bug and also change the existing testcase to check that the mode of the created file is correct.

Quoted 8 lines

---
Changed in v2:
* Separated from qemu_open() change.

 include/io/channel-file.h    |  2 +-
 io/channel-file.c            |  6 +-----
 tests/test-io-channel-file.c | 29 +++++++++++++++++++++++++----
 3 files changed, 27 insertions(+), 10 deletions(-)

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#7 lore raw

PATCH v2 3/4 io: Don't call close multiple times in QIOChannelFile

In reply to Ross Lagerwall

If the file descriptor underlying QIOChannelFile is closed in the io_close() method, don't close it again in the finalize() method since the file descriptor number may have been reused in the meantime.

---
New in v2.

 io/channel-file.c | 1 +
 1 file changed, 1 insertion(+)
io/channel-file.c +1 -0
index 16bf7ed..1f2f710 100644
@@ -178,6 +178,7 @@ static int qio_channel_file_close(QIOChannel *ioc,
                          "Unable to close file");
         return -1;
     }
+    fioc->fd = -1;
     return 0;
 }
 
Signature · 1 line
2.9.5
Reviewed-by on v2 3/4
#8 lore raw

Re: [Qemu-devel] [PATCH v2 3/4] io: Don't call close multiple times in QIOChannelFile

Quoted 5 lines · Ross Lagerwall

If the file descriptor underlying QIOChannelFile is closed in the io_close() method, don't close it again in the finalize() method since the file descriptor number may have been reused in the meantime.

Quoted 22 lines

---
New in v2.

 io/channel-file.c | 1 +
 1 file changed, 1 insertion(+)
io/channel-file.c +1 -1
index 16bf7ed..1f2f710 100644
@@ -178,6 +178,7 @@ static int qio_channel_file_close(QIOChannel *ioc,
                          "Unable to close file");
         return -1;
     }
+    fioc->fd = -1;
     return 0;
 }
 
--

2.9.5

Signature · 1 line
Marc-André Lureau
Reviewed-by on v2 3/4
#9 lore raw

Re: [Qemu-devel] [PATCH v2 3/4] io: Don't call close multiple times in QIOChannelFile

In reply to Ross Lagerwall
Quoted 10 lines · Ross Lagerwall

If the file descriptor underlying QIOChannelFile is closed in the io_close() method, don't close it again in the finalize() method since the file descriptor number may have been reused in the meantime.

---
New in v2.

 io/channel-file.c | 1 +
 1 file changed, 1 insertion(+)

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#10 lore raw

PATCH v2 4/4 io: Add /dev/fdset/ support to QIOChannelFile

In reply to Ross Lagerwall

Add /dev/fdset/ support to QIOChannelFile by calling qemu_open() instead of open() and qemu_close() instead of close(). There is a subtle semantic change since qemu_open() automatically sets O_CLOEXEC, but this doesn't affect any of the users of the function.

---
Changed in v2:
* Split into separate patch.

 io/channel-file.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
io/channel-file.c +3 -3
index 1f2f710..db948ab 100644
@@ -50,7 +50,7 @@ qio_channel_file_new_path(const char *path,
 
     ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE));
 
-    ioc->fd = open(path, flags, mode);
+    ioc->fd = qemu_open(path, flags, mode);
     if (ioc->fd < 0) {
         object_unref(OBJECT(ioc));
         error_setg_errno(errp, errno,
@@ -74,7 +74,7 @@ static void qio_channel_file_finalize(Object *obj)
 {
     QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj);
     if (ioc->fd != -1) {
-        close(ioc->fd);
+        qemu_close(ioc->fd);
         ioc->fd = -1;
     }
 }
@@ -173,7 +173,7 @@ static int qio_channel_file_close(QIOChannel *ioc,
 {
     QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
 
-    if (close(fioc->fd) < 0) {
+    if (qemu_close(fioc->fd) < 0) {
         error_setg_errno(errp, errno,
                          "Unable to close file");
         return -1;
Signature · 1 line
2.9.5
Reviewed-by on v2 4/4
#11 lore raw

Re: [Qemu-devel] [PATCH v2 4/4] io: Add /dev/fdset/ support to QIOChannelFile

Quoted 6 lines · Ross Lagerwall

Add /dev/fdset/ support to QIOChannelFile by calling qemu_open() instead of open() and qemu_close() instead of close(). There is a subtle semantic change since qemu_open() automatically sets O_CLOEXEC, but this doesn't affect any of the users of the function.

Quoted 42 lines

---
Changed in v2:
* Split into separate patch.

 io/channel-file.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
io/channel-file.c +3 -4
index 1f2f710..db948ab 100644
@@ -50,7 +50,7 @@ qio_channel_file_new_path(const char *path,
 
     ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE));
 
-    ioc->fd = open(path, flags, mode);
+    ioc->fd = qemu_open(path, flags, mode);
     if (ioc->fd < 0) {
         object_unref(OBJECT(ioc));
         error_setg_errno(errp, errno,
@@ -74,7 +74,7 @@ static void qio_channel_file_finalize(Object *obj)
 {
     QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj);
     if (ioc->fd != -1) {
-        close(ioc->fd);
+        qemu_close(ioc->fd);
         ioc->fd = -1;
     }
 }
@@ -173,7 +173,7 @@ static int qio_channel_file_close(QIOChannel *ioc,
 {
     QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
 
-    if (close(fioc->fd) < 0) {
+    if (qemu_close(fioc->fd) < 0) {
         error_setg_errno(errp, errno,
                          "Unable to close file");
         return -1;
--

2.9.5

Signature · 1 line
Marc-André Lureau
Reviewed-by on v2 4/4
#12 lore raw

Re: [Qemu-devel] [PATCH v2 4/4] io: Add /dev/fdset/ support to QIOChannelFile

In reply to Ross Lagerwall
Quoted 5 lines · Ross Lagerwall

Add /dev/fdset/ support to QIOChannelFile by calling qemu_open() instead of open() and qemu_close() instead of close(). There is a subtle semantic change since qemu_open() automatically sets O_CLOEXEC, but this doesn't affect any of the users of the function.

Quoted 7 lines

---
Changed in v2:
* Split into separate patch.

 io/channel-file.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#13 lore raw
In reply to Ross Lagerwall
Quoted 14 lines · Ross Lagerwall

Hi,

Here is a bug fix with the use of QIOChannelFile and 2 bug fixes and an improvement to implementation of QIOChannelFile.

Regards,
Ross Lagerwall

Ross Lagerwall (4):
migration: Don't leak IO channels
io: Fix QIOChannelFile when creating and opening read-write
io: Don't call close multiple times in QIOChannelFile
io: Add /dev/fdset/ support to QIOChannelFile

Quoted 6 lines
  include/io/channel-file.h    |  2 +-
  io/channel-file.c            | 11 ++++-------
  migration/savevm.c           |  2 ++
  tests/test-io-channel-file.c | 29 +++++++++++++++++++++++++----
  4 files changed, 32 insertions(+), 12 deletions(-)

Ping for reviews...

v1:
Got feedback from Daniel P. Berrange and Marc-André Lureau.

v2:
Patch 1: Unreviewed
Patch 2: Unreviewed
Patch 3: Reviewed by Marc-André Lureau
Patch 4: Reviewed by Marc-André Lureau

The patch series still applies cleanly on top of master.

Thanks,

Signature · 1 line
Ross Lagerwall
#14 lore raw
Quoted 27 lines · Ross Lagerwall
Quoted 14 lines · Ross Lagerwall

Hi,

Here is a bug fix with the use of QIOChannelFile and 2 bug fixes and an improvement to implementation of QIOChannelFile.

Regards,
Ross Lagerwall

Ross Lagerwall (4):
migration: Don't leak IO channels
io: Fix QIOChannelFile when creating and opening read-write
io: Don't call close multiple times in QIOChannelFile
io: Add /dev/fdset/ support to QIOChannelFile

Quoted 6 lines
  include/io/channel-file.h    |  2 +-
  io/channel-file.c            | 11 ++++-------
  migration/savevm.c           |  2 ++
  tests/test-io-channel-file.c | 29 +++++++++++++++++++++++++----
  4 files changed, 32 insertions(+), 12 deletions(-)

Ping for reviews...

v1:
Got feedback from Daniel P. Berrange and Marc-André Lureau.

Quoted 7 lines

v2:
Patch 1: Unreviewed
Patch 2: Unreviewed
Patch 3: Reviewed by Marc-André Lureau
Patch 4: Reviewed by Marc-André Lureau

The patch series still applies cleanly on top of master.

Sorry my bad for forgetting this. I'll review it now.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#15 lore raw
In reply to Ross Lagerwall
Quoted 5 lines · Ross Lagerwall

Hi,

Here is a bug fix with the use of QIOChannelFile and 2 bug fixes and an improvement to implementation of QIOChannelFile.

Quoted 8 lines

Regards,
Ross Lagerwall

Ross Lagerwall (4):
migration: Don't leak IO channels
io: Fix QIOChannelFile when creating and opening read-write
io: Don't call close multiple times in QIOChannelFile
io: Add /dev/fdset/ support to QIOChannelFile

I've queued the 3 io patches, but will leave the migration one for the migration maintainer's queue.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|