Skip to content

Commit 59e33bc

Browse files
bors[bot]qwandor
andauthored
Merge #1301
1301: Support vsock on Android as well as Linux. r=asomers a=qwandor-google This change depends on rust-lang/libc#1905 so can't be merged until that is merged and released. Co-authored-by: Andrew Walbran <[email protected]>
2 parents 7e46b95 + c573956 commit 59e33bc

File tree

5 files changed

+20
-17
lines changed

5 files changed

+20
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ This project adheres to [Semantic Versioning](http://semver.org/).
66
## [Unreleased] - ReleaseDate
77
### Added
88
- Added Netlink protocol families to the `SockProtocol` enum
9-
(#[1289](https://github.com/nix-rust/nix/pull/1289))
9+
(#[1289](https://github.com/nix-rust/nix/pull/1289))
1010
- Added `clock_gettime`, `clock_settime`, `clock_getres`,
1111
`clock_getcpuclockid` functions and `ClockId` struct.
1212
(#[1281](https://github.com/nix-rust/nix/pull/1281))
1313
- Added wrapper functions for `PTRACE_SYSEMU` and `PTRACE_SYSEMU_SINGLESTEP`.
1414
(#[1300](https://github.com/nix-rust/nix/pull/1300))
15+
- Add support for Vsock on Android rather than just Linux.
16+
(#[1301](https://github.com/nix-rust/nix/pull/1301))
17+
1518
### Changed
1619
- Expose `SeekData` and `SeekHole` on all Linux targets
1720
(#[1284](https://github.com/nix-rust/nix/pull/1284))

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ exclude = [
1717
]
1818

1919
[dependencies]
20-
libc = { version = "0.2.77", features = [ "extra_traits" ] }
20+
libc = { version = "0.2.78", features = [ "extra_traits" ] }
2121
bitflags = "1.1"
2222
cfg-if = "0.1.10"
2323

src/sys/socket/addr.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::sys::socket::addr::sys_control::SysControlAddr;
2323
target_os = "netbsd",
2424
target_os = "openbsd"))]
2525
pub use self::datalink::LinkAddr;
26-
#[cfg(target_os = "linux")]
26+
#[cfg(any(target_os = "android", target_os = "linux"))]
2727
pub use self::vsock::VsockAddr;
2828

2929
/// These constants specify the protocol family to be used
@@ -115,7 +115,7 @@ pub enum AddressFamily {
115115
Alg = libc::AF_ALG,
116116
#[cfg(target_os = "linux")]
117117
Nfc = libc::AF_NFC,
118-
#[cfg(target_os = "linux")]
118+
#[cfg(any(target_os = "android", target_os = "linux"))]
119119
Vsock = libc::AF_VSOCK,
120120
#[cfg(any(target_os = "dragonfly",
121121
target_os = "freebsd",
@@ -242,7 +242,7 @@ impl AddressFamily {
242242
target_os = "netbsd",
243243
target_os = "openbsd"))]
244244
libc::AF_LINK => Some(AddressFamily::Link),
245-
#[cfg(target_os = "linux")]
245+
#[cfg(any(target_os = "android", target_os = "linux"))]
246246
libc::AF_VSOCK => Some(AddressFamily::Vsock),
247247
_ => None
248248
}
@@ -647,7 +647,7 @@ pub enum SockAddr {
647647
target_os = "netbsd",
648648
target_os = "openbsd"))]
649649
Link(LinkAddr),
650-
#[cfg(target_os = "linux")]
650+
#[cfg(any(target_os = "android", target_os = "linux"))]
651651
Vsock(VsockAddr),
652652
}
653653

@@ -675,7 +675,7 @@ impl SockAddr {
675675
SysControlAddr::from_name(sockfd, name, unit).map(|a| SockAddr::SysControl(a))
676676
}
677677

678-
#[cfg(target_os = "linux")]
678+
#[cfg(any(target_os = "android", target_os = "linux"))]
679679
pub fn new_vsock(cid: u32, port: u32) -> SockAddr {
680680
SockAddr::Vsock(VsockAddr::new(cid, port))
681681
}
@@ -700,7 +700,7 @@ impl SockAddr {
700700
target_os = "netbsd",
701701
target_os = "openbsd"))]
702702
SockAddr::Link(..) => AddressFamily::Link,
703-
#[cfg(target_os = "linux")]
703+
#[cfg(any(target_os = "android", target_os = "linux"))]
704704
SockAddr::Vsock(..) => AddressFamily::Vsock,
705705
}
706706
}
@@ -751,7 +751,7 @@ impl SockAddr {
751751
Some(SockAddr::Link(ether_addr))
752752
}
753753
},
754-
#[cfg(target_os = "linux")]
754+
#[cfg(any(target_os = "android", target_os = "linux"))]
755755
Some(AddressFamily::Vsock) => Some(SockAddr::Vsock(
756756
VsockAddr(*(addr as *const libc::sockaddr_vm)))),
757757
// Other address families are currently not supported and simply yield a None
@@ -837,7 +837,7 @@ impl SockAddr {
837837
},
838838
mem::size_of_val(addr) as libc::socklen_t
839839
),
840-
#[cfg(target_os = "linux")]
840+
#[cfg(any(target_os = "android", target_os = "linux"))]
841841
SockAddr::Vsock(VsockAddr(ref sa)) => (
842842
// This cast is always allowed in C
843843
unsafe {
@@ -869,7 +869,7 @@ impl fmt::Display for SockAddr {
869869
target_os = "netbsd",
870870
target_os = "openbsd"))]
871871
SockAddr::Link(ref ether_addr) => ether_addr.fmt(f),
872-
#[cfg(target_os = "linux")]
872+
#[cfg(any(target_os = "android", target_os = "linux"))]
873873
SockAddr::Vsock(ref svm) => svm.fmt(f),
874874
}
875875
}
@@ -1204,7 +1204,7 @@ mod datalink {
12041204
}
12051205
}
12061206

1207-
#[cfg(target_os = "linux")]
1207+
#[cfg(any(target_os = "android", target_os = "linux"))]
12081208
pub mod vsock {
12091209
use crate::sys::socket::addr::AddressFamily;
12101210
use libc::{sa_family_t, sockaddr_vm};

src/sys/socket/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub use self::addr::{
3333
pub use crate::sys::socket::addr::netlink::NetlinkAddr;
3434
#[cfg(any(target_os = "android", target_os = "linux"))]
3535
pub use crate::sys::socket::addr::alg::AlgAddr;
36-
#[cfg(target_os = "linux")]
36+
#[cfg(any(target_os = "android", target_os = "linux"))]
3737
pub use crate::sys::socket::addr::vsock::VsockAddr;
3838

3939
pub use libc::{
@@ -1737,7 +1737,7 @@ pub fn sockaddr_storage_to_addr(
17371737
};
17381738
Ok(SockAddr::Alg(AlgAddr(salg)))
17391739
}
1740-
#[cfg(target_os = "linux")]
1740+
#[cfg(any(target_os = "android", target_os = "linux"))]
17411741
libc::AF_VSOCK => {
17421742
use libc::sockaddr_vm;
17431743
let svm = unsafe {

test/sys/test_socket.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ pub fn test_recv_ipv6pktinfo() {
14111411
}
14121412
}
14131413

1414-
#[cfg(target_os = "linux")]
1414+
#[cfg(any(target_os = "android", target_os = "linux"))]
14151415
#[test]
14161416
pub fn test_vsock() {
14171417
use libc;
@@ -1428,13 +1428,13 @@ pub fn test_vsock() {
14281428
SockFlag::empty(), None)
14291429
.expect("socket failed");
14301430

1431-
// VMADDR_CID_HYPERVISOR and VMADDR_CID_RESERVED are reserved, so we expect
1431+
// VMADDR_CID_HYPERVISOR and VMADDR_CID_LOCAL are reserved, so we expect
14321432
// an EADDRNOTAVAIL error.
14331433
let sockaddr = SockAddr::new_vsock(libc::VMADDR_CID_HYPERVISOR, port);
14341434
assert_eq!(bind(s1, &sockaddr).err(),
14351435
Some(Error::Sys(Errno::EADDRNOTAVAIL)));
14361436

1437-
let sockaddr = SockAddr::new_vsock(libc::VMADDR_CID_RESERVED, port);
1437+
let sockaddr = SockAddr::new_vsock(libc::VMADDR_CID_LOCAL, port);
14381438
assert_eq!(bind(s1, &sockaddr).err(),
14391439
Some(Error::Sys(Errno::EADDRNOTAVAIL)));
14401440

0 commit comments

Comments
 (0)