From 1658801329d4556a67f8526b006201e4df6fb276 Mon Sep 17 00:00:00 2001 From: Asias He Date: Sun, 10 Apr 2011 14:50:31 +0800 Subject: [PATCH] kvm tools: Exit KVM session on Ctrl+a and 'x' This patch makes Ctrl+a escape key and makes Ctrl+a and 'x' terminate the KVM session. It works for both serial and virtio console. If you want to input Ctrl+a to guest, type Ctrl+a twice. [ penberg@kernel.org: Make terminated message more readable. ] Tested-by: Amos Kong Signed-off-by: Asias He Signed-off-by: Pekka Enberg --- term.c | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/term.c b/term.c index 9b235d0..4adf9ef 100644 --- a/term.c +++ b/term.c @@ -9,9 +9,12 @@ #include "kvm/term.h" #include "kvm/util.h" -static struct termios orig_term; +static struct termios orig_term; -int active_console = CONSOLE_8250; +int term_escape_char = 0x01; /* ctrl-a is used for escape */ +bool term_got_escape = false; + +int active_console = CONSOLE_8250; int term_getc(int who) { @@ -22,6 +25,24 @@ int term_getc(int who) if (read_in_full(STDIN_FILENO, &c, 1) < 0) return -1; + + c &= 0xff; + + if (term_got_escape) { + term_got_escape = false; + if (c == 'x') { + printf("\n # KVM session terminated.\n"); + exit(1); + } + if (c == term_escape_char) + return c; + } + + if (c == term_escape_char) { + term_got_escape = true; + return -1; + } + return c; } @@ -39,16 +60,26 @@ int term_putc(int who, char *addr, int cnt) int term_getc_iov(int who, struct iovec *iov, int iovcnt) { - if (who != active_console) - return -1; + int c; - return readv(STDIN_FILENO, iov, iovcnt); + if (who != active_console) + return 0; + + c = term_getc(who); + + if (c < 0) + return 0; + + *((int *)iov[0].iov_base) = c; + iov[0].iov_len = sizeof(int); + + return sizeof(int); } int term_putc_iov(int who, struct iovec *iov, int iovcnt) { if (who != active_console) - return -1; + return 0; return writev(STDOUT_FILENO, iov, iovcnt); }