diff mbox

[PULL,v2,047/106] Add G_IO_HUP handler for socket chardev

Message ID 1403108034-32054-48-git-send-email-mst@redhat.com
State New
Headers show

Commit Message

Michael S. Tsirkin June 18, 2014, 4:18 p.m. UTC
From: Nikolay Nikolaev <n.nikolaev@virtualopensystems.com>

This is used to detect that the remote end has disconnected. Just call
tcp_char_disconnect on receiving this event.

Signed-off-by: Antonios Motakis <a.motakis@virtualopensystems.com>
Signed-off-by: Nikolay Nikolaev <n.nikolaev@virtualopensystems.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
 include/sysemu/char.h |  1 +
 qemu-char.c           | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)
diff mbox

Patch

diff --git a/include/sysemu/char.h b/include/sysemu/char.h
index 930aaf1..3b835f6 100644
--- a/include/sysemu/char.h
+++ b/include/sysemu/char.h
@@ -83,6 +83,7 @@  struct CharDriverState {
     int avail_connections;
     int is_mux;
     guint fd_in_tag;
+    guint fd_hup_tag;
     QemuOpts *opts;
     QTAILQ_ENTRY(CharDriverState) next;
 };
diff --git a/qemu-char.c b/qemu-char.c
index b9bef44..b3bd3b5 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -2680,6 +2680,25 @@  CharDriverState *qemu_chr_open_eventfd(int eventfd)
 }
 #endif
 
+static gboolean tcp_chr_chan_close(GIOChannel *channel, GIOCondition cond,
+                                   void *opaque)
+{
+    CharDriverState *chr = opaque;
+
+    if (cond != G_IO_HUP) {
+        return FALSE;
+    }
+
+    /* connection closed */
+    tcp_chr_disconnect(chr);
+    if (chr->fd_hup_tag) {
+        g_source_remove(chr->fd_hup_tag);
+        chr->fd_hup_tag = 0;
+    }
+
+    return TRUE;
+}
+
 static void tcp_chr_connect(void *opaque)
 {
     CharDriverState *chr = opaque;
@@ -2689,6 +2708,8 @@  static void tcp_chr_connect(void *opaque)
     if (s->chan) {
         chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
                                            tcp_chr_read, chr);
+        chr->fd_hup_tag = g_io_add_watch(s->chan, G_IO_HUP, tcp_chr_chan_close,
+                                         chr);
     }
     qemu_chr_be_generic_open(chr);
 }