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