Skip to content
Merged
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
18 changes: 18 additions & 0 deletions src/uu/tail/src/tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ use uucore::{show, show_error};

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
// When we receive a SIGPIPE signal, we want to terminate the process so
// that we don't print any error messages to stderr. Rust ignores SIGPIPE
// (see https://github.com/rust-lang/rust/issues/62569), so we restore it's
// default action here.
#[cfg(not(target_os = "windows"))]
unsafe {
libc::signal(libc::SIGPIPE, libc::SIG_DFL);
}

let settings = parse_args(args)?;

settings.check_warnings();
Expand Down Expand Up @@ -541,7 +550,16 @@ fn unbounded_tail<T: Read>(reader: &mut BufReader<T>, settings: &Settings) -> UR
}
_ => {}
}
#[cfg(not(target_os = "windows"))]
writer.flush()?;

// SIGPIPE is not available on Windows.
#[cfg(target_os = "windows")]
writer.flush().inspect_err(|err| {
if err.kind() == ErrorKind::BrokenPipe {
std::process::exit(13);
}
})?;
Ok(())
}

Expand Down
13 changes: 13 additions & 0 deletions tests/by-util/test_tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4897,6 +4897,19 @@ fn test_when_piped_input_then_no_broken_pipe() {
}
}

#[test]
fn test_when_output_closed_then_no_broken_pie() {
let mut cmd = new_ucmd!();
let mut child = cmd
.args(&[FOOBAR_TXT])
.set_stdout(Stdio::piped())
.run_no_wait();
// Dropping the stdout should not lead to an error.
// The "Broken pipe" error should be silently ignored.
child.close_stdout();
child.wait().unwrap().fails_silently();
}

#[test]
fn test_child_when_run_with_stderr_to_stdout() {
let ts = TestScenario::new("tail");
Expand Down
6 changes: 5 additions & 1 deletion tests/uutests/src/lib/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,11 @@ impl CmdResult {
#[track_caller]
pub fn fails_silently(&self) -> &Self {
assert!(!self.succeeded());
assert!(self.stderr.is_empty());
assert!(
self.stderr.is_empty(),
"Expected stderr to be empty, but it's:\n{}",
self.stderr_str()
);
Comment on lines +698 to +702
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice little change :)

self
}

Expand Down
Loading