exec.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 munmap __munmap
  31. #define execve __execve
  32. #define _GNU_SOURCE
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <errno.h>
  37. #include <stdarg.h>
  38. #include <limits.h>
  39. #include <unistd.h>
  40. #include <sys/mman.h>
  41. extern char *__strchrnul(const char *s, int c);
  42. /**********************************************************************/
  43. #if defined(__ARCH_HAS_MMU__) || defined(__UCLIBC_UCLINUX_BROKEN_MUNMAP__)
  44. /* We have an MMU, so use alloca() to grab space for buffers and
  45. * arg lists. Also fall back to alloca() if munmap() is broken. */
  46. # define EXEC_ALLOC_SIZE(VAR) /* nothing to do */
  47. # define EXEC_ALLOC(SIZE,VAR) alloca((SIZE))
  48. # define EXEC_FREE(PTR,VAR) ((void)0)
  49. #else
  50. /* We do not have an MMU, so using alloca() is not an option.
  51. * Less obviously, using malloc() is not an option either since
  52. * malloc()ed memory can leak in a vfork() and exec*() situation.
  53. * Therefore, we must use mmap() and unmap() directly.
  54. */
  55. # define EXEC_ALLOC_SIZE(VAR) size_t VAR; /* Semicolon included! */
  56. # define EXEC_ALLOC(SIZE,VAR) __exec_alloc((VAR = (SIZE)))
  57. # define EXEC_FREE(PTR,VAR) __exec_free((PTR),(VAR))
  58. extern void *__exec_alloc(size_t size);
  59. extern void __exec_free(void *ptr, size_t size);
  60. # ifdef L___exec_alloc
  61. void *__exec_alloc(size_t size)
  62. {
  63. void *p;
  64. p = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
  65. return (p != MAP_FAILED) ? p : NULL;
  66. }
  67. void __exec_free(void *ptr, size_t size)
  68. {
  69. if (ptr) {
  70. munmap(ptr, size);
  71. }
  72. }
  73. # endif
  74. #endif
  75. /**********************************************************************/
  76. #ifdef L_execl
  77. int attribute_hidden __execl(const char *path, const char *arg, ...)
  78. {
  79. EXEC_ALLOC_SIZE(size) /* Do NOT add a semicolon! */
  80. int n;
  81. char **argv;
  82. char **p;
  83. va_list args;
  84. n = 0;
  85. va_start(args, arg);
  86. do {
  87. ++n;
  88. } while (va_arg(args, char *));
  89. va_end(args);
  90. p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size);
  91. p[0] = (char *)arg;
  92. va_start(args, arg);
  93. do {
  94. *++p = va_arg(args, char *);
  95. } while (--n);
  96. va_end(args);
  97. n = execve(path, (char *const *) argv, __environ);
  98. EXEC_FREE(argv, size);
  99. return n;
  100. }
  101. strong_alias(__execl,execl)
  102. #endif
  103. /**********************************************************************/
  104. #ifdef L_execv
  105. int execv(__const char *path, char *__const argv[])
  106. {
  107. return execve(path, argv, __environ);
  108. }
  109. #endif
  110. /**********************************************************************/
  111. #ifdef L_execle
  112. int execle(const char *path, const char *arg, ...)
  113. {
  114. EXEC_ALLOC_SIZE(size) /* Do NOT add a semicolon! */
  115. int n;
  116. char **argv;
  117. char **p;
  118. char *const *envp;
  119. va_list args;
  120. n = 0;
  121. va_start(args, arg);
  122. do {
  123. ++n;
  124. } while (va_arg(args, char *));
  125. envp = va_arg(args, char *const *); /* Varies from execl and execlp. */
  126. va_end(args);
  127. p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size);
  128. p[0] = (char *)arg;
  129. va_start(args, arg);
  130. do {
  131. *++p = va_arg(args, char *);
  132. } while (--n);
  133. va_end(args);
  134. n = execve(path, (char *const *) argv, envp);
  135. EXEC_FREE(argv, size);
  136. return n;
  137. }
  138. #endif
  139. /**********************************************************************/
  140. #ifdef L_execlp
  141. extern int __execvp(const char *path, char *const argv[]) attribute_hidden;
  142. int execlp(const char *file, const char *arg, ...)
  143. {
  144. EXEC_ALLOC_SIZE(size) /* Do NOT add a semicolon! */
  145. int n;
  146. char **argv;
  147. char **p;
  148. va_list args;
  149. n = 0;
  150. va_start(args, arg);
  151. do {
  152. ++n;
  153. } while (va_arg(args, char *));
  154. va_end(args);
  155. p = argv = (char **) EXEC_ALLOC((n+1) * sizeof(char *), size);
  156. p[0] = (char *)arg;
  157. va_start(args, arg);
  158. do {
  159. *++p = va_arg(args, char *);
  160. } while (--n);
  161. va_end(args);
  162. n = execvp(file, (char *const *) argv);
  163. EXEC_FREE(argv, size);
  164. return n;
  165. }
  166. #endif
  167. /**********************************************************************/
  168. #ifdef L_execvp
  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 attribute_hidden __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. CHECK_ENOEXEC:
  191. if (errno == ENOEXEC) {
  192. char **nargv;
  193. EXEC_ALLOC_SIZE(size2) /* Do NOT add a semicolon! */
  194. size_t n;
  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 != ENOENT) {
  242. path = s;
  243. goto CHECK_ENOEXEC;
  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. strong_alias(__execvp,execvp)
  260. #endif
  261. /**********************************************************************/