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?
33 /**********************************************************************/
34 #define EXEC_FUNC_COMMON 0
35 #define EXEC_FUNC_EXECVP 1
36 #define EXEC_FUNC_EXECVPE 2
38 #if defined(__ARCH_USE_MMU__)
40 /* We have an MMU, so use alloca() to grab space for buffers and arg lists. */
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)
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*().
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(-)
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)
71 extern void *__exec_alloc(size_t size
, int func
) attribute_hidden
;
73 # ifdef L___exec_alloc
75 void *__exec_alloc(size_t size
, int func
)
77 static void *common_cache
, *execvp_cache
;
78 static size_t common_size
, execvp_size
;
80 void **cache
= (func
? &execvp_cache
: &common_cache
);
81 size_t *cache_size
= (func
? &execvp_size
: &common_size
);
83 if (*cache_size
>= size
)
86 munmap(*cache
, *cache_size
);
89 return *cache
= mmap(0, size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
|MAP_ANONYMOUS
, -1, 0);
91 /* We don't actually handle OOM in the exec funcs ...
92 if (*cache != MAP_FAILED)
95 return (*cache = NULL);
102 /**********************************************************************/
105 int execl(const char *path
, const char *arg
, ...)
107 EXEC_ALLOC_SIZE(size
) /* Do NOT add a semicolon! */
117 } while (va_arg(args
, char *));
120 p
= argv
= (char **) EXEC_ALLOC((n
+1) * sizeof(char *), size
, EXEC_FUNC_COMMON
);
126 *++p
= va_arg(args
, char *);
130 n
= execve(path
, (char *const *) argv
, __environ
);
132 EXEC_FREE(argv
, size
);
136 libc_hidden_def(execl
)
139 /**********************************************************************/
142 int execv(const char *path
, char *const argv
[])
144 return execve(path
, argv
, __environ
);
146 libc_hidden_def(execv
)
149 /**********************************************************************/
152 int execle(const char *path
, const char *arg
, ...)
154 EXEC_ALLOC_SIZE(size
) /* Do NOT add a semicolon! */
165 } while (va_arg(args
, char *));
166 envp
= va_arg(args
, char *const *); /* Varies from execl and execlp. */
169 p
= argv
= (char **) EXEC_ALLOC((n
+1) * sizeof(char *), size
, EXEC_FUNC_COMMON
);
175 *++p
= va_arg(args
, char *);
179 n
= execve(path
, (char *const *) argv
, envp
);
181 EXEC_FREE(argv
, size
);
185 libc_hidden_def(execle
)
188 /**********************************************************************/
191 int execlp(const char *file
, const char *arg
, ...)
193 EXEC_ALLOC_SIZE(size
) /* Do NOT add a semicolon! */
203 } while (va_arg(args
, char *));
206 p
= argv
= (char **) EXEC_ALLOC((n
+1) * sizeof(char *), size
, EXEC_FUNC_COMMON
);
212 *++p
= va_arg(args
, char *);
216 n
= execvp(file
, (char *const *) argv
);
218 EXEC_FREE(argv
, size
);
222 libc_hidden_def(execlp
)
225 /**********************************************************************/
226 #if defined (L_execvp) || defined(L_execvpe)
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)
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
[])
244 EXEC_ALLOC_SIZE(size
= 0) /* Do NOT add a semicolon! */
248 if (!path
|| !*path
) { /* Comply with SUSv3. */
254 if (strchr(path
, '/')) {
255 #if defined (L_execvp)
256 execve(path
, argv
, __environ
);
257 #elif defined (L_execvpe)
258 execve(path
, argv
, envp
);
260 if (errno
== ENOEXEC
) {
262 EXEC_ALLOC_SIZE(size2
) /* Do NOT add a semicolon! */
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
);
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
);
281 EXEC_FREE(nargv
, size2
);
284 if ((p
= getenv("PATH")) != NULL
) {
289 p
= (char *) default_path
;
293 if (plen
> (FILENAME_MAX
- 1)) {
295 __set_errno(ENAMETOOLONG
);
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
);
307 memcpy(s0
, path
, plen
+1);
311 e
= strchrnul(p
, ':');
325 #if defined (L_execvp)
326 execve(s
, argv
, __environ
);
327 #elif defined (L_execvpe)
328 execve(s
, argv
, envp
);
332 if (errno
== ENOEXEC
) {
349 EXEC_FREE(buf
, size
);
353 #if defined (L_execvp)
354 libc_hidden_def(execvp
)
355 #elif defined (L_execvpe)
356 libc_hidden_def(execvpe
)
359 #endif /* #if defined (L_execvp) || defined(L_execvpe) */
360 /**********************************************************************/