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