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