]> git.saurik.com Git - apple/libc.git/blame - gen/exec-fbsd.c
Libc-498.1.5.tar.gz
[apple/libc.git] / gen / exec-fbsd.c
CommitLineData
224c7076
A
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)
35static char sccsid[] = "@(#)exec.c 8.1 (Berkeley) 6/4/93";
36#endif /* LIBC_SCCS and not lint */
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: src/lib/libc/gen/exec.c,v 1.22 2003/07/01 12:30:03 bde Exp $");
39
40#include "namespace.h"
41#include <sys/param.h>
42#include <sys/types.h>
43#include <sys/stat.h>
44#include <errno.h>
45#include <unistd.h>
46#include <stdlib.h>
47#include <string.h>
48#include <stdio.h>
49#include <paths.h>
50
51#include <stdarg.h>
52#include "un-namespace.h"
53
54#include <crt_externs.h>
55#define environ (*_NSGetEnviron())
56
57int
58execl(const char *name, const char *arg, ...)
59{
60 va_list ap;
61 char **argv;
62 int n;
63
64 va_start(ap, arg);
65 n = 1;
66 while (va_arg(ap, char *) != NULL)
67 n++;
68 va_end(ap);
69 argv = alloca((n + 1) * sizeof(*argv));
70 if (argv == NULL) {
71 errno = ENOMEM;
72 return (-1);
73 }
74 va_start(ap, arg);
75 n = 1;
76 argv[0] = (char *)arg;
77 while ((argv[n] = va_arg(ap, char *)) != NULL)
78 n++;
79 va_end(ap);
80 return (_execve(name, argv, environ));
81}
82
83int
84execle(const char *name, const char *arg, ...)
85{
86 va_list ap;
87 char **argv, **envp;
88 int n;
89
90 va_start(ap, arg);
91 n = 1;
92 while (va_arg(ap, char *) != NULL)
93 n++;
94 va_end(ap);
95 argv = alloca((n + 1) * sizeof(*argv));
96 if (argv == NULL) {
97 errno = ENOMEM;
98 return (-1);
99 }
100 va_start(ap, arg);
101 n = 1;
102 argv[0] = (char *)arg;
103 while ((argv[n] = va_arg(ap, char *)) != NULL)
104 n++;
105 envp = va_arg(ap, char **);
106 va_end(ap);
107 return (_execve(name, argv, envp));
108}
109
110int
111execlp(const char *name, const char *arg, ...)
112{
113 va_list ap;
114 char **argv;
115 int n;
116
117 va_start(ap, arg);
118 n = 1;
119 while (va_arg(ap, char *) != NULL)
120 n++;
121 va_end(ap);
122 argv = alloca((n + 1) * sizeof(*argv));
123 if (argv == NULL) {
124 errno = ENOMEM;
125 return (-1);
126 }
127 va_start(ap, arg);
128 n = 1;
129 argv[0] = (char *)arg;
130 while ((argv[n] = va_arg(ap, char *)) != NULL)
131 n++;
132 va_end(ap);
133 return (execvp(name, argv));
134}
135
136int
137execv(name, argv)
138 const char *name;
139 char * const *argv;
140{
141 (void)_execve(name, argv, environ);
142 return (-1);
143}
144
145int
146execvp(const char *name, char * const *argv)
147{
148 const char *path;
149
150 /* Get the path we're searching. */
151 if ((path = getenv("PATH")) == NULL)
152 path = _PATH_DEFPATH;
153
154 return (execvP(name, path, argv));
155}
156
157int
158execvP(name, path, argv)
159 const char *name;
160 const char *path;
161 char * const *argv;
162{
163 char **memp;
164 int cnt, lp, ln;
165 char *p;
166 int eacces, save_errno;
167 char *bp, *cur, buf[MAXPATHLEN];
168 struct stat sb;
169
170 eacces = 0;
171
172 /* If it's an absolute or relative path name, it's easy. */
173 if (index(name, '/')) {
174 bp = (char *)name;
175 cur = NULL;
176 goto retry;
177 }
178 bp = buf;
179
180 /* If it's an empty path name, fail in the usual POSIX way. */
181 if (*name == '\0') {
182 errno = ENOENT;
183 return (-1);
184 }
185
186 cur = alloca(strlen(path) + 1);
187 if (cur == NULL) {
188 errno = ENOMEM;
189 return (-1);
190 }
191 strcpy(cur, path);
192 while ((p = strsep(&cur, ":")) != NULL) {
193 /*
194 * It's a SHELL path -- double, leading and trailing colons
195 * mean the current directory.
196 */
197 if (*p == '\0') {
198 p = ".";
199 lp = 1;
200 } else
201 lp = strlen(p);
202 ln = strlen(name);
203
204 /*
205 * If the path is too long complain. This is a possible
206 * security issue; given a way to make the path too long
207 * the user may execute the wrong program.
208 */
209 if (lp + ln + 2 > sizeof(buf)) {
210 (void)_write(STDERR_FILENO, "execvP: ", 8);
211 (void)_write(STDERR_FILENO, p, lp);
212 (void)_write(STDERR_FILENO, ": path too long\n",
213 16);
214 continue;
215 }
216 bcopy(p, buf, lp);
217 buf[lp] = '/';
218 bcopy(name, buf + lp + 1, ln);
219 buf[lp + ln + 1] = '\0';
220
221retry: (void)_execve(bp, argv, environ);
222 switch (errno) {
223 case E2BIG:
224 goto done;
225 case ELOOP:
226 case ENAMETOOLONG:
227 case ENOENT:
228 break;
229 case ENOEXEC:
230 for (cnt = 0; argv[cnt]; ++cnt)
231 ;
232 memp = alloca((cnt + 2) * sizeof(char *));
233 if (memp == NULL) {
234 /* errno = ENOMEM; XXX override ENOEXEC? */
235 goto done;
236 }
237 memp[0] = "sh";
238 memp[1] = bp;
239 bcopy(argv + 1, memp + 2, cnt * sizeof(char *));
240 (void)_execve(_PATH_BSHELL, memp, environ);
241 goto done;
242 case ENOMEM:
243 goto done;
244 case ENOTDIR:
245 break;
246 case ETXTBSY:
247 /*
248 * We used to retry here, but sh(1) doesn't.
249 */
250 goto done;
251 default:
252 /*
253 * EACCES may be for an inaccessible directory or
254 * a non-executable file. Call stat() to decide
255 * which. This also handles ambiguities for EFAULT
256 * and EIO, and undocumented errors like ESTALE.
257 * We hope that the race for a stat() is unimportant.
258 */
259 save_errno = errno;
260 if (stat(bp, &sb) != 0)
261 break;
262 if (save_errno == EACCES) {
263 eacces = 1;
264 continue;
265 }
266 errno = save_errno;
267 goto done;
268 }
269 }
270 if (eacces)
271 errno = EACCES;
272 else if (cur)
273 errno = ENOENT;
274 /* else use existing errno from _execve */
275done:
276 return (-1);
277}