123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <paths.h>
- #include <string.h>
- #include <errno.h>
- #include <stdarg.h>
- #include <limits.h>
- #include <unistd.h>
- #include <sys/mman.h>
- #define EXEC_FUNC_COMMON 0
- #define EXEC_FUNC_EXECVP 1
- #define EXEC_FUNC_EXECVPE 2
- #if defined(__ARCH_USE_MMU__)
- # define EXEC_ALLOC_SIZE(VAR)
- # define EXEC_ALLOC(SIZE,VAR,FUNC) alloca((SIZE))
- # define EXEC_FREE(PTR,VAR) ((void)0)
- #else
- # define EXEC_ALLOC_SIZE(VAR)
- # define EXEC_ALLOC(SIZE,VAR,FUNC) __exec_alloc((SIZE), FUNC)
- # define EXEC_FREE(PTR,VAR) ((void)0)
- extern void *__exec_alloc(size_t size, int func) attribute_hidden;
- # ifdef L___exec_alloc
- void *__exec_alloc(size_t size, int func)
- {
- static void *common_cache, *execvp_cache;
- static size_t common_size, execvp_size;
- void **cache = (func ? &execvp_cache : &common_cache);
- size_t *cache_size = (func ? &execvp_size : &common_size);
- if (*cache_size >= size)
- return *cache;
- else if (*cache)
- munmap(*cache, *cache_size);
- *cache_size = size;
- return *cache = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
-
- }
- # endif
- #endif
- #ifdef L_execl
- int execl(const char *path, const char *arg, ...)
- {
- EXEC_ALLOC_SIZE(size)
- int n;
- char **argv;
- char **p;
- va_list args;
- n = 0;
- va_start(args, arg);
- do {
- ++n;
- } while (va_arg(args, char *));
- va_end(args);
- p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size, EXEC_FUNC_COMMON);
- p[0] = (char *)arg;
- va_start(args, arg);
- do {
- *++p = va_arg(args, char *);
- } while (--n);
- va_end(args);
- n = execve(path, (char *const *) argv, __environ);
- EXEC_FREE(argv, size);
- return n;
- }
- libc_hidden_def(execl)
- #endif
- #ifdef L_execv
- int execv(const char *path, char *const argv[])
- {
- return execve(path, argv, __environ);
- }
- libc_hidden_def(execv)
- #endif
- #ifdef L_execle
- int execle(const char *path, const char *arg, ...)
- {
- EXEC_ALLOC_SIZE(size)
- int n;
- char **argv;
- char **p;
- char *const *envp;
- va_list args;
- n = 0;
- va_start(args, arg);
- do {
- ++n;
- } while (va_arg(args, char *));
- envp = va_arg(args, char *const *);
- va_end(args);
- p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size, EXEC_FUNC_COMMON);
- p[0] = (char *)arg;
- va_start(args, arg);
- do {
- *++p = va_arg(args, char *);
- } while (--n);
- va_end(args);
- n = execve(path, (char *const *) argv, envp);
- EXEC_FREE(argv, size);
- return n;
- }
- libc_hidden_def(execle)
- #endif
- #ifdef L_execlp
- int execlp(const char *file, const char *arg, ...)
- {
- EXEC_ALLOC_SIZE(size)
- int n;
- char **argv;
- char **p;
- va_list args;
- n = 0;
- va_start(args, arg);
- do {
- ++n;
- } while (va_arg(args, char *));
- va_end(args);
- p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size, EXEC_FUNC_COMMON);
- p[0] = (char *)arg;
- va_start(args, arg);
- do {
- *++p = va_arg(args, char *);
- } while (--n);
- va_end(args);
- n = execvp(file, (char *const *) argv);
- EXEC_FREE(argv, size);
- return n;
- }
- libc_hidden_def(execlp)
- #endif
- #if defined (L_execvp) || defined(L_execvpe)
- static const char default_path[] = ":/bin:/usr/bin";
- #if defined (L_execvp)
- int execvp(const char *path, char *const argv[])
- #elif defined (L_execvpe)
- int execvpe(const char *path, char *const argv[], char *const envp[])
- #endif
- {
- char *buf = NULL;
- char *p;
- char *e;
- char *s0;
- char *s;
- EXEC_ALLOC_SIZE(size = 0)
- size_t len;
- size_t plen;
- if (!path || !*path) {
- BAD:
- __set_errno(ENOENT);
- return -1;
- }
- if (strchr(path, '/')) {
- #if defined (L_execvp)
- execve(path, argv, __environ);
- #elif defined (L_execvpe)
- execve(path, argv, envp);
- #endif
- if (errno == ENOEXEC) {
- char **nargv;
- EXEC_ALLOC_SIZE(size2)
- size_t n;
- RUN_BIN_SH:
-
- for (n=0 ; argv[n] ; n++) {}
- #if defined (L_execvp)
- nargv = (char **) EXEC_ALLOC((n+2) * sizeof(char *), size2, EXEC_FUNC_EXECVP);
- #elif defined (L_execvpe)
- nargv = (char **) EXEC_ALLOC((n+2) * sizeof(char *), size2, EXEC_FUNC_EXECVPE);
- #endif
- nargv[0] = argv[0];
- nargv[1] = (char *)path;
- memcpy(nargv+2, argv+1, n*sizeof(char *));
- #if defined (L_execvp)
- execve(_PATH_BSHELL, nargv, __environ);
- #elif defined (L_execvpe)
- execve(_PATH_BSHELL, nargv, envp);
- #endif
- EXEC_FREE(nargv, size2);
- }
- } else {
- if ((p = getenv("PATH")) != NULL) {
- if (!*p) {
- goto BAD;
- }
- } else {
- p = (char *) default_path;
- }
- plen = strlen(path);
- if (plen > (FILENAME_MAX - 1)) {
- ALL_TOO_LONG:
- __set_errno(ENAMETOOLONG);
- return -1;
- }
- len = (FILENAME_MAX - 1) - plen;
- #if defined (L_execvp)
- buf = EXEC_ALLOC(FILENAME_MAX, size, EXEC_FUNC_EXECVP);
- #elif defined (L_execvpe)
- buf = EXEC_ALLOC(FILENAME_MAX, size, EXEC_FUNC_EXECVPE);
- #endif
- {
- int seen_small = 0;
- s0 = buf + len;
- memcpy(s0, path, plen+1);
- do {
- s = s0;
- e = strchrnul(p, ':');
- if (e > p) {
- plen = e - p;
- if (e[-1] != '/') {
- ++plen;
- }
- if (plen > len) {
- goto NEXT;
- }
- s -= plen;
- memcpy(s, p, plen);
- s[plen-1] = '/';
- }
- #if defined (L_execvp)
- execve(s, argv, __environ);
- #elif defined (L_execvpe)
- execve(s, argv, envp);
- #endif
- seen_small = 1;
- if (errno == ENOEXEC) {
- path = s;
- goto RUN_BIN_SH;
- }
- NEXT:
- if (!*e) {
- if (!seen_small) {
- goto ALL_TOO_LONG;
- }
- break;
- }
- p = e + 1;
- } while (1);
- }
- }
- EXEC_FREE(buf, size);
- return -1;
- }
- #if defined (L_execvp)
- libc_hidden_def(execvp)
- #elif defined (L_execvpe)
- libc_hidden_def(execvpe)
- #endif
- #endif
|