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
6 changes: 0 additions & 6 deletions src/uu/cat/src/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,6 @@ fn cat_handle<R: FdReadable>(
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()) {
Expand Down
8 changes: 0 additions & 8 deletions src/uu/tee/src/tee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")))]
Expand Down
35 changes: 21 additions & 14 deletions src/uucore/src/lib/features/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 <https://github.com/rust-lang/rust/pull/166117>
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() }
Comment thread
oech3 marked this conversation as resolved.
Resolved
}

// Capture SIGPIPE state
Expand Down
Loading