From 12edb24678c8e3080df30234ca9d84abb56c788c Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Tue, 7 Apr 2020 15:28:20 +0100 Subject: [PATCH] vmm: config: Validate that serial/console file mode has a path Signed-off-by: Rob Bradford --- vmm/src/config.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/vmm/src/config.rs b/vmm/src/config.rs index 8efd71344..b416f1f17 100644 --- a/vmm/src/config.rs +++ b/vmm/src/config.rs @@ -62,8 +62,6 @@ pub enum Error { ParsePersistentMemory(OptionParserError), /// Failed parsing console ParseConsole(OptionParserError), - /// Missing file value for console - ParseConsoleFileMissing, /// No mode given for console ParseConsoleInvalidModeGiven, /// Failed parsing device parameters @@ -84,6 +82,8 @@ pub enum ValidationError { DoubleTtyMode, /// No kernel specified KernelMissing, + /// Missing file value for console + ConsoleFileMissing, } type ValidationResult = std::result::Result; @@ -94,6 +94,7 @@ impl fmt::Display for ValidationError { match self { DoubleTtyMode => write!(f, "Console mode tty specified for both serial and console"), KernelMissing => write!(f, "No kernel specified"), + ConsoleFileMissing => write!(f, "Path missing when using file console mode"), } } } @@ -103,9 +104,6 @@ impl fmt::Display for Error { use self::Error::*; match self { ParseConsole(o) => write!(f, "Error parsing --console: {}", o), - ParseConsoleFileMissing => { - write!(f, "Error parsing --console: path missing when using file") - } ParseConsoleInvalidModeGiven => { write!(f, "Error parsing --console: invalid console mode given") } @@ -1037,9 +1035,10 @@ impl ConsoleConfig { mode = ConsoleOutputMode::Null } else if parser.is_set("file") { mode = ConsoleOutputMode::File; - file = Some(PathBuf::from( - parser.get("file").ok_or(Error::ParseConsoleFileMissing)?, - )); + file = + Some(PathBuf::from(parser.get("file").ok_or( + Error::Validation(ValidationError::ConsoleFileMissing), + )?)); } else { return Err(Error::ParseConsoleInvalidModeGiven); } @@ -1201,6 +1200,14 @@ impl VmConfig { return Err(ValidationError::DoubleTtyMode); } + if self.console.mode == ConsoleOutputMode::File && self.console.file.is_none() { + return Err(ValidationError::ConsoleFileMissing); + } + + if self.serial.mode == ConsoleOutputMode::File && self.serial.file.is_none() { + return Err(ValidationError::ConsoleFileMissing); + } + Ok(()) } @@ -1857,6 +1864,11 @@ mod tests { invalid_config.kernel = None; assert!(invalid_config.validate().is_err()); + let mut invalid_config = valid_config.clone(); + invalid_config.serial.mode = ConsoleOutputMode::File; + invalid_config.serial.file = None; + assert!(invalid_config.validate().is_err()); + Ok(()) } }