xref: /onnv-gate/usr/src/lib/libc/port/gen/execvp.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  *	execlp(name, arg,...,0)	(like execl, but does path search)
340Sstevel@tonic-gate  *	execvp(name, argv)	(like execv, but does path search)
350Sstevel@tonic-gate  */
36*6812Sraf 
37*6812Sraf #pragma weak _execlp = execlp
38*6812Sraf #pragma weak _execvp = execvp
39*6812Sraf 
40*6812Sraf #include "lint.h"
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include <unistd.h>
430Sstevel@tonic-gate #include <string.h>
440Sstevel@tonic-gate #include <alloca.h>
450Sstevel@tonic-gate #include <errno.h>
460Sstevel@tonic-gate #include <limits.h>
470Sstevel@tonic-gate #include <stdarg.h>
480Sstevel@tonic-gate #include <stdlib.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate static const char *execat(const char *, const char *, char *);
510Sstevel@tonic-gate 
520Sstevel@tonic-gate extern  int __xpg4;	/* defined in xpg4.c; 0 if not xpg4-compiled program */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*VARARGS1*/
550Sstevel@tonic-gate int
execlp(const char * name,const char * arg0,...)560Sstevel@tonic-gate execlp(const char *name, const char *arg0, ...)
570Sstevel@tonic-gate {
580Sstevel@tonic-gate 	char **argp;
590Sstevel@tonic-gate 	va_list args;
600Sstevel@tonic-gate 	char **argvec;
610Sstevel@tonic-gate 	int err;
620Sstevel@tonic-gate 	int nargs = 0;
630Sstevel@tonic-gate 	char *nextarg;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	/*
660Sstevel@tonic-gate 	 * count the number of arguments in the variable argument list
670Sstevel@tonic-gate 	 * and allocate an argument vector for them on the stack,
680Sstevel@tonic-gate 	 * adding space for a terminating null pointer at the end
690Sstevel@tonic-gate 	 * and one additional space for argv[0] which is no longer
700Sstevel@tonic-gate 	 * counted by the varargs loop.
710Sstevel@tonic-gate 	 */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	va_start(args, arg0);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	while (va_arg(args, char *) != (char *)0)
760Sstevel@tonic-gate 		nargs++;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	va_end(args);
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	/*
810Sstevel@tonic-gate 	 * load the arguments in the variable argument list
820Sstevel@tonic-gate 	 * into the argument vector and add the terminating null pointer
830Sstevel@tonic-gate 	 */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	va_start(args, arg0);
860Sstevel@tonic-gate 	/* workaround for bugid 1242839 */
870Sstevel@tonic-gate 	argvec = alloca((size_t)((nargs + 2) * sizeof (char *)));
880Sstevel@tonic-gate 	nextarg = va_arg(args, char *);
890Sstevel@tonic-gate 	argp = argvec;
900Sstevel@tonic-gate 	*argp++ = (char *)arg0;
910Sstevel@tonic-gate 	while (nargs-- && nextarg != (char *)0) {
920Sstevel@tonic-gate 		*argp = nextarg;
930Sstevel@tonic-gate 		argp++;
940Sstevel@tonic-gate 		nextarg = va_arg(args, char *);
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 	va_end(args);
970Sstevel@tonic-gate 	*argp = (char *)0;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	/*
1000Sstevel@tonic-gate 	 * call execvp()
1010Sstevel@tonic-gate 	 */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	err = execvp(name, argvec);
1040Sstevel@tonic-gate 	return (err);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate int
execvp(const char * name,char * const * argv)1080Sstevel@tonic-gate execvp(const char *name, char *const *argv)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	const char	*pathstr;
1110Sstevel@tonic-gate 	char	fname[PATH_MAX+2];
1120Sstevel@tonic-gate 	char	*newargs[256];
1130Sstevel@tonic-gate 	int	i;
1140Sstevel@tonic-gate 	const char *cp;
1150Sstevel@tonic-gate 	unsigned etxtbsy = 1;
1160Sstevel@tonic-gate 	int eacces = 0;
1170Sstevel@tonic-gate 	char *shpath;
1180Sstevel@tonic-gate 	static const char *sun_path = "/bin/sh";
1190Sstevel@tonic-gate 	static const char *xpg4_path = "/usr/xpg4/bin/sh";
1200Sstevel@tonic-gate 	static const char *shell = "sh";
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	if (*name == '\0') {
1230Sstevel@tonic-gate 		errno = ENOENT;
1240Sstevel@tonic-gate 		return (-1);
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 	if ((pathstr = getenv("PATH")) == NULL) {
1270Sstevel@tonic-gate 		/*
1280Sstevel@tonic-gate 		 * XPG4:  pathstr is equivalent to CSPATH, except that
1290Sstevel@tonic-gate 		 * :/usr/sbin is appended when root, and pathstr must end
1300Sstevel@tonic-gate 		 * with a colon when not root.  Keep these paths in sync
1310Sstevel@tonic-gate 		 * with CSPATH in confstr.c.  Note that pathstr must end
1320Sstevel@tonic-gate 		 * with a colon when not root so that when name doesn't
1330Sstevel@tonic-gate 		 * contain '/', the last call to execat() will result in an
1340Sstevel@tonic-gate 		 * attempt to execv name from the current directory.
1350Sstevel@tonic-gate 		 */
1360Sstevel@tonic-gate 		if (geteuid() == 0 || getuid() == 0) {
1370Sstevel@tonic-gate 			if (__xpg4 == 0) {	/* not XPG4 */
1380Sstevel@tonic-gate 				pathstr = "/usr/sbin:/usr/ccs/bin:/usr/bin";
1390Sstevel@tonic-gate 			} else {		/* XPG4 (CSPATH + /usr/sbin) */
1400Sstevel@tonic-gate 		pathstr = "/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:"
141*6812Sraf 		    "/opt/SUNWspro/bin:/usr/sbin";
1420Sstevel@tonic-gate 			}
1430Sstevel@tonic-gate 		} else {
1440Sstevel@tonic-gate 			if (__xpg4 == 0) {	/* not XPG4 */
1450Sstevel@tonic-gate 				pathstr = "/usr/ccs/bin:/usr/bin:";
1460Sstevel@tonic-gate 			} else {		/* XPG4 (CSPATH) */
1470Sstevel@tonic-gate 				pathstr = "/usr/xpg4/bin:/usr/ccs/bin:"
1480Sstevel@tonic-gate 				    "/usr/bin:/opt/SUNWspro/bin:";
1490Sstevel@tonic-gate 			}
1500Sstevel@tonic-gate 		}
1510Sstevel@tonic-gate 	}
1520Sstevel@tonic-gate 	cp = strchr(name, '/')? (const char *)"": pathstr;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	do {
1550Sstevel@tonic-gate 		cp = execat(cp, name, fname);
1560Sstevel@tonic-gate 	retry:
1570Sstevel@tonic-gate 		/*
1580Sstevel@tonic-gate 		 * 4025035 and 4038378
1590Sstevel@tonic-gate 		 * if a filename begins with a "-" prepend "./" so that
1600Sstevel@tonic-gate 		 * the shell can't interpret it as an option
1610Sstevel@tonic-gate 		 */
1620Sstevel@tonic-gate 		if (*fname == '-') {
1630Sstevel@tonic-gate 			size_t size = strlen(fname) + 1;
1640Sstevel@tonic-gate 			if ((size + 2) > sizeof (fname)) {
1650Sstevel@tonic-gate 				errno = E2BIG;
1660Sstevel@tonic-gate 				return (-1);
1670Sstevel@tonic-gate 			}
1680Sstevel@tonic-gate 			(void) memmove(fname + 2, fname, size);
1690Sstevel@tonic-gate 			fname[0] = '.';
1700Sstevel@tonic-gate 			fname[1] = '/';
1710Sstevel@tonic-gate 		}
1720Sstevel@tonic-gate 		(void) execv(fname, argv);
1730Sstevel@tonic-gate 		switch (errno) {
1740Sstevel@tonic-gate 		case ENOEXEC:
1750Sstevel@tonic-gate 			if (__xpg4 == 0) {	/* not XPG4 */
1760Sstevel@tonic-gate 				shpath = (char *)sun_path;
1770Sstevel@tonic-gate 			} else {		/* XPG4 */
1780Sstevel@tonic-gate 				shpath = (char *)xpg4_path;
1790Sstevel@tonic-gate 			}
1800Sstevel@tonic-gate 			newargs[0] = (char *)shell;
1810Sstevel@tonic-gate 			newargs[1] = fname;
1820Sstevel@tonic-gate 			for (i = 1; (newargs[i + 1] = argv[i]) != NULL; ++i) {
1830Sstevel@tonic-gate 				if (i >= 254) {
1840Sstevel@tonic-gate 					errno = E2BIG;
1850Sstevel@tonic-gate 					return (-1);
1860Sstevel@tonic-gate 				}
1870Sstevel@tonic-gate 			}
1880Sstevel@tonic-gate 			(void) execv((const char *)shpath, newargs);
1890Sstevel@tonic-gate 			return (-1);
1900Sstevel@tonic-gate 		case ETXTBSY:
1910Sstevel@tonic-gate 			if (++etxtbsy > 5)
1920Sstevel@tonic-gate 				return (-1);
1930Sstevel@tonic-gate 			(void) sleep(etxtbsy);
1940Sstevel@tonic-gate 			goto retry;
1950Sstevel@tonic-gate 		case EACCES:
1960Sstevel@tonic-gate 			++eacces;
1970Sstevel@tonic-gate 			break;
1980Sstevel@tonic-gate 		case ENOMEM:
1990Sstevel@tonic-gate 		case E2BIG:
2000Sstevel@tonic-gate 		case EFAULT:
2010Sstevel@tonic-gate 			return (-1);
2020Sstevel@tonic-gate 		}
2030Sstevel@tonic-gate 	} while (cp);
2040Sstevel@tonic-gate 	if (eacces)
2050Sstevel@tonic-gate 		errno = EACCES;
2060Sstevel@tonic-gate 	return (-1);
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate static const char *
execat(const char * s1,const char * s2,char * si)2100Sstevel@tonic-gate execat(const char *s1, const char *s2, char *si)
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate 	char	*s;
2130Sstevel@tonic-gate 	int cnt = PATH_MAX + 1; /* number of characters in s2 */
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	s = si;
2160Sstevel@tonic-gate 	while (*s1 && *s1 != ':') {
2170Sstevel@tonic-gate 		if (cnt > 0) {
2180Sstevel@tonic-gate 			*s++ = *s1++;
2190Sstevel@tonic-gate 			cnt--;
2200Sstevel@tonic-gate 		} else
2210Sstevel@tonic-gate 			s1++;
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate 	if (si != s && cnt > 0) {
2240Sstevel@tonic-gate 		*s++ = '/';
2250Sstevel@tonic-gate 		cnt--;
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 	while (*s2 && cnt > 0) {
2280Sstevel@tonic-gate 		*s++ = *s2++;
2290Sstevel@tonic-gate 		cnt--;
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 	*s = '\0';
2320Sstevel@tonic-gate 	return (*s1 ? ++s1: 0);
2330Sstevel@tonic-gate }
234