1 /* Copyright (C) 2004 Manuel Novoa III
3 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
7 * Initial version of a SUSv3 compliant exec*() functions.
9 * Sigh... Fall back to alloca() if munmap() is broken on uClinux.
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?
32 /**********************************************************************/
33 #define EXEC_FUNC_COMMON 0
34 #define EXEC_FUNC_EXECVP 1
35 #define EXEC_FUNC_EXECVPE 2
37 #if defined(__ARCH_USE_MMU__)
39 /* We have an MMU, so use alloca() to grab space for buffers and arg lists. */
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)
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*().
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(-)
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)
70 extern void *__exec_alloc(size_t size, int func) attribute_hidden;
72 # ifdef L___exec_alloc
74 void *__exec_alloc(size_t size, int func)
76 static void *common_cache, *execvp_cache;
77 static size_t common_size, execvp_size;
79 void **cache = (func ? &execvp_cache : &common_cache);
80 size_t *cache_size = (func ? &execvp_size : &common_size);
82 if (*cache_size >= size)
85 munmap(*cache, *cache_size);
88 return *cache = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
90 /* We don't actually handle OOM in the exec funcs ...
91 if (*cache != MAP_FAILED)
94 return (*cache = NULL);
101 /**********************************************************************/
104 int execl(const char *path, const char *arg, ...)
106 EXEC_ALLOC_SIZE(size) /* Do NOT add a semicolon! */
116 } while (va_arg(args, char *));
119 p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size, EXEC_FUNC_COMMON);
125 *++p = va_arg(args, char *);
129 n = execve(path, (char *const *) argv, __environ);
131 EXEC_FREE(argv, size);
135 libc_hidden_def(execl)
138 /**********************************************************************/
141 int execv(const char *path, char *const argv[])
143 return execve(path, argv, __environ);
145 libc_hidden_def(execv)
148 /**********************************************************************/
151 int execle(const char *path, const char *arg, ...)
153 EXEC_ALLOC_SIZE(size) /* Do NOT add a semicolon! */
164 } while (va_arg(args, char *));
165 envp = va_arg(args, char *const *); /* Varies from execl and execlp. */
168 p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size, EXEC_FUNC_COMMON);
174 *++p = va_arg(args, char *);
178 n = execve(path, (char *const *) argv, envp);
180 EXEC_FREE(argv, size);
184 libc_hidden_def(execle)
187 /**********************************************************************/
190 int execlp(const char *file, const char *arg, ...)
192 EXEC_ALLOC_SIZE(size) /* Do NOT add a semicolon! */
202 } while (va_arg(args, char *));
205 p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size, EXEC_FUNC_COMMON);
211 *++p = va_arg(args, char *);
215 n = execvp(file, (char *const *) argv);
217 EXEC_FREE(argv, size);
221 libc_hidden_def(execlp)
224 /**********************************************************************/
225 #if defined (L_execvp) || defined(L_execvpe)
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)
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[])
243 EXEC_ALLOC_SIZE(size = 0) /* Do NOT add a semicolon! */
247 if (!path || !*path) { /* Comply with SUSv3. */
253 if (strchr(path, '/')) {
254 #if defined (L_execvp)
255 execve(path, argv, __environ);
256 #elif defined (L_execvpe)
257 execve(path, argv, envp);
259 if (errno == ENOEXEC) {
261 EXEC_ALLOC_SIZE(size2) /* Do NOT add a semicolon! */
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);
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);
280 EXEC_FREE(nargv, size2);
283 if ((p = getenv("PATH")) != NULL) {
288 p = (char *) default_path;
292 if (plen > (FILENAME_MAX - 1)) {
294 __set_errno(ENAMETOOLONG);
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);
306 memcpy(s0, path, plen+1);
310 e = strchrnul(p, ':');
324 #if defined (L_execvp)
325 execve(s, argv, __environ);
326 #elif defined (L_execvpe)
327 execve(s, argv, envp);
331 if (errno == ENOEXEC) {
348 EXEC_FREE(buf, size);
352 #if defined (L_execvp)
353 libc_hidden_def(execvp)
354 #elif defined (L_execvpe)
355 libc_hidden_def(execvpe)
358 #endif /* #if defined (L_execvp) || defined(L_execvpe) */
359 /**********************************************************************/