@@ -6,29 +6,32 @@ use crate::{Error, ErrorKind};
6
6
#[ cfg( all( not( unix) , not( windows) ) ) ]
7
7
compile_error ! ( "Only unix and windows support non-blocking pipes! For other OSes, disable the parallel feature." ) ;
8
8
9
- #[ allow ( unused_variables ) ]
10
- pub fn set_non_blocking ( stderr : & mut ChildStderr ) -> Result < ( ) , Error > {
9
+ #[ cfg ( unix ) ]
10
+ pub fn set_non_blocking ( pipe : & impl std :: os :: unix :: io :: AsRawFd ) -> Result < ( ) , Error > {
11
11
// On Unix, switch the pipe to non-blocking mode.
12
12
// On Windows, we have a different way to be non-blocking.
13
- #[ cfg( unix) ]
14
- {
15
- use std:: os:: unix:: io:: AsRawFd ;
16
- let fd = stderr. as_raw_fd ( ) ;
17
- debug_assert_eq ! (
18
- unsafe { libc:: fcntl( fd, libc:: F_GETFL , 0 ) } ,
19
- 0 ,
20
- "stderr should have no flags set"
21
- ) ;
13
+ let fd = pipe. as_raw_fd ( ) ;
14
+ let flags = unsafe { libc:: fcntl ( fd, libc:: F_GETFL , 0 ) } ;
15
+ if flags == -1 {
16
+ return Err ( Error :: new (
17
+ ErrorKind :: IOError ,
18
+ format ! (
19
+ "Failed to get flags for pipe {}: {}" ,
20
+ fd,
21
+ std:: io:: Error :: last_os_error( )
22
+ ) ,
23
+ ) ) ;
24
+ }
22
25
23
- if unsafe { libc:: fcntl ( fd, libc:: F_SETFL , libc:: O_NONBLOCK ) } != 0 {
24
- return Err ( Error :: new (
25
- ErrorKind :: IOError ,
26
- format ! (
27
- "Failed to set flags for child stderr : {}" ,
28
- std :: io :: Error :: last_os_error ( )
29
- ) ,
30
- ) ) ;
31
- }
26
+ if unsafe { libc:: fcntl ( fd, libc:: F_SETFL , flags | libc:: O_NONBLOCK ) } == - 1 {
27
+ return Err ( Error :: new (
28
+ ErrorKind :: IOError ,
29
+ format ! (
30
+ "Failed to set flags for pipe {} : {}" ,
31
+ fd ,
32
+ std :: io :: Error :: last_os_error ( )
33
+ ) ,
34
+ ) ) ;
32
35
}
33
36
34
37
Ok ( ( ) )
0 commit comments