From 9b0996a71f5dd6d826df323456a03786bb5f34de Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Tue, 16 Mar 2021 18:26:10 +0000 Subject: [PATCH] vmm, main: Optionalise creation of API server Only if we have a valid API server path then create the API server. For now this has no functional change there is a default API server path in the clap handling but rather prepares to do so optionally. Signed-off-by: Rob Bradford --- src/main.rs | 22 +++++++++++----------- vmm/src/lib.rs | 9 +++++---- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1a0c17f7d..035636caa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -371,7 +371,7 @@ fn create_app<'a, 'b>( app } -fn start_vmm(cmd_arguments: ArgMatches, api_socket_path: &str) -> Result<(), Error> { +fn start_vmm(cmd_arguments: ArgMatches, api_socket_path: &Option) -> Result<(), Error> { let (api_request_sender, api_request_receiver) = channel(); let api_evt = EventFd::new(EFD_NONBLOCK).map_err(Error::CreateAPIEventFd)?; @@ -435,7 +435,7 @@ fn start_vmm(cmd_arguments: ArgMatches, api_socket_path: &str) -> Result<(), Err let vm_config = config::VmConfig::parse(vm_params).map_err(Error::ParsingConfig)?; println!( - "Cloud Hypervisor Guest\n\tAPI server: {}\n\tvCPUs: {}\n\tMemory: {} MB\n\tKernel: \ + "Cloud Hypervisor Guest\n\tAPI server: {:?}\n\tvCPUs: {}\n\tMemory: {} MB\n\tKernel: \ {:?}\n\tInitramfs: {:?}\n\tKernel cmdline: {}\n\tDisk(s): {:?}", api_socket_path, vm_config.cpus.boot_vcpus, @@ -523,10 +523,7 @@ fn main() { .map(|()| log::set_max_level(log_level)) .expect("Expected to be able to setup logger"); - let api_socket_path = cmd_arguments - .value_of("api-socket") - .expect("Missing argument: api-socket") - .to_string(); + let api_socket_path = cmd_arguments.value_of("api-socket").map(|s| s.to_string()); if let Some(monitor_config) = cmd_arguments.value_of("event-monitor") { let mut parser = OptionParser::new(); @@ -553,12 +550,15 @@ fn main() { event_monitor::set_monitor(file).expect("Expected setting monitor to succeed"); } - if let Err(e) = start_vmm(cmd_arguments, &api_socket_path) { + let exit_code = if let Err(e) = start_vmm(cmd_arguments, &api_socket_path) { eprintln!("{}", e); - std::fs::remove_file(api_socket_path).ok(); - std::process::exit(1); - } - std::fs::remove_file(api_socket_path).ok(); + 1 + } else { + 0 + }; + + api_socket_path.map(|s| std::fs::remove_file(s).ok()); + std::process::exit(exit_code); } #[cfg(test)] diff --git a/vmm/src/lib.rs b/vmm/src/lib.rs index 2836b779d..d3bfb9196 100644 --- a/vmm/src/lib.rs +++ b/vmm/src/lib.rs @@ -245,7 +245,7 @@ impl Serialize for PciDeviceInfo { pub fn start_vmm_thread( vmm_version: String, - http_path: &str, + http_path: &Option, api_event: EventFd, api_sender: Sender, api_receiver: Receiver, @@ -276,9 +276,10 @@ pub fn start_vmm_thread( }) .map_err(Error::VmmThreadSpawn)?; - // The VMM thread is started, we can start serving HTTP requests - api::start_http_thread(http_path, http_api_event, api_sender, seccomp_action)?; - + if let Some(http_path) = http_path { + // The VMM thread is started, we can start serving HTTP requests + api::start_http_thread(http_path, http_api_event, api_sender, seccomp_action)?; + } Ok(thread) }