Skip to content

Commit 39b2790

Browse files
committed
[temp] improve check_fd diagnostic
1 parent 1cd2091 commit 39b2790

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/unix.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,11 @@ impl Client {
125125
// If we're called from `make` *without* the leading + on our rule
126126
// then we'll have `MAKEFLAGS` env vars but won't actually have
127127
// 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))
138133
}
139134

140135
unsafe fn from_fds(read: c_int, write: c_int) -> Client {
@@ -383,25 +378,34 @@ impl Helper {
383378
}
384379
}
385380

386-
fn check_fd(fd: c_int) -> bool {
381+
fn check_fd(fd: c_int) -> io::Result<()> {
387382
#[cfg(feature = "check_pipe")]
388383
unsafe {
389384
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 {
391388
// On android arm and i686 mode_t is u16 and st_mode is u32,
392389
// this generates a type mismatch when S_IFIFO (declared as mode_t)
393390
// is used in operations with st_mode, so we use this workaround
394391
// to get the value of S_IFIFO with the same type of st_mode.
395392
let mut s_ififo = stat.st_mode;
396393
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()) //
400398
}
401399
}
402400
#[cfg(not(feature = "check_pipe"))]
403401
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+
}
405409
}
406410
}
407411

0 commit comments

Comments
 (0)