xref: /freebsd/lib/libc/gen/exec.c (revision bbbe3054ff632f0a8f8abe13fcfea3a0ede0a9b3)
158f0484fSRodney W. Grimes /*-
258f0484fSRodney W. Grimes  * Copyright (c) 1991, 1993
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
658f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
758f0484fSRodney W. Grimes  * are met:
858f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
958f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1058f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1258f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1358f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
1458f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1558f0484fSRodney W. Grimes  *    without specific prior written permission.
1658f0484fSRodney W. Grimes  *
1758f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1858f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1958f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2058f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2158f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2258f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2358f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2458f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2558f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2658f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2758f0484fSRodney W. Grimes  * SUCH DAMAGE.
2858f0484fSRodney W. Grimes  */
2958f0484fSRodney W. Grimes 
3058f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3158f0484fSRodney W. Grimes static char sccsid[] = "@(#)exec.c	8.1 (Berkeley) 6/4/93";
3258f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
3322626efaSDavid E. O'Brien #include <sys/cdefs.h>
3422626efaSDavid E. O'Brien __FBSDID("$FreeBSD$");
3558f0484fSRodney W. Grimes 
36d201fe46SDaniel Eischen #include "namespace.h"
3758f0484fSRodney W. Grimes #include <sys/param.h>
3858f0484fSRodney W. Grimes #include <sys/types.h>
391df595f2SBruce Evans #include <sys/stat.h>
4058f0484fSRodney W. Grimes #include <errno.h>
4158f0484fSRodney W. Grimes #include <unistd.h>
4258f0484fSRodney W. Grimes #include <stdlib.h>
4358f0484fSRodney W. Grimes #include <string.h>
4458f0484fSRodney W. Grimes #include <stdio.h>
4558f0484fSRodney W. Grimes #include <paths.h>
4658f0484fSRodney W. Grimes 
4758f0484fSRodney W. Grimes #include <stdarg.h>
48d201fe46SDaniel Eischen #include "un-namespace.h"
49c605eea9SEd Schouten #include "libc_private.h"
5058f0484fSRodney W. Grimes 
5158f0484fSRodney W. Grimes extern char **environ;
5258f0484fSRodney W. Grimes 
5358f0484fSRodney W. Grimes int
5458f0484fSRodney W. Grimes execl(const char *name, const char *arg, ...)
5558f0484fSRodney W. Grimes {
5658f0484fSRodney W. Grimes 	va_list ap;
57bbbe3054SEd Schouten 	const char **argv;
58dea625c8SDag-Erling Smørgrav 	int n;
59dea625c8SDag-Erling Smørgrav 
6058f0484fSRodney W. Grimes 	va_start(ap, arg);
617ea577e5SDag-Erling Smørgrav 	n = 1;
62dea625c8SDag-Erling Smørgrav 	while (va_arg(ap, char *) != NULL)
63dea625c8SDag-Erling Smørgrav 		n++;
6458f0484fSRodney W. Grimes 	va_end(ap);
65af806462SDag-Erling Smørgrav 	argv = alloca((n + 1) * sizeof(*argv));
66ce04fea4SPeter Wemm 	if (argv == NULL) {
67ce04fea4SPeter Wemm 		errno = ENOMEM;
6858f0484fSRodney W. Grimes 		return (-1);
69ce04fea4SPeter Wemm 	}
70dea625c8SDag-Erling Smørgrav 	va_start(ap, arg);
717ea577e5SDag-Erling Smørgrav 	n = 1;
72bbbe3054SEd Schouten 	argv[0] = arg;
73dea625c8SDag-Erling Smørgrav 	while ((argv[n] = va_arg(ap, char *)) != NULL)
74dea625c8SDag-Erling Smørgrav 		n++;
75dea625c8SDag-Erling Smørgrav 	va_end(ap);
76bbbe3054SEd Schouten 	return (_execve(name, __DECONST(char **, argv), environ));
7758f0484fSRodney W. Grimes }
7858f0484fSRodney W. Grimes 
7958f0484fSRodney W. Grimes int
8058f0484fSRodney W. Grimes execle(const char *name, const char *arg, ...)
8158f0484fSRodney W. Grimes {
8258f0484fSRodney W. Grimes 	va_list ap;
83bbbe3054SEd Schouten 	const char **argv;
84bbbe3054SEd Schouten 	char **envp;
85af806462SDag-Erling Smørgrav 	int n;
8658f0484fSRodney W. Grimes 
8758f0484fSRodney W. Grimes 	va_start(ap, arg);
88af806462SDag-Erling Smørgrav 	n = 1;
89af806462SDag-Erling Smørgrav 	while (va_arg(ap, char *) != NULL)
90af806462SDag-Erling Smørgrav 		n++;
9158f0484fSRodney W. Grimes 	va_end(ap);
92af806462SDag-Erling Smørgrav 	argv = alloca((n + 1) * sizeof(*argv));
93ce04fea4SPeter Wemm 	if (argv == NULL) {
94ce04fea4SPeter Wemm 		errno = ENOMEM;
9558f0484fSRodney W. Grimes 		return (-1);
96ce04fea4SPeter Wemm 	}
97af806462SDag-Erling Smørgrav 	va_start(ap, arg);
98af806462SDag-Erling Smørgrav 	n = 1;
99bbbe3054SEd Schouten 	argv[0] = arg;
100af806462SDag-Erling Smørgrav 	while ((argv[n] = va_arg(ap, char *)) != NULL)
101af806462SDag-Erling Smørgrav 		n++;
102af806462SDag-Erling Smørgrav 	envp = va_arg(ap, char **);
103af806462SDag-Erling Smørgrav 	va_end(ap);
104bbbe3054SEd Schouten 	return (_execve(name, __DECONST(char **, argv), envp));
10558f0484fSRodney W. Grimes }
10658f0484fSRodney W. Grimes 
10758f0484fSRodney W. Grimes int
10858f0484fSRodney W. Grimes execlp(const char *name, const char *arg, ...)
10958f0484fSRodney W. Grimes {
11058f0484fSRodney W. Grimes 	va_list ap;
111bbbe3054SEd Schouten 	const char **argv;
112ce04fea4SPeter Wemm 	int n;
11358f0484fSRodney W. Grimes 
11458f0484fSRodney W. Grimes 	va_start(ap, arg);
115ce04fea4SPeter Wemm 	n = 1;
116ce04fea4SPeter Wemm 	while (va_arg(ap, char *) != NULL)
117ce04fea4SPeter Wemm 		n++;
11858f0484fSRodney W. Grimes 	va_end(ap);
119ce04fea4SPeter Wemm 	argv = alloca((n + 1) * sizeof(*argv));
120ce04fea4SPeter Wemm 	if (argv == NULL) {
121ce04fea4SPeter Wemm 		errno = ENOMEM;
12258f0484fSRodney W. Grimes 		return (-1);
12358f0484fSRodney W. Grimes 	}
124ce04fea4SPeter Wemm 	va_start(ap, arg);
125ce04fea4SPeter Wemm 	n = 1;
126bbbe3054SEd Schouten 	argv[0] = arg;
127ce04fea4SPeter Wemm 	while ((argv[n] = va_arg(ap, char *)) != NULL)
128ce04fea4SPeter Wemm 		n++;
129ce04fea4SPeter Wemm 	va_end(ap);
130bbbe3054SEd Schouten 	return (execvp(name, __DECONST(char **, argv)));
131ce04fea4SPeter Wemm }
13258f0484fSRodney W. Grimes 
13358f0484fSRodney W. Grimes int
13458f0484fSRodney W. Grimes execv(name, argv)
13558f0484fSRodney W. Grimes 	const char *name;
13658f0484fSRodney W. Grimes 	char * const *argv;
13758f0484fSRodney W. Grimes {
138d201fe46SDaniel Eischen 	(void)_execve(name, argv, environ);
13958f0484fSRodney W. Grimes 	return (-1);
14058f0484fSRodney W. Grimes }
14158f0484fSRodney W. Grimes 
14258f0484fSRodney W. Grimes int
14309f49aabSGordon Tetlow execvp(const char *name, char * const *argv)
14409f49aabSGordon Tetlow {
145c605eea9SEd Schouten 	return (_execvpe(name, argv, environ));
14609f49aabSGordon Tetlow }
14709f49aabSGordon Tetlow 
148947aa542SDavid Xu static int
149bbbe3054SEd Schouten execvPe(const char *name, const char *path, char * const *argv,
150bbbe3054SEd Schouten     char * const *envp)
15158f0484fSRodney W. Grimes {
152bbbe3054SEd Schouten 	const char **memp;
153bbbe3054SEd Schouten 	size_t cnt, lp, ln;
1541df595f2SBruce Evans 	int eacces, save_errno;
155bbbe3054SEd Schouten 	char *cur, buf[MAXPATHLEN];
156bbbe3054SEd Schouten 	const char *p, *bp;
1571df595f2SBruce Evans 	struct stat sb;
15858f0484fSRodney W. Grimes 
1591df595f2SBruce Evans 	eacces = 0;
1601ad652a5SBruce Evans 
16158f0484fSRodney W. Grimes 	/* If it's an absolute or relative path name, it's easy. */
16258f0484fSRodney W. Grimes 	if (index(name, '/')) {
163bbbe3054SEd Schouten 		bp = name;
16409f49aabSGordon Tetlow 		cur = NULL;
16558f0484fSRodney W. Grimes 		goto retry;
16658f0484fSRodney W. Grimes 	}
16758f0484fSRodney W. Grimes 	bp = buf;
16858f0484fSRodney W. Grimes 
16976663101SBruce Evans 	/* If it's an empty path name, fail in the usual POSIX way. */
17076663101SBruce Evans 	if (*name == '\0') {
17176663101SBruce Evans 		errno = ENOENT;
17276663101SBruce Evans 		return (-1);
17376663101SBruce Evans 	}
17476663101SBruce Evans 
175ce04fea4SPeter Wemm 	cur = alloca(strlen(path) + 1);
176ce04fea4SPeter Wemm 	if (cur == NULL) {
177ce04fea4SPeter Wemm 		errno = ENOMEM;
178ce04fea4SPeter Wemm 		return (-1);
179ce04fea4SPeter Wemm 	}
180ce04fea4SPeter Wemm 	strcpy(cur, path);
181270f6e44SBruce Evans 	while ((p = strsep(&cur, ":")) != NULL) {
18258f0484fSRodney W. Grimes 		/*
18358f0484fSRodney W. Grimes 		 * It's a SHELL path -- double, leading and trailing colons
18458f0484fSRodney W. Grimes 		 * mean the current directory.
18558f0484fSRodney W. Grimes 		 */
186270f6e44SBruce Evans 		if (*p == '\0') {
18758f0484fSRodney W. Grimes 			p = ".";
18858f0484fSRodney W. Grimes 			lp = 1;
18958f0484fSRodney W. Grimes 		} else
19058f0484fSRodney W. Grimes 			lp = strlen(p);
19158f0484fSRodney W. Grimes 		ln = strlen(name);
19258f0484fSRodney W. Grimes 
19358f0484fSRodney W. Grimes 		/*
19458f0484fSRodney W. Grimes 		 * If the path is too long complain.  This is a possible
19558f0484fSRodney W. Grimes 		 * security issue; given a way to make the path too long
19658f0484fSRodney W. Grimes 		 * the user may execute the wrong program.
19758f0484fSRodney W. Grimes 		 */
19858f0484fSRodney W. Grimes 		if (lp + ln + 2 > sizeof(buf)) {
19909f49aabSGordon Tetlow 			(void)_write(STDERR_FILENO, "execvP: ", 8);
2009233c4d9SJason Evans 			(void)_write(STDERR_FILENO, p, lp);
2019233c4d9SJason Evans 			(void)_write(STDERR_FILENO, ": path too long\n",
20292927338SJason Evans 			    16);
20358f0484fSRodney W. Grimes 			continue;
20458f0484fSRodney W. Grimes 		}
20558f0484fSRodney W. Grimes 		bcopy(p, buf, lp);
20658f0484fSRodney W. Grimes 		buf[lp] = '/';
20758f0484fSRodney W. Grimes 		bcopy(name, buf + lp + 1, ln);
20858f0484fSRodney W. Grimes 		buf[lp + ln + 1] = '\0';
20958f0484fSRodney W. Grimes 
210f67d07f0SEd Schouten retry:		(void)_execve(bp, argv, envp);
21158f0484fSRodney W. Grimes 		switch (errno) {
2121df595f2SBruce Evans 		case E2BIG:
2131df595f2SBruce Evans 			goto done;
2141df595f2SBruce Evans 		case ELOOP:
2151df595f2SBruce Evans 		case ENAMETOOLONG:
21658f0484fSRodney W. Grimes 		case ENOENT:
21758f0484fSRodney W. Grimes 			break;
21858f0484fSRodney W. Grimes 		case ENOEXEC:
219a2c06222SBruce Evans 			for (cnt = 0; argv[cnt]; ++cnt)
220a2c06222SBruce Evans 				;
2215fe5a4ddSPeter Wemm 			memp = alloca((cnt + 2) * sizeof(char *));
222ce04fea4SPeter Wemm 			if (memp == NULL) {
223ce04fea4SPeter Wemm 				/* errno = ENOMEM; XXX override ENOEXEC? */
22458f0484fSRodney W. Grimes 				goto done;
225ce04fea4SPeter Wemm 			}
22658f0484fSRodney W. Grimes 			memp[0] = "sh";
22758f0484fSRodney W. Grimes 			memp[1] = bp;
22858f0484fSRodney W. Grimes 			bcopy(argv + 1, memp + 2, cnt * sizeof(char *));
229bbbe3054SEd Schouten  			(void)_execve(_PATH_BSHELL,
230bbbe3054SEd Schouten 			    __DECONST(char **, memp), envp);
23158f0484fSRodney W. Grimes 			goto done;
2321df595f2SBruce Evans 		case ENOMEM:
2331df595f2SBruce Evans 			goto done;
2341df595f2SBruce Evans 		case ENOTDIR:
2351df595f2SBruce Evans 			break;
23658f0484fSRodney W. Grimes 		case ETXTBSY:
2371df595f2SBruce Evans 			/*
2381df595f2SBruce Evans 			 * We used to retry here, but sh(1) doesn't.
2391df595f2SBruce Evans 			 */
2401df595f2SBruce Evans 			goto done;
24158f0484fSRodney W. Grimes 		default:
2421df595f2SBruce Evans 			/*
2431df595f2SBruce Evans 			 * EACCES may be for an inaccessible directory or
2441df595f2SBruce Evans 			 * a non-executable file.  Call stat() to decide
2451df595f2SBruce Evans 			 * which.  This also handles ambiguities for EFAULT
2461df595f2SBruce Evans 			 * and EIO, and undocumented errors like ESTALE.
2471df595f2SBruce Evans 			 * We hope that the race for a stat() is unimportant.
2481df595f2SBruce Evans 			 */
2491df595f2SBruce Evans 			save_errno = errno;
25070df31a6SBruce Evans 			if (stat(bp, &sb) != 0)
2511df595f2SBruce Evans 				break;
2521df595f2SBruce Evans 			if (save_errno == EACCES) {
2531df595f2SBruce Evans 				eacces = 1;
2541df595f2SBruce Evans 				continue;
2551df595f2SBruce Evans 			}
2561df595f2SBruce Evans 			errno = save_errno;
25758f0484fSRodney W. Grimes 			goto done;
25858f0484fSRodney W. Grimes 		}
25958f0484fSRodney W. Grimes 	}
26058f0484fSRodney W. Grimes 	if (eacces)
26158f0484fSRodney W. Grimes 		errno = EACCES;
2621df595f2SBruce Evans 	else
26358f0484fSRodney W. Grimes 		errno = ENOENT;
264ce04fea4SPeter Wemm done:
26558f0484fSRodney W. Grimes 	return (-1);
26658f0484fSRodney W. Grimes }
267947aa542SDavid Xu 
268947aa542SDavid Xu int
269947aa542SDavid Xu execvP(const char *name, const char *path, char * const argv[])
270947aa542SDavid Xu {
271947aa542SDavid Xu 	return execvPe(name, path, argv, environ);
272947aa542SDavid Xu }
273947aa542SDavid Xu 
274947aa542SDavid Xu int
275c605eea9SEd Schouten _execvpe(const char *name, char * const argv[], char * const envp[])
276947aa542SDavid Xu {
277947aa542SDavid Xu 	const char *path;
278947aa542SDavid Xu 
279947aa542SDavid Xu 	/* Get the path we're searching. */
280947aa542SDavid Xu 	if ((path = getenv("PATH")) == NULL)
281947aa542SDavid Xu 		path = _PATH_DEFPATH;
282947aa542SDavid Xu 
283947aa542SDavid Xu 	return (execvPe(name, path, argv, envp));
284947aa542SDavid Xu }
285