adjust posix_spawn dup2 action behavior to match future requirements
[oweals/musl.git] / src / process / posix_spawn.c
1 #define _GNU_SOURCE
2 #include <spawn.h>
3 #include <sched.h>
4 #include <unistd.h>
5 #include <signal.h>
6 #include <fcntl.h>
7 #include <sys/wait.h>
8 #include "syscall.h"
9 #include "pthread_impl.h"
10 #include "fdop.h"
11 #include "libc.h"
12
13 struct args {
14         int p[2];
15         sigset_t oldmask;
16         const char *path;
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;
21 };
22
23 void __get_handler_set(sigset_t *);
24
25 static int __sys_dup2(int old, int new)
26 {
27 #ifdef SYS_dup2
28         return __syscall(SYS_dup2, old, new);
29 #else
30         return __syscall(SYS_dup3, old, new, 0);
31 #endif
32 }
33
34 static int child(void *args_vp)
35 {
36         int i, ret;
37         struct sigaction sa = {0};
38         struct args *args = args_vp;
39         int p = args->p[1];
40         const posix_spawn_file_actions_t *fa = args->fa;
41         const posix_spawnattr_t *restrict attr = args->attr;
42         sigset_t hset;
43
44         close(args->p[0]);
45
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)) {
58                         if (i-32<3U) {
59                                 sa.sa_handler = SIG_IGN;
60                         } else {
61                                 __libc_sigaction(i, 0, &sa);
62                                 if (sa.sa_handler==SIG_IGN) continue;
63                                 sa.sa_handler = SIG_DFL;
64                         }
65                 } else {
66                         continue;
67                 }
68                 __libc_sigaction(i, &sa, 0);
69         }
70
71         if (attr->__flags & POSIX_SPAWN_SETSID)
72                 if ((ret=__syscall(SYS_setsid)) < 0)
73                         goto fail;
74
75         if (attr->__flags & POSIX_SPAWN_SETPGROUP)
76                 if ((ret=__syscall(SYS_setpgid, 0, attr->__pgrp)))
77                         goto fail;
78
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))) )
85                         goto fail;
86
87         if (fa && fa->__actions) {
88                 struct fdop *op;
89                 int fd;
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. */
96                         if (op->fd == p) {
97                                 ret = __syscall(SYS_dup, p);
98                                 if (ret < 0) goto fail;
99                                 __syscall(SYS_close, p);
100                                 p = ret;
101                         }
102                         switch(op->cmd) {
103                         case FDOP_CLOSE:
104                                 __syscall(SYS_close, op->fd);
105                                 break;
106                         case FDOP_DUP2:
107                                 fd = op->srcfd;
108                                 if (fd != op->fd) {
109                                         if ((ret=__sys_dup2(fd, op->fd))<0)
110                                                 goto fail;
111                                 } else {
112                                         ret = __syscall(SYS_fcntl, fd, F_GETFD);
113                                         ret = __syscall(SYS_fcntl, fd, F_SETFD,
114                                                         ret & ~FD_CLOEXEC);
115                                         if (ret<0)
116                                                 goto fail;
117                                 }
118                                 break;
119                         case FDOP_OPEN:
120                                 fd = __sys_open(op->path, op->oflag, op->mode);
121                                 if ((ret=fd) < 0) goto fail;
122                                 if (fd != op->fd) {
123                                         if ((ret=__sys_dup2(fd, op->fd))<0)
124                                                 goto fail;
125                                         __syscall(SYS_close, fd);
126                                 }
127                                 break;
128                         }
129                 }
130         }
131
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);
137
138         pthread_sigmask(SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
139                 ? &attr->__mask : &args->oldmask, 0);
140
141         args->exec(args->path, args->argv, args->envp);
142         ret = -errno;
143
144 fail:
145         /* Since sizeof errno < PIPE_BUF, the write is atomic. */
146         ret = -ret;
147         if (ret) while (__syscall(SYS_write, p, &ret, sizeof ret) < 0);
148         _exit(127);
149 }
150
151
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])
157 {
158         pid_t pid;
159         char stack[1024+PATH_MAX];
160         int ec=0, cs;
161         struct args args;
162
163         if (pipe2(args.p, O_CLOEXEC))
164                 return errno;
165
166         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
167
168         args.path = path;
169         args.exec = exec;
170         args.fa = fa;
171         args.attr = attr ? attr : &(const posix_spawnattr_t){0};
172         args.argv = argv;
173         args.envp = envp;
174         pthread_sigmask(SIG_BLOCK, SIGALL_SET, &args.oldmask);
175
176         pid = __clone(child, stack+sizeof stack,
177                 CLONE_VM|CLONE_VFORK|SIGCHLD, &args);
178         close(args.p[1]);
179
180         if (pid > 0) {
181                 if (read(args.p[0], &ec, sizeof ec) != sizeof ec) ec = 0;
182                 else waitpid(pid, &(int){0}, 0);
183         } else {
184                 ec = -pid;
185         }
186
187         close(args.p[0]);
188
189         if (!ec && res) *res = pid;
190
191         pthread_sigmask(SIG_SETMASK, &args.oldmask, 0);
192         pthread_setcancelstate(cs, 0);
193
194         return ec;
195 }
196
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])
201 {
202         return __posix_spawnx(res, path, execve, fa, attr, argv, envp);
203 }