spawn.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* Copyright (C) 2000, 2011 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <alloca.h>
  16. #include <unistd.h>
  17. #include <signal.h>
  18. #include <stdbool.h>
  19. #include <sys/syscall.h>
  20. #include <fcntl.h>
  21. #include <sys/resource.h>
  22. #include <not-cancel.h>
  23. #include <internal-signals.h>
  24. #include <spawn.h>
  25. #include "spawn_int.h"
  26. /* The Unix standard contains a long explanation of the way to signal
  27. an error after the fork() was successful. Since no new wait status
  28. was wanted there is no way to signal an error using one of the
  29. available methods. The committee chose to signal an error by a
  30. normal program exit with the exit code 127. */
  31. #define SPAWN_ERROR 127
  32. /* Execute file actions.
  33. * Returns true on error.
  34. */
  35. inline static bool execute_file_actions(const posix_spawn_file_actions_t *fa)
  36. {
  37. struct rlimit64 fdlimit;
  38. bool have_fdlimit = false;
  39. int cnt;
  40. for (cnt = 0; cnt < fa->__used; ++cnt) {
  41. struct __spawn_action *action = &fa->__actions[cnt];
  42. switch (action->tag) {
  43. case spawn_do_close:
  44. if (close_not_cancel(action->action.close_action.fd) != 0) {
  45. if (!have_fdlimit) {
  46. getrlimit64(RLIMIT_NOFILE, &fdlimit);
  47. have_fdlimit = true;
  48. }
  49. /* Only signal errors for file descriptors out of range. */
  50. if (0 > action->action.close_action.fd
  51. || action->action.close_action.fd >= fdlimit.rlim_cur)
  52. /* Signal the error. */
  53. return true;
  54. }
  55. break;
  56. case spawn_do_open:;
  57. int new_fd = open_not_cancel(action->action.open_action.path,
  58. action->action.open_action.oflag
  59. | O_LARGEFILE,
  60. action->action.open_action.mode);
  61. if (new_fd == -1)
  62. return true;
  63. /* Make sure the desired file descriptor is used. */
  64. if (new_fd != action->action.open_action.fd) {
  65. if (dup2(new_fd, action->action.open_action.fd)
  66. != action->action.open_action.fd)
  67. return true;
  68. if (close_not_cancel(new_fd) != 0)
  69. return true;
  70. }
  71. break;
  72. case spawn_do_dup2:
  73. if (dup2(action->action.dup2_action.fd,
  74. action->action.dup2_action.newfd)
  75. != action->action.dup2_action.newfd)
  76. return true;
  77. break;
  78. }
  79. }
  80. return false;
  81. }
  82. #define DANGEROUS (POSIX_SPAWN_SETSIGMASK \
  83. | POSIX_SPAWN_SETSIGDEF \
  84. | POSIX_SPAWN_SETSCHEDPARAM \
  85. | POSIX_SPAWN_SETSCHEDULER \
  86. | POSIX_SPAWN_SETPGROUP \
  87. | POSIX_SPAWN_RESETIDS)
  88. inline static bool is_vfork_safe(short int flags)
  89. {
  90. return ((flags & POSIX_SPAWN_USEVFORK) || !(flags & DANGEROUS));
  91. }
  92. /* Spawn a new process executing PATH with the attributes describes in *ATTRP.
  93. Before running the process perform the actions described in FILE-ACTIONS. */
  94. static int
  95. __spawni(pid_t *pid, const char *file,
  96. const posix_spawn_file_actions_t *fa,
  97. const posix_spawnattr_t *attrp, char *const argv[],
  98. char *const envp[], const char *path)
  99. {
  100. short int flags = attrp ? attrp->__flags : 0;
  101. pid_t new_pid;
  102. if (is_vfork_safe(flags) && !fa)
  103. new_pid = vfork();
  104. else {
  105. #ifdef __ARCH_USE_MMU__
  106. new_pid = fork();
  107. #else
  108. return ENOSYS;
  109. #endif
  110. }
  111. if (new_pid) {
  112. if (new_pid < 0)
  113. return errno;
  114. if (pid)
  115. *pid = new_pid;
  116. return 0;
  117. }
  118. if (flags & POSIX_SPAWN_SETSIGMASK) {
  119. if (sigprocmask(SIG_SETMASK, &attrp->__ss, NULL) != 0)
  120. goto error;
  121. }
  122. if (flags & POSIX_SPAWN_SETSIGDEF) {
  123. /* We have to iterate over all signals. This could possibly be
  124. done better but it requires system specific solutions since
  125. the sigset_t data type can be very different on different
  126. architectures. */
  127. struct sigaction sa;
  128. int sig;
  129. memset(&sa, 0, sizeof(sa));
  130. sigset_t hset;
  131. sigprocmask (SIG_BLOCK, 0, &hset);
  132. for (sig = 1; sig < _NSIG; ++sig) {
  133. if ((flags & POSIX_SPAWN_SETSIGDEF)
  134. && sigismember (&attrp->__sd, sig))
  135. {
  136. sa.sa_handler = SIG_DFL;
  137. }
  138. else if (sigismember (&hset, sig))
  139. {
  140. if (__is_internal_signal (sig))
  141. sa.sa_handler = SIG_IGN;
  142. else
  143. {
  144. __libc_sigaction (sig, 0, &sa);
  145. if (sa.sa_handler == SIG_IGN)
  146. continue;
  147. sa.sa_handler = SIG_DFL;
  148. }
  149. }
  150. else
  151. continue;
  152. __libc_sigaction (sig, &sa, 0);
  153. }
  154. }
  155. if (flags & POSIX_SPAWN_SETSCHEDULER) {
  156. if (sched_setscheduler(0, attrp->__policy, &attrp->__sp) == -1)
  157. goto error;
  158. } else if (flags & POSIX_SPAWN_SETSCHEDPARAM) {
  159. if (sched_setparam(0, &attrp->__sp) == -1)
  160. goto error;
  161. }
  162. if (flags & POSIX_SPAWN_SETPGROUP) {
  163. if (setpgid(0, attrp->__pgrp) != 0)
  164. goto error;
  165. }
  166. if (flags & POSIX_SPAWN_RESETIDS) {
  167. if (seteuid(getuid()) || setegid(getgid()))
  168. goto error;
  169. }
  170. if (fa && execute_file_actions(fa))
  171. goto error;
  172. if (!path || strchr(file, '/')) {
  173. execve(file, argv, envp);
  174. goto error;
  175. }
  176. char *name;
  177. {
  178. size_t filelen = strlen(file) + 1;
  179. size_t pathlen = strlen(path) + 1;
  180. name = alloca(pathlen + filelen);
  181. /* Copy the file name at the top. */
  182. name = (char *) memcpy(name + pathlen, file, filelen);
  183. /* And add the slash. */
  184. *--name = '/';
  185. }
  186. char *p = (char *)path;
  187. do {
  188. char *startp;
  189. path = p;
  190. p = strchrnul(path, ':');
  191. /* Two adjacent colons, or a colon at the beginning or the end
  192. of `PATH' means to search the current directory. */
  193. if (p == path)
  194. startp = name + 1;
  195. else
  196. startp = (char *) memcpy(name - (p - path), path, p - path);
  197. execve(startp, argv, envp);
  198. switch (errno) {
  199. case EACCES:
  200. case ENOENT:
  201. case ESTALE:
  202. case ENOTDIR:
  203. /* Those errors indicate the file is missing or not
  204. executable by us, in which case we want to just try
  205. the next path directory. */
  206. break;
  207. default:
  208. /* Some other error means we found an executable file,
  209. but something went wrong executing it; return the
  210. error to our caller. */
  211. goto error;
  212. }
  213. } while (*p++ != '\0');
  214. error:
  215. _exit(SPAWN_ERROR);
  216. }
  217. /* Spawn a new process executing PATH with the attributes describes in *ATTRP.
  218. Before running the process perform the actions described in FILE-ACTIONS. */
  219. int posix_spawn (pid_t *pid, const char *path,
  220. const posix_spawn_file_actions_t *fa,
  221. const posix_spawnattr_t *attrp, char *const argv[],
  222. char *const envp[])
  223. {
  224. return __spawni(pid, path, fa, attrp, argv, envp, NULL);
  225. }
  226. /* Spawn a new process executing FILE with the attributes describes in *ATTRP.
  227. Before running the process perform the actions described in FILE-ACTIONS. */
  228. int
  229. posix_spawnp(pid_t *pid, const char *file,
  230. const posix_spawn_file_actions_t *fa,
  231. const posix_spawnattr_t *attrp, char *const argv[],
  232. char *const envp[])
  233. {
  234. const char *path = getenv("PATH");
  235. if (!path)
  236. path = ":/bin:/usr/bin";
  237. return __spawni(pid, file, fa, attrp, argv, envp, path);
  238. }