diff mbox series

jobs: Implement pipefail option

Message ID 20220422211013.18195-1-chris@chrisn.me.uk (mailing list archive)
State Accepted
Delegated to: Herbert Xu
Headers show
Series jobs: Implement pipefail option | expand

Commit Message

Chris Novakovic April 22, 2022, 9:10 p.m. UTC
With the pipefail option set, a pipeline's exit status is the exit
status of the rightmost command that failed, or zero if all commands
succeeded.

This is planned for inclusion in the next revision of POSIX [1],
although the details are yet to be finalised. The semantics of this
implementation are the same as those proposed in [2], which have also
been adopted by the BSD shells.

[1] https://www.austingroupbugs.net/view.php?id=789
[2] https://www.austingroupbugs.net/view.php?id=789#c4115
---
 src/dash.1    | 31 ++++++++++++++++++++++++-------
 src/jobs.c    |  9 ++++++++-
 src/options.c |  2 ++
 src/options.h |  5 +++--
 4 files changed, 37 insertions(+), 10 deletions(-)

Comments

Trevor Gross Jan. 8, 2023, 12:34 a.m. UTC | #1
Would it be possible for a maintainer to review this patch? POSIX 2022 must be
nearing release (or already released?) and this option is one of the more
notable additions. Dash is about the only major shell that does not yet support
pipefail [1].

[1] https://unix.stackexchange.com/questions/654885/who-is-responsible-for-providing-set-o-pipefail/654932#654932
Lawrence Velázquez Jan. 8, 2023, 1:20 a.m. UTC | #2
On Sat, Jan 7, 2023, at 7:34 PM, Trevor Gross wrote:
> POSIX 2022 must be nearing release (or already released?)

Issue 8 has not been released.
Herbert Xu April 5, 2024, 10:19 a.m. UTC | #3
On Fri, Apr 22, 2022 at 10:10:13PM +0100, Chris Novakovic wrote:
> With the pipefail option set, a pipeline's exit status is the exit
> status of the rightmost command that failed, or zero if all commands
> succeeded.
> 
> This is planned for inclusion in the next revision of POSIX [1],
> although the details are yet to be finalised. The semantics of this
> implementation are the same as those proposed in [2], which have also
> been adopted by the BSD shells.
> 
> [1] https://www.austingroupbugs.net/view.php?id=789
> [2] https://www.austingroupbugs.net/view.php?id=789#c4115
> ---
>  src/dash.1    | 31 ++++++++++++++++++++++++-------
>  src/jobs.c    |  9 ++++++++-
>  src/options.c |  2 ++
>  src/options.h |  5 +++--
>  4 files changed, 37 insertions(+), 10 deletions(-)

Patch applied.  Thanks.
diff mbox series

Patch

diff --git a/src/dash.1 b/src/dash.1
index ff02237..7a7b162 100644
--- a/src/dash.1
+++ b/src/dash.1
@@ -553,13 +553,17 @@  by redirection operators that are part of the command.
 If the pipeline is not in the background (discussed later), the shell
 waits for all commands to complete.
 .Pp
-If the reserved word ! does not precede the pipeline, the exit status is
-the exit status of the last command specified in the pipeline.
-Otherwise, the exit status is the logical NOT of the exit status of the
-last command.
-That is, if the last command returns zero, the exit status
-is 1; if the last command returns greater than zero, the exit status is
-zero.
+If the
+.Em pipefail
+option was enabled when the shell began execution of the pipeline, the
+pipeline's exit status is the exit status of the last command specified in
+the pipeline that exited with non-zero status, or zero if all commands in
+the pipeline exited with a status of zero. If the
+.Em pipefail
+option was not enabled, the pipeline's exit status is the exit status of
+the last command specified in the pipeline; the exit statuses of any other
+commands are not used. If the reserved word ! precedes the pipeline, its
+exit status is the logical NOT of the exit status described above.
 .Pp
 Because pipeline assignment of standard input or standard output or both
 takes place before redirection, it can be modified by redirection.
@@ -1791,6 +1795,19 @@  if the option is +o,
 the settings are printed in a format suitable for
 reinput to the shell to affect the same option settings.
 .Pp
+In addition to the option names listed in the
+.Sx Argument List Processing
+section, the following options may be specified as arguments
+to -o or +o:
+.Bl -tag -width pipefail
+.It Em pipefail
+Derive the exit status of a pipeline from the exit statuses of all
+of the commands in the pipeline, not just the last command, as
+described in the
+.Sx Pipelines
+section.
+.El
+.Pp
 The third use of the set command is to set the values of the shell's
 positional parameters to the specified args.
 To change the positional
diff --git a/src/jobs.c b/src/jobs.c
index f3b9ffc..78c7bc6 100644
--- a/src/jobs.c
+++ b/src/jobs.c
@@ -1526,8 +1526,15 @@  STATIC int
 getstatus(struct job *job) {
 	int status;
 	int retval;
+	struct procstat *ps;
+
+	ps = job->ps + job->nprocs - 1;
+	status = ps->status;
+	if (pipefail) {
+		while (status == 0 && --ps >= job->ps)
+			status = ps->status;
+	}
 
-	status = job->ps[job->nprocs - 1].status;
 	retval = WEXITSTATUS(status);
 	if (!WIFEXITED(status)) {
 #if JOBS
diff --git a/src/options.c b/src/options.c
index a46c23b..cc0f70d 100644
--- a/src/options.c
+++ b/src/options.c
@@ -80,6 +80,7 @@  static const char *const optnames[NOPTS] = {
 	"notify",
 	"nounset",
 	"nolog",
+	"pipefail",
 	"debug",
 };
 
@@ -101,6 +102,7 @@  const char optletters[NOPTS] = {
 	'u',
 	0,
 	0,
+	0,
 };
 
 char optlist[NOPTS];
diff --git a/src/options.h b/src/options.h
index 975fe33..f421316 100644
--- a/src/options.h
+++ b/src/options.h
@@ -60,9 +60,10 @@  struct shparam {
 #define	bflag optlist[13]
 #define	uflag optlist[14]
 #define	nolog optlist[15]
-#define	debug optlist[16]
+#define	pipefail optlist[16]
+#define	debug optlist[17]
 
-#define NOPTS	17
+#define NOPTS	18
 
 extern const char optletters[NOPTS];
 extern char optlist[NOPTS];