-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(seq):GNU seq-epipe.sh #9184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8587de9
0331118
9890401
53c1167
b449815
1915ad3
68ef63a
bfe30c8
5f3df28
d7b534e
684ba5c
5e272de
1670703
02875d0
ff59aaa
eee1241
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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()); | ||
|
|
@@ -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 { | ||
|
|
@@ -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; | ||
| SIGPIPE_WAS_IGNORED.store(ignored, Ordering::Relaxed); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does it need to be in the unsafe block ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://docs.rs/nix/latest/src/nix/sys/signal.rs.html#880-888
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Splitting this PR is the better approach. |
||
| } | ||
| } | ||
|
|
||
| #[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 | ||
|
|
||
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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