9 #include "pthread_impl.h"
17 const posix_spawn_file_actions_t *fa;
18 const posix_spawnattr_t *restrict attr;
19 char *const *argv, *const *envp;
22 void __get_handler_set(sigset_t *);
24 static int __sys_dup2(int old, int new)
27 return __syscall(SYS_dup2, old, new);
29 return __syscall(SYS_dup3, old, new, 0);
33 static int child(void *args_vp)
36 struct sigaction sa = {0};
37 struct args *args = args_vp;
39 const posix_spawn_file_actions_t *fa = args->fa;
40 const posix_spawnattr_t *restrict attr = args->attr;
45 /* All signal dispositions must be either SIG_DFL or SIG_IGN
46 * before signals are unblocked. Otherwise a signal handler
47 * from the parent might get run in the child while sharing
48 * memory, with unpredictable and dangerous results. To
49 * reduce overhead, sigaction has tracked for us which signals
50 * potentially have a signal handler. */
51 __get_handler_set(&hset);
52 for (i=1; i<_NSIG; i++) {
53 if ((attr->__flags & POSIX_SPAWN_SETSIGDEF)
54 && sigismember(&attr->__def, i)) {
55 sa.sa_handler = SIG_DFL;
56 } else if (sigismember(&hset, i)) {
58 sa.sa_handler = SIG_IGN;
60 __libc_sigaction(i, 0, &sa);
61 if (sa.sa_handler==SIG_IGN) continue;
62 sa.sa_handler = SIG_DFL;
67 __libc_sigaction(i, &sa, 0);
70 if (attr->__flags & POSIX_SPAWN_SETSID)
71 if ((ret=__syscall(SYS_setsid)) < 0)
74 if (attr->__flags & POSIX_SPAWN_SETPGROUP)
75 if ((ret=__syscall(SYS_setpgid, 0, attr->__pgrp)))
78 /* Use syscalls directly because the library functions attempt
79 * to do a multi-threaded synchronized id-change, which would
80 * trash the parent's state. */
81 if (attr->__flags & POSIX_SPAWN_RESETIDS)
82 if ((ret=__syscall(SYS_setgid, __syscall(SYS_getgid))) ||
83 (ret=__syscall(SYS_setuid, __syscall(SYS_getuid))) )
86 if (fa && fa->__actions) {
89 for (op = fa->__actions; op->next; op = op->next);
90 for (; op; op = op->prev) {
91 /* It's possible that a file operation would clobber
92 * the pipe fd used for synchronizing with the
93 * parent. To avoid that, we dup the pipe onto
94 * an unoccupied fd. */
96 ret = __syscall(SYS_dup, p);
97 if (ret < 0) goto fail;
98 __syscall(SYS_close, p);
103 __syscall(SYS_close, op->fd);
108 if ((ret=__sys_dup2(fd, op->fd))<0)
111 ret = __syscall(SYS_fcntl, fd, F_GETFD);
112 ret = __syscall(SYS_fcntl, fd, F_SETFD,
119 fd = __sys_open(op->path, op->oflag, op->mode);
120 if ((ret=fd) < 0) goto fail;
122 if ((ret=__sys_dup2(fd, op->fd))<0)
124 __syscall(SYS_close, fd);
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);
137 pthread_sigmask(SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
138 ? &attr->__mask : &args->oldmask, 0);
140 int (*exec)(const char *, char *const *, char *const *) =
141 attr->__fn ? (int (*)())attr->__fn : execve;
143 exec(args->path, args->argv, args->envp);
147 /* Since sizeof errno < PIPE_BUF, the write is atomic. */
149 if (ret) while (__syscall(SYS_write, p, &ret, sizeof ret) < 0);
154 int posix_spawn(pid_t *restrict res, const char *restrict path,
155 const posix_spawn_file_actions_t *fa,
156 const posix_spawnattr_t *restrict attr,
157 char *const argv[restrict], char *const envp[restrict])
160 char stack[1024+PATH_MAX];
164 if (pipe2(args.p, O_CLOEXEC))
167 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
171 args.attr = attr ? attr : &(const posix_spawnattr_t){0};
174 pthread_sigmask(SIG_BLOCK, SIGALL_SET, &args.oldmask);
176 pid = __clone(child, stack+sizeof stack,
177 CLONE_VM|CLONE_VFORK|SIGCHLD, &args);
181 if (read(args.p[0], &ec, sizeof ec) != sizeof ec) ec = 0;
182 else waitpid(pid, &(int){0}, 0);
189 if (!ec && res) *res = pid;
191 pthread_sigmask(SIG_SETMASK, &args.oldmask, 0);
192 pthread_setcancelstate(cs, 0);