1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 30*0Sstevel@tonic-gate /* All Rights Reserved */ 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate /* 34*0Sstevel@tonic-gate * execlp(name, arg,...,0) (like execl, but does path search) 35*0Sstevel@tonic-gate * execvp(name, argv) (like execv, but does path search) 36*0Sstevel@tonic-gate */ 37*0Sstevel@tonic-gate #pragma weak execlp = _execlp 38*0Sstevel@tonic-gate #pragma weak execvp = _execvp 39*0Sstevel@tonic-gate #include "synonyms.h" 40*0Sstevel@tonic-gate #include <sys/types.h> 41*0Sstevel@tonic-gate #include <unistd.h> 42*0Sstevel@tonic-gate #include <string.h> 43*0Sstevel@tonic-gate #include <alloca.h> 44*0Sstevel@tonic-gate #include <errno.h> 45*0Sstevel@tonic-gate #include <limits.h> 46*0Sstevel@tonic-gate #include <stdarg.h> 47*0Sstevel@tonic-gate #include <stdlib.h> 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate static const char *execat(const char *, const char *, char *); 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate extern int __xpg4; /* defined in xpg4.c; 0 if not xpg4-compiled program */ 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate /*VARARGS1*/ 54*0Sstevel@tonic-gate int 55*0Sstevel@tonic-gate execlp(const char *name, const char *arg0, ...) 56*0Sstevel@tonic-gate { 57*0Sstevel@tonic-gate char **argp; 58*0Sstevel@tonic-gate va_list args; 59*0Sstevel@tonic-gate char **argvec; 60*0Sstevel@tonic-gate int err; 61*0Sstevel@tonic-gate int nargs = 0; 62*0Sstevel@tonic-gate char *nextarg; 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate /* 65*0Sstevel@tonic-gate * count the number of arguments in the variable argument list 66*0Sstevel@tonic-gate * and allocate an argument vector for them on the stack, 67*0Sstevel@tonic-gate * adding space for a terminating null pointer at the end 68*0Sstevel@tonic-gate * and one additional space for argv[0] which is no longer 69*0Sstevel@tonic-gate * counted by the varargs loop. 70*0Sstevel@tonic-gate */ 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate va_start(args, arg0); 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate while (va_arg(args, char *) != (char *)0) 75*0Sstevel@tonic-gate nargs++; 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate va_end(args); 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate /* 80*0Sstevel@tonic-gate * load the arguments in the variable argument list 81*0Sstevel@tonic-gate * into the argument vector and add the terminating null pointer 82*0Sstevel@tonic-gate */ 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate va_start(args, arg0); 85*0Sstevel@tonic-gate /* workaround for bugid 1242839 */ 86*0Sstevel@tonic-gate argvec = alloca((size_t)((nargs + 2) * sizeof (char *))); 87*0Sstevel@tonic-gate nextarg = va_arg(args, char *); 88*0Sstevel@tonic-gate argp = argvec; 89*0Sstevel@tonic-gate *argp++ = (char *)arg0; 90*0Sstevel@tonic-gate while (nargs-- && nextarg != (char *)0) { 91*0Sstevel@tonic-gate *argp = nextarg; 92*0Sstevel@tonic-gate argp++; 93*0Sstevel@tonic-gate nextarg = va_arg(args, char *); 94*0Sstevel@tonic-gate } 95*0Sstevel@tonic-gate va_end(args); 96*0Sstevel@tonic-gate *argp = (char *)0; 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate /* 99*0Sstevel@tonic-gate * call execvp() 100*0Sstevel@tonic-gate */ 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate err = execvp(name, argvec); 103*0Sstevel@tonic-gate return (err); 104*0Sstevel@tonic-gate } 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate int 107*0Sstevel@tonic-gate execvp(const char *name, char *const *argv) 108*0Sstevel@tonic-gate { 109*0Sstevel@tonic-gate const char *pathstr; 110*0Sstevel@tonic-gate char fname[PATH_MAX+2]; 111*0Sstevel@tonic-gate char *newargs[256]; 112*0Sstevel@tonic-gate int i; 113*0Sstevel@tonic-gate const char *cp; 114*0Sstevel@tonic-gate unsigned etxtbsy = 1; 115*0Sstevel@tonic-gate int eacces = 0; 116*0Sstevel@tonic-gate char *shpath; 117*0Sstevel@tonic-gate static const char *sun_path = "/bin/sh"; 118*0Sstevel@tonic-gate static const char *xpg4_path = "/usr/xpg4/bin/sh"; 119*0Sstevel@tonic-gate static const char *shell = "sh"; 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate if (*name == '\0') { 122*0Sstevel@tonic-gate errno = ENOENT; 123*0Sstevel@tonic-gate return (-1); 124*0Sstevel@tonic-gate } 125*0Sstevel@tonic-gate if ((pathstr = getenv("PATH")) == NULL) { 126*0Sstevel@tonic-gate /* 127*0Sstevel@tonic-gate * XPG4: pathstr is equivalent to CSPATH, except that 128*0Sstevel@tonic-gate * :/usr/sbin is appended when root, and pathstr must end 129*0Sstevel@tonic-gate * with a colon when not root. Keep these paths in sync 130*0Sstevel@tonic-gate * with CSPATH in confstr.c. Note that pathstr must end 131*0Sstevel@tonic-gate * with a colon when not root so that when name doesn't 132*0Sstevel@tonic-gate * contain '/', the last call to execat() will result in an 133*0Sstevel@tonic-gate * attempt to execv name from the current directory. 134*0Sstevel@tonic-gate */ 135*0Sstevel@tonic-gate if (geteuid() == 0 || getuid() == 0) { 136*0Sstevel@tonic-gate if (__xpg4 == 0) { /* not XPG4 */ 137*0Sstevel@tonic-gate pathstr = "/usr/sbin:/usr/ccs/bin:/usr/bin"; 138*0Sstevel@tonic-gate } else { /* XPG4 (CSPATH + /usr/sbin) */ 139*0Sstevel@tonic-gate pathstr = "/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:" 140*0Sstevel@tonic-gate "/opt/SUNWspro/bin:/usr/sbin"; 141*0Sstevel@tonic-gate } 142*0Sstevel@tonic-gate } else { 143*0Sstevel@tonic-gate if (__xpg4 == 0) { /* not XPG4 */ 144*0Sstevel@tonic-gate pathstr = "/usr/ccs/bin:/usr/bin:"; 145*0Sstevel@tonic-gate } else { /* XPG4 (CSPATH) */ 146*0Sstevel@tonic-gate pathstr = "/usr/xpg4/bin:/usr/ccs/bin:" 147*0Sstevel@tonic-gate "/usr/bin:/opt/SUNWspro/bin:"; 148*0Sstevel@tonic-gate } 149*0Sstevel@tonic-gate } 150*0Sstevel@tonic-gate } 151*0Sstevel@tonic-gate cp = strchr(name, '/')? (const char *)"": pathstr; 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate do { 154*0Sstevel@tonic-gate cp = execat(cp, name, fname); 155*0Sstevel@tonic-gate retry: 156*0Sstevel@tonic-gate /* 157*0Sstevel@tonic-gate * 4025035 and 4038378 158*0Sstevel@tonic-gate * if a filename begins with a "-" prepend "./" so that 159*0Sstevel@tonic-gate * the shell can't interpret it as an option 160*0Sstevel@tonic-gate */ 161*0Sstevel@tonic-gate if (*fname == '-') { 162*0Sstevel@tonic-gate size_t size = strlen(fname) + 1; 163*0Sstevel@tonic-gate if ((size + 2) > sizeof (fname)) { 164*0Sstevel@tonic-gate errno = E2BIG; 165*0Sstevel@tonic-gate return (-1); 166*0Sstevel@tonic-gate } 167*0Sstevel@tonic-gate (void) memmove(fname + 2, fname, size); 168*0Sstevel@tonic-gate fname[0] = '.'; 169*0Sstevel@tonic-gate fname[1] = '/'; 170*0Sstevel@tonic-gate } 171*0Sstevel@tonic-gate (void) execv(fname, argv); 172*0Sstevel@tonic-gate switch (errno) { 173*0Sstevel@tonic-gate case ENOEXEC: 174*0Sstevel@tonic-gate if (__xpg4 == 0) { /* not XPG4 */ 175*0Sstevel@tonic-gate shpath = (char *)sun_path; 176*0Sstevel@tonic-gate } else { /* XPG4 */ 177*0Sstevel@tonic-gate shpath = (char *)xpg4_path; 178*0Sstevel@tonic-gate } 179*0Sstevel@tonic-gate newargs[0] = (char *)shell; 180*0Sstevel@tonic-gate newargs[1] = fname; 181*0Sstevel@tonic-gate for (i = 1; (newargs[i + 1] = argv[i]) != NULL; ++i) { 182*0Sstevel@tonic-gate if (i >= 254) { 183*0Sstevel@tonic-gate errno = E2BIG; 184*0Sstevel@tonic-gate return (-1); 185*0Sstevel@tonic-gate } 186*0Sstevel@tonic-gate } 187*0Sstevel@tonic-gate (void) execv((const char *)shpath, newargs); 188*0Sstevel@tonic-gate return (-1); 189*0Sstevel@tonic-gate case ETXTBSY: 190*0Sstevel@tonic-gate if (++etxtbsy > 5) 191*0Sstevel@tonic-gate return (-1); 192*0Sstevel@tonic-gate (void) sleep(etxtbsy); 193*0Sstevel@tonic-gate goto retry; 194*0Sstevel@tonic-gate case EACCES: 195*0Sstevel@tonic-gate ++eacces; 196*0Sstevel@tonic-gate break; 197*0Sstevel@tonic-gate case ENOMEM: 198*0Sstevel@tonic-gate case E2BIG: 199*0Sstevel@tonic-gate case EFAULT: 200*0Sstevel@tonic-gate return (-1); 201*0Sstevel@tonic-gate } 202*0Sstevel@tonic-gate } while (cp); 203*0Sstevel@tonic-gate if (eacces) 204*0Sstevel@tonic-gate errno = EACCES; 205*0Sstevel@tonic-gate return (-1); 206*0Sstevel@tonic-gate } 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate static const char * 209*0Sstevel@tonic-gate execat(const char *s1, const char *s2, char *si) 210*0Sstevel@tonic-gate { 211*0Sstevel@tonic-gate char *s; 212*0Sstevel@tonic-gate int cnt = PATH_MAX + 1; /* number of characters in s2 */ 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gate s = si; 215*0Sstevel@tonic-gate while (*s1 && *s1 != ':') { 216*0Sstevel@tonic-gate if (cnt > 0) { 217*0Sstevel@tonic-gate *s++ = *s1++; 218*0Sstevel@tonic-gate cnt--; 219*0Sstevel@tonic-gate } else 220*0Sstevel@tonic-gate s1++; 221*0Sstevel@tonic-gate } 222*0Sstevel@tonic-gate if (si != s && cnt > 0) { 223*0Sstevel@tonic-gate *s++ = '/'; 224*0Sstevel@tonic-gate cnt--; 225*0Sstevel@tonic-gate } 226*0Sstevel@tonic-gate while (*s2 && cnt > 0) { 227*0Sstevel@tonic-gate *s++ = *s2++; 228*0Sstevel@tonic-gate cnt--; 229*0Sstevel@tonic-gate } 230*0Sstevel@tonic-gate *s = '\0'; 231*0Sstevel@tonic-gate return (*s1 ? ++s1: 0); 232*0Sstevel@tonic-gate } 233