Skip to content

Commit

Permalink
Update to latest versions of libc and nix.
Browse files Browse the repository at this point in the history
The new version of nix uses OwnedFd rather than RawFd, which lets us
remove some conversions.

Signed-off-by: Andrew Walbran <[email protected]>
  • Loading branch information
qwandor committed Nov 27, 2023
1 parent 3906e3b commit 324db12
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ edition = "2018"
exclude = ["test_fixture"]

[dependencies]
libc = "0.2.126"
nix = "0.24.2"
libc = "0.2.150"
nix = { version = "0.27.1", features = ["ioctl", "socket"] }

[dev-dependencies]
rand = "0.8.3"
Expand Down
16 changes: 7 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,12 @@ pub use libc::{VMADDR_CID_ANY, VMADDR_CID_HOST, VMADDR_CID_HYPERVISOR, VMADDR_CI
pub use nix::sys::socket::{SockaddrLike, VsockAddr};

fn new_socket() -> Result<OwnedFd> {
let fd = socket(
Ok(socket(
AddressFamily::Vsock,
SockType::Stream,
SockFlag::SOCK_CLOEXEC,
None,
)?;
// SAFETY: We just created a new file descriptor, so we can take ownership of it.
unsafe { Ok(OwnedFd::from_raw_fd(fd)) }
)?)
}

/// An iterator that infinitely accepts connections on a VsockListener.
Expand Down Expand Up @@ -88,7 +86,7 @@ impl VsockListener {
bind(socket.as_raw_fd(), addr)?;

// rust stdlib uses a 128 connection backlog
listen(socket.as_raw_fd(), 128)?;
listen(&socket, 128)?;

Ok(Self { socket })
}
Expand Down Expand Up @@ -145,7 +143,7 @@ impl VsockListener {

/// Retrieve the latest error associated with the underlying socket.
pub fn take_error(&self) -> Result<Option<Error>> {
let error = SocketError.get(self.socket.as_raw_fd())?;
let error = SocketError.get(&self.socket)?;
Ok(if error == 0 {
None
} else {
Expand Down Expand Up @@ -246,18 +244,18 @@ impl VsockStream {
/// Set the timeout on read operations.
pub fn set_read_timeout(&self, dur: Option<Duration>) -> Result<()> {
let timeout = Self::timeval_from_duration(dur)?.into();
Ok(ReceiveTimeout.set(self.socket.as_raw_fd(), &timeout)?)
Ok(ReceiveTimeout.set(&self.socket, &timeout)?)
}

/// Set the timeout on write operations.
pub fn set_write_timeout(&self, dur: Option<Duration>) -> Result<()> {
let timeout = Self::timeval_from_duration(dur)?.into();
Ok(SendTimeout.set(self.socket.as_raw_fd(), &timeout)?)
Ok(SendTimeout.set(&self.socket, &timeout)?)
}

/// Retrieve the latest error associated with the underlying socket.
pub fn take_error(&self) -> Result<Option<Error>> {
let error = SocketError.get(self.socket.as_raw_fd())?;
let error = SocketError.get(&self.socket)?;
Ok(if error == 0 {
None
} else {
Expand Down

0 comments on commit 324db12

Please sign in to comment.