Skip to contentSkip to conversation

Simple qemu-ga fixes and cleanups

PATCH Applied Markus Armbruster 4 participants 17 messages Jan 11, 2013 – Jan 14, 2013

lore
Share & tools
lore
#0 lore raw

cover Simple qemu-ga fixes and cleanups

Markus Armbruster (6):
qemu-ga: Document intentional fall through in channel_event_cb()
qemu-ga: Drop pointless lseek() from ga_open_pidfile()
qemu-ga: Plug file descriptor leak on ga_open_pidfile() error path
qemu-ga: Plug fd leak on ga_channel_listen_accept() error path
qemu-ga: Plug fd leak on ga_channel_open() error paths
qemu-ga: Handle errors uniformely in ga_channel_open()

 qga/channel-posix.c | 13 +++++++++----
 qga/main.c          |  5 ++++-
 2 files changed, 13 insertions(+), 5 deletions(-)
Signature · 1 line
1.7.11.7
#1 lore raw

PATCH 1/6 qemu-ga: Document intentional fall through in channel_event_cb()

For clarity, and to hush up Coverity.

---
 qga/main.c | 1 +
 1 file changed, 1 insertion(+)
qga/main.c +1 -0
index a9b968c..47a6bea 100644
@@ -605,6 +605,7 @@ static gboolean channel_event_cb(GIOCondition condition, gpointer data)
         if (!s->virtio) {
             return false;
         }
+        /* fall through */
     case G_IO_STATUS_AGAIN:
         /* virtio causes us to spin here when no process is attached to
          * host-side chardev. sleep a bit to mitigate this
Signature · 1 line
1.7.11.7
Reviewed-by on v1 1/6
#2 lore raw

Re: [Qemu-devel] [PATCH 1/6] qemu-ga: Document intentional fall through in channel_event_cb()

Quoted 3 lines · Markus Armbruster

For clarity, and to hush up Coverity.

Quoted 19 lines
---
 qga/main.c | 1 +
 1 file changed, 1 insertion(+)
qga/main.c +1 -0
index a9b968c..47a6bea 100644
@@ -605,6 +605,7 @@ static gboolean channel_event_cb(GIOCondition condition, gpointer data)
         if (!s->virtio) {
             return false;
         }
+        /* fall through */
     case G_IO_STATUS_AGAIN:
         /* virtio causes us to spin here when no process is attached to
          * host-side chardev. sleep a bit to mitigate this
Signature · 1 line
1.7.11.7
#3 lore raw

PATCH 2/6 qemu-ga: Drop pointless lseek() from ga_open_pidfile()

In reply to Markus Armbruster

After open(), the file offset is already zero, and neither lockf() nor ftruncate() change it.

---
 qga/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
qga/main.c +1 -1
index 47a6bea..1a22d8d 100644
@@ -276,7 +276,7 @@ static bool ga_open_pidfile(const char *pidfile)
         return false;
     }
 
-    if (ftruncate(pidfd, 0) || lseek(pidfd, 0, SEEK_SET)) {
+    if (ftruncate(pidfd, 0)) {
         g_critical("Failed to truncate pid file");
         goto fail;
     }
Signature · 1 line
1.7.11.7
Reviewed-by on v1 2/6
#4 lore raw

Re: [Qemu-devel] [PATCH 2/6] qemu-ga: Drop pointless lseek() from ga_open_pidfile()

Quoted 4 lines · Markus Armbruster

After open(), the file offset is already zero, and neither lockf() nor ftruncate() change it.

Quoted 20 lines
---
 qga/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
qga/main.c +1 -1
index 47a6bea..1a22d8d 100644
@@ -276,7 +276,7 @@ static bool ga_open_pidfile(const char *pidfile)
         return false;
     }
 
-    if (ftruncate(pidfd, 0) || lseek(pidfd, 0, SEEK_SET)) {
+    if (ftruncate(pidfd, 0)) {
         g_critical("Failed to truncate pid file");
         goto fail;
     }
Signature · 1 line
1.7.11.7
#5 lore raw

PATCH 3/6 qemu-ga: Plug file descriptor leak on ga_open_pidfile() error path

In reply to Markus Armbruster

Spotted by Coverity. Also document why we keep it open on success.

---
 qga/main.c | 2 ++
 1 file changed, 2 insertions(+)
qga/main.c +2 -0
index 1a22d8d..db47427 100644
@@ -286,10 +286,12 @@ static bool ga_open_pidfile(const char *pidfile)
         goto fail;
     }
 
+    /* keep pidfile open & locked forever */
     return true;
 
 fail:
     unlink(pidfile);
+    close(pidfd);
     return false;
 }
 #else /* _WIN32 */
Signature · 1 line
1.7.11.7
Reviewed-by on v1 3/6
#6 lore raw

Re: [Qemu-devel] [PATCH 3/6] qemu-ga: Plug file descriptor leak on ga_open_pidfile() error path

Quoted 3 lines · Markus Armbruster

Spotted by Coverity. Also document why we keep it open on success.

Quoted 24 lines
---
 qga/main.c | 2 ++
 1 file changed, 2 insertions(+)
qga/main.c +2 -0
index 1a22d8d..db47427 100644
@@ -286,10 +286,12 @@ static bool ga_open_pidfile(const char *pidfile)
         goto fail;
     }
 
+    /* keep pidfile open & locked forever */
     return true;
 
 fail:
     unlink(pidfile);
+    close(pidfd);
     return false;
 }
 #else /* _WIN32 */
Signature · 1 line
1.7.11.7
#7 lore raw

PATCH 4/6 qemu-ga: Plug fd leak on ga_channel_listen_accept() error path

In reply to Markus Armbruster

Spotted by Coverity.

---
 qga/channel-posix.c | 1 +
 1 file changed, 1 insertion(+)
qga/channel-posix.c +1 -0
index d4fd628..5579185 100644
@@ -45,6 +45,7 @@ static gboolean ga_channel_listen_accept(GIOChannel *channel,
     ret = ga_channel_client_add(c, client_fd);
     if (ret) {
         g_warning("error setting up connection");
+        close(client_fd);
         goto out;
     }
     accepted = true;
Signature · 1 line
1.7.11.7
Reviewed-by on v1 4/6
#8 lore raw

Re: [Qemu-devel] [PATCH 4/6] qemu-ga: Plug fd leak on ga_channel_listen_accept() error path

Quoted 3 lines · Markus Armbruster

Spotted by Coverity.

Quoted 19 lines
---
 qga/channel-posix.c | 1 +
 1 file changed, 1 insertion(+)
qga/channel-posix.c +1 -0
index d4fd628..5579185 100644
@@ -45,6 +45,7 @@ static gboolean ga_channel_listen_accept(GIOChannel *channel,
     ret = ga_channel_client_add(c, client_fd);
     if (ret) {
         g_warning("error setting up connection");
+        close(client_fd);
         goto out;
     }
     accepted = true;
Signature · 1 line
1.7.11.7
#9 lore raw

PATCH 5/6 qemu-ga: Plug fd leak on ga_channel_open() error paths

In reply to Markus Armbruster

Spotted by Coverity.

---
 qga/channel-posix.c | 1 +
 1 file changed, 1 insertion(+)
qga/channel-posix.c +1 -0
index 5579185..b530808 100644
@@ -153,6 +153,7 @@ static gboolean ga_channel_open(GAChannel *c, const gchar *path, GAChannelMethod
         ret = ga_channel_client_add(c, fd);
         if (ret) {
             g_critical("error adding channel to main loop");
+            close(fd);
             return false;
         }
         break;
Signature · 1 line
1.7.11.7
Reviewed-by on v1 5/6
#10 lore raw

Re: [Qemu-devel] [PATCH 5/6] qemu-ga: Plug fd leak on ga_channel_open() error paths

Quoted 3 lines · Markus Armbruster

Spotted by Coverity.

Quoted 19 lines
---
 qga/channel-posix.c | 1 +
 1 file changed, 1 insertion(+)
qga/channel-posix.c +1 -0
index 5579185..b530808 100644
@@ -153,6 +153,7 @@ static gboolean ga_channel_open(GAChannel *c, const gchar *path, GAChannelMethod
         ret = ga_channel_client_add(c, fd);
         if (ret) {
             g_critical("error adding channel to main loop");
+            close(fd);
             return false;
         }
         break;
Signature · 1 line
1.7.11.7
#11 lore raw

PATCH 6/6 qemu-ga: Handle errors uniformely in ga_channel_open()

In reply to Markus Armbruster

We detect errors in seven places. One reports with g_error(), which calls abort(), the others report with g_critical(). Three of them exit(), three return false.

Always report with g_critical(), and return false.

---
 qga/channel-posix.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
qga/channel-posix.c +7 -4
index b530808..96274f5 100644
@@ -140,14 +140,15 @@ static gboolean ga_channel_open(GAChannel *c, const gchar *path, GAChannelMethod
                            );
         if (fd == -1) {
             g_critical("error opening channel: %s", strerror(errno));
-            exit(EXIT_FAILURE);
+            return false;
         }
 #ifdef CONFIG_SOLARIS
         ret = ioctl(fd, I_SETSIG, S_OUTPUT | S_INPUT | S_HIPRI);
         if (ret == -1) {
             g_critical("error setting event mask for channel: %s",
                        strerror(errno));
-            exit(EXIT_FAILURE);
+            close(fd);
+            return false;
         }
 #endif
         ret = ga_channel_client_add(c, fd);
@@ -163,7 +164,7 @@ static gboolean ga_channel_open(GAChannel *c, const gchar *path, GAChannelMethod
         int fd = qemu_open(path, O_RDWR | O_NOCTTY | O_NONBLOCK);
         if (fd == -1) {
             g_critical("error opening channel: %s", strerror(errno));
-            exit(EXIT_FAILURE);
+            return false;
         }
         tcgetattr(fd, &tio);
         /* set up serial port for non-canonical, dumb byte streaming */
@@ -183,7 +184,9 @@ static gboolean ga_channel_open(GAChannel *c, const gchar *path, GAChannelMethod
         tcsetattr(fd, TCSANOW, &tio);
         ret = ga_channel_client_add(c, fd);
         if (ret) {
-            g_error("error adding channel to main loop");
+            g_critical("error adding channel to main loop");
+            close(fd);
+            return false;
         }
         break;
     }
Signature · 1 line
1.7.11.7
Reviewed-by on v1 6/6
#12 lore raw

Re: [Qemu-devel] [PATCH 6/6] qemu-ga: Handle errors uniformely in ga_channel_open()

Quoted 1 line · Markus Armbruster

We detect errors in seven places. One reports with g_error(), which

Do you mean "in several places"? I can fix this in tree.

Quoted 6 lines

calls abort(), the others report with g_critical(). Three of them exit(), three return false.

Always report with g_critical(), and return false.

Quoted 49 lines
---
 qga/channel-posix.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
qga/channel-posix.c +7 -4
index b530808..96274f5 100644
@@ -140,14 +140,15 @@ static gboolean ga_channel_open(GAChannel *c, const gchar *path, GAChannelMethod
                            );
         if (fd == -1) {
             g_critical("error opening channel: %s", strerror(errno));
-            exit(EXIT_FAILURE);
+            return false;
         }
 #ifdef CONFIG_SOLARIS
         ret = ioctl(fd, I_SETSIG, S_OUTPUT | S_INPUT | S_HIPRI);
         if (ret == -1) {
             g_critical("error setting event mask for channel: %s",
                        strerror(errno));
-            exit(EXIT_FAILURE);
+            close(fd);
+            return false;
         }
 #endif
         ret = ga_channel_client_add(c, fd);
@@ -163,7 +164,7 @@ static gboolean ga_channel_open(GAChannel *c, const gchar *path, GAChannelMethod
         int fd = qemu_open(path, O_RDWR | O_NOCTTY | O_NONBLOCK);
         if (fd == -1) {
             g_critical("error opening channel: %s", strerror(errno));
-            exit(EXIT_FAILURE);
+            return false;
         }
         tcgetattr(fd, &tio);
         /* set up serial port for non-canonical, dumb byte streaming */
@@ -183,7 +184,9 @@ static gboolean ga_channel_open(GAChannel *c, const gchar *path, GAChannelMethod
         tcsetattr(fd, TCSANOW, &tio);
         ret = ga_channel_client_add(c, fd);
         if (ret) {
-            g_error("error adding channel to main loop");
+            g_critical("error adding channel to main loop");
+            close(fd);
+            return false;
         }
         break;
     }
Signature · 1 line
1.7.11.7
#13 lore raw

Re: [Qemu-devel] [PATCH 6/6] qemu-ga: Handle errors uniformely in ga_channel_open()

Quoted 4 lines · mdroth
Quoted 1 line · Markus Armbruster

We detect errors in seven places. One reports with g_error(), which

Do you mean "in several places"? I can fix this in tree.

I counted seven places, but it doesn't really matter, "several" would be fine, too.

Quoted 8 lines
Quoted 6 lines

calls abort(), the others report with g_critical(). Three of them exit(), three return false.

Always report with g_critical(), and return false.

Thanks!

#14 lore raw
In reply to Markus Armbruster
Quoted 7 lines · Markus Armbruster

Markus Armbruster (6):
qemu-ga: Document intentional fall through in channel_event_cb()
qemu-ga: Drop pointless lseek() from ga_open_pidfile()
qemu-ga: Plug file descriptor leak on ga_open_pidfile() error path
qemu-ga: Plug fd leak on ga_channel_listen_accept() error path
qemu-ga: Plug fd leak on ga_channel_open() error paths
qemu-ga: Handle errors uniformely in ga_channel_open()

Series: Reviewed-by: Eric Blake <[email protected]>

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on the v1 series
#15 lore raw
In reply to Markus Armbruster
Quoted 7 lines · On Fri, 11 Jan 2013 11:24:56 +0100 Markus Armbruster <[email protected]> wrote:

Markus Armbruster (6):
qemu-ga: Document intentional fall through in channel_event_cb()
qemu-ga: Drop pointless lseek() from ga_open_pidfile()
qemu-ga: Plug file descriptor leak on ga_open_pidfile() error path
qemu-ga: Plug fd leak on ga_channel_listen_accept() error path
qemu-ga: Plug fd leak on ga_channel_open() error paths
qemu-ga: Handle errors uniformely in ga_channel_open()

series:

Quoted 5 lines
 qga/channel-posix.c | 13 +++++++++----
 qga/main.c          |  5 ++++-
 2 files changed, 13 insertions(+), 5 deletions(-)
Applied
#16 lore raw
In reply to Markus Armbruster
Quoted 11 lines · Markus Armbruster

Markus Armbruster (6):
qemu-ga: Document intentional fall through in channel_event_cb()
qemu-ga: Drop pointless lseek() from ga_open_pidfile()
qemu-ga: Plug file descriptor leak on ga_open_pidfile() error path
qemu-ga: Plug fd leak on ga_channel_listen_accept() error path
qemu-ga: Plug fd leak on ga_channel_open() error paths
qemu-ga: Handle errors uniformely in ga_channel_open()

 qga/channel-posix.c | 13 +++++++++----
 qga/main.c          |  5 ++++-
 2 files changed, 13 insertions(+), 5 deletions(-)

Thanks, applied to qga branch with minor commit msg fix for patch 6.

Quoted 4 lines
Signature · 2 lines
-- 
1.7.11.7