optimize posix_spawn to avoid spurious sigaction syscalls
[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 = __syscall(SYS_open, op->path,
100                                         op->oflag | O_LARGEFILE, op->mode);
101                                 if ((ret=fd) < 0) goto fail;
102                                 if (fd != op->fd) {
103                                         if ((ret=__syscall(SYS_dup2, fd, op->fd))<0)
104                                                 goto fail;
105                                         __syscall(SYS_close, fd);
106                                 }
107                                 break;
108                         }
109                 }
110         }
111
112         /* Close-on-exec flag may have been lost if we moved the pipe
113          * to a different fd. We don't use F_DUPFD_CLOEXEC above because
114          * it would fail on older kernels and atomicity is not needed --
115          * in this process there are no threads or signal handlers. */
116         __syscall(SYS_fcntl, p, F_SETFD, FD_CLOEXEC);
117
118         pthread_sigmask(SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
119                 ? &attr->__mask : &args->oldmask, 0);
120
121         args->exec(args->path, args->argv, args->envp);
122         ret = -errno;
123
124 fail:
125         /* Since sizeof errno < PIPE_BUF, the write is atomic. */
126         ret = -ret;
127         if (ret) while (write(p, &ret, sizeof ret) < 0);
128         _exit(127);
129 }
130
131
132 int __posix_spawnx(pid_t *restrict res, const char *restrict path,
133         int (*exec)(const char *, char *const *, char *const *),
134         const posix_spawn_file_actions_t *fa,
135         const posix_spawnattr_t *restrict attr,
136         char *const argv[restrict], char *const envp[restrict])
137 {
138         pid_t pid;
139         char stack[1024];
140         int ec=0, cs;
141         struct args args;
142
143         if (pipe2(args.p, O_CLOEXEC))
144                 return errno;
145
146         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
147
148         args.path = path;
149         args.exec = exec;
150         args.fa = fa;
151         args.attr = attr ? attr : &(const posix_spawnattr_t){0};
152         args.argv = argv;
153         args.envp = envp;
154         pthread_sigmask(SIG_BLOCK, SIGALL_SET, &args.oldmask);
155
156         pid = __clone(child, stack+sizeof stack,
157                 CLONE_VM|CLONE_VFORK|SIGCHLD, &args);
158         close(args.p[1]);
159
160         if (pid > 0) {
161                 if (read(args.p[0], &ec, sizeof ec) != sizeof ec) ec = 0;
162                 else waitpid(pid, &(int){0}, 0);
163         } else {
164                 ec = -pid;
165         }
166
167         close(args.p[0]);
168
169         if (!ec) *res = pid;
170
171         pthread_sigmask(SIG_SETMASK, &args.oldmask, 0);
172         pthread_setcancelstate(cs, 0);
173
174         return ec;
175 }
176
177 int posix_spawn(pid_t *restrict res, const char *restrict path,
178         const posix_spawn_file_actions_t *fa,
179         const posix_spawnattr_t *restrict attr,
180         char *const argv[restrict], char *const envp[restrict])
181 {
182         return __posix_spawnx(res, path, execve, fa, attr, argv, envp);
183 }