6
6
//! Common functions and macros.
7
7
8
8
use crate :: error:: { Error , Result } ;
9
- #[ cfg( any( feature = "async" , not( target_os = "linux" ) ) ) ]
9
+ #[ cfg( any(
10
+ feature = "async" ,
11
+ not( any( target_os = "linux" , target_os = "android" ) )
12
+ ) ) ]
10
13
use nix:: fcntl:: FdFlag ;
11
14
use nix:: fcntl:: { fcntl, FcntlArg , OFlag } ;
12
15
use nix:: sys:: socket:: * ;
@@ -15,7 +18,7 @@ use std::os::unix::io::RawFd;
15
18
#[ derive( Debug , Clone , Copy , PartialEq ) ]
16
19
pub ( crate ) enum Domain {
17
20
Unix ,
18
- #[ cfg( target_os = "linux" ) ]
21
+ #[ cfg( any ( target_os = "linux" , target_os = "android" ) ) ]
19
22
Vsock ,
20
23
}
21
24
@@ -30,7 +33,7 @@ pub(crate) fn do_listen(listener: RawFd) -> Result<()> {
30
33
listen ( listener, 10 ) . map_err ( |e| Error :: Socket ( e. to_string ( ) ) )
31
34
}
32
35
33
- #[ cfg( target_os = "linux" ) ]
36
+ #[ cfg( any ( target_os = "linux" , target_os = "android" ) ) ]
34
37
fn parse_sockaddr ( addr : & str ) -> Result < ( Domain , & str ) > {
35
38
if let Some ( addr) = addr. strip_prefix ( "unix://" ) {
36
39
return Ok ( ( Domain :: Unix , addr) ) ;
@@ -43,7 +46,7 @@ fn parse_sockaddr(addr: &str) -> Result<(Domain, &str)> {
43
46
Err ( Error :: Others ( format ! ( "Scheme {:?} is not supported" , addr) ) )
44
47
}
45
48
46
- #[ cfg( not( target_os = "linux" ) ) ]
49
+ #[ cfg( not( any ( target_os = "linux" , target_os = "android" ) ) ) ]
47
50
fn parse_sockaddr ( addr : & str ) -> Result < ( Domain , & str ) > {
48
51
if let Some ( addr) = addr. strip_prefix ( "unix://" ) {
49
52
if addr. starts_with ( '@' ) {
@@ -57,7 +60,10 @@ fn parse_sockaddr(addr: &str) -> Result<(Domain, &str)> {
57
60
Err ( Error :: Others ( format ! ( "Scheme {:?} is not supported" , addr) ) )
58
61
}
59
62
60
- #[ cfg( any( feature = "async" , not( target_os = "linux" ) ) ) ]
63
+ #[ cfg( any(
64
+ feature = "async" ,
65
+ not( any( target_os = "linux" , target_os = "android" ) )
66
+ ) ) ]
61
67
pub ( crate ) fn set_fd_close_exec ( fd : RawFd ) -> Result < RawFd > {
62
68
if let Err ( e) = fcntl ( fd, FcntlArg :: F_SETFD ( FdFlag :: FD_CLOEXEC ) ) {
63
69
return Err ( Error :: Others ( format ! (
@@ -69,12 +75,12 @@ pub(crate) fn set_fd_close_exec(fd: RawFd) -> Result<RawFd> {
69
75
}
70
76
71
77
// SOCK_CLOEXEC flag is Linux specific
72
- #[ cfg( target_os = "linux" ) ]
78
+ #[ cfg( any ( target_os = "linux" , target_os = "android" ) ) ]
73
79
pub ( crate ) const SOCK_CLOEXEC : SockFlag = SockFlag :: SOCK_CLOEXEC ;
74
- #[ cfg( not( target_os = "linux" ) ) ]
80
+ #[ cfg( not( any ( target_os = "linux" , target_os = "android" ) ) ) ]
75
81
pub ( crate ) const SOCK_CLOEXEC : SockFlag = SockFlag :: empty ( ) ;
76
82
77
- #[ cfg( target_os = "linux" ) ]
83
+ #[ cfg( any ( target_os = "linux" , target_os = "android" ) ) ]
78
84
fn make_addr ( domain : Domain , sockaddr : & str ) -> Result < UnixAddr > {
79
85
match domain {
80
86
Domain :: Unix => {
@@ -90,7 +96,7 @@ fn make_addr(domain: Domain, sockaddr: &str) -> Result<UnixAddr> {
90
96
}
91
97
}
92
98
93
- #[ cfg( not( target_os = "linux" ) ) ]
99
+ #[ cfg( not( any ( target_os = "linux" , target_os = "android" ) ) ) ]
94
100
fn make_addr ( _domain : Domain , sockaddr : & str ) -> Result < UnixAddr > {
95
101
UnixAddr :: new ( sockaddr) . map_err ( err_to_others_err ! ( e, "" ) )
96
102
}
@@ -114,7 +120,7 @@ fn make_socket(addr: (&str, u32)) -> Result<(RawFd, Domain, SockAddr)> {
114
120
115
121
let ( fd, sockaddr) = match domain {
116
122
Domain :: Unix => get_sock_addr ( domain, sockaddrv) ?,
117
- #[ cfg( target_os = "linux" ) ]
123
+ #[ cfg( any ( target_os = "linux" , target_os = "android" ) ) ]
118
124
Domain :: Vsock => {
119
125
let sockaddr_port_v: Vec < & str > = sockaddrv. split ( ':' ) . collect ( ) ;
120
126
if sockaddr_port_v. len ( ) != 2 {
@@ -143,13 +149,13 @@ fn make_socket(addr: (&str, u32)) -> Result<(RawFd, Domain, SockAddr)> {
143
149
}
144
150
145
151
// Vsock is not supported on non Linux.
146
- #[ cfg( target_os = "linux" ) ]
152
+ #[ cfg( any ( target_os = "linux" , target_os = "android" ) ) ]
147
153
use libc:: VMADDR_CID_ANY ;
148
- #[ cfg( not( target_os = "linux" ) ) ]
154
+ #[ cfg( not( any ( target_os = "linux" , target_os = "android" ) ) ) ]
149
155
const VMADDR_CID_ANY : u32 = 0 ;
150
- #[ cfg( target_os = "linux" ) ]
156
+ #[ cfg( any ( target_os = "linux" , target_os = "android" ) ) ]
151
157
use libc:: VMADDR_CID_HOST ;
152
- #[ cfg( not( target_os = "linux" ) ) ]
158
+ #[ cfg( not( any ( target_os = "linux" , target_os = "android" ) ) ) ]
153
159
const VMADDR_CID_HOST : u32 = 0 ;
154
160
155
161
pub ( crate ) fn do_bind ( sockaddr : & str ) -> Result < ( RawFd , Domain ) > {
@@ -194,7 +200,7 @@ macro_rules! cfg_async {
194
200
mod tests {
195
201
use super :: * ;
196
202
197
- #[ cfg( target_os = "linux" ) ]
203
+ #[ cfg( any ( target_os = "linux" , target_os = "android" ) ) ]
198
204
#[ test]
199
205
fn test_parse_sockaddr ( ) {
200
206
for i in & [
@@ -226,7 +232,7 @@ mod tests {
226
232
}
227
233
}
228
234
229
- #[ cfg( not( target_os = "linux" ) ) ]
235
+ #[ cfg( not( any ( target_os = "linux" , target_os = "android" ) ) ) ]
230
236
#[ test]
231
237
fn test_parse_sockaddr ( ) {
232
238
for i in & [
0 commit comments