From 77da4a95e81a3d27991a2d2af5a93fcdbd4c7cf7 Mon Sep 17 00:00:00 2001 From: Jilles Tjoelker Date: Tue, 27 Nov 2018 21:49:59 +0000 Subject: [PATCH] sh: Use 126 and 127 exit status for failures opening a script This affects scripts named on the command line, named with a '.' special builtin and found via the PATH %func autoloading mechanism. PR: 231986 --- bin/sh/input.c | 8 ++++++-- bin/sh/sh.1 | 8 ++++++-- bin/sh/tests/errors/Makefile | 1 + bin/sh/tests/errors/script-error1.0 | 5 +++++ 4 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 bin/sh/tests/errors/script-error1.0 diff --git a/bin/sh/input.c b/bin/sh/input.c index 391953190b54..d48aab803903 100644 --- a/bin/sh/input.c +++ b/bin/sh/input.c @@ -359,12 +359,16 @@ popstring(void) void setinputfile(const char *fname, int push) { + int e; int fd; int fd2; INTOFF; - if ((fd = open(fname, O_RDONLY | O_CLOEXEC)) < 0) - error("cannot open %s: %s", fname, strerror(errno)); + if ((fd = open(fname, O_RDONLY | O_CLOEXEC)) < 0) { + e = errno; + errorwithstatus(e == ENOENT || e == ENOTDIR ? 127 : 126, + "cannot open %s: %s", fname, strerror(e)); + } if (fd < 10) { fd2 = fcntl(fd, F_DUPFD_CLOEXEC, 10); close(fd); diff --git a/bin/sh/sh.1 b/bin/sh/sh.1 index 3ea5af67637b..3158b41bc87e 100644 --- a/bin/sh/sh.1 +++ b/bin/sh/sh.1 @@ -32,7 +32,7 @@ .\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95 .\" $FreeBSD$ .\" -.Dd July 19, 2018 +.Dd November 27, 2018 .Dt SH 1 .Os .Sh NAME @@ -2819,7 +2819,11 @@ Shell database. Privileged shell profile. .El .Sh EXIT STATUS -Errors that are detected by the shell, such as a syntax error, will +If the +.Ar script +cannot be found, the exit status will be 127; +if it cannot be opened for another reason, the exit status will be 126. +Other errors that are detected by the shell, such as a syntax error, will cause the shell to exit with a non-zero exit status. If the shell is not an interactive shell, the execution of the shell file will be aborted. diff --git a/bin/sh/tests/errors/Makefile b/bin/sh/tests/errors/Makefile index 868f43599973..5f8d16d22295 100644 --- a/bin/sh/tests/errors/Makefile +++ b/bin/sh/tests/errors/Makefile @@ -30,6 +30,7 @@ ${PACKAGE}FILES+= redirection-error5.0 ${PACKAGE}FILES+= redirection-error6.0 ${PACKAGE}FILES+= redirection-error7.0 ${PACKAGE}FILES+= redirection-error8.0 +${PACKAGE}FILES+= script-error1.0 ${PACKAGE}FILES+= write-error1.0 .include diff --git a/bin/sh/tests/errors/script-error1.0 b/bin/sh/tests/errors/script-error1.0 new file mode 100644 index 000000000000..558d9007353f --- /dev/null +++ b/bin/sh/tests/errors/script-error1.0 @@ -0,0 +1,5 @@ +# $FreeBSD$ + +{ stderr=$(${SH} /var/empty/nosuchscript 2>&1 >&3); } 3>&1 +r=$? +[ -n "$stderr" ] && [ "$r" = 127 ]