af1273170a034aa9781ebb277d6f6c1f27f8f9ea
[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_SETPGROUP)
77                 if ((ret=__syscall(SYS_setpgid, 0, attr->__pgrp)))
78                         goto fail;
79
80         /* Use syscalls directly because the library functions attempt
81          * to do a multi-threaded synchronized id-change, which would
82          * trash the parent's state. */
83         if (attr->__flags & POSIX_SPAWN_RESETIDS)
84                 if ((ret=__syscall(SYS_setgid, __syscall(SYS_getgid))) ||
85                     (ret=__syscall(SYS_setuid, __syscall(SYS_getuid))) )
86                         goto fail;
87
88         if (fa && fa->__actions) {
89                 struct fdop *op;
90                 int fd;
91                 for (op = fa->__actions; op->next; op = op->next);
92                 for (; op; op = op->prev) {
93                         /* It's possible that a file operation would clobber
94                          * the pipe fd used for synchronizing with the
95                          * parent. To avoid that, we dup the pipe onto
96                          * an unoccupied fd. */
97                         if (op->fd == p) {
98                                 ret = __syscall(SYS_dup, p);
99                                 if (ret < 0) goto fail;
100                                 __syscall(SYS_close, p);
101                                 p = ret;
102                         }
103                         switch(op->cmd) {
104                         case FDOP_CLOSE:
105                                 __syscall(SYS_close, op->fd);
106                                 break;
107                         case FDOP_DUP2:
108                                 if ((ret=__sys_dup2(op->srcfd, op->fd))<0)
109                                         goto fail;
110                                 break;
111                         case FDOP_OPEN:
112                                 fd = __sys_open(op->path, op->oflag, op->mode);
113                                 if ((ret=fd) < 0) goto fail;
114                                 if (fd != op->fd) {
115                                         if ((ret=__sys_dup2(fd, op->fd))<0)
116                                                 goto fail;
117                                         __syscall(SYS_close, fd);
118                                 }
119                                 break;
120                         }
121                 }
122         }
123
124         /* Close-on-exec flag may have been lost if we moved the pipe
125          * to a different fd. We don't use F_DUPFD_CLOEXEC above because
126          * it would fail on older kernels and atomicity is not needed --
127          * in this process there are no threads or signal handlers. */
128         __syscall(SYS_fcntl, p, F_SETFD, FD_CLOEXEC);
129
130         pthread_sigmask(SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
131                 ? &attr->__mask : &args->oldmask, 0);
132
133         args->exec(args->path, args->argv, args->envp);
134         ret = -errno;
135
136 fail:
137         /* Since sizeof errno < PIPE_BUF, the write is atomic. */
138         ret = -ret;
139         if (ret) while (write(p, &ret, sizeof ret) < 0);
140         _exit(127);
141 }
142
143
144 int __posix_spawnx(pid_t *restrict res, const char *restrict path,
145         int (*exec)(const char *, char *const *, char *const *),
146         const posix_spawn_file_actions_t *fa,
147         const posix_spawnattr_t *restrict attr,
148         char *const argv[restrict], char *const envp[restrict])
149 {
150         pid_t pid;
151         char stack[1024];
152         int ec=0, cs;
153         struct args args;
154
155         if (pipe2(args.p, O_CLOEXEC))
156                 return errno;
157
158         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
159
160         args.path = path;
161         args.exec = exec;
162         args.fa = fa;
163         args.attr = attr ? attr : &(const posix_spawnattr_t){0};
164         args.argv = argv;
165         args.envp = envp;
166         pthread_sigmask(SIG_BLOCK, SIGALL_SET, &args.oldmask);
167
168         pid = __clone(child, stack+sizeof stack,
169                 CLONE_VM|CLONE_VFORK|SIGCHLD, &args);
170         close(args.p[1]);
171
172         if (pid > 0) {
173                 if (read(args.p[0], &ec, sizeof ec) != sizeof ec) ec = 0;
174                 else waitpid(pid, &(int){0}, 0);
175         } else {
176                 ec = -pid;
177         }
178
179         close(args.p[0]);
180
181         if (!ec && res) *res = pid;
182
183         pthread_sigmask(SIG_SETMASK, &args.oldmask, 0);
184         pthread_setcancelstate(cs, 0);
185
186         return ec;
187 }
188
189 int posix_spawn(pid_t *restrict res, const char *restrict path,
190         const posix_spawn_file_actions_t *fa,
191         const posix_spawnattr_t *restrict attr,
192         char *const argv[restrict], char *const envp[restrict])
193 {
194         return __posix_spawnx(res, path, execve, fa, attr, argv, envp);
195 }