xref: /freebsd/lib/libc/gen/exec.c (revision af806462dc8cadb926b382e595fe95cf91a12f98)
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)
351df595f2SBruce Evans #if 0
3658f0484fSRodney W. Grimes static char sccsid[] = "@(#)exec.c	8.1 (Berkeley) 6/4/93";
371df595f2SBruce Evans #endif
381df595f2SBruce Evans static const char rcsid[] =
39af806462SDag-Erling Smørgrav 	"$Id: exec.c,v 1.9 1998/10/14 20:23:40 des Exp $";
4058f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
4158f0484fSRodney W. Grimes 
4258f0484fSRodney W. Grimes #include <sys/param.h>
4358f0484fSRodney W. Grimes #include <sys/types.h>
441df595f2SBruce Evans #include <sys/stat.h>
4558f0484fSRodney W. Grimes #include <errno.h>
4658f0484fSRodney W. Grimes #include <unistd.h>
4758f0484fSRodney W. Grimes #include <stdlib.h>
4858f0484fSRodney W. Grimes #include <string.h>
4958f0484fSRodney W. Grimes #include <stdio.h>
5058f0484fSRodney W. Grimes #include <paths.h>
5158f0484fSRodney W. Grimes 
5258f0484fSRodney W. Grimes #if __STDC__
5358f0484fSRodney W. Grimes #include <stdarg.h>
5458f0484fSRodney W. Grimes #else
5558f0484fSRodney W. Grimes #include <varargs.h>
5658f0484fSRodney W. Grimes #endif
5758f0484fSRodney W. Grimes 
5858f0484fSRodney W. Grimes extern char **environ;
5958f0484fSRodney W. Grimes 
6058f0484fSRodney W. Grimes static char **
6158f0484fSRodney W. Grimes buildargv(ap, arg, envpp)
6258f0484fSRodney W. Grimes 	va_list ap;
6358f0484fSRodney W. Grimes 	const char *arg;
6458f0484fSRodney W. Grimes 	char ***envpp;
6558f0484fSRodney W. Grimes {
66a2c06222SBruce Evans 	register char **argv, **nargv;
67a2c06222SBruce Evans 	register int memsize, off;
6858f0484fSRodney W. Grimes 
6958f0484fSRodney W. Grimes 	argv = NULL;
70a2c06222SBruce Evans 	for (off = memsize = 0;; ++off) {
7158f0484fSRodney W. Grimes 		if (off >= memsize) {
7258f0484fSRodney W. Grimes 			memsize += 50;	/* Starts out at 0. */
7358f0484fSRodney W. Grimes 			memsize *= 2;	/* Ramp up fast. */
74a2c06222SBruce Evans 			nargv = realloc(argv, memsize * sizeof(char *));
75a2c06222SBruce Evans 			if (nargv == NULL) {
76a2c06222SBruce Evans 				free(argv);
7758f0484fSRodney W. Grimes 				return (NULL);
7858f0484fSRodney W. Grimes 			}
79a2c06222SBruce Evans 			argv = nargv;
8058f0484fSRodney W. Grimes 			if (off == 0) {
8158f0484fSRodney W. Grimes 				argv[0] = (char *)arg;
8258f0484fSRodney W. Grimes 				off = 1;
8358f0484fSRodney W. Grimes 			}
8458f0484fSRodney W. Grimes 		}
8558f0484fSRodney W. Grimes 		if (!(argv[off] = va_arg(ap, char *)))
8658f0484fSRodney W. Grimes 			break;
8758f0484fSRodney W. Grimes 	}
8858f0484fSRodney W. Grimes 	/* Get environment pointer if user supposed to provide one. */
8958f0484fSRodney W. Grimes 	if (envpp)
9058f0484fSRodney W. Grimes 		*envpp = va_arg(ap, char **);
9158f0484fSRodney W. Grimes 	return (argv);
9258f0484fSRodney W. Grimes }
9358f0484fSRodney W. Grimes 
9458f0484fSRodney W. Grimes int
9558f0484fSRodney W. Grimes #if __STDC__
9658f0484fSRodney W. Grimes execl(const char *name, const char *arg, ...)
9758f0484fSRodney W. Grimes #else
9858f0484fSRodney W. Grimes execl(name, arg, va_alist)
9958f0484fSRodney W. Grimes 	const char *name;
10058f0484fSRodney W. Grimes 	const char *arg;
10158f0484fSRodney W. Grimes 	va_dcl
10258f0484fSRodney W. Grimes #endif
10358f0484fSRodney W. Grimes {
10458f0484fSRodney W. Grimes 	va_list ap;
10558f0484fSRodney W. Grimes 	char **argv;
106dea625c8SDag-Erling Smørgrav 	int n;
107dea625c8SDag-Erling Smørgrav 
10858f0484fSRodney W. Grimes #if __STDC__
10958f0484fSRodney W. Grimes 	va_start(ap, arg);
11058f0484fSRodney W. Grimes #else
11158f0484fSRodney W. Grimes 	va_start(ap);
11258f0484fSRodney W. Grimes #endif
1137ea577e5SDag-Erling Smørgrav 	n = 1;
114dea625c8SDag-Erling Smørgrav 	while (va_arg(ap, char *) != NULL)
115dea625c8SDag-Erling Smørgrav 		n++;
11658f0484fSRodney W. Grimes 	va_end(ap);
117af806462SDag-Erling Smørgrav 	argv = alloca((n + 1) * sizeof(*argv));
118dea625c8SDag-Erling Smørgrav 	if (argv == NULL)
11958f0484fSRodney W. Grimes 		return (-1);
120dea625c8SDag-Erling Smørgrav #if __STDC__
121dea625c8SDag-Erling Smørgrav 	va_start(ap, arg);
122dea625c8SDag-Erling Smørgrav #else
123dea625c8SDag-Erling Smørgrav 	va_start(ap);
124dea625c8SDag-Erling Smørgrav #endif
1257ea577e5SDag-Erling Smørgrav 	n = 1;
126af806462SDag-Erling Smørgrav 	argv[0] = (char *)arg;
127dea625c8SDag-Erling Smørgrav 	while ((argv[n] = va_arg(ap, char *)) != NULL)
128dea625c8SDag-Erling Smørgrav 		n++;
129dea625c8SDag-Erling Smørgrav 	va_end(ap);
130dea625c8SDag-Erling Smørgrav 	return (execve(name, argv, environ));
13158f0484fSRodney W. Grimes }
13258f0484fSRodney W. Grimes 
13358f0484fSRodney W. Grimes int
13458f0484fSRodney W. Grimes #if __STDC__
13558f0484fSRodney W. Grimes execle(const char *name, const char *arg, ...)
13658f0484fSRodney W. Grimes #else
13758f0484fSRodney W. Grimes execle(name, arg, va_alist)
13858f0484fSRodney W. Grimes 	const char *name;
13958f0484fSRodney W. Grimes 	const char *arg;
14058f0484fSRodney W. Grimes 	va_dcl
14158f0484fSRodney W. Grimes #endif
14258f0484fSRodney W. Grimes {
14358f0484fSRodney W. Grimes 	va_list ap;
14458f0484fSRodney W. Grimes 	char **argv, **envp;
145af806462SDag-Erling Smørgrav 	int n;
14658f0484fSRodney W. Grimes 
14758f0484fSRodney W. Grimes #if __STDC__
14858f0484fSRodney W. Grimes 	va_start(ap, arg);
14958f0484fSRodney W. Grimes #else
15058f0484fSRodney W. Grimes 	va_start(ap);
15158f0484fSRodney W. Grimes #endif
152af806462SDag-Erling Smørgrav 	n = 1;
153af806462SDag-Erling Smørgrav 	while (va_arg(ap, char *) != NULL)
154af806462SDag-Erling Smørgrav 		n++;
15558f0484fSRodney W. Grimes 	va_end(ap);
156af806462SDag-Erling Smørgrav 	argv = alloca((n + 1) * sizeof(*argv));
157af806462SDag-Erling Smørgrav 	if (argv == NULL)
15858f0484fSRodney W. Grimes 		return (-1);
159af806462SDag-Erling Smørgrav #if __STDC__
160af806462SDag-Erling Smørgrav 	va_start(ap, arg);
161af806462SDag-Erling Smørgrav #else
162af806462SDag-Erling Smørgrav 	va_start(ap);
163af806462SDag-Erling Smørgrav #endif
164af806462SDag-Erling Smørgrav 	n = 1;
165af806462SDag-Erling Smørgrav 	argv[0] = (char *)arg;
166af806462SDag-Erling Smørgrav 	while ((argv[n] = va_arg(ap, char *)) != NULL)
167af806462SDag-Erling Smørgrav 		n++;
168af806462SDag-Erling Smørgrav 	envp = va_arg(ap, char **);
169af806462SDag-Erling Smørgrav 	va_end(ap);
170af806462SDag-Erling Smørgrav 	return (execve(name, argv, envp));
17158f0484fSRodney W. Grimes }
17258f0484fSRodney W. Grimes 
17358f0484fSRodney W. Grimes int
17458f0484fSRodney W. Grimes #if __STDC__
17558f0484fSRodney W. Grimes execlp(const char *name, const char *arg, ...)
17658f0484fSRodney W. Grimes #else
17758f0484fSRodney W. Grimes execlp(name, arg, va_alist)
17858f0484fSRodney W. Grimes 	const char *name;
17958f0484fSRodney W. Grimes 	const char *arg;
18058f0484fSRodney W. Grimes 	va_dcl
18158f0484fSRodney W. Grimes #endif
18258f0484fSRodney W. Grimes {
18358f0484fSRodney W. Grimes 	va_list ap;
18458f0484fSRodney W. Grimes 	int sverrno;
18558f0484fSRodney W. Grimes 	char **argv;
18658f0484fSRodney W. Grimes 
18758f0484fSRodney W. Grimes #if __STDC__
18858f0484fSRodney W. Grimes 	va_start(ap, arg);
18958f0484fSRodney W. Grimes #else
19058f0484fSRodney W. Grimes 	va_start(ap);
19158f0484fSRodney W. Grimes #endif
19251295a4dSJordan K. Hubbard 	if ( (argv = buildargv(ap, arg, NULL)) )
19358f0484fSRodney W. Grimes 		(void)execvp(name, argv);
19458f0484fSRodney W. Grimes 	va_end(ap);
19558f0484fSRodney W. Grimes 	sverrno = errno;
19658f0484fSRodney W. Grimes 	free(argv);
19758f0484fSRodney W. Grimes 	errno = sverrno;
19858f0484fSRodney W. Grimes 	return (-1);
19958f0484fSRodney W. Grimes }
20058f0484fSRodney W. Grimes 
20158f0484fSRodney W. Grimes int
20258f0484fSRodney W. Grimes execv(name, argv)
20358f0484fSRodney W. Grimes 	const char *name;
20458f0484fSRodney W. Grimes 	char * const *argv;
20558f0484fSRodney W. Grimes {
20658f0484fSRodney W. Grimes 	(void)execve(name, argv, environ);
20758f0484fSRodney W. Grimes 	return (-1);
20858f0484fSRodney W. Grimes }
20958f0484fSRodney W. Grimes 
21058f0484fSRodney W. Grimes int
21158f0484fSRodney W. Grimes execvp(name, argv)
21258f0484fSRodney W. Grimes 	const char *name;
21358f0484fSRodney W. Grimes 	char * const *argv;
21458f0484fSRodney W. Grimes {
215a2c06222SBruce Evans 	char **memp;
21658f0484fSRodney W. Grimes 	register int cnt, lp, ln;
21758f0484fSRodney W. Grimes 	register char *p;
2181df595f2SBruce Evans 	int eacces, save_errno;
21958f0484fSRodney W. Grimes 	char *bp, *cur, *path, buf[MAXPATHLEN];
2201df595f2SBruce Evans 	struct stat sb;
22158f0484fSRodney W. Grimes 
2221df595f2SBruce Evans 	eacces = 0;
2231ad652a5SBruce Evans 
22458f0484fSRodney W. Grimes 	/* If it's an absolute or relative path name, it's easy. */
22558f0484fSRodney W. Grimes 	if (index(name, '/')) {
22658f0484fSRodney W. Grimes 		bp = (char *)name;
22758f0484fSRodney W. Grimes 		cur = path = NULL;
22858f0484fSRodney W. Grimes 		goto retry;
22958f0484fSRodney W. Grimes 	}
23058f0484fSRodney W. Grimes 	bp = buf;
23158f0484fSRodney W. Grimes 
23276663101SBruce Evans 	/* If it's an empty path name, fail in the usual POSIX way. */
23376663101SBruce Evans 	if (*name == '\0') {
23476663101SBruce Evans 		errno = ENOENT;
23576663101SBruce Evans 		return (-1);
23676663101SBruce Evans 	}
23776663101SBruce Evans 
23858f0484fSRodney W. Grimes 	/* Get the path we're searching. */
23958f0484fSRodney W. Grimes 	if (!(path = getenv("PATH")))
24058f0484fSRodney W. Grimes 		path = _PATH_DEFPATH;
24158f0484fSRodney W. Grimes 	cur = path = strdup(path);
24258f0484fSRodney W. Grimes 
24351295a4dSJordan K. Hubbard 	while ( (p = strsep(&cur, ":")) ) {
24458f0484fSRodney W. Grimes 		/*
24558f0484fSRodney W. Grimes 		 * It's a SHELL path -- double, leading and trailing colons
24658f0484fSRodney W. Grimes 		 * mean the current directory.
24758f0484fSRodney W. Grimes 		 */
24858f0484fSRodney W. Grimes 		if (!*p) {
24958f0484fSRodney W. Grimes 			p = ".";
25058f0484fSRodney W. Grimes 			lp = 1;
25158f0484fSRodney W. Grimes 		} else
25258f0484fSRodney W. Grimes 			lp = strlen(p);
25358f0484fSRodney W. Grimes 		ln = strlen(name);
25458f0484fSRodney W. Grimes 
25558f0484fSRodney W. Grimes 		/*
25658f0484fSRodney W. Grimes 		 * If the path is too long complain.  This is a possible
25758f0484fSRodney W. Grimes 		 * security issue; given a way to make the path too long
25858f0484fSRodney W. Grimes 		 * the user may execute the wrong program.
25958f0484fSRodney W. Grimes 		 */
26058f0484fSRodney W. Grimes 		if (lp + ln + 2 > sizeof(buf)) {
26158f0484fSRodney W. Grimes 			(void)write(STDERR_FILENO, "execvp: ", 8);
26258f0484fSRodney W. Grimes 			(void)write(STDERR_FILENO, p, lp);
26358f0484fSRodney W. Grimes 			(void)write(STDERR_FILENO, ": path too long\n", 16);
26458f0484fSRodney W. Grimes 			continue;
26558f0484fSRodney W. Grimes 		}
26658f0484fSRodney W. Grimes 		bcopy(p, buf, lp);
26758f0484fSRodney W. Grimes 		buf[lp] = '/';
26858f0484fSRodney W. Grimes 		bcopy(name, buf + lp + 1, ln);
26958f0484fSRodney W. Grimes 		buf[lp + ln + 1] = '\0';
27058f0484fSRodney W. Grimes 
27158f0484fSRodney W. Grimes retry:		(void)execve(bp, argv, environ);
27258f0484fSRodney W. Grimes 		switch(errno) {
2731df595f2SBruce Evans 		case E2BIG:
2741df595f2SBruce Evans 			goto done;
2751df595f2SBruce Evans 		case ELOOP:
2761df595f2SBruce Evans 		case ENAMETOOLONG:
27758f0484fSRodney W. Grimes 		case ENOENT:
27858f0484fSRodney W. Grimes 			break;
27958f0484fSRodney W. Grimes 		case ENOEXEC:
280a2c06222SBruce Evans 			for (cnt = 0; argv[cnt]; ++cnt)
281a2c06222SBruce Evans 				;
282a2c06222SBruce Evans 			memp = malloc((cnt + 2) * sizeof(char *));
283a2c06222SBruce Evans 			if (memp == NULL)
28458f0484fSRodney W. Grimes 				goto done;
28558f0484fSRodney W. Grimes 			memp[0] = "sh";
28658f0484fSRodney W. Grimes 			memp[1] = bp;
28758f0484fSRodney W. Grimes 			bcopy(argv + 1, memp + 2, cnt * sizeof(char *));
28858f0484fSRodney W. Grimes 			(void)execve(_PATH_BSHELL, memp, environ);
289a2c06222SBruce Evans 			free(memp);
29058f0484fSRodney W. Grimes 			goto done;
2911df595f2SBruce Evans 		case ENOMEM:
2921df595f2SBruce Evans 			goto done;
2931df595f2SBruce Evans 		case ENOTDIR:
2941df595f2SBruce Evans 			break;
29558f0484fSRodney W. Grimes 		case ETXTBSY:
2961df595f2SBruce Evans 			/*
2971df595f2SBruce Evans 			 * We used to retry here, but sh(1) doesn't.
2981df595f2SBruce Evans 			 */
2991df595f2SBruce Evans 			goto done;
30058f0484fSRodney W. Grimes 		default:
3011df595f2SBruce Evans 			/*
3021df595f2SBruce Evans 			 * EACCES may be for an inaccessible directory or
3031df595f2SBruce Evans 			 * a non-executable file.  Call stat() to decide
3041df595f2SBruce Evans 			 * which.  This also handles ambiguities for EFAULT
3051df595f2SBruce Evans 			 * and EIO, and undocumented errors like ESTALE.
3061df595f2SBruce Evans 			 * We hope that the race for a stat() is unimportant.
3071df595f2SBruce Evans 			 */
3081df595f2SBruce Evans 			save_errno = errno;
30970df31a6SBruce Evans 			if (stat(bp, &sb) != 0)
3101df595f2SBruce Evans 				break;
3111df595f2SBruce Evans 			if (save_errno == EACCES) {
3121df595f2SBruce Evans 				eacces = 1;
3131df595f2SBruce Evans 				continue;
3141df595f2SBruce Evans 			}
3151df595f2SBruce Evans 			errno = save_errno;
31658f0484fSRodney W. Grimes 			goto done;
31758f0484fSRodney W. Grimes 		}
31858f0484fSRodney W. Grimes 	}
31958f0484fSRodney W. Grimes 	if (eacces)
32058f0484fSRodney W. Grimes 		errno = EACCES;
3211df595f2SBruce Evans 	else
32258f0484fSRodney W. Grimes 		errno = ENOENT;
32358f0484fSRodney W. Grimes done:	if (path)
32458f0484fSRodney W. Grimes 		free(path);
32558f0484fSRodney W. Grimes 	return (-1);
32658f0484fSRodney W. Grimes }
327