xref: /freebsd/lib/libc/gen/exec.c (revision 09f49aab843136b36644e57acfa3a727098016e9)
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  * 3. All advertising materials mentioning features or use of this software
1458f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1558f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1658f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
1758f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
1858f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1958f0484fSRodney W. Grimes  *    without specific prior written permission.
2058f0484fSRodney W. Grimes  *
2158f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2258f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2358f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2458f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2558f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2658f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2758f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2858f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2958f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3058f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3158f0484fSRodney W. Grimes  * SUCH DAMAGE.
3258f0484fSRodney W. Grimes  */
3358f0484fSRodney W. Grimes 
3458f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3558f0484fSRodney W. Grimes static char sccsid[] = "@(#)exec.c	8.1 (Berkeley) 6/4/93";
3658f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
3722626efaSDavid E. O'Brien #include <sys/cdefs.h>
3822626efaSDavid E. O'Brien __FBSDID("$FreeBSD$");
3958f0484fSRodney W. Grimes 
40d201fe46SDaniel Eischen #include "namespace.h"
4158f0484fSRodney W. Grimes #include <sys/param.h>
4258f0484fSRodney W. Grimes #include <sys/types.h>
431df595f2SBruce Evans #include <sys/stat.h>
4458f0484fSRodney W. Grimes #include <errno.h>
4558f0484fSRodney W. Grimes #include <unistd.h>
4658f0484fSRodney W. Grimes #include <stdlib.h>
4758f0484fSRodney W. Grimes #include <string.h>
4858f0484fSRodney W. Grimes #include <stdio.h>
4958f0484fSRodney W. Grimes #include <paths.h>
5058f0484fSRodney W. Grimes 
5158f0484fSRodney W. Grimes #include <stdarg.h>
52d201fe46SDaniel Eischen #include "un-namespace.h"
5358f0484fSRodney W. Grimes 
5458f0484fSRodney W. Grimes extern char **environ;
5558f0484fSRodney W. Grimes 
5658f0484fSRodney W. Grimes int
5758f0484fSRodney W. Grimes execl(const char *name, const char *arg, ...)
5858f0484fSRodney W. Grimes {
5958f0484fSRodney W. Grimes 	va_list ap;
6058f0484fSRodney W. Grimes 	char **argv;
61dea625c8SDag-Erling Smørgrav 	int n;
62dea625c8SDag-Erling Smørgrav 
6358f0484fSRodney W. Grimes 	va_start(ap, arg);
647ea577e5SDag-Erling Smørgrav 	n = 1;
65dea625c8SDag-Erling Smørgrav 	while (va_arg(ap, char *) != NULL)
66dea625c8SDag-Erling Smørgrav 		n++;
6758f0484fSRodney W. Grimes 	va_end(ap);
68af806462SDag-Erling Smørgrav 	argv = alloca((n + 1) * sizeof(*argv));
69ce04fea4SPeter Wemm 	if (argv == NULL) {
70ce04fea4SPeter Wemm 		errno = ENOMEM;
7158f0484fSRodney W. Grimes 		return (-1);
72ce04fea4SPeter Wemm 	}
73dea625c8SDag-Erling Smørgrav 	va_start(ap, arg);
747ea577e5SDag-Erling Smørgrav 	n = 1;
75af806462SDag-Erling Smørgrav 	argv[0] = (char *)arg;
76dea625c8SDag-Erling Smørgrav 	while ((argv[n] = va_arg(ap, char *)) != NULL)
77dea625c8SDag-Erling Smørgrav 		n++;
78dea625c8SDag-Erling Smørgrav 	va_end(ap);
79d201fe46SDaniel Eischen 	return (_execve(name, argv, environ));
8058f0484fSRodney W. Grimes }
8158f0484fSRodney W. Grimes 
8258f0484fSRodney W. Grimes int
8358f0484fSRodney W. Grimes execle(const char *name, const char *arg, ...)
8458f0484fSRodney W. Grimes {
8558f0484fSRodney W. Grimes 	va_list ap;
8658f0484fSRodney W. Grimes 	char **argv, **envp;
87af806462SDag-Erling Smørgrav 	int n;
8858f0484fSRodney W. Grimes 
8958f0484fSRodney W. Grimes 	va_start(ap, arg);
90af806462SDag-Erling Smørgrav 	n = 1;
91af806462SDag-Erling Smørgrav 	while (va_arg(ap, char *) != NULL)
92af806462SDag-Erling Smørgrav 		n++;
9358f0484fSRodney W. Grimes 	va_end(ap);
94af806462SDag-Erling Smørgrav 	argv = alloca((n + 1) * sizeof(*argv));
95ce04fea4SPeter Wemm 	if (argv == NULL) {
96ce04fea4SPeter Wemm 		errno = ENOMEM;
9758f0484fSRodney W. Grimes 		return (-1);
98ce04fea4SPeter Wemm 	}
99af806462SDag-Erling Smørgrav 	va_start(ap, arg);
100af806462SDag-Erling Smørgrav 	n = 1;
101af806462SDag-Erling Smørgrav 	argv[0] = (char *)arg;
102af806462SDag-Erling Smørgrav 	while ((argv[n] = va_arg(ap, char *)) != NULL)
103af806462SDag-Erling Smørgrav 		n++;
104af806462SDag-Erling Smørgrav 	envp = va_arg(ap, char **);
105af806462SDag-Erling Smørgrav 	va_end(ap);
106d201fe46SDaniel Eischen 	return (_execve(name, argv, envp));
10758f0484fSRodney W. Grimes }
10858f0484fSRodney W. Grimes 
10958f0484fSRodney W. Grimes int
11058f0484fSRodney W. Grimes execlp(const char *name, const char *arg, ...)
11158f0484fSRodney W. Grimes {
11258f0484fSRodney W. Grimes 	va_list ap;
11358f0484fSRodney W. Grimes 	char **argv;
114ce04fea4SPeter Wemm 	int n;
11558f0484fSRodney W. Grimes 
11658f0484fSRodney W. Grimes 	va_start(ap, arg);
117ce04fea4SPeter Wemm 	n = 1;
118ce04fea4SPeter Wemm 	while (va_arg(ap, char *) != NULL)
119ce04fea4SPeter Wemm 		n++;
12058f0484fSRodney W. Grimes 	va_end(ap);
121ce04fea4SPeter Wemm 	argv = alloca((n + 1) * sizeof(*argv));
122ce04fea4SPeter Wemm 	if (argv == NULL) {
123ce04fea4SPeter Wemm 		errno = ENOMEM;
12458f0484fSRodney W. Grimes 		return (-1);
12558f0484fSRodney W. Grimes 	}
126ce04fea4SPeter Wemm 	va_start(ap, arg);
127ce04fea4SPeter Wemm 	n = 1;
128ce04fea4SPeter Wemm 	argv[0] = (char *)arg;
129ce04fea4SPeter Wemm 	while ((argv[n] = va_arg(ap, char *)) != NULL)
130ce04fea4SPeter Wemm 		n++;
131ce04fea4SPeter Wemm 	va_end(ap);
132ce04fea4SPeter Wemm 	return (execvp(name, argv));
133ce04fea4SPeter Wemm }
13458f0484fSRodney W. Grimes 
13558f0484fSRodney W. Grimes int
13658f0484fSRodney W. Grimes execv(name, argv)
13758f0484fSRodney W. Grimes 	const char *name;
13858f0484fSRodney W. Grimes 	char * const *argv;
13958f0484fSRodney W. Grimes {
140d201fe46SDaniel Eischen 	(void)_execve(name, argv, environ);
14158f0484fSRodney W. Grimes 	return (-1);
14258f0484fSRodney W. Grimes }
14358f0484fSRodney W. Grimes 
14458f0484fSRodney W. Grimes int
14509f49aabSGordon Tetlow execvp(const char *name, char *const *argv)
14609f49aabSGordon Tetlow {
14709f49aabSGordon Tetlow 	const char *path;
14809f49aabSGordon Tetlow 
14909f49aabSGordon Tetlow 	/* Get the path we're searching. */
15009f49aabSGordon Tetlow 	if (!(path = getenv("PATH")))
15109f49aabSGordon Tetlow 		path = _PATH_DEFPATH;
15209f49aabSGordon Tetlow 	return(execvP(name,path,argv));
15309f49aabSGordon Tetlow }
15409f49aabSGordon Tetlow 
15509f49aabSGordon Tetlow int
15609f49aabSGordon Tetlow execvP(name, path, argv)
15758f0484fSRodney W. Grimes 	const char *name;
15809f49aabSGordon Tetlow 	const char *path;
15958f0484fSRodney W. Grimes 	char * const *argv;
16058f0484fSRodney W. Grimes {
161a2c06222SBruce Evans 	char **memp;
16222626efaSDavid E. O'Brien 	int cnt, lp, ln;
16322626efaSDavid E. O'Brien 	char *p;
1641df595f2SBruce Evans 	int eacces, save_errno;
16509f49aabSGordon Tetlow 	char *bp, *cur, buf[MAXPATHLEN];
1661df595f2SBruce Evans 	struct stat sb;
16758f0484fSRodney W. Grimes 
1681df595f2SBruce Evans 	eacces = 0;
1691ad652a5SBruce Evans 
17058f0484fSRodney W. Grimes 	/* If it's an absolute or relative path name, it's easy. */
17158f0484fSRodney W. Grimes 	if (index(name, '/')) {
17258f0484fSRodney W. Grimes 		bp = (char *)name;
17309f49aabSGordon Tetlow 		cur = NULL;
17458f0484fSRodney W. Grimes 		goto retry;
17558f0484fSRodney W. Grimes 	}
17658f0484fSRodney W. Grimes 	bp = buf;
17758f0484fSRodney W. Grimes 
17876663101SBruce Evans 	/* If it's an empty path name, fail in the usual POSIX way. */
17976663101SBruce Evans 	if (*name == '\0') {
18076663101SBruce Evans 		errno = ENOENT;
18176663101SBruce Evans 		return (-1);
18276663101SBruce Evans 	}
18376663101SBruce Evans 
184ce04fea4SPeter Wemm 	cur = alloca(strlen(path) + 1);
185ce04fea4SPeter Wemm 	if (cur == NULL) {
186ce04fea4SPeter Wemm 		errno = ENOMEM;
187ce04fea4SPeter Wemm 		return (-1);
188ce04fea4SPeter Wemm 	}
189ce04fea4SPeter Wemm 	strcpy(cur, path);
19051295a4dSJordan K. Hubbard 	while ( (p = strsep(&cur, ":")) ) {
19158f0484fSRodney W. Grimes 		/*
19258f0484fSRodney W. Grimes 		 * It's a SHELL path -- double, leading and trailing colons
19358f0484fSRodney W. Grimes 		 * mean the current directory.
19458f0484fSRodney W. Grimes 		 */
19558f0484fSRodney W. Grimes 		if (!*p) {
19658f0484fSRodney W. Grimes 			p = ".";
19758f0484fSRodney W. Grimes 			lp = 1;
19858f0484fSRodney W. Grimes 		} else
19958f0484fSRodney W. Grimes 			lp = strlen(p);
20058f0484fSRodney W. Grimes 		ln = strlen(name);
20158f0484fSRodney W. Grimes 
20258f0484fSRodney W. Grimes 		/*
20358f0484fSRodney W. Grimes 		 * If the path is too long complain.  This is a possible
20458f0484fSRodney W. Grimes 		 * security issue; given a way to make the path too long
20558f0484fSRodney W. Grimes 		 * the user may execute the wrong program.
20658f0484fSRodney W. Grimes 		 */
20758f0484fSRodney W. Grimes 		if (lp + ln + 2 > sizeof(buf)) {
20809f49aabSGordon Tetlow 			(void)_write(STDERR_FILENO, "execvP: ", 8);
2099233c4d9SJason Evans 			(void)_write(STDERR_FILENO, p, lp);
2109233c4d9SJason Evans 			(void)_write(STDERR_FILENO, ": path too long\n",
21192927338SJason Evans 			    16);
21258f0484fSRodney W. Grimes 			continue;
21358f0484fSRodney W. Grimes 		}
21458f0484fSRodney W. Grimes 		bcopy(p, buf, lp);
21558f0484fSRodney W. Grimes 		buf[lp] = '/';
21658f0484fSRodney W. Grimes 		bcopy(name, buf + lp + 1, ln);
21758f0484fSRodney W. Grimes 		buf[lp + ln + 1] = '\0';
21858f0484fSRodney W. Grimes 
219d201fe46SDaniel Eischen retry:		(void)_execve(bp, argv, environ);
22058f0484fSRodney W. Grimes 		switch(errno) {
2211df595f2SBruce Evans 		case E2BIG:
2221df595f2SBruce Evans 			goto done;
2231df595f2SBruce Evans 		case ELOOP:
2241df595f2SBruce Evans 		case ENAMETOOLONG:
22558f0484fSRodney W. Grimes 		case ENOENT:
22658f0484fSRodney W. Grimes 			break;
22758f0484fSRodney W. Grimes 		case ENOEXEC:
228a2c06222SBruce Evans 			for (cnt = 0; argv[cnt]; ++cnt)
229a2c06222SBruce Evans 				;
2305fe5a4ddSPeter Wemm 			memp = alloca((cnt + 2) * sizeof(char *));
231ce04fea4SPeter Wemm 			if (memp == NULL) {
232ce04fea4SPeter Wemm 				/* errno = ENOMEM; XXX override ENOEXEC? */
23358f0484fSRodney W. Grimes 				goto done;
234ce04fea4SPeter Wemm 			}
23558f0484fSRodney W. Grimes 			memp[0] = "sh";
23658f0484fSRodney W. Grimes 			memp[1] = bp;
23758f0484fSRodney W. Grimes 			bcopy(argv + 1, memp + 2, cnt * sizeof(char *));
238d201fe46SDaniel Eischen 			(void)_execve(_PATH_BSHELL, memp, environ);
23958f0484fSRodney W. Grimes 			goto done;
2401df595f2SBruce Evans 		case ENOMEM:
2411df595f2SBruce Evans 			goto done;
2421df595f2SBruce Evans 		case ENOTDIR:
2431df595f2SBruce Evans 			break;
24458f0484fSRodney W. Grimes 		case ETXTBSY:
2451df595f2SBruce Evans 			/*
2461df595f2SBruce Evans 			 * We used to retry here, but sh(1) doesn't.
2471df595f2SBruce Evans 			 */
2481df595f2SBruce Evans 			goto done;
24958f0484fSRodney W. Grimes 		default:
2501df595f2SBruce Evans 			/*
2511df595f2SBruce Evans 			 * EACCES may be for an inaccessible directory or
2521df595f2SBruce Evans 			 * a non-executable file.  Call stat() to decide
2531df595f2SBruce Evans 			 * which.  This also handles ambiguities for EFAULT
2541df595f2SBruce Evans 			 * and EIO, and undocumented errors like ESTALE.
2551df595f2SBruce Evans 			 * We hope that the race for a stat() is unimportant.
2561df595f2SBruce Evans 			 */
2571df595f2SBruce Evans 			save_errno = errno;
25870df31a6SBruce Evans 			if (stat(bp, &sb) != 0)
2591df595f2SBruce Evans 				break;
2601df595f2SBruce Evans 			if (save_errno == EACCES) {
2611df595f2SBruce Evans 				eacces = 1;
2621df595f2SBruce Evans 				continue;
2631df595f2SBruce Evans 			}
2641df595f2SBruce Evans 			errno = save_errno;
26558f0484fSRodney W. Grimes 			goto done;
26658f0484fSRodney W. Grimes 		}
26758f0484fSRodney W. Grimes 	}
26858f0484fSRodney W. Grimes 	if (eacces)
26958f0484fSRodney W. Grimes 		errno = EACCES;
2701df595f2SBruce Evans 	else
27158f0484fSRodney W. Grimes 		errno = ENOENT;
272ce04fea4SPeter Wemm done:
27358f0484fSRodney W. Grimes 	return (-1);
27458f0484fSRodney W. Grimes }
275