1 /* Copyright (C) 2004 Manuel Novoa III
2 *
3 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
4 */
5
6 /* Jan 1, 2004
7 * Initial version of a SUSv3 compliant exec*() functions.
8 * Feb 17, 2004
9 * Sigh... Fall back to alloca() if munmap() is broken on uClinux.
10 */
11
12 /* NOTE: Strictly speaking, there could be problems from accessing
13 * __environ in multithreaded programs. The only way around this
14 * that I see is to essentially lock __environ access (modifying
15 * the setenv code), make a copy of the environment table (just the
16 * pointers since the strings themselves are never freed), and then
17 * unlock prior to the execve call. If that fails, then we'd need
18 * to free the storage allocated for the copy. Better ideas anyone?
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <stdarg.h>
26 #include <limits.h>
27 #include <unistd.h>
28 #include <sys/mman.h>
29
30
31
32 /**********************************************************************/
33 #define EXEC_FUNC_COMMON 0
34 #define EXEC_FUNC_EXECVP 1
35 #define EXEC_FUNC_EXECVPE 2
36
37 #if defined(__ARCH_USE_MMU__)
38
39 /* We have an MMU, so use alloca() to grab space for buffers and arg lists. */
40
41 # define EXEC_ALLOC_SIZE(VAR) /* nothing to do */
42 # define EXEC_ALLOC(SIZE,VAR,FUNC) alloca((SIZE))
43 # define EXEC_FREE(PTR,VAR) ((void)0)
44
45 #else
46
47 /* We do not have an MMU, so using alloca() is not an option (as this will
48 * easily overflow the stack in most setups). Less obviously, using malloc()
49 * is not an option either since malloc()ed memory can leak in from a vfork()ed
50 * child into the parent as no one is around after the child calls exec*() to
51 * free() the memory. Therefore, we must use mmap() and unmap() directly,
52 * caching the result as we go. This way we minimize the leak by reusing the
53 * memory with every call to an exec*().
54 *
55 * To prevent recursive use of the same cached memory, we have to give execvp()
56 * its own cache. Here are the nested exec calls (a/-: alloc/no-alloc):
57 * execve(-) -> calls straight to kernel
58 * execl(a) -> execve(-)
59 * execlp(a) -> execvp(a) !! recursive usage !!
60 * execle(a) -> execve(-)
61 * execv(-) -> execve(-)
62 * execvp(a) -> execve(-)
63 * execvpe(a) -> execve(-)
64 */
65
66 # define EXEC_ALLOC_SIZE(VAR) /* nothing to do */
67 # define EXEC_ALLOC(SIZE,VAR,FUNC) __exec_alloc((SIZE), FUNC)
68 # define EXEC_FREE(PTR,VAR) ((void)0)
69
70 extern void *__exec_alloc(size_t size, int func) attribute_hidden;
71
72 # ifdef L___exec_alloc
73
__exec_alloc(size_t size,int func)74 void *__exec_alloc(size_t size, int func)
75 {
76 static void *common_cache, *execvp_cache;
77 static size_t common_size, execvp_size;
78
79 void **cache = (func ? &execvp_cache : &common_cache);
80 size_t *cache_size = (func ? &execvp_size : &common_size);
81
82 if (*cache_size >= size)
83 return *cache;
84 else if (*cache)
85 munmap(*cache, *cache_size);
86
87 *cache_size = size;
88 return *cache = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
89
90 /* We don't actually handle OOM in the exec funcs ...
91 if (*cache != MAP_FAILED)
92 return *cache;
93 else
94 return (*cache = NULL);
95 */
96 }
97
98 # endif
99
100 #endif
101 /**********************************************************************/
102 #ifdef L_execl
103
execl(const char * path,const char * arg,...)104 int execl(const char *path, const char *arg, ...)
105 {
106 EXEC_ALLOC_SIZE(size) /* Do NOT add a semicolon! */
107 int n;
108 char **argv;
109 char **p;
110 va_list args;
111
112 n = 0;
113 va_start(args, arg);
114 do {
115 ++n;
116 } while (va_arg(args, char *));
117 va_end(args);
118
119 p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size, EXEC_FUNC_COMMON);
120
121 p[0] = (char *)arg;
122
123 va_start(args, arg);
124 do {
125 *++p = va_arg(args, char *);
126 } while (--n);
127 va_end(args);
128
129 n = execve(path, (char *const *) argv, __environ);
130
131 EXEC_FREE(argv, size);
132
133 return n;
134 }
libc_hidden_def(execl)135 libc_hidden_def(execl)
136
137 #endif
138 /**********************************************************************/
139 #ifdef L_execv
140
141 int execv(const char *path, char *const argv[])
142 {
143 return execve(path, argv, __environ);
144 }
libc_hidden_def(execv)145 libc_hidden_def(execv)
146
147 #endif
148 /**********************************************************************/
149 #ifdef L_execle
150
151 int execle(const char *path, const char *arg, ...)
152 {
153 EXEC_ALLOC_SIZE(size) /* Do NOT add a semicolon! */
154 int n;
155 char **argv;
156 char **p;
157 char *const *envp;
158 va_list args;
159
160 n = 0;
161 va_start(args, arg);
162 do {
163 ++n;
164 } while (va_arg(args, char *));
165 envp = va_arg(args, char *const *); /* Varies from execl and execlp. */
166 va_end(args);
167
168 p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size, EXEC_FUNC_COMMON);
169
170 p[0] = (char *)arg;
171
172 va_start(args, arg);
173 do {
174 *++p = va_arg(args, char *);
175 } while (--n);
176 va_end(args);
177
178 n = execve(path, (char *const *) argv, envp);
179
180 EXEC_FREE(argv, size);
181
182 return n;
183 }
libc_hidden_def(execle)184 libc_hidden_def(execle)
185
186 #endif
187 /**********************************************************************/
188 #ifdef L_execlp
189
190 int execlp(const char *file, const char *arg, ...)
191 {
192 EXEC_ALLOC_SIZE(size) /* Do NOT add a semicolon! */
193 int n;
194 char **argv;
195 char **p;
196 va_list args;
197
198 n = 0;
199 va_start(args, arg);
200 do {
201 ++n;
202 } while (va_arg(args, char *));
203 va_end(args);
204
205 p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size, EXEC_FUNC_COMMON);
206
207 p[0] = (char *)arg;
208
209 va_start(args, arg);
210 do {
211 *++p = va_arg(args, char *);
212 } while (--n);
213 va_end(args);
214
215 n = execvp(file, (char *const *) argv);
216
217 EXEC_FREE(argv, size);
218
219 return n;
220 }
221 libc_hidden_def(execlp)
222
223 #endif
224 /**********************************************************************/
225 #if defined (L_execvp) || defined(L_execvpe)
226
227
228 /* Use a default path that matches glibc behavior, since SUSv3 says
229 * this is implementation-defined. The default is current working dir,
230 * /bin, and then /usr/bin. */
231 static const char default_path[] = ":/bin:/usr/bin";
232 #if defined (L_execvp)
execvp(const char * path,char * const argv[])233 int execvp(const char *path, char *const argv[])
234 #elif defined (L_execvpe)
235 int execvpe(const char *path, char *const argv[], char *const envp[])
236 #endif
237 {
238 char *buf = NULL;
239 char *p;
240 char *e;
241 char *s0;
242 char *s;
243 EXEC_ALLOC_SIZE(size = 0) /* Do NOT add a semicolon! */
244 size_t len;
245 size_t plen;
246
247 if (!path || !*path) { /* Comply with SUSv3. */
248 BAD:
249 __set_errno(ENOENT);
250 return -1;
251 }
252
253 if (strchr(path, '/')) {
254 #if defined (L_execvp)
255 execve(path, argv, __environ);
256 #elif defined (L_execvpe)
257 execve(path, argv, envp);
258 #endif
259 if (errno == ENOEXEC) {
260 char **nargv;
261 EXEC_ALLOC_SIZE(size2) /* Do NOT add a semicolon! */
262 size_t n;
263 RUN_BIN_SH:
264 /* Need the dimension - 1. We omit counting the trailing
265 * NULL but we actually omit the first entry. */
266 for (n=0 ; argv[n] ; n++) {}
267 #if defined (L_execvp)
268 nargv = (char **) EXEC_ALLOC((n+2) * sizeof(char *), size2, EXEC_FUNC_EXECVP);
269 #elif defined (L_execvpe)
270 nargv = (char **) EXEC_ALLOC((n+2) * sizeof(char *), size2, EXEC_FUNC_EXECVPE);
271 #endif
272 nargv[0] = argv[0];
273 nargv[1] = (char *)path;
274 memcpy(nargv+2, argv+1, n*sizeof(char *));
275 #if defined (L_execvp)
276 execve("/bin/sh", nargv, __environ);
277 #elif defined (L_execvpe)
278 execve("/bin/sh", nargv, envp);
279 #endif
280 EXEC_FREE(nargv, size2);
281 }
282 } else {
283 if ((p = getenv("PATH")) != NULL) {
284 if (!*p) {
285 goto BAD;
286 }
287 } else {
288 p = (char *) default_path;
289 }
290
291 plen = strlen(path);
292 if (plen > (FILENAME_MAX - 1)) {
293 ALL_TOO_LONG:
294 __set_errno(ENAMETOOLONG);
295 return -1;
296 }
297 len = (FILENAME_MAX - 1) - plen;
298 #if defined (L_execvp)
299 buf = EXEC_ALLOC(FILENAME_MAX, size, EXEC_FUNC_EXECVP);
300 #elif defined (L_execvpe)
301 buf = EXEC_ALLOC(FILENAME_MAX, size, EXEC_FUNC_EXECVPE);
302 #endif
303 {
304 int seen_small = 0;
305 s0 = buf + len;
306 memcpy(s0, path, plen+1);
307
308 do {
309 s = s0;
310 e = strchrnul(p, ':');
311 if (e > p) {
312 plen = e - p;
313 if (e[-1] != '/') {
314 ++plen;
315 }
316 if (plen > len) {
317 goto NEXT;
318 }
319 s -= plen;
320 memcpy(s, p, plen);
321 s[plen-1] = '/';
322 }
323
324 #if defined (L_execvp)
325 execve(s, argv, __environ);
326 #elif defined (L_execvpe)
327 execve(s, argv, envp);
328 #endif
329 seen_small = 1;
330
331 if (errno == ENOEXEC) {
332 path = s;
333 goto RUN_BIN_SH;
334 }
335
336 NEXT:
337 if (!*e) {
338 if (!seen_small) {
339 goto ALL_TOO_LONG;
340 }
341 break;
342 }
343 p = e + 1;
344 } while (1);
345 }
346 }
347
348 EXEC_FREE(buf, size);
349
350 return -1;
351 }
352 #if defined (L_execvp)
353 libc_hidden_def(execvp)
354 #elif defined (L_execvpe)
355 libc_hidden_def(execvpe)
356 #endif
357
358 #endif /* #if defined (L_execvp) || defined(L_execvpe) */
359 /**********************************************************************/
360