@@ -44,12 +44,12 @@ pub mod thread_parker;
4444pub mod time;
4545
4646#[ cfg( target_os = "espidf" ) ]
47- pub fn init ( argc : isize , argv : * const * const u8 ) { }
47+ pub fn init ( argc : isize , argv : * const * const u8 , _sigpipe : u8 ) { }
4848
4949#[ cfg( not( target_os = "espidf" ) ) ]
5050// SAFETY: must be called only once during runtime initialization.
5151// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
52- pub unsafe fn init ( argc : isize , argv : * const * const u8 ) {
52+ pub unsafe fn init ( argc : isize , argv : * const * const u8 , sigpipe : u8 ) {
5353 // The standard streams might be closed on application startup. To prevent
5454 // std::io::{stdin, stdout,stderr} objects from using other unrelated file
5555 // resources opened later, we reopen standards streams when they are closed.
@@ -61,8 +61,9 @@ pub unsafe fn init(argc: isize, argv: *const *const u8) {
6161 // want!
6262 //
6363 // Hence, we set SIGPIPE to ignore when the program starts up in order
64- // to prevent this problem.
65- reset_sigpipe ( ) ;
64+ // to prevent this problem. Add `#[unix_sigpipe = "..."]` above `fn main()` to
65+ // alter this behavior.
66+ reset_sigpipe ( sigpipe) ;
6667
6768 stack_overflow:: init ( ) ;
6869 args:: init ( argc, argv) ;
@@ -151,9 +152,25 @@ pub unsafe fn init(argc: isize, argv: *const *const u8) {
151152 }
152153 }
153154
154- unsafe fn reset_sigpipe ( ) {
155+ unsafe fn reset_sigpipe ( # [ allow ( unused_variables ) ] sigpipe : u8 ) {
155156 #[ cfg( not( any( target_os = "emscripten" , target_os = "fuchsia" , target_os = "horizon" ) ) ) ]
156- rtassert ! ( signal( libc:: SIGPIPE , libc:: SIG_IGN ) != libc:: SIG_ERR ) ;
157+ {
158+ // We don't want to add this as a public type to libstd, nor do we want to
159+ // duplicate the code, so we choose to include this compiler file like this.
160+ mod sigpipe {
161+ include ! ( "../../../../../compiler/rustc_session/src/config/sigpipe.rs" ) ;
162+ }
163+
164+ let handler = match sigpipe {
165+ sigpipe:: INHERIT => None ,
166+ sigpipe:: SIG_IGN => Some ( libc:: SIG_IGN ) ,
167+ sigpipe:: SIG_DFL => Some ( libc:: SIG_DFL ) ,
168+ _ => unreachable ! ( ) ,
169+ } ;
170+ if let Some ( handler) = handler {
171+ rtassert ! ( signal( libc:: SIGPIPE , handler) != libc:: SIG_ERR ) ;
172+ }
173+ }
157174 }
158175}
159176
0 commit comments