diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2022-05-09 06:01:22 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2022-05-09 06:01:22 +0000 |
commit | 77274d0eaaa1024a949283ee4da80c22209d4861 (patch) | |
tree | 911bb07584e758873118951a14ea4e361198c485 | |
parent | 90643b9efd28f77294feda0cb7322268e674c0d8 (diff) | |
parent | 34c25bb318dc916018e33e50b1fec97323b38532 (diff) | |
download | vsock-77274d0eaaa1024a949283ee4da80c22209d4861.tar.gz |
Snap for 8558685 from 34c25bb318dc916018e33e50b1fec97323b38532 to tm-frc-documentsui-releaset_frc_doc_330543000t_frc_doc_330443060t_frc_doc_330443000android13-frc-documentsui-release
Change-Id: I7f366a664ffd0995538af0f65d526b686b971dda
-rw-r--r-- | .cargo_vcs_info.json | 2 | ||||
-rw-r--r-- | Android.bp | 2 | ||||
-rw-r--r-- | Cargo.toml | 13 | ||||
-rw-r--r-- | Cargo.toml.orig | 4 | ||||
-rw-r--r-- | METADATA | 10 | ||||
-rw-r--r-- | src/lib.rs | 29 | ||||
-rw-r--r-- | tests/vsock.rs | 7 |
7 files changed, 46 insertions, 21 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index 4fc9144..3d9568d 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,5 @@ { "git": { - "sha1": "56852d4006efce337875594e6a21ec49f0fc9c89" + "sha1": "9b5e180bf04fd1d02db0c1e79c5ddd838dd42328" } } @@ -23,7 +23,7 @@ rust_library { host_supported: true, crate_name: "vsock", cargo_env_compat: true, - cargo_pkg_version: "0.2.4", + cargo_pkg_version: "0.2.6", srcs: ["src/lib.rs"], edition: "2018", rustlibs: [ @@ -3,17 +3,16 @@ # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies -# to registry (e.g., crates.io) dependencies +# to registry (e.g., crates.io) dependencies. # -# If you believe there's an error in this file please file an -# issue against the rust-lang/cargo repository. If you're -# editing this file be aware that the upstream Cargo.toml -# will likely look very different (and much more reasonable) +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "vsock" -version = "0.2.4" +version = "0.2.6" authors = ["fsyncd", "rust-vsock"] exclude = ["test_fixture"] description = "Virtio socket support for Rust" @@ -25,7 +24,7 @@ repository = "https://github.com/rust-vsock/vsock-rs" version = "0.2.79" [dependencies.nix] -version = "0.19.1" +version = "0.23.0" [dev-dependencies.rand] version = "0.8.3" diff --git a/Cargo.toml.orig b/Cargo.toml.orig index aa90128..7a6d39a 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "vsock" -version = "0.2.4" +version = "0.2.6" authors = ["fsyncd", "rust-vsock"] description = "Virtio socket support for Rust" repository = "https://github.com/rust-vsock/vsock-rs" @@ -12,7 +12,7 @@ exclude = ["test_fixture"] [dependencies] libc = "0.2.79" -nix = "0.19.1" +nix = "0.23.0" [dev-dependencies] rand = "0.8.3" @@ -7,13 +7,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/vsock/vsock-0.2.4.crate" + value: "https://static.crates.io/crates/vsock/vsock-0.2.6.crate" } - version: "0.2.4" + version: "0.2.6" license_type: NOTICE last_upgrade_date { - year: 2021 - month: 6 - day: 21 + year: 2022 + month: 3 + day: 1 } } @@ -17,15 +17,17 @@ //! Virtio socket support for Rust. -use std::io::{Error, ErrorKind, Read, Result, Write}; -use std::mem::{self, size_of}; -use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; - use libc::*; +use nix::ioctl_read_bad; use std::ffi::c_void; +use std::fs::File; +use std::io::{Error, ErrorKind, Read, Result, Write}; +use std::mem::{self, size_of}; use std::net::Shutdown; +use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; use std::time::Duration; +pub use libc::{VMADDR_CID_ANY, VMADDR_CID_HOST, VMADDR_CID_HYPERVISOR, VMADDR_CID_LOCAL}; pub use nix::sys::socket::{SockAddr, VsockAddr}; fn new_socket() -> libc::c_int { @@ -505,3 +507,22 @@ impl Drop for VsockStream { unsafe { close(self.socket) }; } } + +const IOCTL_VM_SOCKETS_GET_LOCAL_CID: usize = 0x7b9; +ioctl_read_bad!( + vm_sockets_get_local_cid, + IOCTL_VM_SOCKETS_GET_LOCAL_CID, + u32 +); + +/// Gets the CID of the local machine. +/// +/// Note that when calling [`VsockListener::bind`], you should generally use [`VMADDR_CID_ANY`] +/// instead, and for making a loopback connection you should use [`VMADDR_CID_LOCAL`]. +pub fn get_local_cid() -> Result<u32> { + let f = File::open("/dev/vsock")?; + let mut cid = 0; + // SAFETY: the kernel only modifies the given u32 integer. + unsafe { vm_sockets_get_local_cid(f.as_raw_fd(), &mut cid) }?; + Ok(cid) +} diff --git a/tests/vsock.rs b/tests/vsock.rs index d51bad0..52d908b 100644 --- a/tests/vsock.rs +++ b/tests/vsock.rs @@ -17,7 +17,7 @@ use rand::RngCore; use sha2::{Digest, Sha256}; use std::io::{Read, Write}; -use vsock::{SockAddr, VsockAddr, VsockStream}; +use vsock::{get_local_cid, SockAddr, VsockAddr, VsockStream, VMADDR_CID_HOST}; const TEST_BLOB_SIZE: usize = 1_000_000; const TEST_BLOCK_SIZE: usize = 5_000; @@ -67,3 +67,8 @@ fn test_vsock() { assert_eq!(expected, actual); } + +#[test] +fn test_get_local_cid() { + assert_eq!(get_local_cid().unwrap(), VMADDR_CID_HOST); +} |