1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #if defined(LIBC_SCCS) && !defined(lint) 35 static char sccsid[] = "@(#)exec.c 8.1 (Berkeley) 6/4/93"; 36 #endif /* LIBC_SCCS and not lint */ 37 38 #include <sys/param.h> 39 #include <sys/types.h> 40 #include <errno.h> 41 #include <unistd.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <stdio.h> 45 #include <paths.h> 46 47 #if __STDC__ 48 #include <stdarg.h> 49 #else 50 #include <varargs.h> 51 #endif 52 53 extern char **environ; 54 55 static char ** 56 buildargv(ap, arg, envpp) 57 va_list ap; 58 const char *arg; 59 char ***envpp; 60 { 61 register char **argv, **nargv; 62 register int memsize, off; 63 64 argv = NULL; 65 for (off = memsize = 0;; ++off) { 66 if (off >= memsize) { 67 memsize += 50; /* Starts out at 0. */ 68 memsize *= 2; /* Ramp up fast. */ 69 nargv = realloc(argv, memsize * sizeof(char *)); 70 if (nargv == NULL) { 71 free(argv); 72 return (NULL); 73 } 74 argv = nargv; 75 if (off == 0) { 76 argv[0] = (char *)arg; 77 off = 1; 78 } 79 } 80 if (!(argv[off] = va_arg(ap, char *))) 81 break; 82 } 83 /* Get environment pointer if user supposed to provide one. */ 84 if (envpp) 85 *envpp = va_arg(ap, char **); 86 return (argv); 87 } 88 89 int 90 #if __STDC__ 91 execl(const char *name, const char *arg, ...) 92 #else 93 execl(name, arg, va_alist) 94 const char *name; 95 const char *arg; 96 va_dcl 97 #endif 98 { 99 va_list ap; 100 int sverrno; 101 char **argv; 102 103 #if __STDC__ 104 va_start(ap, arg); 105 #else 106 va_start(ap); 107 #endif 108 if (argv = buildargv(ap, arg, NULL)) 109 (void)execve(name, argv, environ); 110 va_end(ap); 111 sverrno = errno; 112 free(argv); 113 errno = sverrno; 114 return (-1); 115 } 116 117 int 118 #if __STDC__ 119 execle(const char *name, const char *arg, ...) 120 #else 121 execle(name, arg, va_alist) 122 const char *name; 123 const char *arg; 124 va_dcl 125 #endif 126 { 127 va_list ap; 128 int sverrno; 129 char **argv, **envp; 130 131 #if __STDC__ 132 va_start(ap, arg); 133 #else 134 va_start(ap); 135 #endif 136 if (argv = buildargv(ap, arg, &envp)) 137 (void)execve(name, argv, envp); 138 va_end(ap); 139 sverrno = errno; 140 free(argv); 141 errno = sverrno; 142 return (-1); 143 } 144 145 int 146 #if __STDC__ 147 execlp(const char *name, const char *arg, ...) 148 #else 149 execlp(name, arg, va_alist) 150 const char *name; 151 const char *arg; 152 va_dcl 153 #endif 154 { 155 va_list ap; 156 int sverrno; 157 char **argv; 158 159 #if __STDC__ 160 va_start(ap, arg); 161 #else 162 va_start(ap); 163 #endif 164 if (argv = buildargv(ap, arg, NULL)) 165 (void)execvp(name, argv); 166 va_end(ap); 167 sverrno = errno; 168 free(argv); 169 errno = sverrno; 170 return (-1); 171 } 172 173 int 174 execv(name, argv) 175 const char *name; 176 char * const *argv; 177 { 178 (void)execve(name, argv, environ); 179 return (-1); 180 } 181 182 int 183 execvp(name, argv) 184 const char *name; 185 char * const *argv; 186 { 187 char **memp; 188 register int cnt, lp, ln; 189 register char *p; 190 int eacces, etxtbsy; 191 char *bp, *cur, *path, buf[MAXPATHLEN]; 192 193 /* If it's an absolute or relative path name, it's easy. */ 194 if (index(name, '/')) { 195 bp = (char *)name; 196 cur = path = NULL; 197 goto retry; 198 } 199 bp = buf; 200 201 /* Get the path we're searching. */ 202 if (!(path = getenv("PATH"))) 203 path = _PATH_DEFPATH; 204 cur = path = strdup(path); 205 206 eacces = etxtbsy = 0; 207 while (p = strsep(&cur, ":")) { 208 /* 209 * It's a SHELL path -- double, leading and trailing colons 210 * mean the current directory. 211 */ 212 if (!*p) { 213 p = "."; 214 lp = 1; 215 } else 216 lp = strlen(p); 217 ln = strlen(name); 218 219 /* 220 * If the path is too long complain. This is a possible 221 * security issue; given a way to make the path too long 222 * the user may execute the wrong program. 223 */ 224 if (lp + ln + 2 > sizeof(buf)) { 225 (void)write(STDERR_FILENO, "execvp: ", 8); 226 (void)write(STDERR_FILENO, p, lp); 227 (void)write(STDERR_FILENO, ": path too long\n", 16); 228 continue; 229 } 230 bcopy(p, buf, lp); 231 buf[lp] = '/'; 232 bcopy(name, buf + lp + 1, ln); 233 buf[lp + ln + 1] = '\0'; 234 235 retry: (void)execve(bp, argv, environ); 236 switch(errno) { 237 case EACCES: 238 eacces = 1; 239 break; 240 case ENOENT: 241 break; 242 case ENOEXEC: 243 for (cnt = 0; argv[cnt]; ++cnt) 244 ; 245 memp = malloc((cnt + 2) * sizeof(char *)); 246 if (memp == NULL) 247 goto done; 248 memp[0] = "sh"; 249 memp[1] = bp; 250 bcopy(argv + 1, memp + 2, cnt * sizeof(char *)); 251 (void)execve(_PATH_BSHELL, memp, environ); 252 free(memp); 253 goto done; 254 case ETXTBSY: 255 if (etxtbsy < 3) 256 (void)sleep(++etxtbsy); 257 goto retry; 258 default: 259 goto done; 260 } 261 } 262 if (eacces) 263 errno = EACCES; 264 else if (!errno) 265 errno = ENOENT; 266 done: if (path) 267 free(path); 268 return (-1); 269 } 270