svn commit: r341097 - in head/bin/sh: . tests/errors

Jilles Tjoelker jilles at FreeBSD.org
Tue Nov 27 21:50:01 UTC 2018


Author: jilles
Date: Tue Nov 27 21:49:59 2018
New Revision: 341097
URL: https://svnweb.freebsd.org/changeset/base/341097

Log:
  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

Added:
  head/bin/sh/tests/errors/script-error1.0   (contents, props changed)
Modified:
  head/bin/sh/input.c
  head/bin/sh/sh.1
  head/bin/sh/tests/errors/Makefile

Modified: head/bin/sh/input.c
==============================================================================
--- head/bin/sh/input.c	Tue Nov 27 21:40:51 2018	(r341096)
+++ head/bin/sh/input.c	Tue Nov 27 21:49:59 2018	(r341097)
@@ -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);

Modified: head/bin/sh/sh.1
==============================================================================
--- head/bin/sh/sh.1	Tue Nov 27 21:40:51 2018	(r341096)
+++ head/bin/sh/sh.1	Tue Nov 27 21:49:59 2018	(r341097)
@@ -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.

Modified: head/bin/sh/tests/errors/Makefile
==============================================================================
--- head/bin/sh/tests/errors/Makefile	Tue Nov 27 21:40:51 2018	(r341096)
+++ head/bin/sh/tests/errors/Makefile	Tue Nov 27 21:49:59 2018	(r341097)
@@ -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 <bsd.test.mk>

Added: head/bin/sh/tests/errors/script-error1.0
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/bin/sh/tests/errors/script-error1.0	Tue Nov 27 21:49:59 2018	(r341097)
@@ -0,0 +1,5 @@
+# $FreeBSD$
+
+{ stderr=$(${SH} /var/empty/nosuchscript 2>&1 >&3); } 3>&1
+r=$?
+[ -n "$stderr" ] && [ "$r" = 127 ]


More information about the svn-src-all mailing list