From owner-freebsd-bugs@FreeBSD.ORG Sun Jun 26 10:50:09 2011 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 28BE11065670 for ; Sun, 26 Jun 2011 10:50:09 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id F28EA8FC0C for ; Sun, 26 Jun 2011 10:50:08 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.4/8.14.4) with ESMTP id p5QAo2IZ001412 for ; Sun, 26 Jun 2011 10:50:02 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.4/8.14.4/Submit) id p5QAo2RZ001411; Sun, 26 Jun 2011 10:50:02 GMT (envelope-from gnats) Date: Sun, 26 Jun 2011 10:50:02 GMT Message-Id: <201106261050.p5QAo2RZ001411@freefall.freebsd.org> To: freebsd-bugs@FreeBSD.org From: Jilles Tjoelker Cc: Subject: Re: bin/158206: sh(1) doesn't properly return IO errors to conditionals in a script X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Jilles Tjoelker List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 26 Jun 2011 10:50:09 -0000 The following reply was made to PR bin/158206; it has been noted by GNATS. From: Jilles Tjoelker To: bug-followup@FreeBSD.org, culrich@csnstores.com Cc: Subject: Re: bin/158206: sh(1) doesn't properly return IO errors to conditionals in a script Date: Sun, 26 Jun 2011 12:41:00 +0200 Thank you for your bug report. The below patch should fix the problem. It applies to 9-current only. It could be changed to apply to 8-stable but I will not commit this change to 8-stable anyway. It is even a bit late for 9.x, one week into code slush. commit 22f46c6d493531fc8df124675598edbfb9eb8130 Author: Jilles Tjoelker Date: Sun Jun 26 12:25:07 2011 +0200 sh: Detect and flag write errors on stdout in builtins. PR: bin/158206 diff --git a/bin/sh/eval.c b/bin/sh/eval.c index d5da7d3..8330094 100644 --- a/bin/sh/eval.c +++ b/bin/sh/eval.c @@ -1000,6 +1000,7 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd) } handler = &jmploc; redirect(cmd->ncmd.redirect, mode); + outclearerror(out1); /* * If there is no command word, redirection errors should * not be fatal but assignment errors should. @@ -1015,6 +1016,11 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd) builtin_flags = flags; exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv); flushall(); + if (outiserror(out1)) { + warning("write error on stdout"); + if (exitstatus == 0 || exitstatus == 1) + exitstatus = 2; + } cmddone: if (argc > 0) bltinunsetlocale(); diff --git a/bin/sh/output.c b/bin/sh/output.c index b4dab51..d26adce 100644 --- a/bin/sh/output.c +++ b/bin/sh/output.c @@ -239,6 +239,20 @@ freestdout(void) } +int +outiserror(struct output *file) +{ + return (file->flags & OUTPUT_ERR); +} + + +void +outclearerror(struct output *file) +{ + file->flags &= ~OUTPUT_ERR; +} + + void outfmt(struct output *file, const char *fmt, ...) { diff --git a/bin/sh/output.h b/bin/sh/output.h index 5e3b048..51974d8 100644 --- a/bin/sh/output.h +++ b/bin/sh/output.h @@ -66,6 +66,8 @@ void emptyoutbuf(struct output *); void flushall(void); void flushout(struct output *); void freestdout(void); +int outiserror(struct output *); +void outclearerror(struct output *); void outfmt(struct output *, const char *, ...) __printflike(2, 3); void out1fmt(const char *, ...) __printflike(1, 2); void out2fmt_flush(const char *, ...) __printflike(1, 2); diff --git a/tools/regression/bin/sh/errors/write-error1.0 b/tools/regression/bin/sh/errors/write-error1.0 new file mode 100644 index 0000000..fcb52e7 --- /dev/null +++ b/tools/regression/bin/sh/errors/write-error1.0 @@ -0,0 +1,3 @@ +# $FreeBSD$ + +! echo >&- 2>/dev/null -- Jilles Tjoelker