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