Skip to content

Commit b8169a6

Browse files
committed
Auto merge of #97802 - Enselic:add-no_ignore_sigkill-feature, r=joshtriplett
Support `#[unix_sigpipe = "inherit|sig_dfl"]` on `fn main()` to prevent ignoring `SIGPIPE` When enabled, programs don't have to explicitly handle `ErrorKind::BrokenPipe` any longer. Currently, the program ```rust fn main() { loop { println!("hello world"); } } ``` will print an error if used with a short-lived pipe, e.g. % ./main | head -n 1 hello world thread 'main' panicked at 'failed printing to stdout: Broken pipe (os error 32)', library/std/src/io/stdio.rs:1016:9 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace by enabling `#[unix_sigpipe = "sig_dfl"]` like this ```rust #![feature(unix_sigpipe)] #[unix_sigpipe = "sig_dfl"] fn main() { loop { println!("hello world"); } } ``` there is no error, because `SIGPIPE` will not be ignored and thus the program will be killed appropriately: % ./main | head -n 1 hello world The current libstd behaviour of ignoring `SIGPIPE` before `fn main()` can be explicitly requested by using `#[unix_sigpipe = "sig_ign"]`. With `#[unix_sigpipe = "inherit"]`, no change at all is made to `SIGPIPE`, which typically means the behaviour will be the same as `#[unix_sigpipe = "sig_dfl"]`. See rust-lang/rust#62569 and referenced issues for discussions regarding the `SIGPIPE` problem itself See the [this](https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Proposal.3A.20First.20step.20towards.20solving.20the.20SIGPIPE.20problem) Zulip topic for more discussions, including about this PR. Tracking issue: rust-lang/rust#97889
2 parents bfb2016 + 4b3aa91 commit b8169a6

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/main_shim.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_hir::LangItem;
22
use rustc_middle::ty::subst::GenericArg;
33
use rustc_middle::ty::AssocKind;
4-
use rustc_session::config::EntryFnType;
4+
use rustc_session::config::{sigpipe, EntryFnType};
55
use rustc_span::symbol::Ident;
66

77
use crate::prelude::*;
@@ -15,12 +15,12 @@ pub(crate) fn maybe_create_entry_wrapper(
1515
is_jit: bool,
1616
is_primary_cgu: bool,
1717
) {
18-
let (main_def_id, is_main_fn) = match tcx.entry_fn(()) {
18+
let (main_def_id, (is_main_fn, sigpipe)) = match tcx.entry_fn(()) {
1919
Some((def_id, entry_ty)) => (
2020
def_id,
2121
match entry_ty {
22-
EntryFnType::Main => true,
23-
EntryFnType::Start => false,
22+
EntryFnType::Main { sigpipe } => (true, sigpipe),
23+
EntryFnType::Start => (false, sigpipe::DEFAULT),
2424
},
2525
),
2626
None => return,
@@ -35,7 +35,7 @@ pub(crate) fn maybe_create_entry_wrapper(
3535
return;
3636
}
3737

38-
create_entry_fn(tcx, module, unwind_context, main_def_id, is_jit, is_main_fn);
38+
create_entry_fn(tcx, module, unwind_context, main_def_id, is_jit, is_main_fn, sigpipe);
3939

4040
fn create_entry_fn(
4141
tcx: TyCtxt<'_>,
@@ -44,6 +44,7 @@ pub(crate) fn maybe_create_entry_wrapper(
4444
rust_main_def_id: DefId,
4545
ignore_lang_start_wrapper: bool,
4646
is_main_fn: bool,
47+
sigpipe: u8,
4748
) {
4849
let main_ret_ty = tcx.fn_sig(rust_main_def_id).output();
4950
// Given that `main()` has no arguments,
@@ -83,6 +84,7 @@ pub(crate) fn maybe_create_entry_wrapper(
8384
bcx.switch_to_block(block);
8485
let arg_argc = bcx.append_block_param(block, m.target_config().pointer_type());
8586
let arg_argv = bcx.append_block_param(block, m.target_config().pointer_type());
87+
let arg_sigpipe = bcx.ins().iconst(types::I8, sigpipe as i64);
8688

8789
let main_func_ref = m.declare_func_in_func(main_func_id, &mut bcx.func);
8890

@@ -143,7 +145,8 @@ pub(crate) fn maybe_create_entry_wrapper(
143145
let main_val = bcx.ins().func_addr(m.target_config().pointer_type(), main_func_ref);
144146

145147
let func_ref = m.declare_func_in_func(start_func_id, &mut bcx.func);
146-
let call_inst = bcx.ins().call(func_ref, &[main_val, arg_argc, arg_argv]);
148+
let call_inst =
149+
bcx.ins().call(func_ref, &[main_val, arg_argc, arg_argv, arg_sigpipe]);
147150
bcx.inst_results(call_inst)[0]
148151
} else {
149152
// using user-defined start fn

0 commit comments

Comments
 (0)