From 91596f81e8279817d6e1ecd6b812a842213fdff2 Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Sun, 9 Jan 2011 22:15:04 +0200 Subject: [PATCH] kvm: Put terminal in canonical mode As suggested by Ingo Molnar, put terminal in canonical mode. This makes poll() on stdin to react to keystrokes insted of return key and fixes the double echo'd character problem for input. Signed-off-by: Pekka Enberg --- tools/kvm/main.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/tools/kvm/main.c b/tools/kvm/main.c index 599df7191dc1..650f225dfef7 100644 --- a/tools/kvm/main.c +++ b/tools/kvm/main.c @@ -7,6 +7,8 @@ #include "kvm/pci.h" #include +#include +#include #include #include #include @@ -28,6 +30,30 @@ static void usage(char *argv[]) static struct kvm *kvm; +static void tty_set_canon_flag(int fd, int on) +{ + struct termios tty; + int mask = ISTRIP | INLCR | ICRNL | IGNCR | IXON | IXOFF | ICANON | ECHO; + + tcgetattr(fd, &tty); + if (on) + tty.c_lflag |= mask; + else + tty.c_lflag &= ~mask; + tcsetattr(fd, TCSAFLUSH, &tty); +} + +static void shutdown(void) +{ + tty_set_canon_flag(fileno(stdin), 0); +} + +static void handle_sigint(int sig) +{ + shutdown(); + exit(1); +} + static void handle_sigquit(int sig) { kvm__show_registers(kvm); @@ -86,7 +112,6 @@ static void setup_timer(void) die("timer_settime()"); } - int main(int argc, char *argv[]) { const char *kernel_filename = NULL; @@ -98,7 +123,10 @@ int main(int argc, char *argv[]) bool single_step = false; int i; + atexit(shutdown); + signal(SIGQUIT, handle_sigquit); + signal(SIGINT, handle_sigint); for (i = 1; i < argc; i++) { if (option_matches(argv[i], "--kernel=")) { @@ -175,6 +203,8 @@ int main(int argc, char *argv[]) setup_timer(); + tty_set_canon_flag(fileno(stdin), 1); + for (;;) { kvm__run(kvm); -- 2.39.5