Skip to contentSkip to conversation

qemu-char: Do not disconnect when there's data for reading

PATCH Superseded Zifei Tong 2 participants 4 messages Sep 18, 2014 – Sep 19, 2014

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Zifei Tong

author
#0 lore raw

PATCH v2 1/1 qemu-char: Do not disconnect when there's data for reading

After commit 812c1057f6175ac9a9829fa2920a2b5783814193 (Handle G_IO_HUP in tcp_chr_read for tcp chardev), connections are disconnected when in G_IO_HUP condition.

However, it's possible that there is still data for reading in the channel. In that case, the remaining data is not handled.

I saw a related bug when running socat in write-only mode, after

  $ echo "quit" | socat -u - UNIX-CONNECT:qemu-monitor

the monitor won't not run the 'quit' command.

Instead of GIOCondition, this patch uses the return value of tcp_chr_recv() to check the state of connection as suggested by Kirill.

Cc Kirill Batuzov <[email protected]>
Cc Nikolay Nikolaev <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Anthony Liguori <[email protected]>
---
 qemu-char.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
qemu-char.c +1 -7
index 2a3cb9f..6cc69fa 100644
@@ -2692,12 +2692,6 @@ static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
     uint8_t buf[READ_BUF_LEN];
     int len, size;
 
-    if (cond & G_IO_HUP) {
-        /* connection closed */
-        tcp_chr_disconnect(chr);
-        return TRUE;
-    }
-
     if (!s->connected || s->max_size <= 0) {
         return TRUE;
     }
@@ -2705,7 +2699,7 @@ static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
     if (len > s->max_size)
         len = s->max_size;
     size = tcp_chr_recv(chr, (void *)buf, len);
-    if (size == 0) {
+    if (size == 0 || (size < 0 && !(errno == EAGAIN || errno == EINTR))) {
         /* connection closed */
         tcp_chr_disconnect(chr);
     } else if (size > 0) {
Signature · 1 line
2.1.0
#1 lore raw
Quoted 43 lines · Zifei Tong

After commit 812c1057f6175ac9a9829fa2920a2b5783814193 (Handle G_IO_HUP in tcp_chr_read for tcp chardev), connections are disconnected when in G_IO_HUP condition.

However, it's possible that there is still data for reading in the channel. In that case, the remaining data is not handled.

I saw a related bug when running socat in write-only mode, after

  $ echo "quit" | socat -u - UNIX-CONNECT:qemu-monitor

the monitor won't not run the 'quit' command.

Instead of GIOCondition, this patch uses the return value of tcp_chr_recv() to check the state of connection as suggested by Kirill.

Cc Kirill Batuzov <[email protected]>
Cc Nikolay Nikolaev <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Anthony Liguori <[email protected]>
---
 qemu-char.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
qemu-char.c +0 -6
index 2a3cb9f..6cc69fa 100644
@@ -2692,12 +2692,6 @@ static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
     uint8_t buf[READ_BUF_LEN];
     int len, size;
 
-    if (cond & G_IO_HUP) {
-        /* connection closed */
-        tcp_chr_disconnect(chr);
-        return TRUE;
-    }
-
     if (!s->connected || s->max_size <= 0) {
         return TRUE;
     }

qemu-char.c static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) · line 2705

Quoted 8 lines
qemu-char.c +1 -1
@@ -2705,7 +2699,7 @@ static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
     if (len > s->max_size)
         len = s->max_size;
     size = tcp_chr_recv(chr, (void *)buf, len);
-    if (size == 0) {
+    if (size == 0 || (size < 0 && !(errno == EAGAIN || errno == EINTR))) {
         /* connection closed */
         tcp_chr_disconnect(chr);
     } else if (size > 0) {

What about EWOULDBLOCK?

The /* connection closed */ comment is now more inaccurate :)

If size == 0, the peer has shut down its sending end of the connection.

If size < 0, our attempt to receive encountered an error.

I'd just drop the comment.

author
#2 lore raw

Hi Markus,

Quoted 47 lines · Markus Armbruster
Quoted 45 lines · Zifei Tong

After commit 812c1057f6175ac9a9829fa2920a2b5783814193 (Handle G_IO_HUP in tcp_chr_read for tcp chardev), connections are disconnected when in G_IO_HUP condition.

However, it's possible that there is still data for reading in the channel. In that case, the remaining data is not handled.

I saw a related bug when running socat in write-only mode, after

  $ echo "quit" | socat -u - UNIX-CONNECT:qemu-monitor

the monitor won't not run the 'quit' command.

Instead of GIOCondition, this patch uses the return value of tcp_chr_recv() to check the state of connection as suggested by Kirill.

Cc Kirill Batuzov <[email protected]>
Cc Nikolay Nikolaev <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Anthony Liguori <[email protected]>
---
 qemu-char.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
qemu-char.c +0 -6
index 2a3cb9f..6cc69fa 100644
@@ -2692,12 +2692,6 @@ static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
     uint8_t buf[READ_BUF_LEN];
     int len, size;
 
-    if (cond & G_IO_HUP) {
-        /* connection closed */
-        tcp_chr_disconnect(chr);
-        return TRUE;
-    }
-
     if (!s->connected || s->max_size <= 0) {
         return TRUE;
     }
@@ -2705,7 +2699,7 @@ static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
     if (len > s->max_size)
         len = s->max_size;
Quoted 8 lines

qemu-char.c static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) · line 2705

Quoted 6 lines
qemu-char.c +1 -1
     size = tcp_chr_recv(chr, (void *)buf, len);
-    if (size == 0) {
+    if (size == 0 || (size < 0 && !(errno == EAGAIN || errno == EINTR))) {
         /* connection closed */
         tcp_chr_disconnect(chr);
     } else if (size > 0) {

What about EWOULDBLOCK?

I thought EAGAIN and EWOULDBLOCK was the same thing, until Google told me it's not the case on some platforms like HP-UX, AIX and Tru64.

Shall we add one more check to support these old platforms?

Quoted 7 lines

The /* connection closed */ comment is now more inaccurate :)

If size == 0, the peer has shut down its sending end of the connection.

If size < 0, our attempt to receive encountered an error.

I'd just drop the comment.

Will do this in v3.

Thanks,
Zifei

#3 lore raw
Quoted 20 lines · Zifei Tong

Hi Markus,

Quoted 17 lines · Markus Armbruster
Quoted 15 lines · Zifei Tong

After commit 812c1057f6175ac9a9829fa2920a2b5783814193 (Handle G_IO_HUP in tcp_chr_read for tcp chardev), connections are disconnected when in G_IO_HUP condition.

However, it's possible that there is still data for reading in the channel. In that case, the remaining data is not handled.

I saw a related bug when running socat in write-only mode, after

  $ echo "quit" | socat -u - UNIX-CONNECT:qemu-monitor

the monitor won't not run the 'quit' command.

Instead of GIOCondition, this patch uses the return value of tcp_chr_recv() to check the state of connection as suggested by Kirill.

[...]

Quoted 12 lines
Quoted 11 lines

qemu-char.c static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) · line 2705

Quoted 9 lines
qemu-char.c +1 -1
@@ -2705,7 +2699,7 @@ static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
     if (len > s->max_size)
         len = s->max_size;
     size = tcp_chr_recv(chr, (void *)buf, len);
-    if (size == 0) {
+    if (size == 0 || (size < 0 && !(errno == EAGAIN || errno == EINTR))) {
         /* connection closed */
         tcp_chr_disconnect(chr);
     } else if (size > 0) {

What about EWOULDBLOCK?

Quoted 4 lines

I thought EAGAIN and EWOULDBLOCK was the same thing, until Google told me it's not the case on some platforms like HP-UX, AIX and Tru64.

Shall we add one more check to support these old platforms?

Yes, please!

[...]