xref: /minix3/lib/libc/gen/execvp.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: execvp.c,v 1.31 2014/09/26 19:28:03 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras  * Copyright (c) 1991, 1993
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras  * are met:
102fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras  *    without specific prior written permission.
182fe8fb19SBen Gras  *
192fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras  * SUCH DAMAGE.
302fe8fb19SBen Gras  */
312fe8fb19SBen Gras 
322fe8fb19SBen Gras #include <sys/cdefs.h>
332fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
342fe8fb19SBen Gras #if 0
352fe8fb19SBen Gras static char sccsid[] = "@(#)exec.c	8.1 (Berkeley) 6/4/93";
362fe8fb19SBen Gras #else
37*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: execvp.c,v 1.31 2014/09/26 19:28:03 christos Exp $");
382fe8fb19SBen Gras #endif
392fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
402fe8fb19SBen Gras 
412fe8fb19SBen Gras #include "namespace.h"
422fe8fb19SBen Gras #include <assert.h>
432fe8fb19SBen Gras #include <errno.h>
442fe8fb19SBen Gras #include <stdio.h>
452fe8fb19SBen Gras #include <stdlib.h>
462fe8fb19SBen Gras #include <string.h>
472fe8fb19SBen Gras #include <limits.h>
482fe8fb19SBen Gras #include <unistd.h>
492fe8fb19SBen Gras #include <paths.h>
502fe8fb19SBen Gras #include "reentrant.h"
512fe8fb19SBen Gras 
522fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(execvp,_execvp)532fe8fb19SBen Gras __weak_alias(execvp,_execvp)
54*0a6a1f1dSLionel Sambuc __weak_alias(execvpe,_execvpe)
552fe8fb19SBen Gras #endif
562fe8fb19SBen Gras 
572fe8fb19SBen Gras int
58*0a6a1f1dSLionel Sambuc execvpe(const char *name, char * const *argv, char * const * envp)
592fe8fb19SBen Gras {
602fe8fb19SBen Gras 	const char **memp;
612fe8fb19SBen Gras 	int cnt;
622fe8fb19SBen Gras 	size_t lp, ln;
632fe8fb19SBen Gras 	int eacces = 0;
642fe8fb19SBen Gras 	unsigned int etxtbsy = 0;
652fe8fb19SBen Gras 	char buf[PATH_MAX];
662fe8fb19SBen Gras 	const char *bp, *path, *p;
672fe8fb19SBen Gras 
682fe8fb19SBen Gras 	_DIAGASSERT(name != NULL);
692fe8fb19SBen Gras 
702fe8fb19SBen Gras 	/* "" is not a valid filename; check this before traversing PATH. */
712fe8fb19SBen Gras 	if (name[0] == '\0') {
722fe8fb19SBen Gras 		errno = ENOENT;
732fe8fb19SBen Gras 		goto done;
742fe8fb19SBen Gras 	}
752fe8fb19SBen Gras 	ln = strlen(name);
762fe8fb19SBen Gras 	/* If it's an absolute or relative path name, it's easy. */
772fe8fb19SBen Gras 	if (strchr(name, '/')) {
782fe8fb19SBen Gras 		bp = name;
792fe8fb19SBen Gras 		path = "";
802fe8fb19SBen Gras 		goto retry;
812fe8fb19SBen Gras 	}
822fe8fb19SBen Gras 	bp = buf;
832fe8fb19SBen Gras 
842fe8fb19SBen Gras 	/* Get the path we're searching. */
852fe8fb19SBen Gras 	if (!(path = getenv("PATH")))
862fe8fb19SBen Gras 		path = _PATH_DEFPATH;
872fe8fb19SBen Gras 
882fe8fb19SBen Gras 	do {
892fe8fb19SBen Gras 		/* Find the end of this path element. */
902fe8fb19SBen Gras 		for (p = path; *path != 0 && *path != ':'; path++)
912fe8fb19SBen Gras 			continue;
922fe8fb19SBen Gras 		/*
932fe8fb19SBen Gras 		 * It's a SHELL path -- double, leading and trailing colons
942fe8fb19SBen Gras 		 * mean the current directory.
952fe8fb19SBen Gras 		 */
962fe8fb19SBen Gras 		if (p == path) {
972fe8fb19SBen Gras 			p = ".";
982fe8fb19SBen Gras 			lp = 1;
992fe8fb19SBen Gras 		} else
1002fe8fb19SBen Gras 			lp = path - p;
1012fe8fb19SBen Gras 
1022fe8fb19SBen Gras 		/*
1032fe8fb19SBen Gras 		 * If the path is too long complain.  This is a possible
1042fe8fb19SBen Gras 		 * security issue; given a way to make the path too long
1052fe8fb19SBen Gras 		 * the user may execute the wrong program.
1062fe8fb19SBen Gras 		 */
1072fe8fb19SBen Gras 		if (lp + ln + 2 > sizeof(buf)) {
1082fe8fb19SBen Gras 			(void)write(STDERR_FILENO, "execvp: ", 8);
1092fe8fb19SBen Gras 			(void)write(STDERR_FILENO, p, lp);
1102fe8fb19SBen Gras 			(void)write(STDERR_FILENO, ": path too long\n", 16);
1112fe8fb19SBen Gras 			continue;
1122fe8fb19SBen Gras 		}
1132fe8fb19SBen Gras 		memcpy(buf, p, lp);
1142fe8fb19SBen Gras 		buf[lp] = '/';
1152fe8fb19SBen Gras 		memcpy(buf + lp + 1, name, ln);
1162fe8fb19SBen Gras 		buf[lp + ln + 1] = '\0';
1172fe8fb19SBen Gras 
118*0a6a1f1dSLionel Sambuc retry:		(void)execve(bp, argv, envp);
1192fe8fb19SBen Gras 		switch (errno) {
1202fe8fb19SBen Gras 		case EACCES:
1212fe8fb19SBen Gras 			eacces = 1;
1222fe8fb19SBen Gras 			break;
1232fe8fb19SBen Gras 		case ENOTDIR:
1242fe8fb19SBen Gras 		case ENOENT:
1252fe8fb19SBen Gras 			break;
1262fe8fb19SBen Gras 		case ENOEXEC:
1272fe8fb19SBen Gras 			for (cnt = 0; argv[cnt] != NULL; ++cnt)
1282fe8fb19SBen Gras 				continue;
1292fe8fb19SBen Gras 			/*
1302fe8fb19SBen Gras 			 * we can't use malloc here because, if we are doing
1312fe8fb19SBen Gras 			 * vfork+exec, it leaks memory in the parent.
1322fe8fb19SBen Gras 			 */
1332fe8fb19SBen Gras 			if ((memp = alloca((cnt + 2) * sizeof(*memp))) == NULL)
1342fe8fb19SBen Gras 				goto done;
1352fe8fb19SBen Gras 			memp[0] = _PATH_BSHELL;
1362fe8fb19SBen Gras 			memp[1] = bp;
1372fe8fb19SBen Gras 			(void)memcpy(&memp[2], &argv[1], cnt * sizeof(*memp));
138*0a6a1f1dSLionel Sambuc 			(void)execve(_PATH_BSHELL, __UNCONST(memp), envp);
1392fe8fb19SBen Gras 			goto done;
1402fe8fb19SBen Gras 		case ETXTBSY:
1412fe8fb19SBen Gras 			if (etxtbsy < 3)
1422fe8fb19SBen Gras 				(void)sleep(++etxtbsy);
1432fe8fb19SBen Gras 			goto retry;
1442fe8fb19SBen Gras 		default:
1452fe8fb19SBen Gras 			goto done;
1462fe8fb19SBen Gras 		}
1472fe8fb19SBen Gras 	} while (*path++ == ':');	/* Otherwise, *path was NUL */
1482fe8fb19SBen Gras 	if (eacces)
1492fe8fb19SBen Gras 		errno = EACCES;
1502fe8fb19SBen Gras 	else if (!errno)
1512fe8fb19SBen Gras 		errno = ENOENT;
1522fe8fb19SBen Gras done:
1532fe8fb19SBen Gras 	return (-1);
1542fe8fb19SBen Gras }
155*0a6a1f1dSLionel Sambuc 
156*0a6a1f1dSLionel Sambuc extern char **environ;
157*0a6a1f1dSLionel Sambuc 
158*0a6a1f1dSLionel Sambuc int
execvp(const char * name,char * const * argv)159*0a6a1f1dSLionel Sambuc execvp(const char *name, char * const *argv)
160*0a6a1f1dSLionel Sambuc {
161*0a6a1f1dSLionel Sambuc 	return execvpe(name, argv, environ);
162*0a6a1f1dSLionel Sambuc }
163