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?
30 /* libc_hidden_proto(execl) */
31 /* libc_hidden_proto(execle) */
32 /* libc_hidden_proto(execlp) */
33 /* libc_hidden_proto(execv) */
34 /* libc_hidden_proto(execvp) */
36 /* Experimentally off - libc_hidden_proto(memcpy) */
37 /* Experimentally off - libc_hidden_proto(strchr) */
38 /* Experimentally off - libc_hidden_proto(strlen) */
39 /* libc_hidden_proto(execve) */
40 /* libc_hidden_proto(mmap) */
41 /* libc_hidden_proto(munmap) */
42 /* libc_hidden_proto(getenv) */
44 /**********************************************************************/
45 #define EXEC_FUNC_COMMON 0
46 #define EXEC_FUNC_EXECVP 1
47 #if defined(__ARCH_USE_MMU__)
49 /* We have an MMU, so use alloca() to grab space for buffers and arg lists. */
51 # define EXEC_ALLOC_SIZE(VAR) /* nothing to do */
52 # define EXEC_ALLOC(SIZE,VAR,FUNC) alloca((SIZE))
53 # define EXEC_FREE(PTR,VAR) ((void)0)
57 /* We do not have an MMU, so using alloca() is not an option (as this will
58 * easily overflow the stack in most setups). Less obviously, using malloc()
59 * is not an option either since malloc()ed memory can leak in from a vfork()ed
60 * child into the parent as no one is around after the child calls exec*() to
61 * free() the memory. Therefore, we must use mmap() and unmap() directly,
62 * caching the result as we go. This way we minimize the leak by reusing the
63 * memory with every call to an exec*().
65 * To prevent recursive use of the same cached memory, we have to give execvp()
66 * its own cache. Here are the nested exec calls (a/-: alloc/no-alloc):
67 * execve(-) -> calls straight to kernel
68 * execl(a) -> execve(-)
69 * execlp(a) -> execvp(a) !! recursive usage !!
70 * execle(a) -> execve(-)
71 * execv(-) -> execve(-)
72 * execvp(a) -> execve(-)
75 # define EXEC_ALLOC_SIZE(VAR) /* nothing to do */
76 # define EXEC_ALLOC(SIZE,VAR,FUNC) __exec_alloc((SIZE), FUNC)
77 # define EXEC_FREE(PTR,VAR) ((void)0)
79 extern void *__exec_alloc(size_t size, int func) attribute_hidden;
81 # ifdef L___exec_alloc
83 void attribute_hidden *__exec_alloc(size_t size, int func)
85 static void *common_cache, *execvp_cache;
86 static size_t common_size, execvp_size;
88 void **cache = (func ? &execvp_cache : &common_cache);
89 size_t *cache_size = (func ? &execvp_size : &common_size);
91 if (*cache_size >= size)
94 munmap(*cache, *cache_size);
97 return *cache = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
99 /* We don't actually handle OOM in the exec funcs ...
100 if (*cache != MAP_FAILED)
103 return (*cache = NULL);
110 /**********************************************************************/
113 int execl(const char *path, const char *arg, ...)
115 EXEC_ALLOC_SIZE(size) /* Do NOT add a semicolon! */
125 } while (va_arg(args, char *));
128 p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size, EXEC_FUNC_COMMON);
134 *++p = va_arg(args, char *);
138 n = execve(path, (char *const *) argv, __environ);
140 EXEC_FREE(argv, size);
144 libc_hidden_def(execl)
147 /**********************************************************************/
150 int execv(__const char *path, char *__const argv[])
152 return execve(path, argv, __environ);
154 libc_hidden_def(execv)
157 /**********************************************************************/
160 int execle(const char *path, const char *arg, ...)
162 EXEC_ALLOC_SIZE(size) /* Do NOT add a semicolon! */
173 } while (va_arg(args, char *));
174 envp = va_arg(args, char *const *); /* Varies from execl and execlp. */
177 p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size, EXEC_FUNC_COMMON);
183 *++p = va_arg(args, char *);
187 n = execve(path, (char *const *) argv, envp);
189 EXEC_FREE(argv, size);
193 libc_hidden_def(execle)
196 /**********************************************************************/
199 int execlp(const char *file, const char *arg, ...)
201 EXEC_ALLOC_SIZE(size) /* Do NOT add a semicolon! */
211 } while (va_arg(args, char *));
214 p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size, EXEC_FUNC_COMMON);
220 *++p = va_arg(args, char *);
224 n = execvp(file, (char *const *) argv);
226 EXEC_FREE(argv, size);
230 libc_hidden_def(execlp)
233 /**********************************************************************/
236 /* Experimentally off - libc_hidden_proto(strchrnul) */
238 /* Use a default path that matches glibc behavior, since SUSv3 says
239 * this is implementation-defined. The default is current working dir,
240 * /bin, and then /usr/bin. */
241 static const char default_path[] = ":/bin:/usr/bin";
243 int execvp(const char *path, char *const argv[])
250 EXEC_ALLOC_SIZE(size = 0) /* Do NOT add a semicolon! */
254 if (!path || !*path) { /* Comply with SUSv3. */
260 if (strchr(path, '/')) {
261 execve(path, argv, __environ);
262 if (errno == ENOEXEC) {
264 EXEC_ALLOC_SIZE(size2) /* Do NOT add a semicolon! */
267 /* Need the dimension - 1. We omit counting the trailing
268 * NULL but we actually omit the first entry. */
269 for (n=0 ; argv[n] ; n++) {}
270 nargv = (char **) EXEC_ALLOC((n+2) * sizeof(char *), size2, EXEC_FUNC_EXECVP);
272 nargv[1] = (char *)path;
273 memcpy(nargv+2, argv+1, n*sizeof(char *));
274 execve("/bin/sh", nargv, __environ);
275 EXEC_FREE(nargv, size2);
278 if ((p = getenv("PATH")) != NULL) {
283 p = (char *) default_path;
287 if (plen > (FILENAME_MAX - 1)) {
289 __set_errno(ENAMETOOLONG);
292 len = (FILENAME_MAX - 1) - plen;
294 buf = EXEC_ALLOC(FILENAME_MAX, size, EXEC_FUNC_EXECVP);
298 memcpy(s0, path, plen+1);
302 e = strchrnul(p, ':');
316 execve(s, argv, __environ);
320 if (errno == ENOEXEC) {
337 EXEC_FREE(buf, size);
341 libc_hidden_def(execvp)
344 /**********************************************************************/