exec.c 7.3 KB

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