Skip to contentSkip to conversation

qemu-io: fix EOF Ctrl-D handling in qemu-io readline code

PATCH Applied Daniel P. Berrange 3 participants 6 messages Dec 8, 2017 – Feb 12, 2018

v2 of 3 Applied Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 qemu-io: fix EOF Ctrl-D handling in qemu-io readline code

qemu-io puts the TTY into non-canonical mode, which means no EOF processing is done and thus getchar() will never return the EOF constant. Instead we have to query the TTY attributes to determine the configured EOF character (usually Ctrl-D / 0x4), and then explicitly check for that value. This fixes the regression that prevented Ctrl-D from triggering an exit of qemu-io that has existed since readline was first added in

commit 0cf17e181798063c3824c8200ba46f25f54faa1a
Author: Stefan Hajnoczi <[email protected]>
Date: Thu Nov 14 11:54:17 2013 +0100

qemu-io: use readline.c

---
Changed in v2:

- Query termios settings for EOF character

 qemu-io.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
qemu-io.c +25 -1
index c70bde3eb1..fa4972d734 100644
@@ -10,6 +10,9 @@
 #include "qemu/osdep.h"
 #include <getopt.h>
 #include <libgen.h>
+#ifndef _WIN32
+#include <termios.h>
+#endif
 
 #include "qapi/error.h"
 #include "qemu-io.h"
@@ -41,6 +44,26 @@ static bool imageOpts;
 
 static ReadLineState *readline_state;
 
+static int ttyEOF;
+
+static int get_eof_char(void)
+{
+#ifdef _WIN32
+    return 0x4; /* Ctrl-D */
+#else
+    struct termios tty;
+    if (tcgetattr(STDIN_FILENO, &tty) != 0) {
+        if (errno == ENOTTY) {
+            return 0x0; /* just expect read() == 0 */
+        } else {
+            return 0x4; /* Ctrl-D */
+        }
+    }
+
+    return tty.c_cc[VEOF];
+#endif
+}
+
 static int close_f(BlockBackend *blk, int argc, char **argv)
 {
     blk_unref(qemuio_blk);
@@ -322,7 +345,7 @@ static char *fetchline_readline(void)
     readline_start(readline_state, get_prompt(), 0, readline_func, &line);
     while (!line) {
         int ch = getchar();
-        if (ch == EOF) {
+        if (ttyEOF != 0x0 && ch == ttyEOF) {
             break;
         }
         readline_handle_byte(readline_state, ch);
@@ -592,6 +615,7 @@ int main(int argc, char **argv)
     qemuio_add_command(&close_cmd);
 
     if (isatty(STDIN_FILENO)) {
+        ttyEOF = get_eof_char();
         readline_state = readline_init(readline_printf_func,
                                        readline_flush_func,
                                        NULL,
Signature · 1 line
2.14.3
Reviewed-by on v2 1/1
#1 lore raw
Quoted 19 lines · Daniel P. Berrange

qemu-io puts the TTY into non-canonical mode, which means no EOF processing is done and thus getchar() will never return the EOF constant. Instead we have to query the TTY attributes to determine the configured EOF character (usually Ctrl-D / 0x4), and then explicitly check for that value. This fixes the regression that prevented Ctrl-D from triggering an exit of qemu-io that has existed since readline was first added in

commit 0cf17e181798063c3824c8200ba46f25f54faa1a
Author: Stefan Hajnoczi <[email protected]>
Date: Thu Nov 14 11:54:17 2013 +0100

qemu-io: use readline.c

---
Changed in v2:

+++ b/qemu-io.c

qemu-io.c line 15

Quoted 6 lines
qemu-io.c +3 -0
@@ -10,6 +10,9 @@
 #include "qemu/osdep.h"
 #include <getopt.h>
 #include <libgen.h>
+#ifndef _WIN32
+#include <termios.h>
+#endif

Wouldn't a configure probe for the existence of <termios.h> be more reliable than just hard-coding the list of platforms where it is currently not found?

qemu-io.c static bool imageOpts; · line 51

Quoted 12 lines
qemu-io.c +5 -0
 #include "qapi/error.h"
 #include "qemu-io.h"
@@ -41,6 +44,26 @@ static bool imageOpts;
 
 static ReadLineState *readline_state;
 
+static int ttyEOF;
+
+static int get_eof_char(void)
+{
+#ifdef _WIN32

in which case this also should be #if HAVE_TERMIOS_H

But I guess unless someone complains, all other platforms that we care about have termios.h, so a weak:

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#2 lore raw
Quoted 46 lines · Eric Blake
Quoted 19 lines · Daniel P. Berrange

qemu-io puts the TTY into non-canonical mode, which means no EOF processing is done and thus getchar() will never return the EOF constant. Instead we have to query the TTY attributes to determine the configured EOF character (usually Ctrl-D / 0x4), and then explicitly check for that value. This fixes the regression that prevented Ctrl-D from triggering an exit of qemu-io that has existed since readline was first added in

commit 0cf17e181798063c3824c8200ba46f25f54faa1a
Author: Stefan Hajnoczi <[email protected]>
Date: Thu Nov 14 11:54:17 2013 +0100

qemu-io: use readline.c

---
Changed in v2:

+++ b/qemu-io.c

qemu-io.c line 15

Quoted 6 lines
qemu-io.c +3 -0
@@ -10,6 +10,9 @@
 #include "qemu/osdep.h"
 #include <getopt.h>
 #include <libgen.h>
+#ifndef _WIN32
+#include <termios.h>
+#endif

Wouldn't a configure probe for the existence of <termios.h> be more reliable than just hard-coding the list of platforms where it is currently not found?

qemu-io.c static bool imageOpts; · line 51

Quoted 12 lines
qemu-io.c +5 -0
 #include "qapi/error.h"
 #include "qemu-io.h"
@@ -41,6 +44,26 @@ static bool imageOpts;
 
 static ReadLineState *readline_state;
 
+static int ttyEOF;
+
+static int get_eof_char(void)
+{
+#ifdef _WIN32

in which case this also should be #if HAVE_TERMIOS_H

Quoted 2 lines

But I guess unless someone complains, all other platforms that we care about have termios.h, so a weak:

FYI, vl.c already includes termios.h, with merely #ifndef _WIN32 protection, so that shows this is sufficient for our immediate needs.

Quoted 1 line

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw
In reply to Daniel P. Berrange

ping, does any block maintainer want to queue this one ?

Quoted 83 lines · Daniel P. Berrange

qemu-io puts the TTY into non-canonical mode, which means no EOF processing is done and thus getchar() will never return the EOF constant. Instead we have to query the TTY attributes to determine the configured EOF character (usually Ctrl-D / 0x4), and then explicitly check for that value. This fixes the regression that prevented Ctrl-D from triggering an exit of qemu-io that has existed since readline was first added in

commit 0cf17e181798063c3824c8200ba46f25f54faa1a
Author: Stefan Hajnoczi <[email protected]>
Date: Thu Nov 14 11:54:17 2013 +0100

qemu-io: use readline.c

---
Changed in v2:

- Query termios settings for EOF character

 qemu-io.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
qemu-io.c +25 -1
index c70bde3eb1..fa4972d734 100644
@@ -10,6 +10,9 @@
 #include "qemu/osdep.h"
 #include <getopt.h>
 #include <libgen.h>
+#ifndef _WIN32
+#include <termios.h>
+#endif
 
 #include "qapi/error.h"
 #include "qemu-io.h"
@@ -41,6 +44,26 @@ static bool imageOpts;
 
 static ReadLineState *readline_state;
 
+static int ttyEOF;
+
+static int get_eof_char(void)
+{
+#ifdef _WIN32
+    return 0x4; /* Ctrl-D */
+#else
+    struct termios tty;
+    if (tcgetattr(STDIN_FILENO, &tty) != 0) {
+        if (errno == ENOTTY) {
+            return 0x0; /* just expect read() == 0 */
+        } else {
+            return 0x4; /* Ctrl-D */
+        }
+    }
+
+    return tty.c_cc[VEOF];
+#endif
+}
+
 static int close_f(BlockBackend *blk, int argc, char **argv)
 {
     blk_unref(qemuio_blk);
@@ -322,7 +345,7 @@ static char *fetchline_readline(void)
     readline_start(readline_state, get_prompt(), 0, readline_func, &line);
     while (!line) {
         int ch = getchar();
-        if (ch == EOF) {
+        if (ttyEOF != 0x0 && ch == ttyEOF) {
             break;
         }
         readline_handle_byte(readline_state, ch);
@@ -592,6 +615,7 @@ int main(int argc, char **argv)
     qemuio_add_command(&close_cmd);
 
     if (isatty(STDIN_FILENO)) {
+        ttyEOF = get_eof_char();
         readline_state = readline_init(readline_printf_func,
                                        readline_flush_func,
                                        NULL,
Signature · 1 line
2.14.3

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#4 lore raw

Re-ping.

Quoted 78 lines · Daniel P. Berrangé

ping, does any block maintainer want to queue this one ?

Quoted 75 lines · Daniel P. Berrange

qemu-io puts the TTY into non-canonical mode, which means no EOF processing is done and thus getchar() will never return the EOF constant. Instead we have to query the TTY attributes to determine the configured EOF character (usually Ctrl-D / 0x4), and then explicitly check for that value. This fixes the regression that prevented Ctrl-D from triggering an exit of qemu-io that has existed since readline was first added in

commit 0cf17e181798063c3824c8200ba46f25f54faa1a
Author: Stefan Hajnoczi <[email protected]>
Date: Thu Nov 14 11:54:17 2013 +0100

qemu-io: use readline.c

---
Changed in v2:

- Query termios settings for EOF character

 qemu-io.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
qemu-io.c +24 -1
index c70bde3eb1..fa4972d734 100644
@@ -10,6 +10,9 @@
 #include "qemu/osdep.h"
 #include <getopt.h>
 #include <libgen.h>
+#ifndef _WIN32
+#include <termios.h>
+#endif
 
 #include "qapi/error.h"
 #include "qemu-io.h"
@@ -41,6 +44,26 @@ static bool imageOpts;
 
 static ReadLineState *readline_state;
 
+static int ttyEOF;
+
+static int get_eof_char(void)
+{
+#ifdef _WIN32
+    return 0x4; /* Ctrl-D */
+#else
+    struct termios tty;
+    if (tcgetattr(STDIN_FILENO, &tty) != 0) {
+        if (errno == ENOTTY) {
+            return 0x0; /* just expect read() == 0 */
+        } else {
+            return 0x4; /* Ctrl-D */
+        }
+    }
+
+    return tty.c_cc[VEOF];
+#endif
+}
+
 static int close_f(BlockBackend *blk, int argc, char **argv)
 {
     blk_unref(qemuio_blk);
@@ -322,7 +345,7 @@ static char *fetchline_readline(void)
     readline_start(readline_state, get_prompt(), 0, readline_func, &line);
     while (!line) {
         int ch = getchar();
-        if (ch == EOF) {
+        if (ttyEOF != 0x0 && ch == ttyEOF) {
             break;
         }
         readline_handle_byte(readline_state, ch);
@@ -592,6 +615,7 @@ int main(int argc, char **argv)
     qemuio_add_command(&close_cmd);
 
Quoted 16 lines
Quoted 8 lines
qemu-io.c +1 -0
     if (isatty(STDIN_FILENO)) {
+        ttyEOF = get_eof_char();
         readline_state = readline_init(readline_printf_func,
                                        readline_flush_func,
                                        NULL,
Signature · 1 line
2.14.3

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Applied
#5 lore raw
In reply to Daniel P. Berrange

Am 08.12.2017 um 14:34 hat Daniel P. Berrange geschrieben:

Quoted 7 lines

qemu-io puts the TTY into non-canonical mode, which means no EOF processing is done and thus getchar() will never return the EOF constant. Instead we have to query the TTY attributes to determine the configured EOF character (usually Ctrl-D / 0x4), and then explicitly check for that value. This fixes the regression that prevented Ctrl-D from triggering an exit of qemu-io that has existed since readline was first added in

Quoted 7 lines

commit 0cf17e181798063c3824c8200ba46f25f54faa1a
Author: Stefan Hajnoczi <[email protected]>
Date: Thu Nov 14 11:54:17 2013 +0100

qemu-io: use readline.c

Thanks, applied to the block branch.

It would be nice if we also printed a newline when Ctrl-D is pressed. If you want to send a v3 for this, I'll just update the patch in my queue.

Kevin