Skip to content

Commit 7b52018

Browse files
committed
Begin using std::io::on_broken_pipe()
1 parent b57afaf commit 7b52018

19 files changed

Lines changed: 109 additions & 38 deletions

File tree

library/std/src/rt.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ fn handle_rt_panic<T>(e: Box<dyn Any + Send>) -> T {
9191
//
9292
// # The `sigpipe` parameter
9393
//
94+
// FIXME: Update
95+
//
9496
// Since 2014, the Rust runtime on Unix has set the `SIGPIPE` handler to
9597
// `SIG_IGN`. Applications have good reasons to want a different behavior
9698
// though, so there is a `-Zon-broken-pipe` compiler flag that

library/std/src/sys/pal/unix/mod.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
3636
// Hence, we set SIGPIPE to ignore when the program starts up in order
3737
// to prevent this problem. Use `-Zon-broken-pipe=...` to alter this
3838
// behavior.
39+
//
40+
// FIXME: Explain how to use `eii`
3941
reset_sigpipe(sigpipe);
4042

4143
stack_overflow::init();
@@ -156,25 +158,12 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
156158
target_vendor = "unikraft",
157159
)))]
158160
{
159-
// We don't want to add this as a public type to std, nor do we
160-
// want to `include!` a file from the compiler (which would break
161-
// Miri and xargo for example), so we choose to duplicate these
162-
// constants from `compiler/rustc_session/src/config/sigpipe.rs`.
163-
// See the other file for docs. NOTE: Make sure to keep them in
164-
// sync!
165-
mod sigpipe {
166-
pub const DEFAULT: u8 = 0;
167-
pub const INHERIT: u8 = 1;
168-
pub const SIG_IGN: u8 = 2;
169-
pub const SIG_DFL: u8 = 3;
170-
}
171-
172-
let (on_broken_pipe_used, handler) = match sigpipe {
173-
sigpipe::DEFAULT => (false, Some(libc::SIG_IGN)),
174-
sigpipe::INHERIT => (true, None),
175-
sigpipe::SIG_IGN => (true, Some(libc::SIG_IGN)),
176-
sigpipe::SIG_DFL => (true, Some(libc::SIG_DFL)),
177-
_ => unreachable!(),
161+
use crate::io::OnBrokenPipe;
162+
let (on_broken_pipe_used, handler) = match crate::io::on_broken_pipe() {
163+
OnBrokenPipe::BackwardsCompatible => (false, Some(libc::SIG_IGN)),
164+
OnBrokenPipe::Inherit => (true, None),
165+
OnBrokenPipe::Error => (true, Some(libc::SIG_IGN)),
166+
OnBrokenPipe::Kill => (true, Some(libc::SIG_DFL)),
178167
};
179168
if on_broken_pipe_used {
180169
ON_BROKEN_PIPE_USED.store(true, crate::sync::atomic::Ordering::Relaxed);

library/std/src/sys/process/unix/unix.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ impl Command {
352352
// Inherit the signal mask from the parent rather than resetting it (i.e. do not call
353353
// pthread_sigmask).
354354

355+
// FIXME: Update
355356
// If -Zon-broken-pipe is used, don't reset SIGPIPE to SIG_DFL.
356357
// If -Zon-broken-pipe is not used, reset SIGPIPE to SIG_DFL for backward compatibility.
357358
//
@@ -729,6 +730,7 @@ impl Command {
729730
// Inherit the signal mask from this process rather than resetting it (i.e. do not call
730731
// posix_spawnattr_setsigmask).
731732

733+
// FIXME: Update
732734
// If -Zon-broken-pipe is used, don't reset SIGPIPE to SIG_DFL.
733735
// If -Zon-broken-pipe is not used, reset SIGPIPE to SIG_DFL for backward compatibility.
734736
//

src/bootstrap/src/bin/rustc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ fn main() {
137137
cmd.args(lint_flags.split_whitespace());
138138
}
139139

140-
// TODO: Use `sigpipe-default` crate
140+
// FIXME: Use `sigpipe-default` crate
141141

142142
if target.is_some() {
143143
// The stage0 compiler has a special sysroot distinct from what we

src/bootstrap/src/core/build_steps/compile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ pub fn rustc_cargo(
12331233
// Also see the discussion for properly handling I/O errors related to broken pipes, i.e. safe
12341234
// variants of `println!` in
12351235
// <https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Internal.20lint.20for.20raw.20.60print!.60.20and.20.60println!.60.3F>.
1236-
cargo.rustflag("-Zon-broken-pipe=kill");
1236+
// FIXME: cargo.rustflag("-Zon-broken-pipe=kill");
12371237

12381238
// Building with protected visibility reduces the number of dynamic relocations needed, giving
12391239
// us a faster startup time. However GNU ld < 2.40 will error if we try to link a shared object

src/bootstrap/src/core/build_steps/tool.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ pub fn prepare_tool_cargo(
305305
// <https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Internal.20lint.20for.20raw.20.60print!.60.20and.20.60println!.60.3F>
306306
// for proper solutions.
307307
if !path.ends_with("cargo") {
308+
// FIXME: Update this code
308309
// Use an untracked env var `FORCE_ON_BROKEN_PIPE_KILL` here instead of `RUSTFLAGS`.
309310
// `RUSTFLAGS` is tracked by cargo. Conditionally omitting `-Zon-broken-pipe=kill` from
310311
// `RUSTFLAGS` causes unnecessary tool rebuilds due to cache invalidation from building e.g.

src/tools/compiletest/src/runtest.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,7 @@ impl<'test> TestCx<'test> {
12821282
let crate_name = path_to_crate_name(aux_path);
12831283
let mut lib_name = get_lib_name(&crate_name, aux_type);
12841284

1285+
// FIXME: Make this ugly hack more beatiful and easy to understand
12851286
// Some auxiliaries opt out of compiletest's default `--crate-type dylib`
12861287
// (e.g. via `//@ no-prefer-dynamic`). In that case, fall back to an rlib if the
12871288
// expected dylib doesn't exist.

src/tools/miri/src/eval.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,6 @@ fn call_main<'tcx>(
416416

417417
let main_ptr = ecx.fn_ptr(FnVal::Instance(entry_instance));
418418

419-
// Always using DEFAULT is okay since we don't support signals in Miri anyway.
420-
// (This means we are effectively ignoring `-Zon-broken-pipe`.)
421-
let sigpipe = rustc_session::config::sigpipe::DEFAULT;
422-
423419
ecx.call_function(
424420
start_instance,
425421
ExternAbi::Rust,
@@ -431,7 +427,7 @@ fn call_main<'tcx>(
431427
),
432428
argc,
433429
argv,
434-
ImmTy::from_uint(sigpipe, ecx.machine.layouts.u8),
430+
ImmTy::from_uint(0u8 /* unused */, ecx.machine.layouts.u8),
435431
],
436432
Some(&ret_place),
437433
ReturnContinuation::Stop { cleanup: true },

tests/run-make/broken-pipe-no-ice/rmake.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ fn check_broken_pipe_handled_gracefully(bin: Binary, mut cmd: Command) {
3939

4040
#[cfg(not(windows))]
4141
{
42-
// On non-Windows, rustc/rustdoc built with `-Zon-broken-pipe=kill` shouldn't have an exit
42+
// FIXME: Update comment
43+
// On non-Windows, rustc/rustdoc built with `OnBrokenPipe::Kill` shouldn't have an exit
4344
// code of 101 because it should have an wait status that corresponds to SIGPIPE signal
4445
// number.
4546
assert_ne!(status.code(), Some(PANIC_ICE_EXIT_CODE), "{bin:?}");

tests/ui/runtime/on-broken-pipe/auxiliary/assert-inherit-sig_dfl.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
//@ aux-crate: sigpipe_utils=sigpipe-utils.rs
2-
//@ compile-flags: -Zon-broken-pipe=inherit
2+
3+
// FIXME: Should not be needed TODO: Create specific issue
4+
//@ no-prefer-dynamic
5+
6+
#![feature(on_broken_pipe)]
7+
#![feature(extern_item_impls)]
8+
9+
#[std::io::on_broken_pipe]
10+
fn on_broken_pipe() -> std::io::OnBrokenPipe {
11+
std::io::OnBrokenPipe::Inherit
12+
}
313

414
fn main() {
515
sigpipe_utils::assert_sigpipe_handler(sigpipe_utils::SignalHandler::Default);

0 commit comments

Comments
 (0)