218e99
From ddc3793752ae726e761173ce214ab895b936d15a Mon Sep 17 00:00:00 2001
218e99
From: Laszlo Ersek <lersek@redhat.com>
218e99
Date: Fri, 19 Jul 2013 13:05:22 +0200
218e99
Subject: char: io_channel_send: don't lose written bytes
218e99
218e99
RH-Author: Laszlo Ersek <lersek@redhat.com>
218e99
Message-id: <1374239123-4841-2-git-send-email-lersek@redhat.com>
218e99
Patchwork-id: 52615
218e99
O-Subject: [RHEL-7 qemu-kvm PATCH 1/2] char: io_channel_send: don't lose written bytes
218e99
Bugzilla: 970047
218e99
RH-Acked-by: Amit Shah <amit.shah@redhat.com>
218e99
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
218e99
RH-Acked-by: Luiz Capitulino <lcapitulino@redhat.com>
218e99
218e99
The g_io_channel_write_chars() documentation states,
218e99
218e99
  bytes_written: The number of bytes written. This can be nonzero even if
218e99
                 the return value is not G_IO_STATUS_NORMAL. [...]
218e99
218e99
io_channel_send() could lose such bytes before.
218e99
218e99
Furthermore, the (status == G_IO_STATUS_EOF) condition used to evaluate to
218e99
constant false whenever it was reached. When that condition actually held,
218e99
it always led to -1 / EINVAL. This patch (almost) distinguishes
218e99
G_IO_STATUS_EOF only when no bytes have been written, and then treats it
218e99
as an error.
218e99
218e99
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
218e99
Reviewed-by: Amit Shah <amit.shah@redhat.com>
218e99
Reviewed-by: Anthony Liguori <aliguori@us.ibm.com>
218e99
Message-id: 1373998781-29561-2-git-send-email-lersek@redhat.com
218e99
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
218e99
(cherry picked from commit ac8c26f633b01bb32cdf347f9dbd5a80c6712925)
218e99
218e99
diff --git a/qemu-char.c b/qemu-char.c
218e99
index efe706d..c01deae 100644
218e99
--- a/qemu-char.c
218e99
+++ b/qemu-char.c
218e99
@@ -722,35 +722,32 @@ static GIOChannel *io_channel_from_socket(int fd)
218e99
 
218e99
 static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
218e99
 {
218e99
-    GIOStatus status;
218e99
-    size_t offset;
218e99
+    size_t offset = 0;
218e99
+    GIOStatus status = G_IO_STATUS_NORMAL;
218e99
 
218e99
-    offset = 0;
218e99
-    while (offset < len) {
218e99
-        gsize bytes_written;
218e99
+    while (offset < len && status == G_IO_STATUS_NORMAL) {
218e99
+        gsize bytes_written = 0;
218e99
 
218e99
         status = g_io_channel_write_chars(fd, buf + offset, len - offset,
218e99
                                           &bytes_written, NULL);
218e99
-        if (status != G_IO_STATUS_NORMAL) {
218e99
-            if (status == G_IO_STATUS_AGAIN) {
218e99
-                /* If we've written any data, return a partial write. */
218e99
-                if (offset) {
218e99
-                    break;
218e99
-                }
218e99
-                errno = EAGAIN;
218e99
-            } else {
218e99
-                errno = EINVAL;
218e99
-            }
218e99
-
218e99
-            return -1;
218e99
-        } else if (status == G_IO_STATUS_EOF) {
218e99
-            break;
218e99
-        }
218e99
-
218e99
         offset += bytes_written;
218e99
     }
218e99
 
218e99
-    return offset;
218e99
+    if (offset > 0) {
218e99
+        return offset;
218e99
+    }
218e99
+    switch (status) {
218e99
+    case G_IO_STATUS_NORMAL:
218e99
+        g_assert(len == 0);
218e99
+        return 0;
218e99
+    case G_IO_STATUS_AGAIN:
218e99
+        errno = EAGAIN;
218e99
+        return -1;
218e99
+    default:
218e99
+        break;
218e99
+    }
218e99
+    errno = EINVAL;
218e99
+    return -1;
218e99
 }
218e99
 
218e99
 #ifndef _WIN32