exec.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* Copyright (C) 2004 Manuel Novoa III
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Library General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Library General Public
  14. * License along with this library; if not, write to the Free
  15. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. /* Jan 1, 2004
  18. * Initial version of a SUSv3 compliant exec*() functions.
  19. * Feb 17, 2004
  20. * Sigh... Fall back to alloca() if munmap() is broken on uClinux.
  21. */
  22. /* NOTE: Strictly speaking, there could be problems from accessing
  23. * __environ in multithreaded programs. The only way around this
  24. * that I see is to essentially lock __environ access (modifying
  25. * the setenv code), make a copy of the environment table (just the
  26. * pointers since the strings themselves are never freed), and then
  27. * unlock prior to the execve call. If that fails, then we'd need
  28. * to free the storage allocated for the copy. Better ideas anyone?
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <errno.h>
  34. #include <stdarg.h>
  35. #include <limits.h>
  36. #include <unistd.h>
  37. #include <sys/mman.h>
  38. libc_hidden_proto(execl)
  39. libc_hidden_proto(execvp)
  40. libc_hidden_proto(memcpy)
  41. libc_hidden_proto(strchr)
  42. libc_hidden_proto(strlen)
  43. libc_hidden_proto(strchrnul)
  44. libc_hidden_proto(execve)
  45. libc_hidden_proto(mmap)
  46. libc_hidden_proto(munmap)
  47. libc_hidden_proto(getenv)
  48. libc_hidden_proto(__environ)
  49. /**********************************************************************/
  50. #if defined(__ARCH_USE_MMU__) || defined(__UCLIBC_UCLINUX_BROKEN_MUNMAP__)
  51. /* We have an MMU, so use alloca() to grab space for buffers and
  52. * arg lists. Also fall back to alloca() if munmap() is broken. */
  53. # define EXEC_ALLOC_SIZE(VAR) /* nothing to do */
  54. # define EXEC_ALLOC(SIZE,VAR) alloca((SIZE))
  55. # define EXEC_FREE(PTR,VAR) ((void)0)
  56. #else
  57. /* We do not have an MMU, so using alloca() is not an option.
  58. * Less obviously, using malloc() is not an option either since
  59. * malloc()ed memory can leak in a vfork() and exec*() situation.
  60. * Therefore, we must use mmap() and unmap() directly.
  61. */
  62. # define EXEC_ALLOC_SIZE(VAR) size_t VAR; /* Semicolon included! */
  63. # define EXEC_ALLOC(SIZE,VAR) __exec_alloc((VAR = (SIZE)))
  64. # define EXEC_FREE(PTR,VAR) __exec_free((PTR),(VAR))
  65. extern void *__exec_alloc(size_t size) attribute_hidden;
  66. extern void __exec_free(void *ptr, size_t size) attribute_hidden;
  67. # ifdef L___exec_alloc
  68. void attribute_hidden *__exec_alloc(size_t size)
  69. {
  70. void *p;
  71. p = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
  72. return (p != MAP_FAILED) ? p : NULL;
  73. }
  74. void attribute_hidden __exec_free(void *ptr, size_t size)
  75. {
  76. if (ptr) {
  77. munmap(ptr, size);
  78. }
  79. }
  80. # endif
  81. #endif
  82. /**********************************************************************/
  83. #ifdef L_execl
  84. int execl(const char *path, const char *arg, ...)
  85. {
  86. EXEC_ALLOC_SIZE(size) /* Do NOT add a semicolon! */
  87. int n;
  88. char **argv;
  89. char **p;
  90. va_list args;
  91. n = 0;
  92. va_start(args, arg);
  93. do {
  94. ++n;
  95. } while (va_arg(args, char *));
  96. va_end(args);
  97. p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size);
  98. p[0] = (char *)arg;
  99. va_start(args, arg);
  100. do {
  101. *++p = va_arg(args, char *);
  102. } while (--n);
  103. va_end(args);
  104. n = execve(path, (char *const *) argv, __environ);
  105. EXEC_FREE(argv, size);
  106. return n;
  107. }
  108. libc_hidden_def(execl)
  109. #endif
  110. /**********************************************************************/
  111. #ifdef L_execv
  112. int execv(__const char *path, char *__const argv[])
  113. {
  114. return execve(path, argv, __environ);
  115. }
  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);
  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. #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);
  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. #endif
  173. /**********************************************************************/
  174. #ifdef L_execvp
  175. /* Use a default path that matches glibc behavior, since SUSv3 says
  176. * this is implementation-defined. The default is current working dir,
  177. * /bin, and then /usr/bin. */
  178. static const char default_path[] = ":/bin:/usr/bin";
  179. int execvp(const char *path, char *const argv[])
  180. {
  181. char *buf = NULL;
  182. char *p;
  183. char *e;
  184. char *s0;
  185. char *s;
  186. EXEC_ALLOC_SIZE(size = 0) /* Do NOT add a semicolon! */
  187. size_t len;
  188. size_t plen;
  189. if (!path || !*path) { /* Comply with SUSv3. */
  190. BAD:
  191. __set_errno(ENOENT);
  192. return -1;
  193. }
  194. if (strchr(path, '/')) {
  195. execve(path, argv, __environ);
  196. CHECK_ENOEXEC:
  197. if (errno == ENOEXEC) {
  198. char **nargv;
  199. EXEC_ALLOC_SIZE(size2) /* Do NOT add a semicolon! */
  200. size_t n;
  201. /* Need the dimension - 1. We omit counting the trailing
  202. * NULL but we actually omit the first entry. */
  203. for (n=0 ; argv[n] ; n++) {}
  204. nargv = (char **) EXEC_ALLOC((n+2) * sizeof(char *), size2);
  205. nargv[0] = argv[0];
  206. nargv[1] = (char *)path;
  207. memcpy(nargv+2, argv+1, n*sizeof(char *));
  208. execve("/bin/sh", nargv, __environ);
  209. EXEC_FREE(nargv, size2);
  210. }
  211. } else {
  212. if ((p = getenv("PATH")) != NULL) {
  213. if (!*p) {
  214. goto BAD;
  215. }
  216. } else {
  217. p = (char *) default_path;
  218. }
  219. plen = strlen(path);
  220. if (plen > (FILENAME_MAX - 1)) {
  221. ALL_TOO_LONG:
  222. __set_errno(ENAMETOOLONG);
  223. return -1;
  224. }
  225. len = (FILENAME_MAX - 1) - plen;
  226. if ((buf = EXEC_ALLOC(FILENAME_MAX, size)) != NULL) {
  227. int seen_small = 0;
  228. s0 = buf + len;
  229. memcpy(s0, path, plen+1);
  230. do {
  231. s = s0;
  232. e = strchrnul(p, ':');
  233. if (e > p) {
  234. plen = e - p;
  235. if (e[-1] != '/') {
  236. ++plen;
  237. }
  238. if (plen > len) {
  239. goto NEXT;
  240. }
  241. s -= plen;
  242. memcpy(s, p, plen);
  243. s[plen-1] = '/';
  244. }
  245. execve(s, argv, __environ);
  246. seen_small = 1;
  247. if (errno != ENOENT) {
  248. path = s;
  249. goto CHECK_ENOEXEC;
  250. }
  251. NEXT:
  252. if (!*e) {
  253. if (!seen_small) {
  254. goto ALL_TOO_LONG;
  255. }
  256. break;
  257. }
  258. p = e + 1;
  259. } while (1);
  260. }
  261. }
  262. EXEC_FREE(buf, size);
  263. return -1;
  264. }
  265. libc_hidden_def(execvp)
  266. #endif
  267. /**********************************************************************/