summaryrefslogtreecommitdiffstats
path: root/lib/libc/gen/exec.c
diff options
context:
space:
mode:
authorbde <bde@FreeBSD.org>1997-10-14 07:23:16 +0000
committerbde <bde@FreeBSD.org>1997-10-14 07:23:16 +0000
commiteff0c860593642a1d509df6abfcab36fc81f8058 (patch)
tree7fb33e298e116b25823d7ad589a2cf0d5627a7e2 /lib/libc/gen/exec.c
parent7740727dac4ae4e180b68a6bec4241a717b3d0cc (diff)
downloadFreeBSD-src-eff0c860593642a1d509df6abfcab36fc81f8058.zip
FreeBSD-src-eff0c860593642a1d509df6abfcab36fc81f8058.tar.gz
Fixed searching of $PATH in execvp(). Do what sh(1) should do according
to POSIX.2. In particular: - don't retry for ETXTBSY. This matches what sh(1) does. The retry code was broken anyway. It only slept for several seconds for the first few retries. Then it retried without sleeping. - don't abort the search for errors related to the path prefix, in particular for ENAMETOOLONG, ENOTDIR, ELOOP. This fixes PR1487. sh(1) gets this wrong in the opposite direction by never aborting the search. - don't confuse EACCES for errors related to the path prefix with EACCES for errors related to the file. sh(1) gets this wrong. - don't return a stale errno when the search terminates normally without finding anything. The errno for the last unsuccessful execve() was usually returned. This gave too much precedence to pathologies in the last component of $PATH. This bug is irrelevant for sh(1). The implementation still uses the optimization/race-inhibitor of trying to execve() things first. POSIX.2 seems to require looking at file permissions using stat(). We now use stat() after execve() if execve() fails with an ambiguous error. Trying execve() first may actually be a pessimization, since failing execve()s are fundamentally a little slower than stat(), and are significantly slower when a file is found but has unsuitable permissions or points to an unsuitable interpreter. PR: 1487
Diffstat (limited to 'lib/libc/gen/exec.c')
-rw-r--r--lib/libc/gen/exec.c45
1 files changed, 36 insertions, 9 deletions
diff --git a/lib/libc/gen/exec.c b/lib/libc/gen/exec.c
index 72b6db4..c083957 100644
--- a/lib/libc/gen/exec.c
+++ b/lib/libc/gen/exec.c
@@ -32,11 +32,16 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
+#if 0
static char sccsid[] = "@(#)exec.c 8.1 (Berkeley) 6/4/93";
+#endif
+static const char rcsid[] =
+ "$Id$";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
#include <sys/types.h>
+#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
@@ -187,10 +192,11 @@ execvp(name, argv)
char **memp;
register int cnt, lp, ln;
register char *p;
- int eacces, etxtbsy;
+ int eacces, save_errno;
char *bp, *cur, *path, buf[MAXPATHLEN];
+ struct stat sb;
- eacces = etxtbsy = 0;
+ eacces = 0;
/* If it's an absolute or relative path name, it's easy. */
if (index(name, '/')) {
@@ -241,9 +247,10 @@ execvp(name, argv)
retry: (void)execve(bp, argv, environ);
switch(errno) {
- case EACCES:
- eacces = 1;
- break;
+ case E2BIG:
+ goto done;
+ case ELOOP:
+ case ENAMETOOLONG:
case ENOENT:
break;
case ENOEXEC:
@@ -258,17 +265,37 @@ retry: (void)execve(bp, argv, environ);
(void)execve(_PATH_BSHELL, memp, environ);
free(memp);
goto done;
+ case ENOMEM:
+ goto done;
+ case ENOTDIR:
+ break;
case ETXTBSY:
- if (etxtbsy < 3)
- (void)sleep(++etxtbsy);
- goto retry;
+ /*
+ * We used to retry here, but sh(1) doesn't.
+ */
+ goto done;
default:
+ /*
+ * EACCES may be for an inaccessible directory or
+ * a non-executable file. Call stat() to decide
+ * which. This also handles ambiguities for EFAULT
+ * and EIO, and undocumented errors like ESTALE.
+ * We hope that the race for a stat() is unimportant.
+ */
+ save_errno = errno;
+ if (stat(argv[0], &sb) != 0)
+ break;
+ if (save_errno == EACCES) {
+ eacces = 1;
+ continue;
+ }
+ errno = save_errno;
goto done;
}
}
if (eacces)
errno = EACCES;
- else if (!errno)
+ else
errno = ENOENT;
done: if (path)
free(path);
OpenPOWER on IntegriCloud