From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 9980 invoked by alias); 28 Sep 2014 20:16:41 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 33268 Received: (qmail 16403 invoked from network); 28 Sep 2014 20:16:28 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 From: Bart Schaefer Message-id: <140928131627.ZM4749@torch.brasslantern.com> Date: Sun, 28 Sep 2014 13:16:27 -0700 In-reply-to: <20140928191850.61c28815@pws-pc.ntlworld.com> Comments: In reply to Peter Stephenson "Re: SIGPIPE (Re: ZSH history not saved anymore)" (Sep 28, 7:18pm) References: <87mw9qdp7s.fsf@thinkpad-t440p.tsdh.org> <20140924200710.2f764272@pws-pc.ntlworld.com> <8738bg2n1v.fsf@thinkpad-t440p.tsdh.org> <140926000448.ZM30835@torch.brasslantern.com> <878ul6lrw9.fsf@thinkpad-t440p.tsdh.org> <87y4t66td0.fsf@thinkpad-t440p.tsdh.org> <871tqxqyil.fsf@thinkpad-t440p.tsdh.org> <140927105301.ZM31550@torch.brasslantern.com> <20140927214035.7af22023@pws-pc.ntlworld.com> <140927165554.ZM32155@torch.brasslantern.com> <140928110438.ZM27132@torch.brasslantern.com> <20140928191850.61c28815@pws-pc.ntlworld.com> X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zsh-workers@zsh.org Subject: Re: SIGPIPE (Re: ZSH history not saved anymore) MIME-version: 1.0 Content-type: text/plain; charset=us-ascii On Sep 28, 7:18pm, Peter Stephenson wrote: } } So in the first case I presume we're exiting (silently) on SIGPIPE. That } should be just a question of checking if SIGPIPE is trapped and if it isn't } setting the handler to the default in entersubsh(). There's some partial } prior art for this. It gets a little stranger - this is again before the patch: torch% TRAPPIPE() { : } torch% (sleep 5; echo hello; print -u2 $sysparams[pid] continued after PIPE) | (exit) TRAPPIPE: write error: broken pipe echo: write error: broken pipe zsh: write error: broken pipe 3579 continued after PIPE Why did TRAPPIPE() receive a SIGPIPE? And then: torch% TRAPPIPE() { print -u2 $sysparams[pid] } torch% (sleep 5; echo hello; print -u2 $sysparams[pid] continued after PIPE) | (exit) 3659 TRAPPIPE: write error: broken pipe 3659 echo: write error: broken pipe zsh: write error: broken pipe 3659 continued after PIPE Note all calls to TRAPPIPE were in the subshell, and that it was called more than once. I think the following patch now covers keeping all this weird behavior the same, while still zexit()'ng if SHTTY is lost. There may remain cases where a top-level non-interactive shell now calls _exit(SIGPIPE) where it did not before, but I'm not sure how to test for those. Just doing 'kill -PIPE' of 'zsh -fc "sleep 10"' exits 141 both before and after the patch below. TRAPEXIT() has never been called on SIGPIPE. I'm not entirely happy about examining forklevel in the very last hunk, but it's the only value passed out of entersubsh() that seems to make sense for this. That hunk is restoring zsh's handler when a user-defined trap is removed; without it, defining and then removing TRAPPIPE would instead restore the OS default SIGPIPE handling. I'd be grateful if somebody else could invent a regression test for this, perhaps in Test/C03traps.ztst ? diff --git a/Src/exec.c b/Src/exec.c index fb2739a..fbd3095 100644 --- a/Src/exec.c +++ b/Src/exec.c @@ -1005,6 +1005,8 @@ entersubsh(int flags) signal_default(SIGTERM); if (!(sigtrapped[SIGINT] & ZSIG_IGNORED)) signal_default(SIGINT); + if (!(sigtrapped[SIGPIPE])) + signal_default(SIGPIPE); } if (!(sigtrapped[SIGQUIT] & ZSIG_IGNORED)) signal_default(SIGQUIT); diff --git a/Src/init.c b/Src/init.c index 40f46a8..6d005dc 100644 --- a/Src/init.c +++ b/Src/init.c @@ -1134,6 +1134,7 @@ init_signals(void) winch_block(); /* See utils.c:preprompt() */ #endif if (interact) { + install_handler(SIGPIPE); install_handler(SIGALRM); signal_ignore(SIGTERM); } diff --git a/Src/signals.c b/Src/signals.c index cb2b581..84a2609 100644 --- a/Src/signals.c +++ b/Src/signals.c @@ -594,6 +594,17 @@ zhandler(int sig) wait_for_processes(); break; + case SIGPIPE: + if (!handletrap(SIGPIPE)) { + if (!interact) + _exit(SIGPIPE); + else if (!isatty(SHTTY)) { + stopmsg = 1; + zexit(SIGPIPE, 1); + } + } + break; + case SIGHUP: if (!handletrap(SIGHUP)) { stopmsg = 1; @@ -897,6 +908,8 @@ removetrap(int sig) noholdintr(); } else if (sig == SIGHUP) install_handler(sig); + else if (sig == SIGPIPE && interact && !forklevel) + install_handler(sig); else if (sig && sig <= SIGCOUNT && #ifdef SIGWINCH sig != SIGWINCH &&