Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/uu/tee/src/tee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,16 @@ enum Writer {

impl Writer {
pub fn write_all(&mut self, buf: &[u8]) -> Result<()> {
#[cfg(any(unix, target_os = "wasi"))]
use uucore::io::AsFdExt as _;
match self {
// File does not have line buffering
#[cfg(any(unix, target_os = "wasi"))]
Self::File(f) => f.write_all_no_retry(buf),
#[cfg(not(any(unix, target_os = "wasi")))]
Self::File(f) => f.write_all(buf),
#[cfg(any(unix, target_os = "wasi"))]
Self::Stdout(s) => s.write_all(buf),
Self::Stdout(s) => s.0.write_all_no_retry(buf),
#[cfg(not(any(unix, target_os = "wasi")))]
Self::Stdout(s) => {
s.write_all(buf)?;
Expand Down
25 changes: 25 additions & 0 deletions src/uucore/src/lib/mods/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,31 @@ impl<T: AsFd> io::Write for RawWriter<T> {
}
}

// write_all retries with EINTR which is sometimes not compatible with GNU
#[cfg(any(unix, target_os = "wasi"))]
pub trait AsFdExt {
fn write_all_no_retry(&self, buf: &[u8]) -> io::Result<()>;
}
#[cfg(any(unix, target_os = "wasi"))]
impl<T: AsFd> AsFdExt for T {
fn write_all_no_retry(&self, mut buf: &[u8]) -> io::Result<()> {
let fd = self.as_fd();
while !buf.is_empty() {
match rustix::io::write(fd, buf) {
Ok(0) => {
return Err(io::Error::new(
io::ErrorKind::WriteZero,
"failed to write whole buffer",
));
}
Ok(n) => buf = &buf[n..],
Err(e) => return Err(e.into()),
}
}
Ok(())
}
}

/// abstraction wrapper for native file handle / file descriptor
// todo: remove clone introducing additional syscall dependency
pub struct OwnedFileDescriptorOrHandle {
Expand Down
46 changes: 46 additions & 0 deletions tests/by-util/test_tee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,52 @@ mod linux_only {
assert_eq!(at.read(file_out_b), content);
assert!(result.stderr_str().contains("No space left on device"));
}

#[test]
fn test_eintr_on_write_is_not_retried() {
use std::io::Write;
if std::process::Command::new("strace")
.arg("--version")
.output()
.is_err()
{
return;
}

let mut child = std::process::Command::new("strace")
.args([
"-o",
"/dev/null", // suppress strace's own output
"-e",
"inject=write:error=EINTR:when=1",
env!("CARGO_BIN_EXE_coreutils"),
"tee",
])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("failed to spawn strace");

// Write something so tee actually calls write()
if let Some(mut stdin) = child.stdin.take() {
let _ = stdin.write_all(b"hello\n");
// drop stdin to send EOF
}

let result = child.wait_with_output().expect("failed to wait");

let stderr = String::from_utf8_lossy(&result.stderr);

assert!(
!result.status.success(),
"tee should have failed on EINTR but exited zero.\nstderr: {stderr}"
);
assert!(
stderr.contains("Interrupted system call"),
"expected 'Interrupted system call' in stderr, got: {stderr}"
);
}
}

// Additional cross-platform tee tests to cover GNU compatibility around --output-error
Expand Down
Loading