From dca745e48a51ba2ab7bb5d27b19e637cadddd1db Mon Sep 17 00:00:00 2001 From: Sasha Levin Date: Wed, 5 Sep 2012 10:31:48 +0200 Subject: [PATCH] kvm tools: improve term init/exit functions Make the init and exit functions of the term code similar to the rest of the code. Also move in the pty parser into the term code out of builtin-run. Signed-off-by: Sasha Levin Signed-off-by: Pekka Enberg --- builtin-run.c | 19 +++++++++---------- include/kvm/term.h | 6 +++++- term.c | 28 ++++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/builtin-run.c b/builtin-run.c index 944ef3d..d007927 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -146,15 +146,6 @@ static int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, i return 0; } -static int tty_parser(const struct option *opt, const char *arg, int unset) -{ - int tty = atoi(arg); - - term_set_tty(tty); - - return 0; -} - static inline void str_to_mac(const char *str, char *mac) { sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", @@ -988,7 +979,11 @@ static int kvm_cmd_run_init(int argc, const char **argv) if (!kvm->cfg.script) kvm->cfg.script = DEFAULT_SCRIPT; - term_init(); + r = term_init(kvm); + if (r < 0) { + pr_err("term_init() failed with error %d\n", r); + goto fail; + } if (!kvm->cfg.guest_name) { if (kvm->cfg.custom_rootfs) { @@ -1302,6 +1297,10 @@ static void kvm_cmd_run_exit(int guest_ret) if (r < 0) pr_warning("pci__exit() failed with error %d\n", r); + r = term_exit(kvm); + if (r < 0) + pr_warning("pci__exit() failed with error %d\n", r); + r = kvm__exit(kvm); if (r < 0) pr_warning("pci__exit() failed with error %d\n", r); diff --git a/include/kvm/term.h b/include/kvm/term.h index 33d96ce..493ce39 100644 --- a/include/kvm/term.h +++ b/include/kvm/term.h @@ -1,6 +1,8 @@ #ifndef KVM__TERM_H #define KVM__TERM_H +#include "kvm/kvm.h" + #include #include @@ -15,6 +17,8 @@ int term_getc(int term); bool term_readable(int term); void term_set_tty(int term); -void term_init(void); +int term_init(struct kvm *kvm); +int term_exit(struct kvm *kvm); +int tty_parser(const struct option *opt, const char *arg, int unset); #endif /* KVM__TERM_H */ diff --git a/term.c b/term.c index fb7963e..66f4804 100644 --- a/term.c +++ b/term.c @@ -127,13 +127,26 @@ void term_set_tty(int term) term_fds[term][TERM_FD_IN] = term_fds[term][TERM_FD_OUT] = master; } -void term_init(void) +int tty_parser(const struct option *opt, const char *arg, int unset) +{ + int tty = atoi(arg); + + term_set_tty(tty); + + return 0; +} + +int term_init(struct kvm *kvm) { struct termios term; - int i; + int i, r; + + r = tcgetattr(STDIN_FILENO, &orig_term); + if (r < 0) { + pr_warning("unable to save initial standard input settings"); + return r; + } - if (tcgetattr(STDIN_FILENO, &orig_term) < 0) - die("unable to save initial standard input settings"); term = orig_term; term.c_lflag &= ~(ICANON | ECHO | ISIG); @@ -147,4 +160,11 @@ void term_init(void) signal(SIGTERM, term_sig_cleanup); atexit(term_cleanup); + + return 0; +} + +int term_exit(struct kvm *kvm) +{ + return 0; }