16308fb70737d9b2330a78b2cb7cd1e86869ba31
[musl] / 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         const posix_spawn_file_actions_t *fa;
18         const posix_spawnattr_t *restrict attr;
19         char *const *argv, *const *envp;
20 };
21
22 void __get_handler_set(sigset_t *);
23
24 static int __sys_dup2(int old, int new)
25 {
26 #ifdef SYS_dup2
27         return __syscall(SYS_dup2, old, new);
28 #else
29         return __syscall(SYS_dup3, old, new, 0);
30 #endif
31 }
32
33 static int child(void *args_vp)
34 {
35         int i, ret;
36         struct sigaction sa = {0};
37         struct args *args = args_vp;
38         int p = args->p[1];
39         const posix_spawn_file_actions_t *fa = args->fa;
40         const posix_spawnattr_t *restrict attr = args->attr;
41         sigset_t hset;
42
43         close(args->p[0]);
44
45         /* All signal dispositions must be either SIG_DFL or SIG_IGN
46          * before signals are unblocked. Otherwise a signal handler
47          * from the parent might get run in the child while sharing
48          * memory, with unpredictable and dangerous results. To
49          * reduce overhead, sigaction has tracked for us which signals
50          * potentially have a signal handler. */
51         __get_handler_set(&hset);
52         for (i=1; i<_NSIG; i++) {
53                 if ((attr->__flags & POSIX_SPAWN_SETSIGDEF)
54                      && sigismember(&attr->__def, i)) {
55                         sa.sa_handler = SIG_DFL;
56                 } else if (sigismember(&hset, i)) {
57                         if (i-32<3U) {
58                                 sa.sa_handler = SIG_IGN;
59                         } else {
60                                 __libc_sigaction(i, 0, &sa);
61                                 if (sa.sa_handler==SIG_IGN) continue;
62                                 sa.sa_handler = SIG_DFL;
63                         }
64                 } else {
65                         continue;
66                 }
67                 __libc_sigaction(i, &sa, 0);
68         }
69
70         if (attr->__flags & POSIX_SPAWN_SETSID)
71                 if ((ret=__syscall(SYS_setsid)) < 0)
72                         goto fail;
73
74         if (attr->__flags & POSIX_SPAWN_SETPGROUP)
75                 if ((ret=__syscall(SYS_setpgid, 0, attr->__pgrp)))
76                         goto fail;
77
78         /* Use syscalls directly because the library functions attempt
79          * to do a multi-threaded synchronized id-change, which would
80          * trash the parent's state. */
81         if (attr->__flags & POSIX_SPAWN_RESETIDS)
82                 if ((ret=__syscall(SYS_setgid, __syscall(SYS_getgid))) ||
83                     (ret=__syscall(SYS_setuid, __syscall(SYS_getuid))) )
84                         goto fail;
85
86         if (fa && fa->__actions) {
87                 struct fdop *op;
88                 int fd;
89                 for (op = fa->__actions; op->next; op = op->next);
90                 for (; op; op = op->prev) {
91                         /* It's possible that a file operation would clobber
92                          * the pipe fd used for synchronizing with the
93                          * parent. To avoid that, we dup the pipe onto
94                          * an unoccupied fd. */
95                         if (op->fd == p) {
96                                 ret = __syscall(SYS_dup, p);
97                                 if (ret < 0) goto fail;
98                                 __syscall(SYS_close, p);
99                                 p = ret;
100                         }
101                         switch(op->cmd) {
102                         case FDOP_CLOSE:
103                                 __syscall(SYS_close, op->fd);
104                                 break;
105                         case FDOP_DUP2:
106                                 fd = op->srcfd;
107                                 if (fd != op->fd) {
108                                         if ((ret=__sys_dup2(fd, op->fd))<0)
109                                                 goto fail;
110                                 } else {
111                                         ret = __syscall(SYS_fcntl, fd, F_GETFD);
112                                         ret = __syscall(SYS_fcntl, fd, F_SETFD,
113                                                         ret & ~FD_CLOEXEC);
114                                         if (ret<0)
115                                                 goto fail;
116                                 }
117                                 break;
118                         case FDOP_OPEN:
119                                 fd = __sys_open(op->path, op->oflag, op->mode);
120                                 if ((ret=fd) < 0) goto fail;
121                                 if (fd != op->fd) {
122                                         if ((ret=__sys_dup2(fd, op->fd))<0)
123                                                 goto fail;
124                                         __syscall(SYS_close, fd);
125                                 }
126                                 break;
127                         }
128                 }
129         }
130
131         /* Close-on-exec flag may have been lost if we moved the pipe
132          * to a different fd. We don't use F_DUPFD_CLOEXEC above because
133          * it would fail on older kernels and atomicity is not needed --
134          * in this process there are no threads or signal handlers. */
135         __syscall(SYS_fcntl, p, F_SETFD, FD_CLOEXEC);
136
137         pthread_sigmask(SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
138                 ? &attr->__mask : &args->oldmask, 0);
139
140         int (*exec)(const char *, char *const *, char *const *) =
141                 attr->__fn ? (int (*)())attr->__fn : execve;
142
143         exec(args->path, args->argv, args->envp);
144         ret = -errno;
145
146 fail:
147         /* Since sizeof errno < PIPE_BUF, the write is atomic. */
148         ret = -ret;
149         if (ret) while (__syscall(SYS_write, p, &ret, sizeof ret) < 0);
150         _exit(127);
151 }
152
153
154 int posix_spawn(pid_t *restrict res, const char *restrict path,
155         const posix_spawn_file_actions_t *fa,
156         const posix_spawnattr_t *restrict attr,
157         char *const argv[restrict], char *const envp[restrict])
158 {
159         pid_t pid;
160         char stack[1024+PATH_MAX];
161         int ec=0, cs;
162         struct args args;
163
164         if (pipe2(args.p, O_CLOEXEC))
165                 return errno;
166
167         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
168
169         args.path = path;
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 }