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