@@ -12,27 +12,29 @@ pub struct AnonPipe(FileDesc);
1212pub fn anon_pipe ( ) -> io:: Result < ( AnonPipe , AnonPipe ) > {
1313 let mut fds = [ 0 ; 2 ] ;
1414
15- // Unfortunately the only known way right now to create atomically set the
16- // CLOEXEC flag is to use the `pipe2` syscall on Linux. This was added in
17- // 2.6.27, glibc 2.9 and musl 0.9.3.
18- if cfg ! ( any(
19- target_os = "dragonfly" ,
20- target_os = "freebsd" ,
21- target_os = "linux" ,
22- target_os = "netbsd" ,
23- target_os = "openbsd" ,
24- target_os = "redox"
25- ) ) {
26- cvt ( unsafe { libc:: pipe2 ( fds. as_mut_ptr ( ) , libc:: O_CLOEXEC ) } ) ?;
27- Ok ( ( AnonPipe ( FileDesc :: new ( fds[ 0 ] ) ) , AnonPipe ( FileDesc :: new ( fds[ 1 ] ) ) ) )
28- } else {
29- cvt ( unsafe { libc:: pipe ( fds. as_mut_ptr ( ) ) } ) ?;
30-
31- let fd0 = FileDesc :: new ( fds[ 0 ] ) ;
32- let fd1 = FileDesc :: new ( fds[ 1 ] ) ;
33- fd0. set_cloexec ( ) ?;
34- fd1. set_cloexec ( ) ?;
35- Ok ( ( AnonPipe ( fd0) , AnonPipe ( fd1) ) )
15+ // The only known way right now to create atomically set the CLOEXEC flag is
16+ // to use the `pipe2` syscall. This was added to Linux in 2.6.27, glibc 2.9
17+ // and musl 0.9.3, and some other targets also have it.
18+ cfg_if:: cfg_if! {
19+ if #[ cfg( any(
20+ target_os = "dragonfly" ,
21+ target_os = "freebsd" ,
22+ target_os = "linux" ,
23+ target_os = "netbsd" ,
24+ target_os = "openbsd" ,
25+ target_os = "redox"
26+ ) ) ] {
27+ cvt( unsafe { libc:: pipe2( fds. as_mut_ptr( ) , libc:: O_CLOEXEC ) } ) ?;
28+ Ok ( ( AnonPipe ( FileDesc :: new( fds[ 0 ] ) ) , AnonPipe ( FileDesc :: new( fds[ 1 ] ) ) ) )
29+ } else {
30+ cvt( unsafe { libc:: pipe( fds. as_mut_ptr( ) ) } ) ?;
31+
32+ let fd0 = FileDesc :: new( fds[ 0 ] ) ;
33+ let fd1 = FileDesc :: new( fds[ 1 ] ) ;
34+ fd0. set_cloexec( ) ?;
35+ fd1. set_cloexec( ) ?;
36+ Ok ( ( AnonPipe ( fd0) , AnonPipe ( fd1) ) )
37+ }
3638 }
3739}
3840
0 commit comments