9 #include "pthread_impl.h"
17 int (*exec)(const char *, char *const *, char *const *);
18 const posix_spawn_file_actions_t *fa;
19 const posix_spawnattr_t *restrict attr;
20 char *const *argv, *const *envp;
23 void __get_handler_set(sigset_t *);
25 static int child(void *args_vp)
28 struct sigaction sa = {0};
29 struct args *args = args_vp;
31 const posix_spawn_file_actions_t *fa = args->fa;
32 const posix_spawnattr_t *restrict attr = args->attr;
37 /* All signal dispositions must be either SIG_DFL or SIG_IGN
38 * before signals are unblocked. Otherwise a signal handler
39 * from the parent might get run in the child while sharing
40 * memory, with unpredictable and dangerous results. To
41 * reduce overhead, sigaction has tracked for us which signals
42 * potentially have a signal handler. */
43 __get_handler_set(&hset);
44 for (i=1; i<_NSIG; i++) {
45 if ((attr->__flags & POSIX_SPAWN_SETSIGDEF)
46 && sigismember(&attr->__def, i)) {
47 sa.sa_handler = SIG_DFL;
48 } else if (sigismember(&hset, i)) {
50 sa.sa_handler = SIG_IGN;
52 __libc_sigaction(i, 0, &sa);
53 if (sa.sa_handler==SIG_IGN) continue;
54 sa.sa_handler = SIG_DFL;
59 __libc_sigaction(i, &sa, 0);
62 if (attr->__flags & POSIX_SPAWN_SETPGROUP)
63 if ((ret=__syscall(SYS_setpgid, 0, attr->__pgrp)))
66 /* Use syscalls directly because pthread state because the
67 * library functions attempt to do a multi-threaded synchronized
68 * id-change, which would trash the parent's state. */
69 if (attr->__flags & POSIX_SPAWN_RESETIDS)
70 if ((ret=__syscall(SYS_setgid, __syscall(SYS_getgid))) ||
71 (ret=__syscall(SYS_setuid, __syscall(SYS_getuid))) )
74 if (fa && fa->__actions) {
77 for (op = fa->__actions; op->next; op = op->next);
78 for (; op; op = op->prev) {
79 /* It's possible that a file operation would clobber
80 * the pipe fd used for synchronizing with the
81 * parent. To avoid that, we dup the pipe onto
82 * an unoccupied fd. */
84 ret = __syscall(SYS_dup, p);
85 if (ret < 0) goto fail;
86 __syscall(SYS_close, p);
91 if ((ret=__syscall(SYS_close, op->fd)))
95 if ((ret=__syscall(SYS_dup2, op->srcfd, op->fd))<0)
99 fd = __syscall(SYS_open, op->path,
100 op->oflag | O_LARGEFILE, op->mode);
101 if ((ret=fd) < 0) goto fail;
103 if ((ret=__syscall(SYS_dup2, fd, op->fd))<0)
105 __syscall(SYS_close, fd);
112 /* Close-on-exec flag may have been lost if we moved the pipe
113 * to a different fd. We don't use F_DUPFD_CLOEXEC above because
114 * it would fail on older kernels and atomicity is not needed --
115 * in this process there are no threads or signal handlers. */
116 __syscall(SYS_fcntl, p, F_SETFD, FD_CLOEXEC);
118 pthread_sigmask(SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
119 ? &attr->__mask : &args->oldmask, 0);
121 args->exec(args->path, args->argv, args->envp);
125 /* Since sizeof errno < PIPE_BUF, the write is atomic. */
127 if (ret) while (write(p, &ret, sizeof ret) < 0);
132 int __posix_spawnx(pid_t *restrict res, const char *restrict path,
133 int (*exec)(const char *, char *const *, char *const *),
134 const posix_spawn_file_actions_t *fa,
135 const posix_spawnattr_t *restrict attr,
136 char *const argv[restrict], char *const envp[restrict])
143 if (pipe2(args.p, O_CLOEXEC))
146 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
151 args.attr = attr ? attr : &(const posix_spawnattr_t){0};
154 pthread_sigmask(SIG_BLOCK, SIGALL_SET, &args.oldmask);
156 pid = __clone(child, stack+sizeof stack,
157 CLONE_VM|CLONE_VFORK|SIGCHLD, &args);
161 if (read(args.p[0], &ec, sizeof ec) != sizeof ec) ec = 0;
162 else waitpid(pid, &(int){0}, 0);
169 if (!ec && res) *res = pid;
171 pthread_sigmask(SIG_SETMASK, &args.oldmask, 0);
172 pthread_setcancelstate(cs, 0);
177 int posix_spawn(pid_t *restrict res, const char *restrict path,
178 const posix_spawn_file_actions_t *fa,
179 const posix_spawnattr_t *restrict attr,
180 char *const argv[restrict], char *const envp[restrict])
182 return __posix_spawnx(res, path, execve, fa, attr, argv, envp);