xref: /freebsd/lib/libc/gen/exec.c (revision dea625c8721c5a896b6d2e8c5209fc6ef4802f01)
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[] =
39dea625c8SDag-Erling Smørgrav 	"$Id: exec.c,v 1.7 1997/11/20 15:09:38 bde 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 
108dea625c8SDag-Erling Smørgrav 	/* The following code is ugly, but makes execl() vfork()-safe. */
10958f0484fSRodney W. Grimes 
11058f0484fSRodney W. Grimes #if __STDC__
11158f0484fSRodney W. Grimes 	va_start(ap, arg);
11258f0484fSRodney W. Grimes #else
11358f0484fSRodney W. Grimes 	va_start(ap);
11458f0484fSRodney W. Grimes #endif
115dea625c8SDag-Erling Smørgrav 	n = 0;
116dea625c8SDag-Erling Smørgrav 	while (va_arg(ap, char *) != NULL)
117dea625c8SDag-Erling Smørgrav 		n++ ;
11858f0484fSRodney W. Grimes 	va_end(ap);
119dea625c8SDag-Erling Smørgrav 	argv = (char **)alloca((n + 1) * sizeof(*argv));
120dea625c8SDag-Erling Smørgrav 	if (argv == NULL)
12158f0484fSRodney W. Grimes 		return (-1);
122dea625c8SDag-Erling Smørgrav #if __STDC__
123dea625c8SDag-Erling Smørgrav 	va_start(ap, arg);
124dea625c8SDag-Erling Smørgrav #else
125dea625c8SDag-Erling Smørgrav 	va_start(ap);
126dea625c8SDag-Erling Smørgrav #endif
127dea625c8SDag-Erling Smørgrav 	n = 0;
128dea625c8SDag-Erling Smørgrav 	while ((argv[n] = va_arg(ap, char *)) != NULL)
129dea625c8SDag-Erling Smørgrav 		n++;
130dea625c8SDag-Erling Smørgrav 	va_end(ap);
131dea625c8SDag-Erling Smørgrav 	return (execve(name, argv, environ));
13258f0484fSRodney W. Grimes }
13358f0484fSRodney W. Grimes 
13458f0484fSRodney W. Grimes int
13558f0484fSRodney W. Grimes #if __STDC__
13658f0484fSRodney W. Grimes execle(const char *name, const char *arg, ...)
13758f0484fSRodney W. Grimes #else
13858f0484fSRodney W. Grimes execle(name, arg, va_alist)
13958f0484fSRodney W. Grimes 	const char *name;
14058f0484fSRodney W. Grimes 	const char *arg;
14158f0484fSRodney W. Grimes 	va_dcl
14258f0484fSRodney W. Grimes #endif
14358f0484fSRodney W. Grimes {
14458f0484fSRodney W. Grimes 	va_list ap;
14558f0484fSRodney W. Grimes 	int sverrno;
14658f0484fSRodney W. Grimes 	char **argv, **envp;
14758f0484fSRodney W. Grimes 
14858f0484fSRodney W. Grimes #if __STDC__
14958f0484fSRodney W. Grimes 	va_start(ap, arg);
15058f0484fSRodney W. Grimes #else
15158f0484fSRodney W. Grimes 	va_start(ap);
15258f0484fSRodney W. Grimes #endif
15351295a4dSJordan K. Hubbard 	if ( (argv = buildargv(ap, arg, &envp)) )
15458f0484fSRodney W. Grimes 		(void)execve(name, argv, envp);
15558f0484fSRodney W. Grimes 	va_end(ap);
15658f0484fSRodney W. Grimes 	sverrno = errno;
15758f0484fSRodney W. Grimes 	free(argv);
15858f0484fSRodney W. Grimes 	errno = sverrno;
15958f0484fSRodney W. Grimes 	return (-1);
16058f0484fSRodney W. Grimes }
16158f0484fSRodney W. Grimes 
16258f0484fSRodney W. Grimes int
16358f0484fSRodney W. Grimes #if __STDC__
16458f0484fSRodney W. Grimes execlp(const char *name, const char *arg, ...)
16558f0484fSRodney W. Grimes #else
16658f0484fSRodney W. Grimes execlp(name, arg, va_alist)
16758f0484fSRodney W. Grimes 	const char *name;
16858f0484fSRodney W. Grimes 	const char *arg;
16958f0484fSRodney W. Grimes 	va_dcl
17058f0484fSRodney W. Grimes #endif
17158f0484fSRodney W. Grimes {
17258f0484fSRodney W. Grimes 	va_list ap;
17358f0484fSRodney W. Grimes 	int sverrno;
17458f0484fSRodney W. Grimes 	char **argv;
17558f0484fSRodney W. Grimes 
17658f0484fSRodney W. Grimes #if __STDC__
17758f0484fSRodney W. Grimes 	va_start(ap, arg);
17858f0484fSRodney W. Grimes #else
17958f0484fSRodney W. Grimes 	va_start(ap);
18058f0484fSRodney W. Grimes #endif
18151295a4dSJordan K. Hubbard 	if ( (argv = buildargv(ap, arg, NULL)) )
18258f0484fSRodney W. Grimes 		(void)execvp(name, argv);
18358f0484fSRodney W. Grimes 	va_end(ap);
18458f0484fSRodney W. Grimes 	sverrno = errno;
18558f0484fSRodney W. Grimes 	free(argv);
18658f0484fSRodney W. Grimes 	errno = sverrno;
18758f0484fSRodney W. Grimes 	return (-1);
18858f0484fSRodney W. Grimes }
18958f0484fSRodney W. Grimes 
19058f0484fSRodney W. Grimes int
19158f0484fSRodney W. Grimes execv(name, argv)
19258f0484fSRodney W. Grimes 	const char *name;
19358f0484fSRodney W. Grimes 	char * const *argv;
19458f0484fSRodney W. Grimes {
19558f0484fSRodney W. Grimes 	(void)execve(name, argv, environ);
19658f0484fSRodney W. Grimes 	return (-1);
19758f0484fSRodney W. Grimes }
19858f0484fSRodney W. Grimes 
19958f0484fSRodney W. Grimes int
20058f0484fSRodney W. Grimes execvp(name, argv)
20158f0484fSRodney W. Grimes 	const char *name;
20258f0484fSRodney W. Grimes 	char * const *argv;
20358f0484fSRodney W. Grimes {
204a2c06222SBruce Evans 	char **memp;
20558f0484fSRodney W. Grimes 	register int cnt, lp, ln;
20658f0484fSRodney W. Grimes 	register char *p;
2071df595f2SBruce Evans 	int eacces, save_errno;
20858f0484fSRodney W. Grimes 	char *bp, *cur, *path, buf[MAXPATHLEN];
2091df595f2SBruce Evans 	struct stat sb;
21058f0484fSRodney W. Grimes 
2111df595f2SBruce Evans 	eacces = 0;
2121ad652a5SBruce Evans 
21358f0484fSRodney W. Grimes 	/* If it's an absolute or relative path name, it's easy. */
21458f0484fSRodney W. Grimes 	if (index(name, '/')) {
21558f0484fSRodney W. Grimes 		bp = (char *)name;
21658f0484fSRodney W. Grimes 		cur = path = NULL;
21758f0484fSRodney W. Grimes 		goto retry;
21858f0484fSRodney W. Grimes 	}
21958f0484fSRodney W. Grimes 	bp = buf;
22058f0484fSRodney W. Grimes 
22176663101SBruce Evans 	/* If it's an empty path name, fail in the usual POSIX way. */
22276663101SBruce Evans 	if (*name == '\0') {
22376663101SBruce Evans 		errno = ENOENT;
22476663101SBruce Evans 		return (-1);
22576663101SBruce Evans 	}
22676663101SBruce Evans 
22758f0484fSRodney W. Grimes 	/* Get the path we're searching. */
22858f0484fSRodney W. Grimes 	if (!(path = getenv("PATH")))
22958f0484fSRodney W. Grimes 		path = _PATH_DEFPATH;
23058f0484fSRodney W. Grimes 	cur = path = strdup(path);
23158f0484fSRodney W. Grimes 
23251295a4dSJordan K. Hubbard 	while ( (p = strsep(&cur, ":")) ) {
23358f0484fSRodney W. Grimes 		/*
23458f0484fSRodney W. Grimes 		 * It's a SHELL path -- double, leading and trailing colons
23558f0484fSRodney W. Grimes 		 * mean the current directory.
23658f0484fSRodney W. Grimes 		 */
23758f0484fSRodney W. Grimes 		if (!*p) {
23858f0484fSRodney W. Grimes 			p = ".";
23958f0484fSRodney W. Grimes 			lp = 1;
24058f0484fSRodney W. Grimes 		} else
24158f0484fSRodney W. Grimes 			lp = strlen(p);
24258f0484fSRodney W. Grimes 		ln = strlen(name);
24358f0484fSRodney W. Grimes 
24458f0484fSRodney W. Grimes 		/*
24558f0484fSRodney W. Grimes 		 * If the path is too long complain.  This is a possible
24658f0484fSRodney W. Grimes 		 * security issue; given a way to make the path too long
24758f0484fSRodney W. Grimes 		 * the user may execute the wrong program.
24858f0484fSRodney W. Grimes 		 */
24958f0484fSRodney W. Grimes 		if (lp + ln + 2 > sizeof(buf)) {
25058f0484fSRodney W. Grimes 			(void)write(STDERR_FILENO, "execvp: ", 8);
25158f0484fSRodney W. Grimes 			(void)write(STDERR_FILENO, p, lp);
25258f0484fSRodney W. Grimes 			(void)write(STDERR_FILENO, ": path too long\n", 16);
25358f0484fSRodney W. Grimes 			continue;
25458f0484fSRodney W. Grimes 		}
25558f0484fSRodney W. Grimes 		bcopy(p, buf, lp);
25658f0484fSRodney W. Grimes 		buf[lp] = '/';
25758f0484fSRodney W. Grimes 		bcopy(name, buf + lp + 1, ln);
25858f0484fSRodney W. Grimes 		buf[lp + ln + 1] = '\0';
25958f0484fSRodney W. Grimes 
26058f0484fSRodney W. Grimes retry:		(void)execve(bp, argv, environ);
26158f0484fSRodney W. Grimes 		switch(errno) {
2621df595f2SBruce Evans 		case E2BIG:
2631df595f2SBruce Evans 			goto done;
2641df595f2SBruce Evans 		case ELOOP:
2651df595f2SBruce Evans 		case ENAMETOOLONG:
26658f0484fSRodney W. Grimes 		case ENOENT:
26758f0484fSRodney W. Grimes 			break;
26858f0484fSRodney W. Grimes 		case ENOEXEC:
269a2c06222SBruce Evans 			for (cnt = 0; argv[cnt]; ++cnt)
270a2c06222SBruce Evans 				;
271a2c06222SBruce Evans 			memp = malloc((cnt + 2) * sizeof(char *));
272a2c06222SBruce Evans 			if (memp == NULL)
27358f0484fSRodney W. Grimes 				goto done;
27458f0484fSRodney W. Grimes 			memp[0] = "sh";
27558f0484fSRodney W. Grimes 			memp[1] = bp;
27658f0484fSRodney W. Grimes 			bcopy(argv + 1, memp + 2, cnt * sizeof(char *));
27758f0484fSRodney W. Grimes 			(void)execve(_PATH_BSHELL, memp, environ);
278a2c06222SBruce Evans 			free(memp);
27958f0484fSRodney W. Grimes 			goto done;
2801df595f2SBruce Evans 		case ENOMEM:
2811df595f2SBruce Evans 			goto done;
2821df595f2SBruce Evans 		case ENOTDIR:
2831df595f2SBruce Evans 			break;
28458f0484fSRodney W. Grimes 		case ETXTBSY:
2851df595f2SBruce Evans 			/*
2861df595f2SBruce Evans 			 * We used to retry here, but sh(1) doesn't.
2871df595f2SBruce Evans 			 */
2881df595f2SBruce Evans 			goto done;
28958f0484fSRodney W. Grimes 		default:
2901df595f2SBruce Evans 			/*
2911df595f2SBruce Evans 			 * EACCES may be for an inaccessible directory or
2921df595f2SBruce Evans 			 * a non-executable file.  Call stat() to decide
2931df595f2SBruce Evans 			 * which.  This also handles ambiguities for EFAULT
2941df595f2SBruce Evans 			 * and EIO, and undocumented errors like ESTALE.
2951df595f2SBruce Evans 			 * We hope that the race for a stat() is unimportant.
2961df595f2SBruce Evans 			 */
2971df595f2SBruce Evans 			save_errno = errno;
29870df31a6SBruce Evans 			if (stat(bp, &sb) != 0)
2991df595f2SBruce Evans 				break;
3001df595f2SBruce Evans 			if (save_errno == EACCES) {
3011df595f2SBruce Evans 				eacces = 1;
3021df595f2SBruce Evans 				continue;
3031df595f2SBruce Evans 			}
3041df595f2SBruce Evans 			errno = save_errno;
30558f0484fSRodney W. Grimes 			goto done;
30658f0484fSRodney W. Grimes 		}
30758f0484fSRodney W. Grimes 	}
30858f0484fSRodney W. Grimes 	if (eacces)
30958f0484fSRodney W. Grimes 		errno = EACCES;
3101df595f2SBruce Evans 	else
31158f0484fSRodney W. Grimes 		errno = ENOENT;
31258f0484fSRodney W. Grimes done:	if (path)
31358f0484fSRodney W. Grimes 		free(path);
31458f0484fSRodney W. Grimes 	return (-1);
31558f0484fSRodney W. Grimes }
316