9 #include "pthread_impl.h"
16 weak_alias(dummy_0, __acquire_ptc);
17 weak_alias(dummy_0, __release_ptc);
23 int (*exec)(const char *, char *const *, char *const *);
24 const posix_spawn_file_actions_t *fa;
25 const posix_spawnattr_t *restrict attr;
26 char *const *argv, *const *envp;
29 static int child(void *args_vp)
33 struct args *args = args_vp;
35 const posix_spawn_file_actions_t *fa = args->fa;
36 const posix_spawnattr_t *restrict attr = args->attr;
40 /* All signal dispositions must be either SIG_DFL or SIG_IGN
41 * before signals are unblocked. Otherwise a signal handler
42 * from the parent might get run in the child while sharing
43 * memory, with unpredictable and dangerous results. */
44 for (i=1; i<_NSIG; i++) {
45 __libc_sigaction(i, 0, &sa);
46 if (sa.sa_handler!=SIG_DFL && (sa.sa_handler!=SIG_IGN ||
47 ((attr->__flags & POSIX_SPAWN_SETSIGDEF)
48 && sigismember(&attr->__def, i) ))) {
49 sa.sa_handler = SIG_DFL;
50 __libc_sigaction(i, &sa, 0);
54 if (attr->__flags & POSIX_SPAWN_SETPGROUP)
55 if ((ret=__syscall(SYS_setpgid, 0, attr->__pgrp)))
58 /* Use syscalls directly because pthread state because the
59 * library functions attempt to do a multi-threaded synchronized
60 * id-change, which would trash the parent's state. */
61 if (attr->__flags & POSIX_SPAWN_RESETIDS)
62 if ((ret=__syscall(SYS_setgid, __syscall(SYS_getgid))) ||
63 (ret=__syscall(SYS_setuid, __syscall(SYS_getuid))) )
66 if (fa && fa->__actions) {
69 for (op = fa->__actions; op->next; op = op->next);
70 for (; op; op = op->prev) {
71 /* It's possible that a file operation would clobber
72 * the pipe fd used for synchronizing with the
73 * parent. To avoid that, we dup the pipe onto
74 * an unoccupied fd. */
76 ret = __syscall(SYS_dup, p);
77 if (ret < 0) goto fail;
78 __syscall(SYS_close, p);
83 if ((ret=__syscall(SYS_close, op->fd)))
87 if ((ret=__syscall(SYS_dup2, op->srcfd, op->fd))<0)
91 fd = __syscall(SYS_open, op->path,
92 op->oflag | O_LARGEFILE, op->mode);
93 if ((ret=fd) < 0) goto fail;
95 if ((ret=__syscall(SYS_dup2, fd, op->fd))<0)
97 __syscall(SYS_close, fd);
104 /* Close-on-exec flag may have been lost if we moved the pipe
105 * to a different fd. We don't use F_DUPFD_CLOEXEC above because
106 * it would fail on older kernels and atomicity is not needed --
107 * in this process there are no threads or signal handlers. */
108 __syscall(SYS_fcntl, p, F_SETFD, FD_CLOEXEC);
110 pthread_sigmask(SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
111 ? &attr->__mask : &args->oldmask, 0);
113 args->exec(args->path, args->argv, args->envp);
116 /* Since sizeof errno < PIPE_BUF, the write is atomic. */
118 if (ret) while (write(p, &ret, sizeof ret) < 0);
123 int __posix_spawnx(pid_t *restrict res, const char *restrict path,
124 int (*exec)(const char *, char *const *, char *const *),
125 const posix_spawn_file_actions_t *fa,
126 const posix_spawnattr_t *restrict attr,
127 char *const argv[restrict], char *const envp[restrict])
134 if (pipe2(args.p, O_CLOEXEC))
137 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
142 args.attr = attr ? attr : &(const posix_spawnattr_t){0};
145 pthread_sigmask(SIG_BLOCK, SIGALL_SET, &args.oldmask);
147 /* This lock prevents setuid/setgid operations while the parent
148 * is sharing memory with the child. Situations where processes
149 * with different permissions share VM are fundamentally unsafe. */
151 pid = __clone(child, stack+sizeof stack, CLONE_VM|SIGCHLD, &args);
155 if (read(args.p[0], &ec, sizeof ec) != sizeof ec) ec = 0;
156 else waitpid(pid, &(int){0}, 0);
161 /* At this point, the child has either exited or successfully
162 * performed exec, so the lock may be released. */
168 pthread_sigmask(SIG_SETMASK, &args.oldmask, 0);
169 pthread_setcancelstate(cs, 0);
174 int posix_spawn(pid_t *restrict res, const char *restrict path,
175 const posix_spawn_file_actions_t *fa,
176 const posix_spawnattr_t *restrict attr,
177 char *const argv[restrict], char *const envp[restrict])
179 return __posix_spawnx(res, path, execve, fa, attr, argv, envp);