From 53da854252d567f660a74740c75fc426346bba20 Mon Sep 17 00:00:00 2001 From: Akira Moroo Date: Mon, 6 May 2024 12:56:14 +0900 Subject: [PATCH] build: Add support for macOS VM Socket on macOS is used in the guest of Virtualization.framework VM. Signed-off-by: Akira Moroo --- Cargo.toml | 2 +- src/lib.rs | 6 +++--- src/stream.rs | 4 ++-- tests/vsock.rs | 6 +++++- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b32aef1..ff721f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ tonic-conn = ["tonic"] bytes = "1.3.0" futures = "0.3" libc = "0.2.138" -vsock = "0.5.0" +vsock = "0.5.1" tokio = { version = "1", features = ["net", "sync"] } tonic = { version = "0.12.0", optional = true } diff --git a/src/lib.rs b/src/lib.rs index 9fa4f84..b601082 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,6 +28,6 @@ pub use stream::VsockStream; #[cfg(feature = "tonic-conn")] #[cfg_attr(docsrs, doc(cfg(feature = "tonic-conn")))] pub use tonic_support::VsockConnectInfo; -pub use vsock::{ - VsockAddr, VMADDR_CID_ANY, VMADDR_CID_HOST, VMADDR_CID_HYPERVISOR, VMADDR_CID_LOCAL, -}; +#[cfg(any(target_os = "linux", target_os = "android"))] +pub use vsock::VMADDR_CID_LOCAL; +pub use vsock::{VsockAddr, VMADDR_CID_ANY, VMADDR_CID_HOST, VMADDR_CID_HYPERVISOR}; diff --git a/src/stream.rs b/src/stream.rs index 73963fe..fc971dd 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -74,12 +74,12 @@ impl VsockStream { /// Open a connection to a remote host. pub async fn connect(addr: VsockAddr) -> Result { - let socket = unsafe { socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC, 0) }; + let socket = unsafe { socket(AF_VSOCK, SOCK_STREAM, 0) }; if socket < 0 { return Err(Error::last_os_error()); } - if unsafe { fcntl(socket, F_SETFL, O_NONBLOCK) } < 0 { + if unsafe { fcntl(socket, F_SETFL, O_NONBLOCK | O_CLOEXEC) } < 0 { let _ = unsafe { close(socket) }; return Err(Error::last_os_error()); } diff --git a/tests/vsock.rs b/tests/vsock.rs index 067e97a..e52b9af 100644 --- a/tests/vsock.rs +++ b/tests/vsock.rs @@ -17,7 +17,9 @@ use rand::RngCore; use sha2::{Digest, Sha256}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; -use tokio_vsock::{VsockAddr, VsockListener, VsockStream}; +#[cfg(target_os = "linux")] +use tokio_vsock::VsockListener; +use tokio_vsock::{VsockAddr, VsockStream}; const TEST_BLOB_SIZE: usize = 100_000; const TEST_BLOCK_SIZE: usize = 5_000; @@ -90,6 +92,7 @@ async fn test_vsock_conn_error() { /// /// source: https://github.com/tokio-rs/tokio/blob/fc9518b62714daac9a38b46c698b94ac5d5b1ca2/tokio/tests/tcp_split.rs #[tokio::test] +#[cfg(target_os = "linux")] async fn split_vsock() { const MSG: &[u8] = b"split"; const PORT: u32 = 8002; @@ -139,6 +142,7 @@ async fn split_vsock() { /// /// source: https://github.com/tokio-rs/tokio/blob/fc9518b62714daac9a38b46c698b94ac5d5b1ca2/tokio/tests/tcp_split.rs #[tokio::test] +#[cfg(target_os = "linux")] async fn into_split_vsock() { const MSG: &[u8] = b"split"; const PORT: u32 = 8001;