exec.c 7.1 KB

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