mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2025-02-05 18:20:34 +00:00
Add try_clone() and FromRawDescriptor for Tube and UnixSeqpacket
BUG=b:179755651 TEST=cargo test Change-Id: Icf77f7427972b940215a2a4d95aa09ae409e4ff0 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2987590 Auto-Submit: Chirantan Ekbote <chirantan@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Commit-Queue: Chirantan Ekbote <chirantan@chromium.org> Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
This commit is contained in:
parent
e9778a0b59
commit
af9dccf591
2 changed files with 25 additions and 1 deletions
|
@ -35,6 +35,8 @@ pub enum Error {
|
||||||
SetRecvTimeout(io::Error),
|
SetRecvTimeout(io::Error),
|
||||||
#[error("failed to create async tube: {0}")]
|
#[error("failed to create async tube: {0}")]
|
||||||
CreateAsync(cros_async::AsyncError),
|
CreateAsync(cros_async::AsyncError),
|
||||||
|
#[error("failed to clone UnixSeqpacket: {0}")]
|
||||||
|
Clone(io::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
@ -68,6 +70,10 @@ impl Tube {
|
||||||
Ok(AsyncTube { inner })
|
Ok(AsyncTube { inner })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn try_clone(&self) -> Result<Self> {
|
||||||
|
self.socket.try_clone().map(Tube::new).map_err(Error::Clone)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn send<T: Serialize>(&self, msg: &T) -> Result<()> {
|
pub fn send<T: Serialize>(&self, msg: &T) -> Result<()> {
|
||||||
let msg_serialize = SerializeDescriptors::new(&msg);
|
let msg_serialize = SerializeDescriptors::new(&msg);
|
||||||
let msg_json = serde_json::to_vec(&msg_serialize).map_err(Error::Json)?;
|
let msg_json = serde_json::to_vec(&msg_serialize).map_err(Error::Json)?;
|
||||||
|
@ -117,6 +123,15 @@ impl Tube {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromRawDescriptor for Tube {
|
||||||
|
unsafe fn from_raw_descriptor(descriptor: RawDescriptor) -> Self {
|
||||||
|
Tube {
|
||||||
|
socket: UnixSeqpacket::from_raw_descriptor(descriptor),
|
||||||
|
_unsync_marker: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl AsRawDescriptor for Tube {
|
impl AsRawDescriptor for Tube {
|
||||||
fn as_raw_descriptor(&self) -> RawDescriptor {
|
fn as_raw_descriptor(&self) -> RawDescriptor {
|
||||||
self.socket.as_raw_descriptor()
|
self.socket.as_raw_descriptor()
|
||||||
|
|
|
@ -23,7 +23,10 @@ use libc::{
|
||||||
};
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::sock_ctrl_msg::{ScmSocket, SCM_SOCKET_MAX_FD_COUNT};
|
use crate::{
|
||||||
|
sock_ctrl_msg::{ScmSocket, SCM_SOCKET_MAX_FD_COUNT},
|
||||||
|
FromRawDescriptor,
|
||||||
|
};
|
||||||
use crate::{AsRawDescriptor, RawDescriptor};
|
use crate::{AsRawDescriptor, RawDescriptor};
|
||||||
|
|
||||||
/// Assist in handling both IP version 4 and IP version 6.
|
/// Assist in handling both IP version 4 and IP version 6.
|
||||||
|
@ -604,6 +607,12 @@ impl FromRawFd for UnixSeqpacket {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromRawDescriptor for UnixSeqpacket {
|
||||||
|
unsafe fn from_raw_descriptor(descriptor: RawDescriptor) -> Self {
|
||||||
|
Self { fd: descriptor }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl AsRawFd for UnixSeqpacket {
|
impl AsRawFd for UnixSeqpacket {
|
||||||
fn as_raw_fd(&self) -> RawFd {
|
fn as_raw_fd(&self) -> RawFd {
|
||||||
self.fd
|
self.fd
|
||||||
|
|
Loading…
Reference in a new issue