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);
30 return __syscall(SYS_dup3, old, new, 0);
34 static int child(void *args_vp)
37 struct sigaction sa = {0};
38 struct args *args = args_vp;
40 const posix_spawn_file_actions_t *fa = args->fa;
41 const posix_spawnattr_t *restrict attr = args->attr;
46 /* All signal dispositions must be either SIG_DFL or SIG_IGN
47 * before signals are unblocked. Otherwise a signal handler
48 * from the parent might get run in the child while sharing
49 * memory, with unpredictable and dangerous results. To
50 * reduce overhead, sigaction has tracked for us which signals
51 * potentially have a signal handler. */
52 __get_handler_set(&hset);
53 for (i=1; i<_NSIG; i++) {
54 if ((attr->__flags & POSIX_SPAWN_SETSIGDEF)
55 && sigismember(&attr->__def, i)) {
56 sa.sa_handler = SIG_DFL;
57 } else if (sigismember(&hset, i)) {
59 sa.sa_handler = SIG_IGN;
61 __libc_sigaction(i, 0, &sa);
62 if (sa.sa_handler==SIG_IGN) continue;
63 sa.sa_handler = SIG_DFL;
68 __libc_sigaction(i, &sa, 0);
71 if (attr->__flags & POSIX_SPAWN_SETSID)
72 if ((ret=__syscall(SYS_setsid)) < 0)
75 if (attr->__flags & POSIX_SPAWN_SETPGROUP)
76 if ((ret=__syscall(SYS_setpgid, 0, attr->__pgrp)))
79 /* Use syscalls directly because the library functions attempt
80 * to do a multi-threaded synchronized id-change, which would
81 * trash the parent's state. */
82 if (attr->__flags & POSIX_SPAWN_RESETIDS)
83 if ((ret=__syscall(SYS_setgid, __syscall(SYS_getgid))) ||
84 (ret=__syscall(SYS_setuid, __syscall(SYS_getuid))) )
87 if (fa && fa->__actions) {
90 for (op = fa->__actions; op->next; op = op->next);
91 for (; op; op = op->prev) {
92 /* It's possible that a file operation would clobber
93 * the pipe fd used for synchronizing with the
94 * parent. To avoid that, we dup the pipe onto
95 * an unoccupied fd. */
97 ret = __syscall(SYS_dup, p);
98 if (ret < 0) goto fail;
99 __syscall(SYS_close, p);
104 __syscall(SYS_close, op->fd);
109 if ((ret=__sys_dup2(fd, op->fd))<0)
112 ret = __syscall(SYS_fcntl, fd, F_GETFD);
113 ret = __syscall(SYS_fcntl, fd, F_SETFD,
120 fd = __sys_open(op->path, op->oflag, op->mode);
121 if ((ret=fd) < 0) goto fail;
123 if ((ret=__sys_dup2(fd, op->fd))<0)
125 __syscall(SYS_close, fd);
132 /* Close-on-exec flag may have been lost if we moved the pipe
133 * to a different fd. We don't use F_DUPFD_CLOEXEC above because
134 * it would fail on older kernels and atomicity is not needed --
135 * in this process there are no threads or signal handlers. */
136 __syscall(SYS_fcntl, p, F_SETFD, FD_CLOEXEC);
138 pthread_sigmask(SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
139 ? &attr->__mask : &args->oldmask, 0);
141 args->exec(args->path, args->argv, args->envp);
145 /* Since sizeof errno < PIPE_BUF, the write is atomic. */
147 if (ret) while (__syscall(SYS_write, p, &ret, sizeof ret) < 0);
152 int __posix_spawnx(pid_t *restrict res, const char *restrict path,
153 int (*exec)(const char *, char *const *, char *const *),
154 const posix_spawn_file_actions_t *fa,
155 const posix_spawnattr_t *restrict attr,
156 char *const argv[restrict], char *const envp[restrict])
159 char stack[1024+PATH_MAX];
163 if (pipe2(args.p, O_CLOEXEC))
166 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);
197 int posix_spawn(pid_t *restrict res, const char *restrict path,
198 const posix_spawn_file_actions_t *fa,
199 const posix_spawnattr_t *restrict attr,
200 char *const argv[restrict], char *const envp[restrict])
202 return __posix_spawnx(res, path, execve, fa, attr, argv, envp);