From 37e285bf7b495b5a10905333aef0155c26f6d9b3 Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Wed, 23 Dec 2020 15:10:52 -0800 Subject: [PATCH] agent: Make debug console async Fixes: #1209 Signed-off-by: Maksym Pavlenko --- src/agent/Cargo.toml | 2 +- src/agent/src/main.rs | 43 +++++++++++++++++-------------------------- 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/src/agent/Cargo.toml b/src/agent/Cargo.toml index 1d75a2d8cb..cde97aba50 100644 --- a/src/agent/Cargo.toml +++ b/src/agent/Cargo.toml @@ -23,7 +23,7 @@ scopeguard = "1.0.0" regex = "1" async-trait = "0.1.42" -tokio = { version = "0.2", features = ["rt-core", "sync", "uds", "stream", "macros", "io-util", "time", "signal", "io-std", "process",] } +tokio = { version = "0.2", features = ["rt-core", "sync", "uds", "stream", "macros", "io-util", "time", "signal", "io-std", "process", "blocking"] } futures = "0.3" netlink-sys = { version = "0.4.0", features = ["tokio_socket",]} tokio-vsock = "0.2.2" diff --git a/src/agent/src/main.rs b/src/agent/src/main.rs index dba964022b..2073d1b2b3 100644 --- a/src/agent/src/main.rs +++ b/src/agent/src/main.rs @@ -257,25 +257,23 @@ async fn start_sandbox(logger: &Logger, config: &agentConfig, init_mode: bool) - let shells = SHELLS.clone(); let debug_console_vport = config.debug_console_vport as u32; - // TODO: async the debug console - let mut _shell_handle: Option> = None; - if config.debug_console { + let shell_handle = if config.debug_console { let thread_logger = logger.clone(); + let shells = shells.lock().unwrap().to_vec(); - let builder = std::thread::Builder::new(); - - let handle = builder.spawn(move || { - let shells = shells.lock().unwrap(); - let result = setup_debug_console(&thread_logger, shells.to_vec(), debug_console_vport); + let handle = tokio::task::spawn_blocking(move || { + let result = setup_debug_console(&thread_logger, shells, debug_console_vport); if result.is_err() { // Report error, but don't fail warn!(thread_logger, "failed to setup debug console"; "error" => format!("{}", result.unwrap_err())); } - })?; + }); - _shell_handle = Some(handle); - } + Some(handle) + } else { + None + }; // Initialize unique sandbox structure. let mut s = Sandbox::new(&logger).context("Failed to create sandbox")?; @@ -304,6 +302,10 @@ async fn start_sandbox(logger: &Logger, config: &agentConfig, init_mode: bool) - let _ = rx.await?; server.shutdown().await?; + if let Some(handle) = shell_handle { + handle.await.map_err(|e| anyhow!("{:?}", e))?; + } + Ok(()) } @@ -454,9 +456,6 @@ lazy_static! { }; } -// pub static mut LOG_LEVEL: ; -// pub static mut TRACE_MODE: ; - use crate::config::agentConfig; use nix::sys::stat::Mode; use std::os::unix::io::{FromRawFd, RawFd}; @@ -464,18 +463,10 @@ use std::path::PathBuf; use std::process::exit; fn setup_debug_console(logger: &Logger, shells: Vec, port: u32) -> Result<()> { - let mut shell: &str = ""; - for sh in shells.iter() { - let binary = PathBuf::from(sh); - if binary.exists() { - shell = sh; - break; - } - } - - if shell == "" { - return Err(anyhow!("no shell found to launch debug console")); - } + let shell = shells + .iter() + .find(|sh| PathBuf::from(sh).exists()) + .ok_or_else(|| anyhow!("no shell found to launch debug console"))?; if port > 0 { let listenfd = socket::socket(