exec.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. #define EXEC_FUNC_EXECVPE 2
  30. #if defined(__ARCH_USE_MMU__)
  31. /* We have an MMU, so use alloca() to grab space for buffers and arg lists. */
  32. # define EXEC_ALLOC_SIZE(VAR) /* nothing to do */
  33. # define EXEC_ALLOC(SIZE,VAR,FUNC) alloca((SIZE))
  34. # define EXEC_FREE(PTR,VAR) ((void)0)
  35. #else
  36. /* We do not have an MMU, so using alloca() is not an option (as this will
  37. * easily overflow the stack in most setups). Less obviously, using malloc()
  38. * is not an option either since malloc()ed memory can leak in from a vfork()ed
  39. * child into the parent as no one is around after the child calls exec*() to
  40. * free() the memory. Therefore, we must use mmap() and unmap() directly,
  41. * caching the result as we go. This way we minimize the leak by reusing the
  42. * memory with every call to an exec*().
  43. *
  44. * To prevent recursive use of the same cached memory, we have to give execvp()
  45. * its own cache. Here are the nested exec calls (a/-: alloc/no-alloc):
  46. * execve(-) -> calls straight to kernel
  47. * execl(a) -> execve(-)
  48. * execlp(a) -> execvp(a) !! recursive usage !!
  49. * execle(a) -> execve(-)
  50. * execv(-) -> execve(-)
  51. * execvp(a) -> execve(-)
  52. * execvpe(a) -> execve(-)
  53. */
  54. # define EXEC_ALLOC_SIZE(VAR) /* nothing to do */
  55. # define EXEC_ALLOC(SIZE,VAR,FUNC) __exec_alloc((SIZE), FUNC)
  56. # define EXEC_FREE(PTR,VAR) ((void)0)
  57. extern void *__exec_alloc(size_t size, int func) attribute_hidden;
  58. # ifdef L___exec_alloc
  59. void *__exec_alloc(size_t size, int func)
  60. {
  61. static void *common_cache, *execvp_cache;
  62. static size_t common_size, execvp_size;
  63. void **cache = (func ? &execvp_cache : &common_cache);
  64. size_t *cache_size = (func ? &execvp_size : &common_size);
  65. if (*cache_size >= size)
  66. return *cache;
  67. else if (*cache)
  68. munmap(*cache, *cache_size);
  69. *cache_size = size;
  70. return *cache = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
  71. /* We don't actually handle OOM in the exec funcs ...
  72. if (*cache != MAP_FAILED)
  73. return *cache;
  74. else
  75. return (*cache = NULL);
  76. */
  77. }
  78. # endif
  79. #endif
  80. /**********************************************************************/
  81. #ifdef L_execl
  82. int execl(const char *path, const char *arg, ...)
  83. {
  84. EXEC_ALLOC_SIZE(size) /* Do NOT add a semicolon! */
  85. int n;
  86. char **argv;
  87. char **p;
  88. va_list args;
  89. n = 0;
  90. va_start(args, arg);
  91. do {
  92. ++n;
  93. } while (va_arg(args, char *));
  94. va_end(args);
  95. p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size, EXEC_FUNC_COMMON);
  96. p[0] = (char *)arg;
  97. va_start(args, arg);
  98. do {
  99. *++p = va_arg(args, char *);
  100. } while (--n);
  101. va_end(args);
  102. n = execve(path, (char *const *) argv, __environ);
  103. EXEC_FREE(argv, size);
  104. return n;
  105. }
  106. libc_hidden_def(execl)
  107. #endif
  108. /**********************************************************************/
  109. #ifdef L_execv
  110. int execv(const char *path, char *const argv[])
  111. {
  112. return execve(path, argv, __environ);
  113. }
  114. libc_hidden_def(execv)
  115. #endif
  116. /**********************************************************************/
  117. #ifdef L_execle
  118. int execle(const char *path, const char *arg, ...)
  119. {
  120. EXEC_ALLOC_SIZE(size) /* Do NOT add a semicolon! */
  121. int n;
  122. char **argv;
  123. char **p;
  124. char *const *envp;
  125. va_list args;
  126. n = 0;
  127. va_start(args, arg);
  128. do {
  129. ++n;
  130. } while (va_arg(args, char *));
  131. envp = va_arg(args, char *const *); /* Varies from execl and execlp. */
  132. va_end(args);
  133. p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size, EXEC_FUNC_COMMON);
  134. p[0] = (char *)arg;
  135. va_start(args, arg);
  136. do {
  137. *++p = va_arg(args, char *);
  138. } while (--n);
  139. va_end(args);
  140. n = execve(path, (char *const *) argv, envp);
  141. EXEC_FREE(argv, size);
  142. return n;
  143. }
  144. libc_hidden_def(execle)
  145. #endif
  146. /**********************************************************************/
  147. #ifdef L_execlp
  148. int execlp(const char *file, const char *arg, ...)
  149. {
  150. EXEC_ALLOC_SIZE(size) /* Do NOT add a semicolon! */
  151. int n;
  152. char **argv;
  153. char **p;
  154. va_list args;
  155. n = 0;
  156. va_start(args, arg);
  157. do {
  158. ++n;
  159. } while (va_arg(args, char *));
  160. va_end(args);
  161. p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size, EXEC_FUNC_COMMON);
  162. p[0] = (char *)arg;
  163. va_start(args, arg);
  164. do {
  165. *++p = va_arg(args, char *);
  166. } while (--n);
  167. va_end(args);
  168. n = execvp(file, (char *const *) argv);
  169. EXEC_FREE(argv, size);
  170. return n;
  171. }
  172. libc_hidden_def(execlp)
  173. #endif
  174. /**********************************************************************/
  175. #if defined (L_execvp) || defined(L_execvpe)
  176. /* Use a default path that matches glibc behavior, since SUSv3 says
  177. * this is implementation-defined. The default is current working dir,
  178. * /bin, and then /usr/bin. */
  179. static const char default_path[] = ":/bin:/usr/bin";
  180. #if defined (L_execvp)
  181. int execvp(const char *path, char *const argv[])
  182. #elif defined (L_execvpe)
  183. int execvpe(const char *path, char *const argv[], char *const envp[])
  184. #endif
  185. {
  186. char *buf = NULL;
  187. char *p;
  188. char *e;
  189. char *s0;
  190. char *s;
  191. EXEC_ALLOC_SIZE(size = 0) /* Do NOT add a semicolon! */
  192. size_t len;
  193. size_t plen;
  194. if (!path || !*path) { /* Comply with SUSv3. */
  195. BAD:
  196. __set_errno(ENOENT);
  197. return -1;
  198. }
  199. if (strchr(path, '/')) {
  200. #if defined (L_execvp)
  201. execve(path, argv, __environ);
  202. #elif defined (L_execvpe)
  203. execve(path, argv, envp);
  204. #endif
  205. if (errno == ENOEXEC) {
  206. char **nargv;
  207. EXEC_ALLOC_SIZE(size2) /* Do NOT add a semicolon! */
  208. size_t n;
  209. RUN_BIN_SH:
  210. /* Need the dimension - 1. We omit counting the trailing
  211. * NULL but we actually omit the first entry. */
  212. for (n=0 ; argv[n] ; n++) {}
  213. #if defined (L_execvp)
  214. nargv = (char **) EXEC_ALLOC((n+2) * sizeof(char *), size2, EXEC_FUNC_EXECVP);
  215. #elif defined (L_execvpe)
  216. nargv = (char **) EXEC_ALLOC((n+2) * sizeof(char *), size2, EXEC_FUNC_EXECVPE);
  217. #endif
  218. nargv[0] = argv[0];
  219. nargv[1] = (char *)path;
  220. memcpy(nargv+2, argv+1, n*sizeof(char *));
  221. #if defined (L_execvp)
  222. execve("/bin/sh", nargv, __environ);
  223. #elif defined (L_execvpe)
  224. execve("/bin/sh", nargv, envp);
  225. #endif
  226. EXEC_FREE(nargv, size2);
  227. }
  228. } else {
  229. if ((p = getenv("PATH")) != NULL) {
  230. if (!*p) {
  231. goto BAD;
  232. }
  233. } else {
  234. p = (char *) default_path;
  235. }
  236. plen = strlen(path);
  237. if (plen > (FILENAME_MAX - 1)) {
  238. ALL_TOO_LONG:
  239. __set_errno(ENAMETOOLONG);
  240. return -1;
  241. }
  242. len = (FILENAME_MAX - 1) - plen;
  243. #if defined (L_execvp)
  244. buf = EXEC_ALLOC(FILENAME_MAX, size, EXEC_FUNC_EXECVP);
  245. #elif defined (L_execvpe)
  246. buf = EXEC_ALLOC(FILENAME_MAX, size, EXEC_FUNC_EXECVPE);
  247. #endif
  248. {
  249. int seen_small = 0;
  250. s0 = buf + len;
  251. memcpy(s0, path, plen+1);
  252. do {
  253. s = s0;
  254. e = strchrnul(p, ':');
  255. if (e > p) {
  256. plen = e - p;
  257. if (e[-1] != '/') {
  258. ++plen;
  259. }
  260. if (plen > len) {
  261. goto NEXT;
  262. }
  263. s -= plen;
  264. memcpy(s, p, plen);
  265. s[plen-1] = '/';
  266. }
  267. #if defined (L_execvp)
  268. execve(s, argv, __environ);
  269. #elif defined (L_execvpe)
  270. execve(s, argv, envp);
  271. #endif
  272. seen_small = 1;
  273. if (errno == ENOEXEC) {
  274. path = s;
  275. goto RUN_BIN_SH;
  276. }
  277. NEXT:
  278. if (!*e) {
  279. if (!seen_small) {
  280. goto ALL_TOO_LONG;
  281. }
  282. break;
  283. }
  284. p = e + 1;
  285. } while (1);
  286. }
  287. }
  288. EXEC_FREE(buf, size);
  289. return -1;
  290. }
  291. #if defined (L_execvp)
  292. libc_hidden_def(execvp)
  293. #elif defined (L_execvpe)
  294. libc_hidden_def(execvpe)
  295. #endif
  296. #endif /* #if defined (L_execvp) || defined(L_execvpe) */
  297. /**********************************************************************/