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 __sys_dup2(int old, int new)
28 return __syscall(SYS_dup2, old, new);
31 int r = __syscall(SYS_fcntl, old, F_GETFD);
34 return __syscall(SYS_dup3, old, new, 0);
39 static int child(void *args_vp)
42 struct sigaction sa = {0};
43 struct args *args = args_vp;
45 const posix_spawn_file_actions_t *fa = args->fa;
46 const posix_spawnattr_t *restrict attr = args->attr;
51 /* All signal dispositions must be either SIG_DFL or SIG_IGN
52 * before signals are unblocked. Otherwise a signal handler
53 * from the parent might get run in the child while sharing
54 * memory, with unpredictable and dangerous results. To
55 * reduce overhead, sigaction has tracked for us which signals
56 * potentially have a signal handler. */
57 __get_handler_set(&hset);
58 for (i=1; i<_NSIG; i++) {
59 if ((attr->__flags & POSIX_SPAWN_SETSIGDEF)
60 && sigismember(&attr->__def, i)) {
61 sa.sa_handler = SIG_DFL;
62 } else if (sigismember(&hset, i)) {
64 sa.sa_handler = SIG_IGN;
66 __libc_sigaction(i, 0, &sa);
67 if (sa.sa_handler==SIG_IGN) continue;
68 sa.sa_handler = SIG_DFL;
73 __libc_sigaction(i, &sa, 0);
76 if (attr->__flags & POSIX_SPAWN_SETPGROUP)
77 if ((ret=__syscall(SYS_setpgid, 0, attr->__pgrp)))
80 /* Use syscalls directly because the library functions attempt
81 * to do a multi-threaded synchronized id-change, which would
82 * trash the parent's state. */
83 if (attr->__flags & POSIX_SPAWN_RESETIDS)
84 if ((ret=__syscall(SYS_setgid, __syscall(SYS_getgid))) ||
85 (ret=__syscall(SYS_setuid, __syscall(SYS_getuid))) )
88 if (fa && fa->__actions) {
91 for (op = fa->__actions; op->next; op = op->next);
92 for (; op; op = op->prev) {
93 /* It's possible that a file operation would clobber
94 * the pipe fd used for synchronizing with the
95 * parent. To avoid that, we dup the pipe onto
96 * an unoccupied fd. */
98 ret = __syscall(SYS_dup, p);
99 if (ret < 0) goto fail;
100 __syscall(SYS_close, p);
105 __syscall(SYS_close, op->fd);
108 if ((ret=__sys_dup2(op->srcfd, op->fd))<0)
112 fd = __sys_open(op->path, op->oflag, op->mode);
113 if ((ret=fd) < 0) goto fail;
115 if ((ret=__sys_dup2(fd, op->fd))<0)
117 __syscall(SYS_close, fd);
124 /* Close-on-exec flag may have been lost if we moved the pipe
125 * to a different fd. We don't use F_DUPFD_CLOEXEC above because
126 * it would fail on older kernels and atomicity is not needed --
127 * in this process there are no threads or signal handlers. */
128 __syscall(SYS_fcntl, p, F_SETFD, FD_CLOEXEC);
130 pthread_sigmask(SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
131 ? &attr->__mask : &args->oldmask, 0);
133 args->exec(args->path, args->argv, args->envp);
137 /* Since sizeof errno < PIPE_BUF, the write is atomic. */
139 if (ret) while (write(p, &ret, sizeof ret) < 0);
144 int __posix_spawnx(pid_t *restrict res, const char *restrict path,
145 int (*exec)(const char *, char *const *, char *const *),
146 const posix_spawn_file_actions_t *fa,
147 const posix_spawnattr_t *restrict attr,
148 char *const argv[restrict], char *const envp[restrict])
155 if (pipe2(args.p, O_CLOEXEC))
158 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
163 args.attr = attr ? attr : &(const posix_spawnattr_t){0};
166 pthread_sigmask(SIG_BLOCK, SIGALL_SET, &args.oldmask);
168 pid = __clone(child, stack+sizeof stack,
169 CLONE_VM|CLONE_VFORK|SIGCHLD, &args);
173 if (read(args.p[0], &ec, sizeof ec) != sizeof ec) ec = 0;
174 else waitpid(pid, &(int){0}, 0);
181 if (!ec && res) *res = pid;
183 pthread_sigmask(SIG_SETMASK, &args.oldmask, 0);
184 pthread_setcancelstate(cs, 0);
189 int posix_spawn(pid_t *restrict res, const char *restrict path,
190 const posix_spawn_file_actions_t *fa,
191 const posix_spawnattr_t *restrict attr,
192 char *const argv[restrict], char *const envp[restrict])
194 return __posix_spawnx(res, path, execve, fa, attr, argv, envp);