@@ -93,60 +93,61 @@ pub fn init() {
9393 reset_sigpipe ( ) ;
9494 }
9595
96- // In the case when all file descriptors are open, the poll has been
97- // observed to perform better than fcntl (on GNU/Linux).
98- #[ cfg( not( any(
99- miri,
100- target_os = "emscripten" ,
101- target_os = "fuchsia" ,
102- // The poll on Darwin doesn't set POLLNVAL for closed fds.
103- target_os = "macos" ,
104- target_os = "ios" ,
105- target_os = "redox" ,
106- ) ) ) ]
107- unsafe fn sanitize_standard_fds ( ) {
108- use crate :: sys:: os:: errno;
109- let pfds: & mut [ _ ] = & mut [
110- libc:: pollfd { fd : 0 , events : 0 , revents : 0 } ,
111- libc:: pollfd { fd : 1 , events : 0 , revents : 0 } ,
112- libc:: pollfd { fd : 2 , events : 0 , revents : 0 } ,
113- ] ;
114- while libc:: poll ( pfds. as_mut_ptr ( ) , 3 , 0 ) == -1 {
115- if errno ( ) == libc:: EINTR {
116- continue ;
117- }
118- libc:: abort ( ) ;
119- }
120- for pfd in pfds {
121- if pfd. revents & libc:: POLLNVAL == 0 {
122- continue ;
123- }
124- if libc:: open ( "/dev/null\0 " . as_ptr ( ) . cast ( ) , libc:: O_RDWR , 0 ) == -1 {
125- // If the stream is closed but we failed to reopen it, abort the
126- // process. Otherwise we wouldn't preserve the safety of
127- // operations on the corresponding Rust object Stdin, Stdout, or
128- // Stderr.
129- libc:: abort ( ) ;
130- }
131- }
132- }
133- #[ cfg( any( target_os = "macos" , target_os = "ios" , target_os = "redox" ) ) ]
134- unsafe fn sanitize_standard_fds ( ) {
135- use crate :: sys:: os:: errno;
136- for fd in 0 ..3 {
137- if libc:: fcntl ( fd, libc:: F_GETFD ) == -1 && errno ( ) == libc:: EBADF {
138- if libc:: open ( "/dev/null\0 " . as_ptr ( ) . cast ( ) , libc:: O_RDWR , 0 ) == -1 {
96+ cfg_if:: cfg_if! {
97+ if #[ cfg( miri) ] {
98+ // The standard fds are always available in Miri.
99+ unsafe fn sanitize_standard_fds( ) { }
100+ } else if #[ cfg( not( any(
101+ target_os = "emscripten" ,
102+ target_os = "fuchsia" ,
103+ // The poll on Darwin doesn't set POLLNVAL for closed fds.
104+ target_os = "macos" ,
105+ target_os = "ios" ,
106+ target_os = "redox" ,
107+ ) ) ) ] {
108+ // In the case when all file descriptors are open, the poll has been
109+ // observed to perform better than fcntl (on GNU/Linux).
110+ unsafe fn sanitize_standard_fds( ) {
111+ use crate :: sys:: os:: errno;
112+ let pfds: & mut [ _] = & mut [
113+ libc:: pollfd { fd: 0 , events: 0 , revents: 0 } ,
114+ libc:: pollfd { fd: 1 , events: 0 , revents: 0 } ,
115+ libc:: pollfd { fd: 2 , events: 0 , revents: 0 } ,
116+ ] ;
117+ while libc:: poll( pfds. as_mut_ptr( ) , 3 , 0 ) == -1 {
118+ if errno( ) == libc:: EINTR {
119+ continue ;
120+ }
139121 libc:: abort( ) ;
140122 }
123+ for pfd in pfds {
124+ if pfd. revents & libc:: POLLNVAL == 0 {
125+ continue ;
126+ }
127+ if libc:: open( "/dev/null\0 " . as_ptr( ) . cast( ) , libc:: O_RDWR , 0 ) == -1 {
128+ // If the stream is closed but we failed to reopen it, abort the
129+ // process. Otherwise we wouldn't preserve the safety of
130+ // operations on the corresponding Rust object Stdin, Stdout, or
131+ // Stderr.
132+ libc:: abort( ) ;
133+ }
134+ }
135+ }
136+ } else if #[ cfg( any( target_os = "macos" , target_os = "ios" , target_os = "redox" ) ) ] {
137+ unsafe fn sanitize_standard_fds( ) {
138+ use crate :: sys:: os:: errno;
139+ for fd in 0 ..3 {
140+ if libc:: fcntl( fd, libc:: F_GETFD ) == -1 && errno( ) == libc:: EBADF {
141+ if libc:: open( "/dev/null\0 " . as_ptr( ) . cast( ) , libc:: O_RDWR , 0 ) == -1 {
142+ libc:: abort( ) ;
143+ }
144+ }
145+ }
141146 }
147+ } else {
148+ unsafe fn sanitize_standard_fds( ) { }
142149 }
143150 }
144- #[ cfg( any(
145- // The standard fds are always available in Miri.
146- miri,
147- target_os = "emscripten" ,
148- target_os = "fuchsia" ) ) ]
149- unsafe fn sanitize_standard_fds ( ) { }
150151
151152 #[ cfg( not( any( target_os = "emscripten" , target_os = "fuchsia" ) ) ) ]
152153 unsafe fn reset_sigpipe ( ) {
0 commit comments