Expect stream to implement ScmSocket trait

The stream used by the Connection structure is expected to implement
both Read and Write traits. Since we want the server to be able to
receive file descriptors through control messages mechanism, this patch
extends the expectations regarding the stream by adding ScmSocket to the
list of traits.

Since the stream is a UnixStream structure, and since vmm-sys-util
already provides an ScmSocket implementation for UnixStream, extending
the list of traits is very straightforward.

Relying on the newly added trait, the server now reads incoming bytes
through recv_with_fd() function, which replaces the former call to
read(). This change has no intent of modifying the former behavior from
the read(), which is why the returned Option<File> is ignored for now.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf
2021-07-12 17:28:52 +02:00
committed by georgepisaltu
parent 81a3c71efb
commit 3c4cc3aa91
3 changed files with 24 additions and 16 deletions

View File

@@ -117,8 +117,10 @@ pub enum ConnectionError {
InvalidWrite,
/// The request parsing has failed.
ParseError(RequestError),
/// Could not perform a stream operation successfully.
StreamError(std::io::Error),
/// Could not perform a read operation from stream successfully.
StreamReadError(vmm_sys_util::errno::Error),
/// Could not perform a write operation to stream successfully.
StreamWriteError(std::io::Error),
}
impl Display for ConnectionError {
@@ -127,7 +129,8 @@ impl Display for ConnectionError {
Self::ConnectionClosed => write!(f, "Connection closed."),
Self::InvalidWrite => write!(f, "Invalid write attempt."),
Self::ParseError(inner) => write!(f, "Parsing error: {}", inner),
Self::StreamError(inner) => write!(f, "Stream error: {}", inner),
Self::StreamReadError(inner) => write!(f, "Reading stream error: {}", inner),
Self::StreamWriteError(inner) => write!(f, "Writing stream error: {}", inner),
}
}
}
@@ -322,7 +325,10 @@ mod tests {
match (self, other) {
(ParseError(ref e), ParseError(ref other_e)) => e.eq(other_e),
(ConnectionClosed, ConnectionClosed) => true,
(StreamError(ref e), StreamError(ref other_e)) => {
(StreamReadError(ref e), StreamReadError(ref other_e)) => {
format!("{}", e).eq(&format!("{}", other_e))
}
(StreamWriteError(ref e), StreamWriteError(ref other_e)) => {
format!("{}", e).eq(&format!("{}", other_e))
}
(InvalidWrite, InvalidWrite) => true,
@@ -489,9 +495,9 @@ mod tests {
assert_eq!(
format!(
"{}",
ConnectionError::StreamError(std::io::Error::from_raw_os_error(11))
ConnectionError::StreamWriteError(std::io::Error::from_raw_os_error(11))
),
"Stream error: Resource temporarily unavailable (os error 11)"
"Writing stream error: Resource temporarily unavailable (os error 11)"
);
}