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 static int child(void *args_vp)
27 struct args *args = args_vp;
29 const posix_spawn_file_actions_t *fa = args->fa;
30 const posix_spawnattr_t *restrict attr = args->attr;
34 /* All signal dispositions must be either SIG_DFL or SIG_IGN
35 * before signals are unblocked. Otherwise a signal handler
36 * from the parent might get run in the child while sharing
37 * memory, with unpredictable and dangerous results. */
38 for (i=1; i<_NSIG; i++) {
39 __libc_sigaction(i, 0, &sa);
40 if (sa.sa_handler!=SIG_DFL && (sa.sa_handler!=SIG_IGN ||
41 ((attr->__flags & POSIX_SPAWN_SETSIGDEF)
42 && sigismember(&attr->__def, i) ))) {
43 sa.sa_handler = SIG_DFL;
44 __libc_sigaction(i, &sa, 0);
48 if (attr->__flags & POSIX_SPAWN_SETPGROUP)
49 if ((ret=__syscall(SYS_setpgid, 0, attr->__pgrp)))
52 /* Use syscalls directly because pthread state because the
53 * library functions attempt to do a multi-threaded synchronized
54 * id-change, which would trash the parent's state. */
55 if (attr->__flags & POSIX_SPAWN_RESETIDS)
56 if ((ret=__syscall(SYS_setgid, __syscall(SYS_getgid))) ||
57 (ret=__syscall(SYS_setuid, __syscall(SYS_getuid))) )
60 if (fa && fa->__actions) {
63 for (op = fa->__actions; op->next; op = op->next);
64 for (; op; op = op->prev) {
65 /* It's possible that a file operation would clobber
66 * the pipe fd used for synchronizing with the
67 * parent. To avoid that, we dup the pipe onto
68 * an unoccupied fd. */
70 ret = __syscall(SYS_dup, p);
71 if (ret < 0) goto fail;
72 __syscall(SYS_close, p);
77 if ((ret=__syscall(SYS_close, op->fd)))
81 if ((ret=__syscall(SYS_dup2, op->srcfd, op->fd))<0)
85 fd = __syscall(SYS_open, op->path,
86 op->oflag | O_LARGEFILE, op->mode);
87 if ((ret=fd) < 0) goto fail;
89 if ((ret=__syscall(SYS_dup2, fd, op->fd))<0)
91 __syscall(SYS_close, fd);
98 /* Close-on-exec flag may have been lost if we moved the pipe
99 * to a different fd. We don't use F_DUPFD_CLOEXEC above because
100 * it would fail on older kernels and atomicity is not needed --
101 * in this process there are no threads or signal handlers. */
102 __syscall(SYS_fcntl, p, F_SETFD, FD_CLOEXEC);
104 pthread_sigmask(SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
105 ? &attr->__mask : &args->oldmask, 0);
107 args->exec(args->path, args->argv, args->envp);
110 /* Since sizeof errno < PIPE_BUF, the write is atomic. */
112 if (ret) while (write(p, &ret, sizeof ret) < 0);
117 int __posix_spawnx(pid_t *restrict res, const char *restrict path,
118 int (*exec)(const char *, char *const *, char *const *),
119 const posix_spawn_file_actions_t *fa,
120 const posix_spawnattr_t *restrict attr,
121 char *const argv[restrict], char *const envp[restrict])
128 if (pipe2(args.p, O_CLOEXEC))
131 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
136 args.attr = attr ? attr : &(const posix_spawnattr_t){0};
139 pthread_sigmask(SIG_BLOCK, SIGALL_SET, &args.oldmask);
141 pid = __clone(child, stack+sizeof stack,
142 CLONE_VM|CLONE_VFORK|SIGCHLD, &args);
146 if (read(args.p[0], &ec, sizeof ec) != sizeof ec) ec = 0;
147 else waitpid(pid, &(int){0}, 0);
156 pthread_sigmask(SIG_SETMASK, &args.oldmask, 0);
157 pthread_setcancelstate(cs, 0);
162 int posix_spawn(pid_t *restrict res, const char *restrict path,
163 const posix_spawn_file_actions_t *fa,
164 const posix_spawnattr_t *restrict attr,
165 char *const argv[restrict], char *const envp[restrict])
167 return __posix_spawnx(res, path, execve, fa, attr, argv, envp);