1
0

posix_spawn.c 4.8 KB

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