exec.c 6.8 KB

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