Skip to content
Closed
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/uu/seq/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ path = "src/seq.rs"
[dependencies]
bigdecimal = { workspace = true }
clap = { workspace = true }
nix = { workspace = true }
num-bigint = { workspace = true }
num-traits = { workspace = true }
thiserror = { workspace = true }
Expand Down
49 changes: 45 additions & 4 deletions src/uu/seq/src/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// spell-checker:ignore (ToDO) bigdecimal extendedbigdecimal numberparse hexadecimalfloat biguint
use std::ffi::{OsStr, OsString};
use std::io::{BufWriter, Write, stdout};
#[cfg(unix)]
use std::sync::atomic::{AtomicBool, Ordering};

use clap::{Arg, ArgAction, Command};
use num_bigint::BigUint;
Expand Down Expand Up @@ -225,9 +227,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
padding,
);

match result {
Ok(()) => Ok(()),
Err(err) if err.kind() == std::io::ErrorKind::BrokenPipe => {
let sigpipe_ignored = sigpipe_is_ignored();
if let Err(err) = result {
if err.kind() == std::io::ErrorKind::BrokenPipe {
// GNU seq prints the Broken pipe message but still exits with status 0
// unless SIGPIPE was explicitly ignored, in which case it should fail.
let err = err.map_err_context(|| "write error".into());
Expand All @@ -238,8 +240,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}
Ok(())
}
Err(err) => Err(err.map_err_context(|| "write error".into())),
return Err(err.map_err_context(|| "write error".into()));
}
Ok(())
}

pub fn uu_app() -> Command {
Expand Down Expand Up @@ -353,6 +356,44 @@ fn fast_print_seq(
Ok(())
}

#[cfg(unix)]
static SIGPIPE_WAS_IGNORED: AtomicBool = AtomicBool::new(false);

#[cfg(unix)]
/// # Safety
/// This function runs once at process initialization and only observes the
/// current `SIGPIPE` handler, so there are no extra safety requirements for callers.
unsafe extern "C" fn capture_sigpipe_state() {
use nix::libc;
use std::{mem::MaybeUninit, ptr};

let mut current = MaybeUninit::<libc::sigaction>::uninit();
if unsafe { libc::sigaction(libc::SIGPIPE, ptr::null(), current.as_mut_ptr()) } == 0 {
let ignored = unsafe { current.assume_init() }.sa_sigaction == libc::SIG_IGN;
Copy link
Contributor

Choose a reason for hiding this comment

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

are you sure it is correct ?
it- should compare sa_handler field instead, as sa_sigaction is used for SA_SIGINFO handlers no ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess the way that I'm validating this is by using this approach and then seeing if it can be applied to fix all of the SIGPIPE related tests: #9646

I'll do a bit more research but I guess I don't currently have a test case where this doesn't match the expected behaviour

SIGPIPE_WAS_IGNORED.store(ignored, Ordering::Relaxed);
Copy link
Contributor

Choose a reason for hiding this comment

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

does it need to be in the unsafe block ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

@Ecordonnier Ecordonnier Dec 11, 2025

Choose a reason for hiding this comment

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

Can this be generalized and moved to src/uucore/src/lib/lib.rs ? In principle this is needed for all utilities. If it can be generalized, it could be used for #8919

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Splitting this PR is the better approach.
Implementing it as-is in this PR will make things complicated.

}
}

#[cfg(all(unix, not(target_os = "macos")))]
#[used]
#[unsafe(link_section = ".init_array")]
static CAPTURE_SIGPIPE_STATE: unsafe extern "C" fn() = capture_sigpipe_state;

#[cfg(all(unix, target_os = "macos"))]
#[used]
#[unsafe(link_section = "__DATA,__mod_init_func")]
static CAPTURE_SIGPIPE_STATE_APPLE: unsafe extern "C" fn() = capture_sigpipe_state;

#[cfg(unix)]
fn sigpipe_is_ignored() -> bool {
SIGPIPE_WAS_IGNORED.load(Ordering::Relaxed)
}

#[cfg(not(unix))]
const fn sigpipe_is_ignored() -> bool {
false
}

fn done_printing<T: Zero + PartialOrd>(next: &T, increment: &T, last: &T) -> bool {
if increment >= &T::zero() {
next > last
Expand Down
Loading