spawn.c 6.8 KB

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