pub trait ScmSocket {
// Required method
fn socket_fd(&self) -> RawFd;
// Provided methods
fn send_with_fd<D: IntoIovec>(&self, buf: D, fd: RawFd) -> Result<usize> { ... }
fn send_with_fds<D: IntoIovec>(
&self,
bufs: &[D],
fds: &[RawFd],
) -> Result<usize> { ... }
fn recv_with_fd(&self, buf: &mut [u8]) -> Result<(usize, Option<File>)> { ... }
unsafe fn recv_with_fds(
&self,
iovecs: &mut [iovec],
fds: &mut [RawFd],
) -> Result<(usize, usize)> { ... }
}
Expand description
Trait for file descriptors can send and receive socket control messages via sendmsg
and
recvmsg
.
§Examples
extern crate vmm_sys_util;
use vmm_sys_util::sock_ctrl_msg::ScmSocket;
let (s1, s2) = UnixDatagram::pair().expect("failed to create socket pair");
let evt = EventFd::new(0).expect("failed to create eventfd");
let write_count = s1
.send_with_fds(&[[237].as_ref()], &[evt.as_raw_fd()])
.expect("failed to send fd");
let mut files = [0; 2];
let mut buf = [0u8];
let mut iovecs = [iovec {
iov_base: buf.as_mut_ptr() as *mut c_void,
iov_len: buf.len(),
}];
let (read_count, file_count) = unsafe {
s2.recv_with_fds(&mut iovecs[..], &mut files)
.expect("failed to recv fd")
};
let mut file = unsafe { File::from_raw_fd(files[0]) };
file.write(unsafe { from_raw_parts(&1203u64 as *const u64 as *const u8, 8) })
.expect("failed to write to sent fd");
assert_eq!(evt.read().expect("failed to read from eventfd"), 1203);
Required Methods§
Provided Methods§
Sourcefn send_with_fd<D: IntoIovec>(&self, buf: D, fd: RawFd) -> Result<usize>
fn send_with_fd<D: IntoIovec>(&self, buf: D, fd: RawFd) -> Result<usize>
Sends the given data and file descriptor over the socket.
On success, returns the number of bytes sent.
§Arguments
buf
- A buffer of data to send on thesocket
.fd
- A file descriptors to be sent.
Sourcefn send_with_fds<D: IntoIovec>(
&self,
bufs: &[D],
fds: &[RawFd],
) -> Result<usize>
fn send_with_fds<D: IntoIovec>( &self, bufs: &[D], fds: &[RawFd], ) -> Result<usize>
Sends the given data and file descriptors over the socket.
On success, returns the number of bytes sent.
§Arguments
bufs
- A list of data buffer to send on thesocket
.fds
- A list of file descriptors to be sent.
Sourcefn recv_with_fd(&self, buf: &mut [u8]) -> Result<(usize, Option<File>)>
fn recv_with_fd(&self, buf: &mut [u8]) -> Result<(usize, Option<File>)>
Receives data and potentially a file descriptor from the socket.
On success, returns the number of bytes and an optional file descriptor.
§Arguments
buf
- A buffer to receive data from the socket.
Sourceunsafe fn recv_with_fds(
&self,
iovecs: &mut [iovec],
fds: &mut [RawFd],
) -> Result<(usize, usize)>
unsafe fn recv_with_fds( &self, iovecs: &mut [iovec], fds: &mut [RawFd], ) -> Result<(usize, usize)>
Receives data and file descriptors from the socket.
On success, returns the number of bytes and file descriptors received as a tuple
(bytes count, files count)
.
§Arguments
iovecs
- A list of iovec to receive data from the socket.fds
- A slice ofRawFd
s to put the received file descriptors into. On success, the number of valid file descriptors is indicated by the second element of the returned tuple. The caller owns these file descriptors, but they will not be closed on drop like aFile
-like type would be. It is recommended that each valid file descriptor gets wrapped in a drop type that closes it after this returns.
§Safety
It is the callers responsibility to ensure it is safe for arbitrary data to be written to the iovec pointers.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.