Home | History | Annotate | Line # | Download | only in gen
execvp.c revision 1.30.16.2
      1  1.30.16.2  yamt /*	$NetBSD: execvp.c,v 1.30.16.2 2007/07/20 12:41:08 yamt Exp $	*/
      2  1.30.16.2  yamt 
      3  1.30.16.2  yamt /*-
      4  1.30.16.2  yamt  * Copyright (c) 1991, 1993
      5  1.30.16.2  yamt  *	The Regents of the University of California.  All rights reserved.
      6  1.30.16.2  yamt  *
      7  1.30.16.2  yamt  * Redistribution and use in source and binary forms, with or without
      8  1.30.16.2  yamt  * modification, are permitted provided that the following conditions
      9  1.30.16.2  yamt  * are met:
     10  1.30.16.2  yamt  * 1. Redistributions of source code must retain the above copyright
     11  1.30.16.2  yamt  *    notice, this list of conditions and the following disclaimer.
     12  1.30.16.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.30.16.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     14  1.30.16.2  yamt  *    documentation and/or other materials provided with the distribution.
     15  1.30.16.2  yamt  * 3. Neither the name of the University nor the names of its contributors
     16  1.30.16.2  yamt  *    may be used to endorse or promote products derived from this software
     17  1.30.16.2  yamt  *    without specific prior written permission.
     18  1.30.16.2  yamt  *
     19  1.30.16.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.30.16.2  yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.30.16.2  yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.30.16.2  yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.30.16.2  yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.30.16.2  yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.30.16.2  yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.30.16.2  yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.30.16.2  yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.30.16.2  yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.30.16.2  yamt  * SUCH DAMAGE.
     30  1.30.16.2  yamt  */
     31  1.30.16.2  yamt 
     32  1.30.16.2  yamt #include <sys/cdefs.h>
     33  1.30.16.2  yamt #if defined(LIBC_SCCS) && !defined(lint)
     34  1.30.16.2  yamt #if 0
     35  1.30.16.2  yamt static char sccsid[] = "@(#)exec.c	8.1 (Berkeley) 6/4/93";
     36  1.30.16.2  yamt #else
     37  1.30.16.2  yamt __RCSID("$NetBSD: execvp.c,v 1.30.16.2 2007/07/20 12:41:08 yamt Exp $");
     38  1.30.16.2  yamt #endif
     39  1.30.16.2  yamt #endif /* LIBC_SCCS and not lint */
     40  1.30.16.2  yamt 
     41  1.30.16.2  yamt #include "namespace.h"
     42  1.30.16.2  yamt #include <assert.h>
     43  1.30.16.2  yamt #include <errno.h>
     44  1.30.16.2  yamt #include <stdio.h>
     45  1.30.16.2  yamt #include <stdlib.h>
     46  1.30.16.2  yamt #include <string.h>
     47  1.30.16.2  yamt #include <limits.h>
     48  1.30.16.2  yamt #include <unistd.h>
     49  1.30.16.2  yamt #include <paths.h>
     50  1.30.16.2  yamt #include "reentrant.h"
     51  1.30.16.2  yamt 
     52  1.30.16.2  yamt #ifdef __weak_alias
     53  1.30.16.2  yamt __weak_alias(execvp,_execvp)
     54  1.30.16.2  yamt #endif
     55  1.30.16.2  yamt 
     56  1.30.16.2  yamt extern char **environ;
     57  1.30.16.2  yamt 
     58  1.30.16.2  yamt int
     59  1.30.16.2  yamt execvp(const char *name, char * const *argv)
     60  1.30.16.2  yamt {
     61  1.30.16.2  yamt 	const char **memp;
     62  1.30.16.2  yamt 	int cnt;
     63  1.30.16.2  yamt 	size_t lp, ln;
     64  1.30.16.2  yamt 	int eacces = 0;
     65  1.30.16.2  yamt 	unsigned int etxtbsy = 0;
     66  1.30.16.2  yamt 	char buf[PATH_MAX];
     67  1.30.16.2  yamt 	const char *bp, *path, *p;
     68  1.30.16.2  yamt 
     69  1.30.16.2  yamt 	_DIAGASSERT(name != NULL);
     70  1.30.16.2  yamt 
     71  1.30.16.2  yamt 	/* "" is not a valid filename; check this before traversing PATH. */
     72  1.30.16.2  yamt 	if (name[0] == '\0') {
     73  1.30.16.2  yamt 		errno = ENOENT;
     74  1.30.16.2  yamt 		goto done;
     75  1.30.16.2  yamt 	}
     76  1.30.16.2  yamt 	ln = strlen(name);
     77  1.30.16.2  yamt 	/* If it's an absolute or relative path name, it's easy. */
     78  1.30.16.2  yamt 	if (strchr(name, '/')) {
     79  1.30.16.2  yamt 		bp = name;
     80  1.30.16.2  yamt 		path = "";
     81  1.30.16.2  yamt 		goto retry;
     82  1.30.16.2  yamt 	}
     83  1.30.16.2  yamt 	bp = buf;
     84  1.30.16.2  yamt 
     85  1.30.16.2  yamt 	/* Get the path we're searching. */
     86  1.30.16.2  yamt 	if (!(path = getenv("PATH")))
     87  1.30.16.2  yamt 		path = _PATH_DEFPATH;
     88  1.30.16.2  yamt 
     89  1.30.16.2  yamt 	do {
     90  1.30.16.2  yamt 		/* Find the end of this path element. */
     91  1.30.16.2  yamt 		for (p = path; *path != 0 && *path != ':'; path++)
     92  1.30.16.2  yamt 			continue;
     93  1.30.16.2  yamt 		/*
     94  1.30.16.2  yamt 		 * It's a SHELL path -- double, leading and trailing colons
     95  1.30.16.2  yamt 		 * mean the current directory.
     96  1.30.16.2  yamt 		 */
     97  1.30.16.2  yamt 		if (p == path) {
     98  1.30.16.2  yamt 			p = ".";
     99  1.30.16.2  yamt 			lp = 1;
    100  1.30.16.2  yamt 		} else
    101  1.30.16.2  yamt 			lp = path - p;
    102  1.30.16.2  yamt 
    103  1.30.16.2  yamt 		/*
    104  1.30.16.2  yamt 		 * If the path is too long complain.  This is a possible
    105  1.30.16.2  yamt 		 * security issue; given a way to make the path too long
    106  1.30.16.2  yamt 		 * the user may execute the wrong program.
    107  1.30.16.2  yamt 		 */
    108  1.30.16.2  yamt 		if (lp + ln + 2 > sizeof(buf)) {
    109  1.30.16.2  yamt 			(void)write(STDERR_FILENO, "execvp: ", 8);
    110  1.30.16.2  yamt 			(void)write(STDERR_FILENO, p, lp);
    111  1.30.16.2  yamt 			(void)write(STDERR_FILENO, ": path too long\n", 16);
    112  1.30.16.2  yamt 			continue;
    113  1.30.16.2  yamt 		}
    114  1.30.16.2  yamt 		memcpy(buf, p, lp);
    115  1.30.16.2  yamt 		buf[lp] = '/';
    116  1.30.16.2  yamt 		memcpy(buf + lp + 1, name, ln);
    117  1.30.16.2  yamt 		buf[lp + ln + 1] = '\0';
    118  1.30.16.2  yamt 
    119  1.30.16.2  yamt retry:		(void)execve(bp, argv, environ);
    120  1.30.16.2  yamt 		switch (errno) {
    121  1.30.16.2  yamt 		case EACCES:
    122  1.30.16.2  yamt 			eacces = 1;
    123  1.30.16.2  yamt 			break;
    124  1.30.16.2  yamt 		case ENOTDIR:
    125  1.30.16.2  yamt 		case ENOENT:
    126  1.30.16.2  yamt 			break;
    127  1.30.16.2  yamt 		case ENOEXEC:
    128  1.30.16.2  yamt 			for (cnt = 0; argv[cnt] != NULL; ++cnt)
    129  1.30.16.2  yamt 				continue;
    130  1.30.16.2  yamt 			/*
    131  1.30.16.2  yamt 			 * we can't use malloc here because, if we are doing
    132  1.30.16.2  yamt 			 * vfork+exec, it leaks memory in the parent.
    133  1.30.16.2  yamt 			 */
    134  1.30.16.2  yamt 			if ((memp = alloca((cnt + 2) * sizeof(*memp))) == NULL)
    135  1.30.16.2  yamt 				goto done;
    136  1.30.16.2  yamt 			memp[0] = _PATH_BSHELL;
    137  1.30.16.2  yamt 			memp[1] = bp;
    138  1.30.16.2  yamt 			(void)memcpy(&memp[2], &argv[1], cnt * sizeof(*memp));
    139  1.30.16.2  yamt 			(void)execve(_PATH_BSHELL, __UNCONST(memp), environ);
    140  1.30.16.2  yamt 			goto done;
    141  1.30.16.2  yamt 		case ETXTBSY:
    142  1.30.16.2  yamt 			if (etxtbsy < 3)
    143  1.30.16.2  yamt 				(void)sleep(++etxtbsy);
    144  1.30.16.2  yamt 			goto retry;
    145  1.30.16.2  yamt 		default:
    146  1.30.16.2  yamt 			goto done;
    147  1.30.16.2  yamt 		}
    148  1.30.16.2  yamt 	} while (*path++ == ':');	/* Otherwise, *path was NUL */
    149  1.30.16.2  yamt 	if (eacces)
    150  1.30.16.2  yamt 		errno = EACCES;
    151  1.30.16.2  yamt 	else if (!errno)
    152  1.30.16.2  yamt 		errno = ENOENT;
    153  1.30.16.2  yamt done:
    154  1.30.16.2  yamt 	return (-1);
    155  1.30.16.2  yamt }
    156