@@ -61,27 +61,30 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
61
61
}
62
62
63
63
unsafe fn sanitize_standard_fds ( ) {
64
- let mut opened_devnull = -1 ;
65
- let mut open_devnull = || {
66
- #[ cfg( not( all( target_os = "linux" , target_env = "gnu" ) ) ) ]
67
- use libc:: open as open64;
68
- #[ cfg( all( target_os = "linux" , target_env = "gnu" ) ) ]
69
- use libc:: open64;
70
-
71
- if opened_devnull != -1 {
72
- if libc:: dup ( opened_devnull) != -1 {
73
- return ;
64
+ macro_rules! open_devnull {
65
+ ( $opened_devnull: expr) => {
66
+ ' od: {
67
+ #[ cfg( not( all( target_os = "linux" , target_env = "gnu" ) ) ) ]
68
+ use libc:: open as open64;
69
+ #[ cfg( all( target_os = "linux" , target_env = "gnu" ) ) ]
70
+ use libc:: open64;
71
+
72
+ if $opened_devnull != -1 {
73
+ if libc:: dup( $opened_devnull) != -1 {
74
+ break ' od;
75
+ }
76
+ }
77
+ $opened_devnull = open64( c"/dev/null" . as_ptr( ) , libc:: O_RDWR , 0 ) ;
78
+ if $opened_devnull == -1 {
79
+ // If the stream is closed but we failed to reopen it, abort the
80
+ // process. Otherwise we wouldn't preserve the safety of
81
+ // operations on the corresponding Rust object Stdin, Stdout, or
82
+ // Stderr.
83
+ libc:: abort( ) ;
84
+ }
74
85
}
75
- }
76
- opened_devnull = open64 ( c"/dev/null" . as_ptr ( ) , libc:: O_RDWR , 0 ) ;
77
- if opened_devnull == -1 {
78
- // If the stream is closed but we failed to reopen it, abort the
79
- // process. Otherwise we wouldn't preserve the safety of
80
- // operations on the corresponding Rust object Stdin, Stdout, or
81
- // Stderr.
82
- libc:: abort ( ) ;
83
- }
84
- } ;
86
+ } ;
87
+ }
85
88
86
89
// fast path with a single syscall for systems with poll()
87
90
#[ cfg( not( any(
@@ -99,6 +102,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
99
102
) ) ) ]
100
103
' poll: {
101
104
use crate :: sys:: os:: errno;
105
+ let mut opened_devnull = -1 ;
102
106
let pfds: & mut [ _ ] = & mut [
103
107
libc:: pollfd { fd : 0 , events : 0 , revents : 0 } ,
104
108
libc:: pollfd { fd : 1 , events : 0 , revents : 0 } ,
@@ -125,7 +129,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
125
129
if pfd. revents & libc:: POLLNVAL == 0 {
126
130
continue ;
127
131
}
128
- open_devnull ( ) ;
132
+ open_devnull ! ( opened_devnull ) ;
129
133
}
130
134
return ;
131
135
}
@@ -143,9 +147,10 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
143
147
) ) ) ]
144
148
{
145
149
use crate :: sys:: os:: errno;
150
+ let mut opened_devnull = -1 ;
146
151
for fd in 0 ..3 {
147
152
if libc:: fcntl ( fd, libc:: F_GETFD ) == -1 && errno ( ) == libc:: EBADF {
148
- open_devnull ( ) ;
153
+ open_devnull ! ( opened_devnull ) ;
149
154
}
150
155
}
151
156
}
0 commit comments