exec.c 7.8 KB

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