exec.c 7.4 KB

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