xref: /freebsd/lib/libc/gen/exec.c (revision 8ccd0b876e67fda6249f294ff484798cc1e1569f)
158f0484fSRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
458f0484fSRodney W. Grimes  * Copyright (c) 1991, 1993
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
858f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
958f0484fSRodney W. Grimes  * are met:
1058f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1258f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1458f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1658f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1758f0484fSRodney W. Grimes  *    without specific prior written permission.
1858f0484fSRodney W. Grimes  *
1958f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2058f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2158f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2258f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2358f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2458f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2558f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2658f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2758f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2858f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2958f0484fSRodney W. Grimes  * SUCH DAMAGE.
3058f0484fSRodney W. Grimes  */
3158f0484fSRodney W. Grimes 
32d201fe46SDaniel Eischen #include "namespace.h"
3358f0484fSRodney W. Grimes #include <sys/param.h>
341df595f2SBruce Evans #include <sys/stat.h>
3558f0484fSRodney W. Grimes #include <errno.h>
3658f0484fSRodney W. Grimes #include <unistd.h>
3758f0484fSRodney W. Grimes #include <stdlib.h>
3858f0484fSRodney W. Grimes #include <string.h>
3958f0484fSRodney W. Grimes #include <stdio.h>
4058f0484fSRodney W. Grimes #include <paths.h>
4158f0484fSRodney W. Grimes 
4258f0484fSRodney W. Grimes #include <stdarg.h>
43d201fe46SDaniel Eischen #include "un-namespace.h"
44c605eea9SEd Schouten #include "libc_private.h"
4558f0484fSRodney W. Grimes 
46f0fbdf1fSKyle Evans static const char execvPe_err_preamble[] = "execvP: ";
47f0fbdf1fSKyle Evans static const char execvPe_err_trailer[] = ": path too long\n";
48f0fbdf1fSKyle Evans 
4958f0484fSRodney W. Grimes int
execl(const char * name,const char * arg,...)5058f0484fSRodney W. Grimes execl(const char *name, const char *arg, ...)
5158f0484fSRodney W. Grimes {
5258f0484fSRodney W. Grimes 	va_list ap;
53bbbe3054SEd Schouten 	const char **argv;
54dea625c8SDag-Erling Smørgrav 	int n;
55dea625c8SDag-Erling Smørgrav 
5658f0484fSRodney W. Grimes 	va_start(ap, arg);
577ea577e5SDag-Erling Smørgrav 	n = 1;
58dea625c8SDag-Erling Smørgrav 	while (va_arg(ap, char *) != NULL)
59dea625c8SDag-Erling Smørgrav 		n++;
6058f0484fSRodney W. Grimes 	va_end(ap);
61af806462SDag-Erling Smørgrav 	argv = alloca((n + 1) * sizeof(*argv));
62ce04fea4SPeter Wemm 	if (argv == NULL) {
63ce04fea4SPeter Wemm 		errno = ENOMEM;
6458f0484fSRodney W. Grimes 		return (-1);
65ce04fea4SPeter Wemm 	}
66dea625c8SDag-Erling Smørgrav 	va_start(ap, arg);
677ea577e5SDag-Erling Smørgrav 	n = 1;
68bbbe3054SEd Schouten 	argv[0] = arg;
69dea625c8SDag-Erling Smørgrav 	while ((argv[n] = va_arg(ap, char *)) != NULL)
70dea625c8SDag-Erling Smørgrav 		n++;
71dea625c8SDag-Erling Smørgrav 	va_end(ap);
72bbbe3054SEd Schouten 	return (_execve(name, __DECONST(char **, argv), environ));
7358f0484fSRodney W. Grimes }
7458f0484fSRodney W. Grimes 
7558f0484fSRodney W. Grimes int
execle(const char * name,const char * arg,...)7658f0484fSRodney W. Grimes execle(const char *name, const char *arg, ...)
7758f0484fSRodney W. Grimes {
7858f0484fSRodney W. Grimes 	va_list ap;
79bbbe3054SEd Schouten 	const char **argv;
80bbbe3054SEd Schouten 	char **envp;
81af806462SDag-Erling Smørgrav 	int n;
8258f0484fSRodney W. Grimes 
8358f0484fSRodney W. Grimes 	va_start(ap, arg);
84af806462SDag-Erling Smørgrav 	n = 1;
85af806462SDag-Erling Smørgrav 	while (va_arg(ap, char *) != NULL)
86af806462SDag-Erling Smørgrav 		n++;
8758f0484fSRodney W. Grimes 	va_end(ap);
88af806462SDag-Erling Smørgrav 	argv = alloca((n + 1) * sizeof(*argv));
89ce04fea4SPeter Wemm 	if (argv == NULL) {
90ce04fea4SPeter Wemm 		errno = ENOMEM;
9158f0484fSRodney W. Grimes 		return (-1);
92ce04fea4SPeter Wemm 	}
93af806462SDag-Erling Smørgrav 	va_start(ap, arg);
94af806462SDag-Erling Smørgrav 	n = 1;
95bbbe3054SEd Schouten 	argv[0] = arg;
96af806462SDag-Erling Smørgrav 	while ((argv[n] = va_arg(ap, char *)) != NULL)
97af806462SDag-Erling Smørgrav 		n++;
98af806462SDag-Erling Smørgrav 	envp = va_arg(ap, char **);
99af806462SDag-Erling Smørgrav 	va_end(ap);
100bbbe3054SEd Schouten 	return (_execve(name, __DECONST(char **, argv), envp));
10158f0484fSRodney W. Grimes }
10258f0484fSRodney W. Grimes 
10358f0484fSRodney W. Grimes int
execlp(const char * name,const char * arg,...)10458f0484fSRodney W. Grimes execlp(const char *name, const char *arg, ...)
10558f0484fSRodney W. Grimes {
10658f0484fSRodney W. Grimes 	va_list ap;
107bbbe3054SEd Schouten 	const char **argv;
108ce04fea4SPeter Wemm 	int n;
10958f0484fSRodney W. Grimes 
11058f0484fSRodney W. Grimes 	va_start(ap, arg);
111ce04fea4SPeter Wemm 	n = 1;
112ce04fea4SPeter Wemm 	while (va_arg(ap, char *) != NULL)
113ce04fea4SPeter Wemm 		n++;
11458f0484fSRodney W. Grimes 	va_end(ap);
115ce04fea4SPeter Wemm 	argv = alloca((n + 1) * sizeof(*argv));
116ce04fea4SPeter Wemm 	if (argv == NULL) {
117ce04fea4SPeter Wemm 		errno = ENOMEM;
11858f0484fSRodney W. Grimes 		return (-1);
11958f0484fSRodney W. Grimes 	}
120ce04fea4SPeter Wemm 	va_start(ap, arg);
121ce04fea4SPeter Wemm 	n = 1;
122bbbe3054SEd Schouten 	argv[0] = arg;
123ce04fea4SPeter Wemm 	while ((argv[n] = va_arg(ap, char *)) != NULL)
124ce04fea4SPeter Wemm 		n++;
125ce04fea4SPeter Wemm 	va_end(ap);
126bbbe3054SEd Schouten 	return (execvp(name, __DECONST(char **, argv)));
127ce04fea4SPeter Wemm }
12858f0484fSRodney W. Grimes 
12958f0484fSRodney W. Grimes int
execv(const char * name,char * const * argv)13055b6b759SCraig Rodrigues execv(const char *name, char * const *argv)
13158f0484fSRodney W. Grimes {
132d201fe46SDaniel Eischen 	(void)_execve(name, argv, environ);
13358f0484fSRodney W. Grimes 	return (-1);
13458f0484fSRodney W. Grimes }
13558f0484fSRodney W. Grimes 
13658f0484fSRodney W. Grimes int
execvp(const char * name,char * const * argv)13709f49aabSGordon Tetlow execvp(const char *name, char * const *argv)
13809f49aabSGordon Tetlow {
139*8ccd0b87SBrooks Davis 	return (execvpe(name, argv, environ));
14009f49aabSGordon Tetlow }
14109f49aabSGordon Tetlow 
142947aa542SDavid Xu static int
execvPe(const char * name,const char * path,char * const * argv,char * const * envp)143bbbe3054SEd Schouten execvPe(const char *name, const char *path, char * const *argv,
144bbbe3054SEd Schouten     char * const *envp)
14558f0484fSRodney W. Grimes {
146bbbe3054SEd Schouten 	const char **memp;
147bbbe3054SEd Schouten 	size_t cnt, lp, ln;
1481df595f2SBruce Evans 	int eacces, save_errno;
149f0fbdf1fSKyle Evans 	char buf[MAXPATHLEN];
150f0fbdf1fSKyle Evans 	const char *bp, *np, *op, *p;
1511df595f2SBruce Evans 	struct stat sb;
15258f0484fSRodney W. Grimes 
1531df595f2SBruce Evans 	eacces = 0;
1541ad652a5SBruce Evans 
15558f0484fSRodney W. Grimes 	/* If it's an absolute or relative path name, it's easy. */
156b3608ae1SEd Schouten 	if (strchr(name, '/')) {
157bbbe3054SEd Schouten 		bp = name;
158f0fbdf1fSKyle Evans 		op = NULL;
15958f0484fSRodney W. Grimes 		goto retry;
16058f0484fSRodney W. Grimes 	}
16158f0484fSRodney W. Grimes 	bp = buf;
16258f0484fSRodney W. Grimes 
16376663101SBruce Evans 	/* If it's an empty path name, fail in the usual POSIX way. */
16476663101SBruce Evans 	if (*name == '\0') {
16576663101SBruce Evans 		errno = ENOENT;
16676663101SBruce Evans 		return (-1);
16776663101SBruce Evans 	}
16876663101SBruce Evans 
169f0fbdf1fSKyle Evans 	op = path;
170f0fbdf1fSKyle Evans 	ln = strlen(name);
171f0fbdf1fSKyle Evans 	while (op != NULL) {
172f0fbdf1fSKyle Evans 		np = strchrnul(op, ':');
173f0fbdf1fSKyle Evans 
17458f0484fSRodney W. Grimes 		/*
17558f0484fSRodney W. Grimes 		 * It's a SHELL path -- double, leading and trailing colons
17658f0484fSRodney W. Grimes 		 * mean the current directory.
17758f0484fSRodney W. Grimes 		 */
178f0fbdf1fSKyle Evans 		if (np == op) {
179f0fbdf1fSKyle Evans 			/* Empty component. */
18058f0484fSRodney W. Grimes 			p = ".";
18158f0484fSRodney W. Grimes 			lp = 1;
182f0fbdf1fSKyle Evans 		} else {
183f0fbdf1fSKyle Evans 			/* Non-empty component. */
184f0fbdf1fSKyle Evans 			p = op;
185f0fbdf1fSKyle Evans 			lp = np - op;
186f0fbdf1fSKyle Evans 		}
187f0fbdf1fSKyle Evans 
188f0fbdf1fSKyle Evans 		/* Advance to the next component or terminate after this. */
189f0fbdf1fSKyle Evans 		if (*np == '\0')
190f0fbdf1fSKyle Evans 			op = NULL;
191f0fbdf1fSKyle Evans 		else
192f0fbdf1fSKyle Evans 			op = np + 1;
19358f0484fSRodney W. Grimes 
19458f0484fSRodney W. Grimes 		/*
19558f0484fSRodney W. Grimes 		 * If the path is too long complain.  This is a possible
19658f0484fSRodney W. Grimes 		 * security issue; given a way to make the path too long
19758f0484fSRodney W. Grimes 		 * the user may execute the wrong program.
19858f0484fSRodney W. Grimes 		 */
19958f0484fSRodney W. Grimes 		if (lp + ln + 2 > sizeof(buf)) {
200f0fbdf1fSKyle Evans 			(void)_write(STDERR_FILENO, execvPe_err_preamble,
201f0fbdf1fSKyle Evans 			    sizeof(execvPe_err_preamble) - 1);
2029233c4d9SJason Evans 			(void)_write(STDERR_FILENO, p, lp);
203f0fbdf1fSKyle Evans 			(void)_write(STDERR_FILENO, execvPe_err_trailer,
204f0fbdf1fSKyle Evans 			    sizeof(execvPe_err_trailer) - 1);
20558f0484fSRodney W. Grimes 			continue;
20658f0484fSRodney W. Grimes 		}
20758f0484fSRodney W. Grimes 		bcopy(p, buf, lp);
20858f0484fSRodney W. Grimes 		buf[lp] = '/';
20958f0484fSRodney W. Grimes 		bcopy(name, buf + lp + 1, ln);
21058f0484fSRodney W. Grimes 		buf[lp + ln + 1] = '\0';
21158f0484fSRodney W. Grimes 
212f67d07f0SEd Schouten retry:		(void)_execve(bp, argv, envp);
21358f0484fSRodney W. Grimes 		switch (errno) {
2141df595f2SBruce Evans 		case E2BIG:
2151df595f2SBruce Evans 			goto done;
2161df595f2SBruce Evans 		case ELOOP:
2171df595f2SBruce Evans 		case ENAMETOOLONG:
21858f0484fSRodney W. Grimes 		case ENOENT:
21958f0484fSRodney W. Grimes 			break;
22058f0484fSRodney W. Grimes 		case ENOEXEC:
221a2c06222SBruce Evans 			for (cnt = 0; argv[cnt]; ++cnt)
222a2c06222SBruce Evans 				;
223301cb491SKyle Evans 
224301cb491SKyle Evans 			/*
225301cb491SKyle Evans 			 * cnt may be 0 above; always allocate at least
226301cb491SKyle Evans 			 * 3 entries so that we can at least fit "sh", bp, and
227301cb491SKyle Evans 			 * the NULL terminator.  We can rely on cnt to take into
228301cb491SKyle Evans 			 * account the NULL terminator in all other scenarios,
229301cb491SKyle Evans 			 * as we drop argv[0].
230301cb491SKyle Evans 			 */
231301cb491SKyle Evans 			memp = alloca(MAX(3, cnt + 2) * sizeof(char *));
232ce04fea4SPeter Wemm 			if (memp == NULL) {
233ce04fea4SPeter Wemm 				/* errno = ENOMEM; XXX override ENOEXEC? */
23458f0484fSRodney W. Grimes 				goto done;
235ce04fea4SPeter Wemm 			}
236301cb491SKyle Evans 			if (cnt > 0) {
237301cb491SKyle Evans 				memp[0] = argv[0];
23858f0484fSRodney W. Grimes 				memp[1] = bp;
23958f0484fSRodney W. Grimes 				bcopy(argv + 1, memp + 2, cnt * sizeof(char *));
240301cb491SKyle Evans 			} else {
241301cb491SKyle Evans 				memp[0] = "sh";
242301cb491SKyle Evans 				memp[1] = bp;
243301cb491SKyle Evans 				memp[2] = NULL;
244301cb491SKyle Evans 			}
245bbbe3054SEd Schouten  			(void)_execve(_PATH_BSHELL,
246bbbe3054SEd Schouten 			    __DECONST(char **, memp), envp);
24758f0484fSRodney W. Grimes 			goto done;
2481df595f2SBruce Evans 		case ENOMEM:
2491df595f2SBruce Evans 			goto done;
2501df595f2SBruce Evans 		case ENOTDIR:
2511df595f2SBruce Evans 			break;
25258f0484fSRodney W. Grimes 		case ETXTBSY:
2531df595f2SBruce Evans 			/*
2541df595f2SBruce Evans 			 * We used to retry here, but sh(1) doesn't.
2551df595f2SBruce Evans 			 */
2561df595f2SBruce Evans 			goto done;
25758f0484fSRodney W. Grimes 		default:
2581df595f2SBruce Evans 			/*
2591df595f2SBruce Evans 			 * EACCES may be for an inaccessible directory or
2601df595f2SBruce Evans 			 * a non-executable file.  Call stat() to decide
2611df595f2SBruce Evans 			 * which.  This also handles ambiguities for EFAULT
2621df595f2SBruce Evans 			 * and EIO, and undocumented errors like ESTALE.
2631df595f2SBruce Evans 			 * We hope that the race for a stat() is unimportant.
2641df595f2SBruce Evans 			 */
2651df595f2SBruce Evans 			save_errno = errno;
26670df31a6SBruce Evans 			if (stat(bp, &sb) != 0)
2671df595f2SBruce Evans 				break;
2681df595f2SBruce Evans 			if (save_errno == EACCES) {
2691df595f2SBruce Evans 				eacces = 1;
2701df595f2SBruce Evans 				continue;
2711df595f2SBruce Evans 			}
2721df595f2SBruce Evans 			errno = save_errno;
27358f0484fSRodney W. Grimes 			goto done;
27458f0484fSRodney W. Grimes 		}
27558f0484fSRodney W. Grimes 	}
27658f0484fSRodney W. Grimes 	if (eacces)
27758f0484fSRodney W. Grimes 		errno = EACCES;
2781df595f2SBruce Evans 	else
27958f0484fSRodney W. Grimes 		errno = ENOENT;
280ce04fea4SPeter Wemm done:
28158f0484fSRodney W. Grimes 	return (-1);
28258f0484fSRodney W. Grimes }
283947aa542SDavid Xu 
284947aa542SDavid Xu int
execvP(const char * name,const char * path,char * const argv[])285947aa542SDavid Xu execvP(const char *name, const char *path, char * const argv[])
286947aa542SDavid Xu {
287947aa542SDavid Xu 	return execvPe(name, path, argv, environ);
288947aa542SDavid Xu }
289947aa542SDavid Xu 
290947aa542SDavid Xu int
execvpe(const char * name,char * const argv[],char * const envp[])291*8ccd0b87SBrooks Davis execvpe(const char *name, char * const argv[], char * const envp[])
292947aa542SDavid Xu {
293947aa542SDavid Xu 	const char *path;
294947aa542SDavid Xu 
295947aa542SDavid Xu 	/* Get the path we're searching. */
296947aa542SDavid Xu 	if ((path = getenv("PATH")) == NULL)
297947aa542SDavid Xu 		path = _PATH_DEFPATH;
298947aa542SDavid Xu 
299947aa542SDavid Xu 	return (execvPe(name, path, argv, envp));
300947aa542SDavid Xu }
301