exec.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. /* Jan 1, 2004
  6. * Initial version of a SUSv3 compliant exec*() functions.
  7. * Feb 17, 2004
  8. * Sigh... Fall back to alloca() if munmap() is broken on uClinux.
  9. */
  10. /* NOTE: Strictly speaking, there could be problems from accessing
  11. * __environ in multithreaded programs. The only way around this
  12. * that I see is to essentially lock __environ access (modifying
  13. * the setenv code), make a copy of the environment table (just the
  14. * pointers since the strings themselves are never freed), and then
  15. * unlock prior to the execve call. If that fails, then we'd need
  16. * to free the storage allocated for the copy. Better ideas anyone?
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <stdarg.h>
  23. #include <limits.h>
  24. #include <unistd.h>
  25. #include <sys/mman.h>
  26. /**********************************************************************/
  27. #define EXEC_FUNC_COMMON 0
  28. #define EXEC_FUNC_EXECVP 1
  29. #if defined(__ARCH_USE_MMU__)
  30. /* We have an MMU, so use alloca() to grab space for buffers and arg lists. */
  31. # define EXEC_ALLOC_SIZE(VAR) /* nothing to do */
  32. # define EXEC_ALLOC(SIZE,VAR,FUNC) alloca((SIZE))
  33. # define EXEC_FREE(PTR,VAR) ((void)0)
  34. #else
  35. /* We do not have an MMU, so using alloca() is not an option (as this will
  36. * easily overflow the stack in most setups). Less obviously, using malloc()
  37. * is not an option either since malloc()ed memory can leak in from a vfork()ed
  38. * child into the parent as no one is around after the child calls exec*() to
  39. * free() the memory. Therefore, we must use mmap() and unmap() directly,
  40. * caching the result as we go. This way we minimize the leak by reusing the
  41. * memory with every call to an exec*().
  42. *
  43. * To prevent recursive use of the same cached memory, we have to give execvp()
  44. * its own cache. Here are the nested exec calls (a/-: alloc/no-alloc):
  45. * execve(-) -> calls straight to kernel
  46. * execl(a) -> execve(-)
  47. * execlp(a) -> execvp(a) !! recursive usage !!
  48. * execle(a) -> execve(-)
  49. * execv(-) -> execve(-)
  50. * execvp(a) -> execve(-)
  51. */
  52. # define EXEC_ALLOC_SIZE(VAR) /* nothing to do */
  53. # define EXEC_ALLOC(SIZE,VAR,FUNC) __exec_alloc((SIZE), FUNC)
  54. # define EXEC_FREE(PTR,VAR) ((void)0)
  55. extern void *__exec_alloc(size_t size, int func) attribute_hidden;
  56. # ifdef L___exec_alloc
  57. void attribute_hidden *__exec_alloc(size_t size, int func)
  58. {
  59. static void *common_cache, *execvp_cache;
  60. static size_t common_size, execvp_size;
  61. void **cache = (func ? &execvp_cache : &common_cache);
  62. size_t *cache_size = (func ? &execvp_size : &common_size);
  63. if (*cache_size >= size)
  64. return *cache;
  65. else if (*cache)
  66. munmap(*cache, *cache_size);
  67. *cache_size = size;
  68. return *cache = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
  69. /* We don't actually handle OOM in the exec funcs ...
  70. if (*cache != MAP_FAILED)
  71. return *cache;
  72. else
  73. return (*cache = NULL);
  74. */
  75. }
  76. # endif
  77. #endif
  78. /**********************************************************************/
  79. #ifdef L_execl
  80. int execl(const char *path, const char *arg, ...)
  81. {
  82. EXEC_ALLOC_SIZE(size) /* Do NOT add a semicolon! */
  83. int n;
  84. char **argv;
  85. char **p;
  86. va_list args;
  87. n = 0;
  88. va_start(args, arg);
  89. do {
  90. ++n;
  91. } while (va_arg(args, char *));
  92. va_end(args);
  93. p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size, EXEC_FUNC_COMMON);
  94. p[0] = (char *)arg;
  95. va_start(args, arg);
  96. do {
  97. *++p = va_arg(args, char *);
  98. } while (--n);
  99. va_end(args);
  100. n = execve(path, (char *const *) argv, __environ);
  101. EXEC_FREE(argv, size);
  102. return n;
  103. }
  104. libc_hidden_def(execl)
  105. #endif
  106. /**********************************************************************/
  107. #ifdef L_execv
  108. int execv(__const char *path, char *__const argv[])
  109. {
  110. return execve(path, argv, __environ);
  111. }
  112. libc_hidden_def(execv)
  113. #endif
  114. /**********************************************************************/
  115. #ifdef L_execle
  116. int execle(const char *path, const char *arg, ...)
  117. {
  118. EXEC_ALLOC_SIZE(size) /* Do NOT add a semicolon! */
  119. int n;
  120. char **argv;
  121. char **p;
  122. char *const *envp;
  123. va_list args;
  124. n = 0;
  125. va_start(args, arg);
  126. do {
  127. ++n;
  128. } while (va_arg(args, char *));
  129. envp = va_arg(args, char *const *); /* Varies from execl and execlp. */
  130. va_end(args);
  131. p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size, EXEC_FUNC_COMMON);
  132. p[0] = (char *)arg;
  133. va_start(args, arg);
  134. do {
  135. *++p = va_arg(args, char *);
  136. } while (--n);
  137. va_end(args);
  138. n = execve(path, (char *const *) argv, envp);
  139. EXEC_FREE(argv, size);
  140. return n;
  141. }
  142. libc_hidden_def(execle)
  143. #endif
  144. /**********************************************************************/
  145. #ifdef L_execlp
  146. int execlp(const char *file, const char *arg, ...)
  147. {
  148. EXEC_ALLOC_SIZE(size) /* Do NOT add a semicolon! */
  149. int n;
  150. char **argv;
  151. char **p;
  152. va_list args;
  153. n = 0;
  154. va_start(args, arg);
  155. do {
  156. ++n;
  157. } while (va_arg(args, char *));
  158. va_end(args);
  159. p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size, EXEC_FUNC_COMMON);
  160. p[0] = (char *)arg;
  161. va_start(args, arg);
  162. do {
  163. *++p = va_arg(args, char *);
  164. } while (--n);
  165. va_end(args);
  166. n = execvp(file, (char *const *) argv);
  167. EXEC_FREE(argv, size);
  168. return n;
  169. }
  170. libc_hidden_def(execlp)
  171. #endif
  172. /**********************************************************************/
  173. #ifdef L_execvp
  174. /* Use a default path that matches glibc behavior, since SUSv3 says
  175. * this is implementation-defined. The default is current working dir,
  176. * /bin, and then /usr/bin. */
  177. static const char default_path[] = ":/bin:/usr/bin";
  178. int execvp(const char *path, char *const argv[])
  179. {
  180. char *buf = NULL;
  181. char *p;
  182. char *e;
  183. char *s0;
  184. char *s;
  185. EXEC_ALLOC_SIZE(size = 0) /* Do NOT add a semicolon! */
  186. size_t len;
  187. size_t plen;
  188. if (!path || !*path) { /* Comply with SUSv3. */
  189. BAD:
  190. __set_errno(ENOENT);
  191. return -1;
  192. }
  193. if (strchr(path, '/')) {
  194. execve(path, argv, __environ);
  195. if (errno == ENOEXEC) {
  196. char **nargv;
  197. EXEC_ALLOC_SIZE(size2) /* Do NOT add a semicolon! */
  198. size_t n;
  199. RUN_BIN_SH:
  200. /* Need the dimension - 1. We omit counting the trailing
  201. * NULL but we actually omit the first entry. */
  202. for (n=0 ; argv[n] ; n++) {}
  203. nargv = (char **) EXEC_ALLOC((n+2) * sizeof(char *), size2, EXEC_FUNC_EXECVP);
  204. nargv[0] = argv[0];
  205. nargv[1] = (char *)path;
  206. memcpy(nargv+2, argv+1, n*sizeof(char *));
  207. execve("/bin/sh", nargv, __environ);
  208. EXEC_FREE(nargv, size2);
  209. }
  210. } else {
  211. if ((p = getenv("PATH")) != NULL) {
  212. if (!*p) {
  213. goto BAD;
  214. }
  215. } else {
  216. p = (char *) default_path;
  217. }
  218. plen = strlen(path);
  219. if (plen > (FILENAME_MAX - 1)) {
  220. ALL_TOO_LONG:
  221. __set_errno(ENAMETOOLONG);
  222. return -1;
  223. }
  224. len = (FILENAME_MAX - 1) - plen;
  225. buf = EXEC_ALLOC(FILENAME_MAX, size, EXEC_FUNC_EXECVP);
  226. {
  227. int seen_small = 0;
  228. s0 = buf + len;
  229. memcpy(s0, path, plen+1);
  230. do {
  231. s = s0;
  232. e = strchrnul(p, ':');
  233. if (e > p) {
  234. plen = e - p;
  235. if (e[-1] != '/') {
  236. ++plen;
  237. }
  238. if (plen > len) {
  239. goto NEXT;
  240. }
  241. s -= plen;
  242. memcpy(s, p, plen);
  243. s[plen-1] = '/';
  244. }
  245. execve(s, argv, __environ);
  246. seen_small = 1;
  247. if (errno == ENOEXEC) {
  248. path = s;
  249. goto RUN_BIN_SH;
  250. }
  251. NEXT:
  252. if (!*e) {
  253. if (!seen_small) {
  254. goto ALL_TOO_LONG;
  255. }
  256. break;
  257. }
  258. p = e + 1;
  259. } while (1);
  260. }
  261. }
  262. EXEC_FREE(buf, size);
  263. return -1;
  264. }
  265. libc_hidden_def(execvp)
  266. #endif
  267. /**********************************************************************/