serial_manager: Remove serial socket

Remove the backend socket of serial port while shutting down guest.

Signed-off-by: Praveen K Paladugu <prapal@linux.microsoft.com>
This commit is contained in:
Praveen K Paladugu 2023-06-12 18:50:45 +00:00 committed by Rob Bradford
parent 6d1077fc3c
commit 044f3f758e

View File

@ -73,6 +73,10 @@ pub enum Error {
/// Cannot shutdown the connection /// Cannot shutdown the connection
#[error("Error shutting down a connection: {0}")] #[error("Error shutting down a connection: {0}")]
ShutdownConnection(#[source] io::Error), ShutdownConnection(#[source] io::Error),
/// Cannot remove the serial socket
#[error("Error removing serial socket: {0}")]
RemoveUnixSocket(#[source] io::Error),
} }
pub type Result<T> = result::Result<T, Error>; pub type Result<T> = result::Result<T, Error>;
@ -108,6 +112,7 @@ pub struct SerialManager {
handle: Option<thread::JoinHandle<()>>, handle: Option<thread::JoinHandle<()>>,
pty_write_out: Option<Arc<AtomicBool>>, pty_write_out: Option<Arc<AtomicBool>>,
mode: ConsoleOutputMode, mode: ConsoleOutputMode,
socket_path: Option<PathBuf>,
} }
impl SerialManager { impl SerialManager {
@ -118,6 +123,8 @@ impl SerialManager {
mode: ConsoleOutputMode, mode: ConsoleOutputMode,
socket: Option<PathBuf>, socket: Option<PathBuf>,
) -> Result<Option<Self>> { ) -> Result<Option<Self>> {
let mut socket_path: Option<PathBuf> = None;
let in_file = match mode { let in_file = match mode {
ConsoleOutputMode::Pty => { ConsoleOutputMode::Pty => {
if let Some(pty_pair) = pty_pair { if let Some(pty_pair) = pty_pair {
@ -154,9 +161,10 @@ impl SerialManager {
} }
} }
ConsoleOutputMode::Socket => { ConsoleOutputMode::Socket => {
if let Some(socket_path) = socket { if let Some(path_in_socket) = socket {
let listener = socket_path = Some(path_in_socket.clone());
UnixListener::bind(socket_path.as_path()).map_err(Error::BindUnixSocket)?; let listener = UnixListener::bind(path_in_socket.as_path())
.map_err(Error::BindUnixSocket)?;
// SAFETY: listener is valid and will return valid fd // SAFETY: listener is valid and will return valid fd
unsafe { File::from_raw_fd(listener.into_raw_fd()) } unsafe { File::from_raw_fd(listener.into_raw_fd()) }
} else { } else {
@ -216,6 +224,7 @@ impl SerialManager {
handle: None, handle: None,
pty_write_out, pty_write_out,
mode, mode,
socket_path,
})) }))
} }
@ -358,7 +367,6 @@ impl SerialManager {
serial_reader serial_reader
.shutdown(Shutdown::Both) .shutdown(Shutdown::Both)
.map_err(Error::ShutdownConnection)?; .map_err(Error::ShutdownConnection)?;
reader = None; reader = None;
serial serial
.as_ref() .as_ref()
@ -429,5 +437,12 @@ impl Drop for SerialManager {
if let Some(handle) = self.handle.take() { if let Some(handle) = self.handle.take() {
handle.join().ok(); 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();
}
}
} }
} }