From 044f3f758ee7c79aad56dca9bc43b874ac042c84 Mon Sep 17 00:00:00 2001 From: Praveen K Paladugu Date: Mon, 12 Jun 2023 18:50:45 +0000 Subject: [PATCH] serial_manager: Remove serial socket Remove the backend socket of serial port while shutting down guest. Signed-off-by: Praveen K Paladugu --- vmm/src/serial_manager.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/vmm/src/serial_manager.rs b/vmm/src/serial_manager.rs index ee775ad7c..426a983f0 100644 --- a/vmm/src/serial_manager.rs +++ b/vmm/src/serial_manager.rs @@ -73,6 +73,10 @@ pub enum Error { /// Cannot shutdown the connection #[error("Error shutting down a connection: {0}")] ShutdownConnection(#[source] io::Error), + + /// Cannot remove the serial socket + #[error("Error removing serial socket: {0}")] + RemoveUnixSocket(#[source] io::Error), } pub type Result = result::Result; @@ -108,6 +112,7 @@ pub struct SerialManager { handle: Option>, pty_write_out: Option>, mode: ConsoleOutputMode, + socket_path: Option, } impl SerialManager { @@ -118,6 +123,8 @@ impl SerialManager { mode: ConsoleOutputMode, socket: Option, ) -> Result> { + let mut socket_path: Option = None; + let in_file = match mode { ConsoleOutputMode::Pty => { if let Some(pty_pair) = pty_pair { @@ -154,9 +161,10 @@ impl SerialManager { } } ConsoleOutputMode::Socket => { - if let Some(socket_path) = socket { - let listener = - UnixListener::bind(socket_path.as_path()).map_err(Error::BindUnixSocket)?; + if let Some(path_in_socket) = socket { + socket_path = Some(path_in_socket.clone()); + let listener = UnixListener::bind(path_in_socket.as_path()) + .map_err(Error::BindUnixSocket)?; // SAFETY: listener is valid and will return valid fd unsafe { File::from_raw_fd(listener.into_raw_fd()) } } else { @@ -216,6 +224,7 @@ impl SerialManager { handle: None, pty_write_out, mode, + socket_path, })) } @@ -358,7 +367,6 @@ impl SerialManager { serial_reader .shutdown(Shutdown::Both) .map_err(Error::ShutdownConnection)?; - reader = None; serial .as_ref() @@ -429,5 +437,12 @@ impl Drop for SerialManager { if let Some(handle) = self.handle.take() { handle.join().ok(); } + if self.mode == ConsoleOutputMode::Socket { + if let Some(socket_path) = self.socket_path.as_ref() { + std::fs::remove_file(socket_path.as_os_str()) + .map_err(Error::RemoveUnixSocket) + .ok(); + } + } } }