exec.c 8.2 KB

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