xref: /titanic_50/usr/src/lib/libc/port/gen/execvp.c (revision 6a5408e613dc0b372f722907d6c0b18e99c182dd)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57257d1b4Sraf  * Common Development and Distribution License (the "License").
67257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  *	execlp(name, arg,...,0)	(like execl, but does path search)
327c478bd9Sstevel@tonic-gate  *	execvp(name, argv)	(like execv, but does path search)
337c478bd9Sstevel@tonic-gate  */
347257d1b4Sraf 
357257d1b4Sraf #pragma weak _execlp = execlp
367257d1b4Sraf #pragma weak _execvp = execvp
377257d1b4Sraf 
387257d1b4Sraf #include "lint.h"
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <unistd.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
427c478bd9Sstevel@tonic-gate #include <alloca.h>
437c478bd9Sstevel@tonic-gate #include <errno.h>
447c478bd9Sstevel@tonic-gate #include <limits.h>
457c478bd9Sstevel@tonic-gate #include <stdarg.h>
467c478bd9Sstevel@tonic-gate #include <stdlib.h>
47*6a5408e6SRichard Lowe #include <paths.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static const char *execat(const char *, const char *, char *);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate extern  int __xpg4;	/* defined in xpg4.c; 0 if not xpg4-compiled program */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*VARARGS1*/
547c478bd9Sstevel@tonic-gate int
execlp(const char * name,const char * arg0,...)557c478bd9Sstevel@tonic-gate execlp(const char *name, const char *arg0, ...)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	char **argp;
587c478bd9Sstevel@tonic-gate 	va_list args;
597c478bd9Sstevel@tonic-gate 	char **argvec;
607c478bd9Sstevel@tonic-gate 	int err;
617c478bd9Sstevel@tonic-gate 	int nargs = 0;
627c478bd9Sstevel@tonic-gate 	char *nextarg;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/*
657c478bd9Sstevel@tonic-gate 	 * count the number of arguments in the variable argument list
667c478bd9Sstevel@tonic-gate 	 * and allocate an argument vector for them on the stack,
677c478bd9Sstevel@tonic-gate 	 * adding space for a terminating null pointer at the end
687c478bd9Sstevel@tonic-gate 	 * and one additional space for argv[0] which is no longer
697c478bd9Sstevel@tonic-gate 	 * counted by the varargs loop.
707c478bd9Sstevel@tonic-gate 	 */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	va_start(args, arg0);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	while (va_arg(args, char *) != (char *)0)
757c478bd9Sstevel@tonic-gate 		nargs++;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	va_end(args);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	/*
807c478bd9Sstevel@tonic-gate 	 * load the arguments in the variable argument list
817c478bd9Sstevel@tonic-gate 	 * into the argument vector and add the terminating null pointer
827c478bd9Sstevel@tonic-gate 	 */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	va_start(args, arg0);
857c478bd9Sstevel@tonic-gate 	/* workaround for bugid 1242839 */
867c478bd9Sstevel@tonic-gate 	argvec = alloca((size_t)((nargs + 2) * sizeof (char *)));
877c478bd9Sstevel@tonic-gate 	nextarg = va_arg(args, char *);
887c478bd9Sstevel@tonic-gate 	argp = argvec;
897c478bd9Sstevel@tonic-gate 	*argp++ = (char *)arg0;
907c478bd9Sstevel@tonic-gate 	while (nargs-- && nextarg != (char *)0) {
917c478bd9Sstevel@tonic-gate 		*argp = nextarg;
927c478bd9Sstevel@tonic-gate 		argp++;
937c478bd9Sstevel@tonic-gate 		nextarg = va_arg(args, char *);
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 	va_end(args);
967c478bd9Sstevel@tonic-gate 	*argp = (char *)0;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	/*
997c478bd9Sstevel@tonic-gate 	 * call execvp()
1007c478bd9Sstevel@tonic-gate 	 */
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	err = execvp(name, argvec);
1037c478bd9Sstevel@tonic-gate 	return (err);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate int
execvp(const char * name,char * const * argv)1077c478bd9Sstevel@tonic-gate execvp(const char *name, char *const *argv)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	const char	*pathstr;
1107c478bd9Sstevel@tonic-gate 	char	fname[PATH_MAX+2];
1117c478bd9Sstevel@tonic-gate 	char	*newargs[256];
1127c478bd9Sstevel@tonic-gate 	int	i;
1137c478bd9Sstevel@tonic-gate 	const char *cp;
1147c478bd9Sstevel@tonic-gate 	unsigned etxtbsy = 1;
1157c478bd9Sstevel@tonic-gate 	int eacces = 0;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (*name == '\0') {
1187c478bd9Sstevel@tonic-gate 		errno = ENOENT;
1197c478bd9Sstevel@tonic-gate 		return (-1);
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 	if ((pathstr = getenv("PATH")) == NULL) {
1227c478bd9Sstevel@tonic-gate 		/*
1237c478bd9Sstevel@tonic-gate 		 * XPG4:  pathstr is equivalent to CSPATH, except that
1247c478bd9Sstevel@tonic-gate 		 * :/usr/sbin is appended when root, and pathstr must end
1257c478bd9Sstevel@tonic-gate 		 * with a colon when not root.  Keep these paths in sync
1267c478bd9Sstevel@tonic-gate 		 * with CSPATH in confstr.c.  Note that pathstr must end
1277c478bd9Sstevel@tonic-gate 		 * with a colon when not root so that when name doesn't
1287c478bd9Sstevel@tonic-gate 		 * contain '/', the last call to execat() will result in an
1297c478bd9Sstevel@tonic-gate 		 * attempt to execv name from the current directory.
1307c478bd9Sstevel@tonic-gate 		 */
1317c478bd9Sstevel@tonic-gate 		if (geteuid() == 0 || getuid() == 0) {
1327c478bd9Sstevel@tonic-gate 			if (__xpg4 == 0) {	/* not XPG4 */
1337c478bd9Sstevel@tonic-gate 				pathstr = "/usr/sbin:/usr/ccs/bin:/usr/bin";
1347c478bd9Sstevel@tonic-gate 			} else {		/* XPG4 (CSPATH + /usr/sbin) */
1357c478bd9Sstevel@tonic-gate 				pathstr = "/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:"
1367c478bd9Sstevel@tonic-gate 				    "/opt/SUNWspro/bin:/usr/sbin";
1377c478bd9Sstevel@tonic-gate 			}
1387c478bd9Sstevel@tonic-gate 		} else {
1397c478bd9Sstevel@tonic-gate 			if (__xpg4 == 0) {	/* not XPG4 */
1407c478bd9Sstevel@tonic-gate 				pathstr = "/usr/ccs/bin:/usr/bin:";
1417c478bd9Sstevel@tonic-gate 			} else {		/* XPG4 (CSPATH) */
1427c478bd9Sstevel@tonic-gate 				pathstr = "/usr/xpg4/bin:/usr/ccs/bin:"
1437c478bd9Sstevel@tonic-gate 				    "/usr/bin:/opt/SUNWspro/bin:";
1447c478bd9Sstevel@tonic-gate 			}
1457c478bd9Sstevel@tonic-gate 		}
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 	cp = strchr(name, '/')? (const char *)"": pathstr;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	do {
1507c478bd9Sstevel@tonic-gate 		cp = execat(cp, name, fname);
1517c478bd9Sstevel@tonic-gate 	retry:
1527c478bd9Sstevel@tonic-gate 		/*
1537c478bd9Sstevel@tonic-gate 		 * 4025035 and 4038378
1547c478bd9Sstevel@tonic-gate 		 * if a filename begins with a "-" prepend "./" so that
1557c478bd9Sstevel@tonic-gate 		 * the shell can't interpret it as an option
1567c478bd9Sstevel@tonic-gate 		 */
1577c478bd9Sstevel@tonic-gate 		if (*fname == '-') {
1587c478bd9Sstevel@tonic-gate 			size_t size = strlen(fname) + 1;
1597c478bd9Sstevel@tonic-gate 			if ((size + 2) > sizeof (fname)) {
1607c478bd9Sstevel@tonic-gate 				errno = E2BIG;
1617c478bd9Sstevel@tonic-gate 				return (-1);
1627c478bd9Sstevel@tonic-gate 			}
1637c478bd9Sstevel@tonic-gate 			(void) memmove(fname + 2, fname, size);
1647c478bd9Sstevel@tonic-gate 			fname[0] = '.';
1657c478bd9Sstevel@tonic-gate 			fname[1] = '/';
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 		(void) execv(fname, argv);
1687c478bd9Sstevel@tonic-gate 		switch (errno) {
1697c478bd9Sstevel@tonic-gate 		case ENOEXEC:
170*6a5408e6SRichard Lowe 			newargs[0] = "sh";
1717c478bd9Sstevel@tonic-gate 			newargs[1] = fname;
1727c478bd9Sstevel@tonic-gate 			for (i = 1; (newargs[i + 1] = argv[i]) != NULL; ++i) {
1737c478bd9Sstevel@tonic-gate 				if (i >= 254) {
1747c478bd9Sstevel@tonic-gate 					errno = E2BIG;
1757c478bd9Sstevel@tonic-gate 					return (-1);
1767c478bd9Sstevel@tonic-gate 				}
1777c478bd9Sstevel@tonic-gate 			}
178*6a5408e6SRichard Lowe 			(void) execv(_PATH_BSHELL, newargs);
1797c478bd9Sstevel@tonic-gate 			return (-1);
1807c478bd9Sstevel@tonic-gate 		case ETXTBSY:
1817c478bd9Sstevel@tonic-gate 			if (++etxtbsy > 5)
1827c478bd9Sstevel@tonic-gate 				return (-1);
1837c478bd9Sstevel@tonic-gate 			(void) sleep(etxtbsy);
1847c478bd9Sstevel@tonic-gate 			goto retry;
1857c478bd9Sstevel@tonic-gate 		case EACCES:
1867c478bd9Sstevel@tonic-gate 			++eacces;
1877c478bd9Sstevel@tonic-gate 			break;
1887c478bd9Sstevel@tonic-gate 		case ENOMEM:
1897c478bd9Sstevel@tonic-gate 		case E2BIG:
1907c478bd9Sstevel@tonic-gate 		case EFAULT:
1917c478bd9Sstevel@tonic-gate 			return (-1);
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 	} while (cp);
1947c478bd9Sstevel@tonic-gate 	if (eacces)
1957c478bd9Sstevel@tonic-gate 		errno = EACCES;
1967c478bd9Sstevel@tonic-gate 	return (-1);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate static const char *
execat(const char * s1,const char * s2,char * si)2007c478bd9Sstevel@tonic-gate execat(const char *s1, const char *s2, char *si)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	char	*s;
2037c478bd9Sstevel@tonic-gate 	int cnt = PATH_MAX + 1; /* number of characters in s2 */
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	s = si;
2067c478bd9Sstevel@tonic-gate 	while (*s1 && *s1 != ':') {
2077c478bd9Sstevel@tonic-gate 		if (cnt > 0) {
2087c478bd9Sstevel@tonic-gate 			*s++ = *s1++;
2097c478bd9Sstevel@tonic-gate 			cnt--;
2107c478bd9Sstevel@tonic-gate 		} else
2117c478bd9Sstevel@tonic-gate 			s1++;
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 	if (si != s && cnt > 0) {
2147c478bd9Sstevel@tonic-gate 		*s++ = '/';
2157c478bd9Sstevel@tonic-gate 		cnt--;
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 	while (*s2 && cnt > 0) {
2187c478bd9Sstevel@tonic-gate 		*s++ = *s2++;
2197c478bd9Sstevel@tonic-gate 		cnt--;
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 	*s = '\0';
2227c478bd9Sstevel@tonic-gate 	return (*s1 ? ++s1: 0);
2237c478bd9Sstevel@tonic-gate }
224