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 <errno.h>
8 #include <sys/wait.h>
9 #include "syscall.h"
10 #include "lock.h"
11 #include "pthread_impl.h"
12 #include "fdop.h"
13 #include <unsupported_api.h>
14
15 struct args {
16 int p[2];
17 sigset_t oldmask;
18 const char *path;
19 const posix_spawn_file_actions_t *fa;
20 const posix_spawnattr_t *restrict attr;
21 char *const *argv, *const *envp;
22 };
23
__sys_dup2(int old,int new)24 static int __sys_dup2(int old, int new)
25 {
26 #ifdef SYS_dup2
27 return __syscall(SYS_dup2, old, new);
28 #else
29 return __syscall(SYS_dup3, old, new, 0);
30 #endif
31 }
32
child(void * args_vp)33 static int child(void *args_vp)
34 {
35 int i, ret;
36 struct sigaction sa = {0};
37 struct args *args = args_vp;
38 int p = args->p[1];
39 const posix_spawn_file_actions_t *fa = args->fa;
40 const posix_spawnattr_t *restrict attr = args->attr;
41 sigset_t hset;
42
43 close(args->p[0]);
44
45 /* All signal dispositions must be either SIG_DFL or SIG_IGN
46 * before signals are unblocked. Otherwise a signal handler
47 * from the parent might get run in the child while sharing
48 * memory, with unpredictable and dangerous results. To
49 * reduce overhead, sigaction has tracked for us which signals
50 * potentially have a signal handler. */
51 __get_handler_set(&hset);
52 for (i=1; i<_NSIG; i++) {
53 if ((attr->__flags & POSIX_SPAWN_SETSIGDEF)
54 && sigismember(&attr->__def, i)) {
55 sa.sa_handler = SIG_DFL;
56 } else if (sigismember(&hset, i)) {
57 if (i-32<3U) {
58 sa.sa_handler = SIG_IGN;
59 } else {
60 __libc_sigaction(i, 0, &sa);
61 if (sa.sa_handler==SIG_IGN) continue;
62 sa.sa_handler = SIG_DFL;
63 }
64 } else {
65 continue;
66 }
67 __libc_sigaction(i, &sa, 0);
68 }
69
70 if (attr->__flags & POSIX_SPAWN_SETSID)
71 if ((ret=__syscall(SYS_setsid)) < 0)
72 goto fail;
73
74 if (attr->__flags & POSIX_SPAWN_SETPGROUP)
75 if ((ret=__syscall(SYS_setpgid, 0, attr->__pgrp)))
76 goto fail;
77
78 /* Use syscalls directly because the library functions attempt
79 * to do a multi-threaded synchronized id-change, which would
80 * trash the parent's state. */
81 if (attr->__flags & POSIX_SPAWN_RESETIDS)
82 if ((ret=__syscall(SYS_setgid, __syscall(SYS_getgid))) ||
83 (ret=__syscall(SYS_setuid, __syscall(SYS_getuid))) )
84 goto fail;
85
86 if (fa && fa->__actions) {
87 struct fdop *op;
88 int fd;
89 for (op = fa->__actions; op->next; op = op->next);
90 for (; op; op = op->prev) {
91 /* It's possible that a file operation would clobber
92 * the pipe fd used for synchronizing with the
93 * parent. To avoid that, we dup the pipe onto
94 * an unoccupied fd. */
95 if (op->fd == p) {
96 ret = __syscall(SYS_dup, p);
97 if (ret < 0) goto fail;
98 __syscall(SYS_close, p);
99 p = ret;
100 }
101 switch(op->cmd) {
102 case FDOP_CLOSE:
103 __syscall(SYS_close, op->fd);
104 break;
105 case FDOP_DUP2:
106 fd = op->srcfd;
107 if (fd == p) {
108 ret = -EBADF;
109 goto fail;
110 }
111 if (fd != op->fd) {
112 if ((ret=__sys_dup2(fd, op->fd))<0)
113 goto fail;
114 } else {
115 ret = __syscall(SYS_fcntl, fd, F_GETFD);
116 ret = __syscall(SYS_fcntl, fd, F_SETFD,
117 ret & ~FD_CLOEXEC);
118 if (ret<0)
119 goto fail;
120 }
121 break;
122 case FDOP_OPEN:
123 fd = __sys_open(op->path, op->oflag, op->mode);
124 if ((ret=fd) < 0) goto fail;
125 if (fd != op->fd) {
126 if ((ret=__sys_dup2(fd, op->fd))<0)
127 goto fail;
128 __syscall(SYS_close, fd);
129 }
130 break;
131 case FDOP_CHDIR:
132 ret = __syscall(SYS_chdir, op->path);
133 if (ret<0) goto fail;
134 break;
135 case FDOP_FCHDIR:
136 ret = __syscall(SYS_fchdir, op->fd);
137 if (ret<0) goto fail;
138 break;
139 }
140 }
141 }
142
143 /* Close-on-exec flag may have been lost if we moved the pipe
144 * to a different fd. We don't use F_DUPFD_CLOEXEC above because
145 * it would fail on older kernels and atomicity is not needed --
146 * in this process there are no threads or signal handlers. */
147 __syscall(SYS_fcntl, p, F_SETFD, FD_CLOEXEC);
148
149 pthread_sigmask(SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
150 ? &attr->__mask : &args->oldmask, 0);
151
152 int (*exec)(const char *, char *const *, char *const *) =
153 attr->__fn ? (int (*)())attr->__fn : execve;
154
155 exec(args->path, args->argv, args->envp);
156 ret = -errno;
157
158 fail:
159 /* Since sizeof errno < PIPE_BUF, the write is atomic. */
160 ret = -ret;
161 if (ret) {
162 int r;
163 do r = __syscall(SYS_write, p, &ret, sizeof ret);
164 while (r<0 && r!=-EPIPE);
165 }
166 _exit(127);
167 }
168
169
posix_spawn(pid_t * restrict res,const char * restrict path,const posix_spawn_file_actions_t * fa,const posix_spawnattr_t * restrict attr,char * const argv[restrict],char * const envp[restrict])170 int posix_spawn(pid_t *restrict res, const char *restrict path,
171 const posix_spawn_file_actions_t *fa,
172 const posix_spawnattr_t *restrict attr,
173 char *const argv[restrict], char *const envp[restrict])
174 {
175 pid_t pid;
176 char stack[1024+PATH_MAX];
177 int ec=0, cs;
178 struct args args;
179
180 UNSUPPORTED_API_VOID(LITEOS_A);
181 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
182
183 args.path = path;
184 args.fa = fa;
185 args.attr = attr ? attr : &(const posix_spawnattr_t){0};
186 args.argv = argv;
187 args.envp = envp;
188 pthread_sigmask(SIG_BLOCK, SIGALL_SET, &args.oldmask);
189
190 /* The lock guards both against seeing a SIGABRT disposition change
191 * by abort and against leaking the pipe fd to fork-without-exec. */
192 LOCK(__abort_lock);
193
194 if (pipe2(args.p, O_CLOEXEC)) {
195 UNLOCK(__abort_lock);
196 ec = errno;
197 goto fail;
198 }
199
200 pid = __clone(child, stack+sizeof stack,
201 CLONE_VM|CLONE_VFORK|SIGCHLD, &args);
202 close(args.p[1]);
203 UNLOCK(__abort_lock);
204
205 if (pid > 0) {
206 if (read(args.p[0], &ec, sizeof ec) != sizeof ec) ec = 0;
207 else waitpid(pid, &(int){0}, 0);
208 } else {
209 ec = -pid;
210 }
211
212 close(args.p[0]);
213
214 if (!ec && res) *res = pid;
215
216 fail:
217 pthread_sigmask(SIG_SETMASK, &args.oldmask, 0);
218 pthread_setcancelstate(cs, 0);
219
220 return ec;
221 }
222