posix_spawn.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #define _GNU_SOURCE
  2. #include <spawn.h>
  3. #include <sched.h>
  4. #include <unistd.h>
  5. #include <signal.h>
  6. #include <fcntl.h>
  7. #include <sys/wait.h>
  8. #include "syscall.h"
  9. #include "lock.h"
  10. #include "pthread_impl.h"
  11. #include "fdop.h"
  12. struct args {
  13. int p[2];
  14. sigset_t oldmask;
  15. const char *path;
  16. const posix_spawn_file_actions_t *fa;
  17. const posix_spawnattr_t *restrict attr;
  18. char *const *argv, *const *envp;
  19. };
  20. static int __sys_dup2(int old, int new)
  21. {
  22. #ifdef SYS_dup2
  23. return __syscall(SYS_dup2, old, new);
  24. #else
  25. return __syscall(SYS_dup3, old, new, 0);
  26. #endif
  27. }
  28. static int child(void *args_vp)
  29. {
  30. int i, ret;
  31. struct sigaction sa = {0};
  32. struct args *args = args_vp;
  33. int p = args->p[1];
  34. const posix_spawn_file_actions_t *fa = args->fa;
  35. const posix_spawnattr_t *restrict attr = args->attr;
  36. sigset_t hset;
  37. close(args->p[0]);
  38. /* All signal dispositions must be either SIG_DFL or SIG_IGN
  39. * before signals are unblocked. Otherwise a signal handler
  40. * from the parent might get run in the child while sharing
  41. * memory, with unpredictable and dangerous results. To
  42. * reduce overhead, sigaction has tracked for us which signals
  43. * potentially have a signal handler. */
  44. __get_handler_set(&hset);
  45. for (i=1; i<_NSIG; i++) {
  46. if ((attr->__flags & POSIX_SPAWN_SETSIGDEF)
  47. && sigismember(&attr->__def, i)) {
  48. sa.sa_handler = SIG_DFL;
  49. } else if (sigismember(&hset, i)) {
  50. if (i-32<3U) {
  51. sa.sa_handler = SIG_IGN;
  52. } else {
  53. __libc_sigaction(i, 0, &sa);
  54. if (sa.sa_handler==SIG_IGN) continue;
  55. sa.sa_handler = SIG_DFL;
  56. }
  57. } else {
  58. continue;
  59. }
  60. __libc_sigaction(i, &sa, 0);
  61. }
  62. if (attr->__flags & POSIX_SPAWN_SETSID)
  63. if ((ret=__syscall(SYS_setsid)) < 0)
  64. goto fail;
  65. if (attr->__flags & POSIX_SPAWN_SETPGROUP)
  66. if ((ret=__syscall(SYS_setpgid, 0, attr->__pgrp)))
  67. goto fail;
  68. /* Use syscalls directly because the library functions attempt
  69. * to do a multi-threaded synchronized id-change, which would
  70. * trash the parent's state. */
  71. if (attr->__flags & POSIX_SPAWN_RESETIDS)
  72. if ((ret=__syscall(SYS_setgid, __syscall(SYS_getgid))) ||
  73. (ret=__syscall(SYS_setuid, __syscall(SYS_getuid))) )
  74. goto fail;
  75. if (fa && fa->__actions) {
  76. struct fdop *op;
  77. int fd;
  78. for (op = fa->__actions; op->next; op = op->next);
  79. for (; op; op = op->prev) {
  80. /* It's possible that a file operation would clobber
  81. * the pipe fd used for synchronizing with the
  82. * parent. To avoid that, we dup the pipe onto
  83. * an unoccupied fd. */
  84. if (op->fd == p) {
  85. ret = __syscall(SYS_dup, p);
  86. if (ret < 0) goto fail;
  87. __syscall(SYS_close, p);
  88. p = ret;
  89. }
  90. switch(op->cmd) {
  91. case FDOP_CLOSE:
  92. __syscall(SYS_close, op->fd);
  93. break;
  94. case FDOP_DUP2:
  95. fd = op->srcfd;
  96. if (fd == p) {
  97. ret = -EBADF;
  98. goto fail;
  99. }
  100. if (fd != op->fd) {
  101. if ((ret=__sys_dup2(fd, op->fd))<0)
  102. goto fail;
  103. } else {
  104. ret = __syscall(SYS_fcntl, fd, F_GETFD);
  105. ret = __syscall(SYS_fcntl, fd, F_SETFD,
  106. ret & ~FD_CLOEXEC);
  107. if (ret<0)
  108. goto fail;
  109. }
  110. break;
  111. case FDOP_OPEN:
  112. fd = __sys_open(op->path, op->oflag, op->mode);
  113. if ((ret=fd) < 0) goto fail;
  114. if (fd != op->fd) {
  115. if ((ret=__sys_dup2(fd, op->fd))<0)
  116. goto fail;
  117. __syscall(SYS_close, fd);
  118. }
  119. break;
  120. case FDOP_CHDIR:
  121. ret = __syscall(SYS_chdir, op->path);
  122. if (ret<0) goto fail;
  123. break;
  124. case FDOP_FCHDIR:
  125. ret = __syscall(SYS_fchdir, op->fd);
  126. if (ret<0) goto fail;
  127. break;
  128. }
  129. }
  130. }
  131. /* Close-on-exec flag may have been lost if we moved the pipe
  132. * to a different fd. We don't use F_DUPFD_CLOEXEC above because
  133. * it would fail on older kernels and atomicity is not needed --
  134. * in this process there are no threads or signal handlers. */
  135. __syscall(SYS_fcntl, p, F_SETFD, FD_CLOEXEC);
  136. pthread_sigmask(SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
  137. ? &attr->__mask : &args->oldmask, 0);
  138. int (*exec)(const char *, char *const *, char *const *) =
  139. attr->__fn ? (int (*)())attr->__fn : execve;
  140. exec(args->path, args->argv, args->envp);
  141. ret = -errno;
  142. fail:
  143. /* Since sizeof errno < PIPE_BUF, the write is atomic. */
  144. ret = -ret;
  145. if (ret) while (__syscall(SYS_write, p, &ret, sizeof ret) < 0);
  146. _exit(127);
  147. }
  148. int posix_spawn(pid_t *restrict res, const char *restrict path,
  149. const posix_spawn_file_actions_t *fa,
  150. const posix_spawnattr_t *restrict attr,
  151. char *const argv[restrict], char *const envp[restrict])
  152. {
  153. pid_t pid;
  154. char stack[1024+PATH_MAX];
  155. int ec=0, cs;
  156. struct args args;
  157. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
  158. args.path = path;
  159. args.fa = fa;
  160. args.attr = attr ? attr : &(const posix_spawnattr_t){0};
  161. args.argv = argv;
  162. args.envp = envp;
  163. pthread_sigmask(SIG_BLOCK, SIGALL_SET, &args.oldmask);
  164. /* The lock guards both against seeing a SIGABRT disposition change
  165. * by abort and against leaking the pipe fd to fork-without-exec. */
  166. LOCK(__abort_lock);
  167. if (pipe2(args.p, O_CLOEXEC)) {
  168. UNLOCK(__abort_lock);
  169. ec = errno;
  170. goto fail;
  171. }
  172. pid = __clone(child, stack+sizeof stack,
  173. CLONE_VM|CLONE_VFORK|SIGCHLD, &args);
  174. close(args.p[1]);
  175. UNLOCK(__abort_lock);
  176. if (pid > 0) {
  177. if (read(args.p[0], &ec, sizeof ec) != sizeof ec) ec = 0;
  178. else waitpid(pid, &(int){0}, 0);
  179. } else {
  180. ec = -pid;
  181. }
  182. close(args.p[0]);
  183. if (!ec && res) *res = pid;
  184. fail:
  185. pthread_sigmask(SIG_SETMASK, &args.oldmask, 0);
  186. pthread_setcancelstate(cs, 0);
  187. return ec;
  188. }