Skip to content

Commit 4b3aa91

Browse files
committedAug 28, 2022
Support #[unix_sigpipe = "inherit|sig_dfl|sig_ign"] on fn main()
This makes it possible to instruct libstd to never touch the signal handler for `SIGPIPE`, which makes programs pipeable by default (e.g. with `./your-program | head -n 1`) without `ErrorKind::BrokenPipe` errors.
1 parent f48af91 commit 4b3aa91

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)
Please sign in to comment.