From 68c2706780031e3aac6cccea0f7b867ad5eff13a Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 26 Mar 2014 14:34:52 -0700 Subject: native: Ignore SIGPIPE by default Some unix platforms will send a SIGPIPE signal instead of returning EPIPE from a syscall by default. The native runtime doesn't install a SIGPIPE handler, causing the program to die immediately in this case. This brings the behavior in line with libgreen by ignoring SIGPIPE and propagating EPIPE upwards to the application in the form of an IoError. Closes #13123 --- src/libnative/lib.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/libnative') diff --git a/src/libnative/lib.rs b/src/libnative/lib.rs index 34e85a9819a..59b3437ad9a 100644 --- a/src/libnative/lib.rs +++ b/src/libnative/lib.rs @@ -97,6 +97,24 @@ pub fn start(argc: int, argv: **u8, main: proc()) -> int { // frames above our current position. let my_stack_bottom = my_stack_top + 20000 - OS_DEFAULT_STACK_ESTIMATE; + // When using libgreen, one of the first things that we do is to turn off + // the SIGPIPE signal (set it to ignore). By default, some platforms will + // send a *signal* when a EPIPE error would otherwise be delivered. This + // runtime doesn't install a SIGPIPE handler, causing it to kill the + // program, which isn't exactly what we want! + // + // Hence, we set SIGPIPE to ignore when the program starts up in order to + // prevent this problem. + #[cfg(windows)] fn ignore_sigpipe() {} + #[cfg(unix)] fn ignore_sigpipe() { + use std::libc; + use std::libc::funcs::posix01::signal::signal; + unsafe { + assert!(signal(libc::SIGPIPE, libc::SIG_IGN) != -1); + } + } + ignore_sigpipe(); + rt::init(argc, argv); let mut exit_code = None; let mut main = Some(main); -- cgit 1.4.1-3-g733a5