66//! Common functions and macros.
77
88use 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+ ) ) ]
1013use nix:: fcntl:: FdFlag ;
1114use nix:: fcntl:: { fcntl, FcntlArg , OFlag } ;
1215use nix:: sys:: socket:: * ;
@@ -15,7 +18,7 @@ use std::os::unix::io::RawFd;
1518#[ derive( Debug , Clone , Copy , PartialEq ) ]
1619pub ( crate ) enum Domain {
1720 Unix ,
18- #[ cfg( target_os = "linux" ) ]
21+ #[ cfg( any ( target_os = "linux" , target_os = "android" ) ) ]
1922 Vsock ,
2023}
2124
@@ -30,7 +33,7 @@ pub(crate) fn do_listen(listener: RawFd) -> Result<()> {
3033 listen ( listener, 10 ) . map_err ( |e| Error :: Socket ( e. to_string ( ) ) )
3134}
3235
33- #[ cfg( target_os = "linux" ) ]
36+ #[ cfg( any ( target_os = "linux" , target_os = "android" ) ) ]
3437fn parse_sockaddr ( addr : & str ) -> Result < ( Domain , & str ) > {
3538 if let Some ( addr) = addr. strip_prefix ( "unix://" ) {
3639 return Ok ( ( Domain :: Unix , addr) ) ;
@@ -43,7 +46,7 @@ fn parse_sockaddr(addr: &str) -> Result<(Domain, &str)> {
4346 Err ( Error :: Others ( format ! ( "Scheme {:?} is not supported" , addr) ) )
4447}
4548
46- #[ cfg( not( target_os = "linux" ) ) ]
49+ #[ cfg( not( any ( target_os = "linux" , target_os = "android" ) ) ) ]
4750fn parse_sockaddr ( addr : & str ) -> Result < ( Domain , & str ) > {
4851 if let Some ( addr) = addr. strip_prefix ( "unix://" ) {
4952 if addr. starts_with ( '@' ) {
@@ -57,7 +60,10 @@ fn parse_sockaddr(addr: &str) -> Result<(Domain, &str)> {
5760 Err ( Error :: Others ( format ! ( "Scheme {:?} is not supported" , addr) ) )
5861}
5962
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+ ) ) ]
6167pub ( crate ) fn set_fd_close_exec ( fd : RawFd ) -> Result < RawFd > {
6268 if let Err ( e) = fcntl ( fd, FcntlArg :: F_SETFD ( FdFlag :: FD_CLOEXEC ) ) {
6369 return Err ( Error :: Others ( format ! (
@@ -69,12 +75,12 @@ pub(crate) fn set_fd_close_exec(fd: RawFd) -> Result<RawFd> {
6975}
7076
7177// SOCK_CLOEXEC flag is Linux specific
72- #[ cfg( target_os = "linux" ) ]
78+ #[ cfg( any ( target_os = "linux" , target_os = "android" ) ) ]
7379pub ( crate ) const SOCK_CLOEXEC : SockFlag = SockFlag :: SOCK_CLOEXEC ;
74- #[ cfg( not( target_os = "linux" ) ) ]
80+ #[ cfg( not( any ( target_os = "linux" , target_os = "android" ) ) ) ]
7581pub ( crate ) const SOCK_CLOEXEC : SockFlag = SockFlag :: empty ( ) ;
7682
77- #[ cfg( target_os = "linux" ) ]
83+ #[ cfg( any ( target_os = "linux" , target_os = "android" ) ) ]
7884fn make_addr ( domain : Domain , sockaddr : & str ) -> Result < UnixAddr > {
7985 match domain {
8086 Domain :: Unix => {
@@ -90,7 +96,7 @@ fn make_addr(domain: Domain, sockaddr: &str) -> Result<UnixAddr> {
9096 }
9197}
9298
93- #[ cfg( not( target_os = "linux" ) ) ]
99+ #[ cfg( not( any ( target_os = "linux" , target_os = "android" ) ) ) ]
94100fn make_addr ( _domain : Domain , sockaddr : & str ) -> Result < UnixAddr > {
95101 UnixAddr :: new ( sockaddr) . map_err ( err_to_others_err ! ( e, "" ) )
96102}
@@ -114,7 +120,7 @@ fn make_socket(addr: (&str, u32)) -> Result<(RawFd, Domain, SockAddr)> {
114120
115121 let ( fd, sockaddr) = match domain {
116122 Domain :: Unix => get_sock_addr ( domain, sockaddrv) ?,
117- #[ cfg( target_os = "linux" ) ]
123+ #[ cfg( any ( target_os = "linux" , target_os = "android" ) ) ]
118124 Domain :: Vsock => {
119125 let sockaddr_port_v: Vec < & str > = sockaddrv. split ( ':' ) . collect ( ) ;
120126 if sockaddr_port_v. len ( ) != 2 {
@@ -143,13 +149,13 @@ fn make_socket(addr: (&str, u32)) -> Result<(RawFd, Domain, SockAddr)> {
143149}
144150
145151// Vsock is not supported on non Linux.
146- #[ cfg( target_os = "linux" ) ]
152+ #[ cfg( any ( target_os = "linux" , target_os = "android" ) ) ]
147153use libc:: VMADDR_CID_ANY ;
148- #[ cfg( not( target_os = "linux" ) ) ]
154+ #[ cfg( not( any ( target_os = "linux" , target_os = "android" ) ) ) ]
149155const VMADDR_CID_ANY : u32 = 0 ;
150- #[ cfg( target_os = "linux" ) ]
156+ #[ cfg( any ( target_os = "linux" , target_os = "android" ) ) ]
151157use libc:: VMADDR_CID_HOST ;
152- #[ cfg( not( target_os = "linux" ) ) ]
158+ #[ cfg( not( any ( target_os = "linux" , target_os = "android" ) ) ) ]
153159const VMADDR_CID_HOST : u32 = 0 ;
154160
155161pub ( crate ) fn do_bind ( sockaddr : & str ) -> Result < ( RawFd , Domain ) > {
@@ -194,7 +200,7 @@ macro_rules! cfg_async {
194200mod tests {
195201 use super :: * ;
196202
197- #[ cfg( target_os = "linux" ) ]
203+ #[ cfg( any ( target_os = "linux" , target_os = "android" ) ) ]
198204 #[ test]
199205 fn test_parse_sockaddr ( ) {
200206 for i in & [
@@ -226,7 +232,7 @@ mod tests {
226232 }
227233 }
228234
229- #[ cfg( not( target_os = "linux" ) ) ]
235+ #[ cfg( not( any ( target_os = "linux" , target_os = "android" ) ) ) ]
230236 #[ test]
231237 fn test_parse_sockaddr ( ) {
232238 for i in & [
0 commit comments