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