exec.c 7.0 KB

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