From 2e664dca642c028dc1326ca7d0abd377adca95ab Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Tue, 28 Jun 2022 16:01:08 +0100 Subject: [PATCH] vmm: Always reset the console mode on VMM exit Tested: 1. SIGTERM based 2. VM shutdown/poweroff 3. Injected VM boot failure after calling Vm::setup_tty() Fixes: #4248 Signed-off-by: Rob Bradford --- src/main.rs | 8 ++++++++ vmm/src/vm.rs | 13 +++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index c77a7a0e0..f4c7745df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,7 @@ use thiserror::Error; use vmm::config; use vmm_sys_util::eventfd::EventFd; use vmm_sys_util::signal::block_signal; +use vmm_sys_util::terminal::Terminal; #[derive(Error, Debug)] enum Error { @@ -623,6 +624,13 @@ fn main() { } }; + let on_tty = unsafe { libc::isatty(libc::STDIN_FILENO as i32) } != 0; + if on_tty { + // Don't forget to set the terminal in canonical mode + // before to exit. + std::io::stdin().lock().set_canon_mode().unwrap(); + } + std::process::exit(exit_code); } diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index ba19cde34..ee4cd4d32 100644 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -1694,13 +1694,14 @@ impl Vm { console_input_clone.update_console_size(); } SIGTERM | SIGINT => { - if on_tty { - io::stdin() - .lock() - .set_canon_mode() - .expect("failed to restore terminal mode"); - } if exit_evt.write(1).is_err() { + // Resetting the terminal is usually done as the VMM exits + if on_tty { + io::stdin() + .lock() + .set_canon_mode() + .expect("failed to restore terminal mode"); + } std::process::exit(1); } }