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