<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">From 90f4799a588a921b407310cb30e6c580e2a8e282 Mon Sep 17 00:00:00 2001
Message-Id: &lt;90f4799a588a921b407310cb30e6c580e2a8e282.1368111914.git.minovotn@redhat.com&gt;
In-Reply-To: &lt;405603258af5154387bea676be1f904b6713f6ae.1368111913.git.minovotn@redhat.com&gt;
References: &lt;405603258af5154387bea676be1f904b6713f6ae.1368111913.git.minovotn@redhat.com&gt;
From: Amit Shah &lt;amit.shah@redhat.com&gt;
Date: Wed, 24 Apr 2013 08:18:09 +0200
Subject: [PATCH 35/65] qemu-char: convert pty to GIOChannel

RH-Author: Amit Shah &lt;amit.shah@redhat.com&gt;
Message-id: &lt;bb1fffc727f0641470c22f215d3e8bc8b8d120ae.1366724981.git.amit.shah@redhat.com&gt;
Patchwork-id: 50811
O-Subject: [RHEL6.5 qemu-kvm PATCH 35/65] qemu-char: convert pty to GIOChannel
Bugzilla: 909059
RH-Acked-by: Hans de Goede &lt;hdegoede@redhat.com&gt;
RH-Acked-by: Gerd Hoffmann &lt;kraxel@redhat.com&gt;
RH-Acked-by: Paolo Bonzini &lt;pbonzini@redhat.com&gt;

From: Anthony Liguori &lt;aliguori@us.ibm.com&gt;

Signed-off-by: Anthony Liguori &lt;aliguori@us.ibm.com&gt;
Signed-off-by: Amit Shah &lt;amit.shah@redhat.com&gt;
Message-id: 339eebf3c59a450b0354056e9ac4b41f67230831.1362505276.git.amit.shah@redhat.com
Signed-off-by: Anthony Liguori &lt;aliguori@us.ibm.com&gt;
(cherry picked from commit 093d3a20055282e282ba056addbe59b79e13a32f)

Signed-off-by: Amit Shah &lt;amit.shah@redhat.com&gt;

Conflicts:
	qemu-char.c

    * upstream did s/qemu_new_timer/qemu_new_timer_ms/

fixup convert to pty
---
 qemu-char.c | 43 +++++++++++++++++++++++++++----------------
 1 file changed, 27 insertions(+), 16 deletions(-)

Signed-off-by: Michal Novotny &lt;minovotn@redhat.com&gt;
---
 qemu-char.c | 43 +++++++++++++++++++++++++++----------------
 1 file changed, 27 insertions(+), 16 deletions(-)

diff --git a/qemu-char.c b/qemu-char.c
index 413b12c..208fd89 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -943,7 +943,8 @@ static void cfmakeraw (struct termios *termios_p)
     || defined(__GLIBC__)
 
 typedef struct {
-    int fd;
+    GIOChannel *fd;
+    guint fd_tag;
     int connected;
     int polling;
     int read_bytes;
@@ -962,7 +963,7 @@ static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
         pty_chr_update_read_handler(chr);
         return 0;
     }
-    return send_all(s-&gt;fd, buf, len);
+    return io_channel_send_all(s-&gt;fd, buf, len);
 }
 
 static int pty_chr_read_poll(void *opaque)
@@ -974,36 +975,39 @@ static int pty_chr_read_poll(void *opaque)
     return s-&gt;read_bytes;
 }
 
-static void pty_chr_read(void *opaque)
+static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
 {
     CharDriverState *chr = opaque;
     PtyCharDriver *s = chr-&gt;opaque;
-    int size, len;
+    gsize size, len;
     uint8_t buf[READ_BUF_LEN];
+    GIOStatus status;
 
     len = sizeof(buf);
     if (len &gt; s-&gt;read_bytes)
         len = s-&gt;read_bytes;
     if (len == 0)
-        return;
-    size = read(s-&gt;fd, buf, len);
-    if ((size == -1 &amp;&amp; errno == EIO) ||
-        (size == 0)) {
+        return FALSE;
+    status = g_io_channel_read_chars(s-&gt;fd, (gchar *)buf, len, &amp;size, NULL);
+    if (status != G_IO_STATUS_NORMAL) {
         pty_chr_state(chr, 0);
-        return;
-    }
-    if (size &gt; 0) {
+        return FALSE;
+    } else {
         pty_chr_state(chr, 1);
         qemu_chr_be_write(chr, buf, size);
     }
+    return TRUE;
 }
 
 static void pty_chr_update_read_handler(CharDriverState *chr)
 {
     PtyCharDriver *s = chr-&gt;opaque;
 
-    qemu_set_fd_handler2(s-&gt;fd, pty_chr_read_poll,
-                         pty_chr_read, NULL, chr);
+    if (s-&gt;fd_tag) {
+        g_source_remove(s-&gt;fd_tag);
+    }
+
+    s-&gt;fd_tag = io_add_watch_poll(s-&gt;fd, pty_chr_read_poll, pty_chr_read, chr);
     s-&gt;polling = 1;
     /*
      * Short timeout here: just need wait long enougth that qemu makes
@@ -1021,7 +1025,8 @@ static void pty_chr_state(CharDriverState *chr, int connected)
     PtyCharDriver *s = chr-&gt;opaque;
 
     if (!connected) {
-        qemu_set_fd_handler2(s-&gt;fd, NULL, NULL, NULL, NULL);
+        g_source_remove(s-&gt;fd_tag);
+        s-&gt;fd_tag = 0;
         s-&gt;connected = 0;
         s-&gt;polling = 0;
         /* (re-)connect poll interval for idle guests: once per second.
@@ -1056,9 +1061,14 @@ static void pty_chr_timer(void *opaque)
 static void pty_chr_close(struct CharDriverState *chr)
 {
     PtyCharDriver *s = chr-&gt;opaque;
+    int fd;
 
-    qemu_set_fd_handler2(s-&gt;fd, NULL, NULL, NULL, NULL);
-    close(s-&gt;fd);
+    if (s-&gt;fd_tag) {
+        g_source_remove(s-&gt;fd_tag);
+    }
+    fd = g_io_channel_unix_get_fd(s-&gt;fd);
+    g_io_channel_unref(s-&gt;fd);
+    close(fd);
     qemu_del_timer(s-&gt;timer);
     qemu_free_timer(s-&gt;timer);
     g_free(s);
@@ -1103,6 +1113,7 @@ static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
     chr-&gt;chr_update_read_handler = pty_chr_update_read_handler;
     chr-&gt;chr_close = pty_chr_close;
 
+    s-&gt;fd = io_channel_from_fd(master_fd);
     s-&gt;timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
 
     return chr;
-- 
1.7.11.7

</pre></body></html>