Skip to content

tee: report bad file descriptor when stdin is closed - #15737

Open
fixfon wants to merge 1 commit into
uutils:mainfrom
fixfon:fix/10154-tee-closed-stdin
Open

fixfon wants to merge 1 commit into
uutils:mainfrom
fixfon:fix/10154-tee-closed-stdin

Conversation

@fixfon

@fixfon fixfon commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

Refs #10154

tee <&- exited 0 without a word. Rust's runtime reopens a closed stdin as /dev/null before uumain runs, so tee read EOF and finished normally. copy_unbuffered now checks uucore::signals::stdin_was_closed() before the first read and reports the existing read error: Bad file descriptor message, exit code 1. The output files are opened and truncated before that point, like they are for any other read error. The check is the same one #15253 added to cat, and it is cfg(unix) only.

I added test_closed_stdin to tests/by-util/test_tee.rs. It starts tee file through sh -c 'exec <&-; ...', expects exit code 1, the error on stderr, no stdout and an empty file. It fails without the fix (tee exits 0) and passes with it. cargo fmt --all -- --check and clippy -p uu_tee --all-targets -D warnings are clean. The rest of test_tee passes except test_tee_output_not_buffered, which fails the same way on main on my machine; I did not dig into why. Tested on macOS only. The issue lists other tools too, this PR only touches tee.

Comment thread src/uu/tee/src/tee.rs
// A closed stdin is reopened as /dev/null before `uumain` runs, so
// reading it would succeed silently instead of failing with EBADF.
#[cfg(unix)]
if uucore::signals::stdin_was_closed() {

This comment was marked as resolved.

Comment thread src/uu/tee/src/tee.rs Outdated

// A closed stdin is reopened as /dev/null before `uumain` runs, so
// reading it would succeed silently instead of failing with EBADF.
#[cfg(unix)]

This comment was marked as resolved.

Comment thread src/uu/tee/src/tee.rs Outdated
Comment on lines +155 to +156
// A closed stdin is reopened as /dev/null before `uumain` runs, so
// reading it would succeed silently instead of failing with EBADF.

This comment was marked as resolved.

Comment thread src/uu/tee/src/tee.rs Outdated
let input = rustix::stdio::stdin();
#[cfg(not(any(unix, target_os = "wasi")))]
let mut input = io::stdin();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

And squash commits

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, thanks.

@fixfon
fixfon force-pushed the fix/10154-tee-closed-stdin branch from e8b4b41 to 62aa0f5 Compare October 25, 2026 13:46

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants