From: Alyssa Ross <hi@alyssa.is>
To: devel@spectrum-os.org
Subject: [PATCH crosvm 1/2] msg_socket: introduce UnixSeqpacketExt
Date: Sun, 14 Jun 2020 11:43:43 +0000 [thread overview]
Message-ID: <20200614114344.22642-2-hi@alyssa.is> (raw)
In-Reply-To: <20200614114344.22642-1-hi@alyssa.is>
Occasionally, it is useful to be able to use UnixSeqpacket as a type
that can represent any kind of MsgSocket. For example, to keep some
MsgSockets of different types in a Vec. In this case, it may be known
what type of messages should be sent over a socket, even though that
may not be represantable in the type system.
To accomodate this situation, this patch introduces send_msg_on_socket
and recv_msg_on_socket methods on UnixSeqpacket, that can be used to
send or receive any kind of MsgOnSocket. The caller is obviously
responsible for ensuring that the messages being sent are of the type
expected by the socket.
This lack of type safety for message types is not ideal, and so
MsgSender and MsgReceiver should still be preferred wherever possible.
---
msg_socket/src/lib.rs | 52 ++++++++++++++++++++++++++-----------------
1 file changed, 32 insertions(+), 20 deletions(-)
diff --git a/msg_socket/src/lib.rs b/msg_socket/src/lib.rs
index 540d49d7..be860701 100644
--- a/msg_socket/src/lib.rs
+++ b/msg_socket/src/lib.rs
@@ -123,42 +123,38 @@ impl<M: MsgOnSocket> AsRawFd for Receiver<M> {
}
}
-/// Types that could send a message.
-pub trait MsgSender: AsRef<UnixSeqpacket> {
- type M: MsgOnSocket;
- fn send(&self, msg: &Self::M) -> MsgResult<()> {
+pub trait UnixSeqpacketExt {
+ fn send_msg_on_socket<M: MsgOnSocket>(&self, msg: &M) -> MsgResult<()>;
+ fn recv_msg_on_socket<M: MsgOnSocket>(&self) -> MsgResult<M>;
+}
+
+impl UnixSeqpacketExt for UnixSeqpacket {
+ fn send_msg_on_socket<M: MsgOnSocket>(&self, msg: &M) -> MsgResult<()> {
let msg_size = msg.msg_size();
let fd_size = msg.fd_count();
let mut msg_buffer: Vec<u8> = vec![0; msg_size];
let mut fd_buffer: Vec<RawFd> = vec![0; fd_size];
let fd_size = msg.write_to_buffer(&mut msg_buffer, &mut fd_buffer)?;
- let sock: &UnixSeqpacket = self.as_ref();
if fd_size == 0 {
- handle_eintr!(sock.send(&msg_buffer))
+ handle_eintr!(self.send(&msg_buffer))
.map_err(|e| MsgError::Send(SysError::new(e.raw_os_error().unwrap_or(0))))?;
} else {
let ioslice = IoSlice::new(&msg_buffer[..]);
- sock.send_with_fds(&[ioslice], &fd_buffer[0..fd_size])
+ self.send_with_fds(&[ioslice], &fd_buffer[0..fd_size])
.map_err(MsgError::Send)?;
}
Ok(())
}
-}
-
-/// Types that could receive a message.
-pub trait MsgReceiver: AsRef<UnixSeqpacket> {
- type M: MsgOnSocket;
- fn recv(&self) -> MsgResult<Self::M> {
- let sock: &UnixSeqpacket = self.as_ref();
+ fn recv_msg_on_socket<M: MsgOnSocket>(&self) -> MsgResult<M> {
let (msg_buffer, fd_buffer) = {
- if Self::M::uses_fd() {
- sock.recv_as_vec_with_fds()
+ if M::uses_fd() {
+ self.recv_as_vec_with_fds()
.map_err(|e| MsgError::Recv(SysError::new(e.raw_os_error().unwrap_or(0))))?
} else {
(
- sock.recv_as_vec().map_err(|e| {
+ self.recv_as_vec().map_err(|e| {
MsgError::Recv(SysError::new(e.raw_os_error().unwrap_or(0)))
})?,
vec![],
@@ -166,11 +162,11 @@ pub trait MsgReceiver: AsRef<UnixSeqpacket> {
}
};
- if msg_buffer.len() == 0 && Self::M::fixed_size() != Some(0) {
+ if msg_buffer.len() == 0 && M::fixed_size() != Some(0) {
return Err(MsgError::RecvZero);
}
- if let Some(fixed_size) = Self::M::fixed_size() {
+ if let Some(fixed_size) = M::fixed_size() {
if fixed_size != msg_buffer.len() {
return Err(MsgError::BadRecvSize {
expected: fixed_size,
@@ -180,7 +176,7 @@ pub trait MsgReceiver: AsRef<UnixSeqpacket> {
}
// Safe because fd buffer is read from socket.
- let (v, read_fd_size) = unsafe { Self::M::read_from_buffer(&msg_buffer, &fd_buffer)? };
+ let (v, read_fd_size) = unsafe { M::read_from_buffer(&msg_buffer, &fd_buffer)? };
if fd_buffer.len() != read_fd_size {
return Err(MsgError::NotExpectFd);
}
@@ -188,6 +184,22 @@ pub trait MsgReceiver: AsRef<UnixSeqpacket> {
}
}
+/// Types that could send a message.
+pub trait MsgSender: AsRef<UnixSeqpacket> {
+ type M: MsgOnSocket;
+ fn send(&self, msg: &Self::M) -> MsgResult<()> {
+ self.as_ref().send_msg_on_socket(msg)
+ }
+}
+
+/// Types that could receive a message.
+pub trait MsgReceiver: AsRef<UnixSeqpacket> {
+ type M: MsgOnSocket;
+ fn recv(&self) -> MsgResult<Self::M> {
+ self.as_ref().recv_msg_on_socket()
+ }
+}
+
impl<I: MsgOnSocket, O: MsgOnSocket> MsgSender for MsgSocket<I, O> {
type M = I;
}
--
2.26.2
next prev parent reply other threads:[~2020-06-14 11:44 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-06-14 11:43 [PATCH crosvm 0/2] Fix deadlock on early VmRequest Alyssa Ross
2020-06-14 11:43 ` Alyssa Ross [this message]
2020-06-16 0:17 ` [PATCH crosvm 1/2] msg_socket: introduce UnixSeqpacketExt Cole Helbling
2020-06-16 9:32 ` Alyssa Ross
2020-06-22 22:06 ` Cole Helbling
2020-06-23 2:32 ` Alyssa Ross
2020-06-25 1:54 ` impaqt
2020-07-09 13:24 ` Alyssa Ross
2020-06-14 11:43 ` [PATCH crosvm 2/2] crosvm: fix deadlock on early VmRequest Alyssa Ross
2020-06-16 1:08 ` Cole Helbling
2020-06-16 9:39 ` Alyssa Ross
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200614114344.22642-2-hi@alyssa.is \
--to=hi@alyssa.is \
--cc=devel@spectrum-os.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://spectrum-os.org/git/crosvm
https://spectrum-os.org/git/doc
https://spectrum-os.org/git/mktuntap
https://spectrum-os.org/git/nixpkgs
https://spectrum-os.org/git/spectrum
https://spectrum-os.org/git/ucspi-vsock
https://spectrum-os.org/git/www
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).