exec.c 7.3 KB

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