@@ -125,16 +125,11 @@ impl Client {
125
125
// If we're called from `make` *without* the leading + on our rule
126
126
// then we'll have `MAKEFLAGS` env vars but won't actually have
127
127
// access to the file descriptors.
128
- if check_fd ( read) && check_fd ( write) {
129
- drop ( set_cloexec ( read, true ) ) ;
130
- drop ( set_cloexec ( write, true ) ) ;
131
- Ok ( Client :: from_fds ( read, write) )
132
- } else {
133
- Err ( io:: Error :: new (
134
- io:: ErrorKind :: InvalidData ,
135
- "invalid file descriptors" ,
136
- ) )
137
- }
128
+ check_fd ( read) ?;
129
+ check_fd ( write) ?;
130
+ drop ( set_cloexec ( read, true ) ) ;
131
+ drop ( set_cloexec ( write, true ) ) ;
132
+ Ok ( Client :: from_fds ( read, write) )
138
133
}
139
134
140
135
unsafe fn from_fds ( read : c_int , write : c_int ) -> Client {
@@ -383,25 +378,34 @@ impl Helper {
383
378
}
384
379
}
385
380
386
- fn check_fd ( fd : c_int ) -> bool {
381
+ fn check_fd ( fd : c_int ) -> io :: Result < ( ) > {
387
382
#[ cfg( feature = "check_pipe" ) ]
388
383
unsafe {
389
384
let mut stat = mem:: zeroed ( ) ;
390
- if libc:: fstat ( fd, & mut stat) == 0 {
385
+ if libc:: fstat ( fd, & mut stat) == -1 {
386
+ Err ( io:: Error :: last_os_error ( ) )
387
+ } else {
391
388
// On android arm and i686 mode_t is u16 and st_mode is u32,
392
389
// this generates a type mismatch when S_IFIFO (declared as mode_t)
393
390
// is used in operations with st_mode, so we use this workaround
394
391
// to get the value of S_IFIFO with the same type of st_mode.
395
392
let mut s_ififo = stat. st_mode ;
396
393
s_ififo = libc:: S_IFIFO as _ ;
397
- stat. st_mode & s_ififo == s_ififo
398
- } else {
399
- false
394
+ if stat. st_mode & s_ififo == s_ififo {
395
+ return Ok ( ( ) ) ;
396
+ }
397
+ Err ( io:: Error :: last_os_error ( ) ) //
400
398
}
401
399
}
402
400
#[ cfg( not( feature = "check_pipe" ) ) ]
403
401
unsafe {
404
- libc:: fcntl ( fd, libc:: F_GETFD ) != -1
402
+ match libc:: fcntl ( fd, libc:: F_GETFD ) {
403
+ r if r == -1 => Err ( io:: Error :: new (
404
+ io:: ErrorKind :: InvalidData ,
405
+ format ! ( "{fd} is not a pipe" ) ,
406
+ ) ) ,
407
+ _ => Ok ( ( ) ) ,
408
+ }
405
409
}
406
410
}
407
411
0 commit comments