From a791a881361c1a5622a553187063e1201ef2f9fe Mon Sep 17 00:00:00 2001 From: oech3 <79379754+oech3@users.noreply.github.com> Date: Sat, 28 Nov 2026 16:32:48 +0900 Subject: [PATCH] uucore: catch closed stdio by rustix::io --- src/uu/cat/src/cat.rs | 6 ----- src/uu/tee/src/tee.rs | 8 ------ src/uucore/src/lib/features/signals.rs | 35 +++++++++++++++----------- 3 files changed, 21 insertions(+), 28 deletions(-) diff --git a/src/uu/cat/src/cat.rs b/src/uu/cat/src/cat.rs index e939ff0dfaf..f882f74a003 100644 --- a/src/uu/cat/src/cat.rs +++ b/src/uu/cat/src/cat.rs @@ -372,12 +372,6 @@ fn cat_handle( fn cat_path(path: &OsString, options: &OutputOptions, state: &mut OutputState) -> CatResult<()> { match get_input_type(path)? { InputType::StdIn => { - // A closed stdin is reopened as /dev/null before `uumain` runs, so - // reading it would succeed silently instead of failing with EBADF. - #[cfg(all(unix, not(target_os = "fuchsia")))] - if uucore::signals::stdin_was_closed() { - return Err(io::Error::from(rustix::io::Errno::BADF).into()); - } let stdin = io::stdin(); let is_interactive = stdin.is_terminal(); if !is_safe_overwrite(&stdin, &io::stdout()) { diff --git a/src/uu/tee/src/tee.rs b/src/uu/tee/src/tee.rs index 8f1f6bf9d63..d468bd724ae 100644 --- a/src/uu/tee/src/tee.rs +++ b/src/uu/tee/src/tee.rs @@ -147,14 +147,6 @@ impl MultiWriter { #[cfg(not(any(unix, target_os = "wasi")))] use io::Read as _; const BUF_SIZE: usize = 32 * 1024; - #[cfg(all(unix, not(target_os = "fuchsia")))] - if uucore::signals::stdin_was_closed() { - show_error!( - "{}", - translate!("tee-error-stdin", "error" => strip_errno(&rustix::io::Errno::BADF.into())) - ); - return Err(()); - } #[cfg(any(unix, target_os = "wasi"))] let input = rustix::stdio::stdin(); #[cfg(not(any(unix, target_os = "wasi")))] diff --git a/src/uucore/src/lib/features/signals.rs b/src/uucore/src/lib/features/signals.rs index 18c162824c4..2d82a875129 100644 --- a/src/uucore/src/lib/features/signals.rs +++ b/src/uucore/src/lib/features/signals.rs @@ -749,7 +749,7 @@ static STARTUP_STATE_WAS_CAPTURED: AtomicBool = AtomicBool::new(false); /// Captures stdio and SIGPIPE state at process initialization, before `main()` runs. /// /// # Safety -/// Called from `.init_array` before `main()`. Only reads current state. +/// Called from `.init_array` before `main()`. Only reads stdio/SIGPIPE state and reopens closed stdio. #[cfg(unix)] #[allow(clippy::missing_safety_doc)] pub unsafe extern "C" fn capture_startup_state() { @@ -763,19 +763,26 @@ pub unsafe extern "C" fn capture_startup_state() { } // Capture stdio state - unsafe { - STDIN_WAS_CLOSED.store( - libc::fcntl(libc::STDIN_FILENO, libc::F_GETFD) == -1, - Ordering::Relaxed, - ); - STDOUT_WAS_CLOSED.store( - libc::fcntl(libc::STDOUT_FILENO, libc::F_GETFD) == -1, - Ordering::Relaxed, - ); - STDERR_WAS_CLOSED.store( - libc::fcntl(libc::STDERR_FILENO, libc::F_GETFD) == -1, - Ordering::Relaxed, - ); + let is_closed = |i: libc::c_int| unsafe { libc::fcntl(i, libc::F_GETFD) == -1 }; + let stdin_was_closed = is_closed(libc::STDIN_FILENO); + STDIN_WAS_CLOSED.store(stdin_was_closed, Ordering::Relaxed); + let stdout_was_closed = is_closed(libc::STDOUT_FILENO); + STDOUT_WAS_CLOSED.store(stdout_was_closed, Ordering::Relaxed); + let stderr_was_closed = is_closed(libc::STDERR_FILENO); + STDERR_WAS_CLOSED.store(stderr_was_closed, Ordering::Relaxed); + // open read-only stdout and stderr, and write-only stdin. Then `rustix::io` reports `EBADF`. + // std will support `EBADF` in the future + let dev_null = c"/dev/null".as_ptr(); + // SAFETY: `dev_null` is a valid NUL-terminated path. + if (stdin_was_closed && unsafe { libc::open(dev_null, libc::O_WRONLY) } != libc::STDIN_FILENO) + || (stdout_was_closed + && unsafe { libc::open(dev_null, libc::O_RDONLY) } != libc::STDOUT_FILENO) + || (stderr_was_closed + && unsafe { libc::open(dev_null, libc::O_RDONLY) } != libc::STDERR_FILENO) + { + // `std` crashes when `/dev` is not mounted and stdio was closed. + // SAFETY: no preconditions. + unsafe { libc::abort() } } // Capture SIGPIPE state