exec.c 6.9 KB

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