From 3bdb8fe9312c4134f21385e0dfa80b471cf2b66f Mon Sep 17 00:00:00 2001
From: Amit Shah <amit.shah@redhat.com>
Date: Tue, 12 Jul 2011 09:23:48 -0300
Subject: [RHEL6 qemu-kvm PATCH 2/2] virtio-console: Prevent abort()s in case of host chardev close

RH-Author: Amit Shah <amit.shah@redhat.com>
Message-id: <b41249aba350ff43a4d5dbfff171a597f566b1fa.1310462624.git.amit.shah@redhat.com>
Patchwork-id: 29495
O-Subject: [RHEL6.2 PATCH 1/1] virtio-console: Prevent abort()s in case of host chardev close
Bugzilla: 720535
RH-Acked-by: Jes Sorensen <Jes.Sorensen@redhat.com>
RH-Acked-by: Juan Quintela <quintela@redhat.com>
RH-Acked-by: Gerd Hoffmann <kraxel@redhat.com>

A host chardev could close just before the guest sends some data to be
written.  This will cause an -EPIPE error.  This shouldn't be propagated
to virtio-serial-bus.

Ideally we should close the port once -EPIPE is received, but since the
chardev interface doesn't return such meaningful values to its users,
all we get is -1 for any kind of error.  Just return 0 for now and wait
for chardevs to return better error messages to act better on the return
messages.

Bugzilla: 720535
Upstream: No flow control upstream; slightly different patch posted.

Signed-off-by: Amit Shah <amit.shah@redhat.com>
---
 hw/virtio-console.c |   18 +++++++++++++++++-
 1 files changed, 17 insertions(+), 1 deletions(-)

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/virtio-console.c |   18 +++++++++++++++++-
 1 files changed, 17 insertions(+), 1 deletions(-)

diff --git a/hw/virtio-console.c b/hw/virtio-console.c
index 1287939..923bc8c 100644
--- a/hw/virtio-console.c
+++ b/hw/virtio-console.c
@@ -34,8 +34,24 @@ static void chr_write_unblocked(void *opaque)
 static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
 {
     VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
+    ssize_t ret;
 
-    return qemu_chr_write(vcon->chr, buf, len);
+    ret = qemu_chr_write(vcon->chr, buf, len);
+    if (ret < 0 && ret != -EAGAIN) {
+        /*
+         * Ideally we'd get a better error code than just -1, but
+         * that's what the chardev interface gives us right now.  If
+         * we had a finer-grained message, like -EPIPE, we could close
+         * this connection.  Absent such error messages, the most we
+         * can do is to return 0 here.
+         *
+         * This will prevent stray -1 values to go to
+         * virtio-serial-bus.c and cause abort()s in
+         * do_flush_queued_data().
+         */
+        ret = 0;
+    }
+    return ret;
 }
 
 /* Callback function that's called when the guest opens the port */
-- 
1.7.3.2

