@@ -18,16 +18,6 @@ pub extern crate libc as netc;
1818
1919pub type wrlen_t = size_t ;
2020
21- // See below for the usage of SOCK_CLOEXEC, but this constant is only defined on
22- // Linux currently (e.g., support doesn't exist on other platforms). In order to
23- // get name resolution to work and things to compile we just define a dummy
24- // SOCK_CLOEXEC here for other platforms. Note that the dummy constant isn't
25- // actually ever used (the blocks below are wrapped in `if cfg!` as well.
26- #[ cfg( target_os = "linux" ) ]
27- use libc:: SOCK_CLOEXEC ;
28- #[ cfg( not( target_os = "linux" ) ) ]
29- const SOCK_CLOEXEC : c_int = 0 ;
30-
3121pub struct Socket ( FileDesc ) ;
3222
3323pub fn init ( ) { }
@@ -69,8 +59,9 @@ impl Socket {
6959 // this option, however, was added in 2.6.27, and we still support
7060 // 2.6.18 as a kernel, so if the returned error is EINVAL we
7161 // fallthrough to the fallback.
72- if cfg ! ( target_os = "linux" ) {
73- match cvt ( libc:: socket ( fam, ty | SOCK_CLOEXEC , 0 ) ) {
62+ #[ cfg( target_os = "linux" ) ]
63+ {
64+ match cvt ( libc:: socket ( fam, ty | libc:: SOCK_CLOEXEC , 0 ) ) {
7465 Ok ( fd) => return Ok ( Socket ( FileDesc :: new ( fd) ) ) ,
7566 Err ( ref e) if e. raw_os_error ( ) == Some ( libc:: EINVAL ) => { }
7667 Err ( e) => return Err ( e) ,
@@ -96,8 +87,9 @@ impl Socket {
9687 let mut fds = [ 0 , 0 ] ;
9788
9889 // Like above, see if we can set cloexec atomically
99- if cfg ! ( target_os = "linux" ) {
100- match cvt ( libc:: socketpair ( fam, ty | SOCK_CLOEXEC , 0 , fds. as_mut_ptr ( ) ) ) {
90+ #[ cfg( target_os = "linux" ) ]
91+ {
92+ match cvt ( libc:: socketpair ( fam, ty | libc:: SOCK_CLOEXEC , 0 , fds. as_mut_ptr ( ) ) ) {
10193 Ok ( _) => {
10294 return Ok ( ( Socket ( FileDesc :: new ( fds[ 0 ] ) ) , Socket ( FileDesc :: new ( fds[ 1 ] ) ) ) ) ;
10395 }
@@ -187,7 +179,8 @@ impl Socket {
187179 // atomically set the CLOEXEC flag is to use the `accept4` syscall on
188180 // Linux. This was added in 2.6.28, however, and because we support
189181 // 2.6.18 we must detect this support dynamically.
190- if cfg ! ( target_os = "linux" ) {
182+ #[ cfg( target_os = "linux" ) ]
183+ {
191184 syscall ! {
192185 fn accept4(
193186 fd: c_int,
@@ -196,7 +189,7 @@ impl Socket {
196189 flags: c_int
197190 ) -> c_int
198191 }
199- let res = cvt_r ( || unsafe { accept4 ( self . 0 . raw ( ) , storage, len, SOCK_CLOEXEC ) } ) ;
192+ let res = cvt_r ( || unsafe { accept4 ( self . 0 . raw ( ) , storage, len, libc :: SOCK_CLOEXEC ) } ) ;
200193 match res {
201194 Ok ( fd) => return Ok ( Socket ( FileDesc :: new ( fd) ) ) ,
202195 Err ( ref e) if e. raw_os_error ( ) == Some ( libc:: ENOSYS ) => { }
0 commit comments