diff --git a/Cargo.lock b/Cargo.lock index 70de649eef1..b1411ad53b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3959,6 +3959,7 @@ version = "0.5.0" dependencies = [ "clap", "fluent", + "libc", "memchr", "memmap2", "regex", diff --git a/src/uu/dd/Cargo.toml b/src/uu/dd/Cargo.toml index 6dbc6c2ffa0..f7941f5d3d2 100644 --- a/src/uu/dd/Cargo.toml +++ b/src/uu/dd/Cargo.toml @@ -26,6 +26,7 @@ uucore = { workspace = true, features = [ "parser-size", "quoting-style", "fs", + "signals", ] } thiserror = { workspace = true } fluent = { workspace = true } diff --git a/src/uu/dd/src/dd.rs b/src/uu/dd/src/dd.rs index 567f803d390..9a1646c8944 100644 --- a/src/uu/dd/src/dd.rs +++ b/src/uu/dd/src/dd.rs @@ -5,6 +5,9 @@ // spell-checker:ignore fname, ftype, tname, fpath, specfile, testfile, unspec, ifile, ofile, outfile, fullblock, urand, fileio, atoe, atoibm, behaviour, bmax, bremain, cflags, creat, ctable, ctty, datastructures, doesnt, etoa, fileout, fname, gnudd, iconvflags, iseek, nocache, noctty, noerror, nofollow, nolinks, nonblock, oconvflags, oseek, outfile, parseargs, rlen, rmax, rremain, rsofar, rstat, sigusr, wlen, wstat seekable oconv canonicalized fadvise Fadvise FADV DONTNEED ESPIPE bufferedoutput, SETFL +#[cfg(unix)] +uucore::init_startup_state_capture!(); + mod blocks; mod bufferedoutput; mod conversion_tables; @@ -1471,6 +1474,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { .unwrap_or_default(), )?; + #[cfg(unix)] + if uucore::signals::stderr_was_closed() && settings.status != Some(StatusLevel::None) { + return Err(USimpleError::new(1, "write error")); + } + let i = match settings.infile { #[cfg(unix)] Some(ref infile) if is_fifo(infile) => Input::new_fifo(Path::new(&infile), &settings)?, diff --git a/src/uu/dd/src/progress.rs b/src/uu/dd/src/progress.rs index 2ad61cf1b30..416c95f27d5 100644 --- a/src/uu/dd/src/progress.rs +++ b/src/uu/dd/src/progress.rs @@ -18,7 +18,7 @@ use std::time::Duration; #[cfg(target_os = "linux")] use signal_hook::iterator::Handle; use uucore::{ - error::UResult, + error::{UResult, set_exit_code}, format::num_format::{FloatVariant, Formatter}, locale::setup_localization, translate, @@ -231,7 +231,9 @@ impl ProgUpdate { /// See [`ProgUpdate::write_io_lines`] for more information. pub(crate) fn print_io_lines(&self) { let mut stderr = std::io::stderr(); - self.write_io_lines(&mut stderr).unwrap(); + if self.write_io_lines(&mut stderr).is_err() { + set_exit_code(1); + } } /// Re-print the number of bytes written, duration, and throughput. @@ -240,7 +242,9 @@ impl ProgUpdate { pub(crate) fn reprint_prog_line(&self) { let mut stderr = std::io::stderr(); let rewrite = true; - self.write_prog_line(&mut stderr, rewrite).unwrap(); + if self.write_prog_line(&mut stderr, rewrite).is_err() { + set_exit_code(1); + } } /// Write all summary statistics. @@ -248,7 +252,9 @@ impl ProgUpdate { /// See [`ProgUpdate::write_transfer_stats`] for more information. pub(crate) fn print_transfer_stats(&self, new_line: bool) { let mut stderr = std::io::stderr(); - self.write_transfer_stats(&mut stderr, new_line).unwrap(); + if self.write_transfer_stats(&mut stderr, new_line).is_err() { + set_exit_code(1); + } } /// Write all the final statistics. diff --git a/src/uu/seq/src/seq.rs b/src/uu/seq/src/seq.rs index 1e94a9901cd..b931cc8b11d 100644 --- a/src/uu/seq/src/seq.rs +++ b/src/uu/seq/src/seq.rs @@ -94,7 +94,7 @@ fn select_precision( // Initialize SIGPIPE state capture at process startup (Unix only) #[cfg(unix)] -uucore::init_sigpipe_capture!(); +uucore::init_startup_state_capture!(); #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { diff --git a/src/uu/tac/Cargo.toml b/src/uu/tac/Cargo.toml index bd1652935f5..82ec5c5bc78 100644 --- a/src/uu/tac/Cargo.toml +++ b/src/uu/tac/Cargo.toml @@ -24,7 +24,8 @@ memchr = { workspace = true } memmap2 = { workspace = true } regex = { workspace = true } clap = { workspace = true } -uucore = { workspace = true } +libc = { workspace = true } +uucore = { workspace = true, features = ["signals"] } thiserror = { workspace = true } fluent = { workspace = true } tempfile = { workspace = true } diff --git a/src/uu/tac/src/tac.rs b/src/uu/tac/src/tac.rs index 15e34baf82e..e1686f459b4 100644 --- a/src/uu/tac/src/tac.rs +++ b/src/uu/tac/src/tac.rs @@ -4,6 +4,9 @@ // file that was distributed with this source code. // spell-checker:ignore (ToDO) sbytes slen dlen memmem memmap Mmap mmap SIGBUS +#[cfg(unix)] +uucore::init_startup_state_capture!(); + mod error; use clap::{Arg, ArgAction, Command}; @@ -16,8 +19,9 @@ use std::{ io::copy, path::Path, }; -use uucore::error::UError; -use uucore::error::UResult; +#[cfg(unix)] +use uucore::error::set_exit_code; +use uucore::error::{UError, UResult}; use uucore::{format_usage, show}; use crate::error::TacError; @@ -238,6 +242,17 @@ fn tac(filenames: &[OsString], before: bool, regex: bool, separator: &str) -> UR let buf; let data: &[u8] = if filename == "-" { + #[cfg(unix)] + if uucore::signals::stdin_was_closed() { + let e: Box = TacError::ReadError( + OsString::from("-"), + std::io::Error::from_raw_os_error(libc::EBADF), + ) + .into(); + show!(e); + set_exit_code(1); + continue; + } if let Some(mmap1) = try_mmap_stdin() { mmap = mmap1; &mmap diff --git a/src/uu/tail/src/paths.rs b/src/uu/tail/src/paths.rs index 3f37091d8cd..6eaeae9801b 100644 --- a/src/uu/tail/src/paths.rs +++ b/src/uu/tail/src/paths.rs @@ -229,14 +229,13 @@ pub fn path_is_tailable(path: &Path) -> bool { } #[inline] +#[cfg(unix)] +pub fn stdin_is_bad_fd() -> bool { + uucore::signals::stdin_was_closed() +} + +#[inline] +#[cfg(not(unix))] pub fn stdin_is_bad_fd() -> bool { - // FIXME : Rust's stdlib is reopening fds as /dev/null - // see also: https://github.com/uutils/coreutils/issues/2873 - // (gnu/tests/tail-2/follow-stdin.sh fails because of this) - //#[cfg(unix)] - { - //platform::stdin_is_bad_fd() - } - //#[cfg(not(unix))] false } diff --git a/src/uu/tail/src/tail.rs b/src/uu/tail/src/tail.rs index 3a16f670b63..c37f70dc6cf 100644 --- a/src/uu/tail/src/tail.rs +++ b/src/uu/tail/src/tail.rs @@ -33,11 +33,14 @@ use std::fs::File; use std::io::{self, BufReader, BufWriter, ErrorKind, Read, Seek, SeekFrom, Write, stdin, stdout}; use std::path::{Path, PathBuf}; use uucore::display::Quotable; -use uucore::error::{FromIo, UResult, USimpleError, get_exit_code, set_exit_code}; +use uucore::error::{FromIo, UResult, USimpleError, set_exit_code}; use uucore::translate; use uucore::{show, show_error}; +#[cfg(unix)] +uucore::init_startup_state_capture!(); + #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { // When we receive a SIGPIPE signal, we want to terminate the process so @@ -103,10 +106,6 @@ fn uu_tail(settings: &Settings) -> UResult<()> { } } - if get_exit_code() > 0 && paths::stdin_is_bad_fd() { - show_error!("{}: {}", text::DASH, translate!("tail-bad-fd")); - } - Ok(()) } @@ -267,6 +266,17 @@ fn tail_stdin( } } + // Check if stdin was closed before Rust reopened it as /dev/null + if paths::stdin_is_bad_fd() { + set_exit_code(1); + show_error!( + "{}", + translate!("tail-error-cannot-fstat", "file" => translate!("tail-stdin-header").quote(), "error" => translate!("tail-bad-fd")) + ); + show_error!("{}", translate!("tail-no-files-remaining")); + return Ok(()); + } + match input.resolve() { // fifo Some(path) => { diff --git a/src/uu/timeout/src/timeout.rs b/src/uu/timeout/src/timeout.rs index de20bec83c3..f8a04b7e149 100644 --- a/src/uu/timeout/src/timeout.rs +++ b/src/uu/timeout/src/timeout.rs @@ -4,12 +4,15 @@ // file that was distributed with this source code. // spell-checker:ignore (ToDO) tstr sigstr cmdname setpgid sigchld getpid +#[cfg(unix)] +uucore::init_startup_state_capture!(); + mod status; use crate::status::ExitStatus; use clap::{Arg, ArgAction, Command}; use std::io::ErrorKind; -use std::os::unix::process::ExitStatusExt; +use std::os::unix::process::{CommandExt, ExitStatusExt}; use std::process::{self, Child, Stdio}; use std::sync::atomic::{self, AtomicBool}; use std::time::Duration; @@ -323,23 +326,34 @@ fn timeout( #[cfg(unix)] enable_pipe_errors()?; - let process = &mut process::Command::new(&cmd[0]) + let mut command = process::Command::new(&cmd[0]); + command .args(&cmd[1..]) .stdin(Stdio::inherit()) .stdout(Stdio::inherit()) - .stderr(Stdio::inherit()) - .spawn() - .map_err(|err| { - let status_code = match err.kind() { - ErrorKind::NotFound => ExitStatus::CommandNotFound.into(), - ErrorKind::PermissionDenied => ExitStatus::CannotInvoke.into(), - _ => ExitStatus::CannotInvoke.into(), - }; - USimpleError::new( - status_code, - translate!("timeout-error-failed-to-execute-process", "error" => err), - ) - })?; + .stderr(Stdio::inherit()); + + // If stdin was closed before Rust reopened it as /dev/null, close it in child + if uucore::signals::stdin_was_closed() { + unsafe { + command.pre_exec(|| { + libc::close(libc::STDIN_FILENO); + Ok(()) + }); + } + } + + let process = &mut command.spawn().map_err(|err| { + let status_code = match err.kind() { + ErrorKind::NotFound => ExitStatus::CommandNotFound.into(), + ErrorKind::PermissionDenied => ExitStatus::CannotInvoke.into(), + _ => ExitStatus::CannotInvoke.into(), + }; + USimpleError::new( + status_code, + translate!("timeout-error-failed-to-execute-process", "error" => err), + ) + })?; unblock_sigchld(); catch_sigterm(); // Wait for the child process for the specified time period. diff --git a/src/uucore/src/lib/features/signals.rs b/src/uucore/src/lib/features/signals.rs index 1c4d684a747..9577a91e80a 100644 --- a/src/uucore/src/lib/features/signals.rs +++ b/src/uucore/src/lib/features/signals.rs @@ -3,7 +3,7 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -// spell-checker:ignore (vars/api) fcntl setrlimit setitimer rubout pollable sysconf pgrp pfds revents POLLRDBAND POLLERR +// spell-checker:ignore (vars/api) fcntl setrlimit setitimer rubout pollable sysconf pgrp GETFD pfds revents POLLRDBAND POLLERR // spell-checker:ignore (vars/signals) ABRT ALRM CHLD SEGV SIGABRT SIGALRM SIGBUS SIGCHLD SIGCONT SIGDANGER SIGEMT SIGFPE SIGHUP SIGILL SIGINFO SIGINT SIGIO SIGIOT SIGKILL SIGMIGRATE SIGMSG SIGPIPE SIGPRE SIGPROF SIGPWR SIGQUIT SIGSEGV SIGSTOP SIGSYS SIGTALRM SIGTERM SIGTRAP SIGTSTP SIGTHR SIGTTIN SIGTTOU SIGURG SIGUSR SIGVIRT SIGVTALRM SIGWINCH SIGXCPU SIGXFSZ STKFLT PWR THR TSTP TTIN TTOU VIRT VTALRM XCPU XFSZ SIGCLD SIGPOLL SIGWAITING SIGAIOCANCEL SIGLWP SIGFREEZE SIGTHAW SIGCANCEL SIGLOST SIGXRES SIGJVM SIGRTMIN SIGRT SIGRTMAX TALRM AIOCANCEL XRES RTMIN RTMAX LTOSTOP //! This module provides a way to handle signals in a platform-independent way. @@ -426,23 +426,49 @@ pub fn ignore_interrupts() -> Result<(), Errno> { unsafe { signal(SIGINT, SigIgn) }.map(|_| ()) } -// SIGPIPE state capture - captures whether SIGPIPE was ignored at process startup +// Detect closed stdin/stdout before Rust reopens them as /dev/null (see issue #2873) #[cfg(unix)] use std::sync::atomic::{AtomicBool, Ordering}; +#[cfg(unix)] +static STDIN_WAS_CLOSED: AtomicBool = AtomicBool::new(false); +#[cfg(unix)] +static STDOUT_WAS_CLOSED: AtomicBool = AtomicBool::new(false); +#[cfg(unix)] +static STDERR_WAS_CLOSED: AtomicBool = AtomicBool::new(false); + +// SIGPIPE state capture - captures whether SIGPIPE was ignored at process startup #[cfg(unix)] static SIGPIPE_WAS_IGNORED: AtomicBool = AtomicBool::new(false); -/// Captures SIGPIPE state at process initialization, before main() runs. +/// Captures stdio and SIGPIPE state at process initialization, before main() runs. /// /// # Safety -/// Called from `.init_array` before main(). Only reads current SIGPIPE handler state. +/// Called from `.init_array` before main(). Only reads current state. #[cfg(unix)] -pub unsafe extern "C" fn capture_sigpipe_state() { +#[allow(clippy::missing_safety_doc)] +pub unsafe extern "C" fn capture_startup_state() { use nix::libc; use std::mem::MaybeUninit; use std::ptr; + // 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, + ); + } + + // Capture SIGPIPE state let mut current = MaybeUninit::::uninit(); // SAFETY: sigaction with null new-action just queries current state if unsafe { libc::sigaction(libc::SIGPIPE, ptr::null(), current.as_mut_ptr()) } == 0 { @@ -452,31 +478,61 @@ pub unsafe extern "C" fn capture_sigpipe_state() { } } -/// Initializes SIGPIPE state capture. Call once at crate root level. +/// Initializes startup state capture. Call once at crate root level. #[macro_export] #[cfg(unix)] -macro_rules! init_sigpipe_capture { +macro_rules! init_startup_state_capture { () => { #[cfg(all(unix, not(target_os = "macos")))] #[used] #[unsafe(link_section = ".init_array")] - static CAPTURE_SIGPIPE_STATE: unsafe extern "C" fn() = - $crate::signals::capture_sigpipe_state; + static CAPTURE_STARTUP_STATE: unsafe extern "C" fn() = + $crate::signals::capture_startup_state; #[cfg(all(unix, target_os = "macos"))] #[used] #[unsafe(link_section = "__DATA,__mod_init_func")] - static CAPTURE_SIGPIPE_STATE: unsafe extern "C" fn() = - $crate::signals::capture_sigpipe_state; + static CAPTURE_STARTUP_STATE: unsafe extern "C" fn() = + $crate::signals::capture_startup_state; }; } #[macro_export] #[cfg(not(unix))] -macro_rules! init_sigpipe_capture { +macro_rules! init_startup_state_capture { () => {}; } +#[cfg(unix)] +pub fn stdin_was_closed() -> bool { + STDIN_WAS_CLOSED.load(Ordering::Relaxed) +} + +#[cfg(not(unix))] +pub const fn stdin_was_closed() -> bool { + false +} + +#[cfg(unix)] +pub fn stdout_was_closed() -> bool { + STDOUT_WAS_CLOSED.load(Ordering::Relaxed) +} + +#[cfg(not(unix))] +pub const fn stdout_was_closed() -> bool { + false +} + +#[cfg(unix)] +pub fn stderr_was_closed() -> bool { + STDERR_WAS_CLOSED.load(Ordering::Relaxed) +} + +#[cfg(not(unix))] +pub const fn stderr_was_closed() -> bool { + false +} + /// Returns whether SIGPIPE was ignored at process startup. #[cfg(unix)] pub fn sigpipe_was_ignored() -> bool {