blob: 3588d0242f1dad95238b7b7468071697a2b7065b [file] [log] [blame]
Elliott Hughes14e3ff92017-10-06 16:58:36 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <spawn.h>
30
Dan Albertc972ea72017-10-13 14:32:43 -070031#include <errno.h>
Elliott Hughes14e3ff92017-10-06 16:58:36 -070032#include <fcntl.h>
33#include <signal.h>
34#include <stdlib.h>
Dan Albertc972ea72017-10-13 14:32:43 -070035#include <string.h>
Elliott Hughes14e3ff92017-10-06 16:58:36 -070036#include <unistd.h>
37
38#include "private/ScopedSignalBlocker.h"
39
40enum Action {
41 kOpen,
42 kClose,
43 kDup2
44};
45
46struct __posix_spawn_file_action {
47 __posix_spawn_file_action* next;
48
49 Action what;
50 int fd;
51 int new_fd;
52 char* path;
53 int flags;
54 mode_t mode;
55
56 void Do() {
57 if (what == kOpen) {
58 fd = open(path, flags, mode);
59 if (fd == -1) _exit(127);
60 // If it didn't land where we wanted it, move it.
61 if (fd != new_fd) {
62 if (dup2(fd, new_fd) == -1) _exit(127);
63 close(fd);
64 }
65 } else if (what == kClose) {
66 // Failure to close is ignored.
67 close(fd);
68 } else {
69 if (dup2(fd, new_fd) == -1) _exit(127);
70 }
71 }
72};
73
74struct __posix_spawn_file_actions {
75 __posix_spawn_file_action* head;
76 __posix_spawn_file_action* last;
77
78 void Do() {
79 for (__posix_spawn_file_action* action = head; action != nullptr; action = action->next) {
80 action->Do();
81 }
82 }
83};
84
85struct __posix_spawnattr {
86 short flags;
87 pid_t pgroup;
88 sched_param schedparam;
89 int schedpolicy;
90 sigset_t sigmask;
91 sigset_t sigdefault;
92
93 void Do() {
94 bool use_sigdefault = ((flags & POSIX_SPAWN_SETSIGDEF) != 0);
95
96 for (int s = 1; s < _NSIG; ++s) {
97 struct sigaction sa;
98 if (sigaction(s, nullptr, &sa) == -1) _exit(127);
99 if (sa.sa_handler == SIG_DFL) continue;
100 // POSIX: "Signals set to be caught by the calling process shall be set to the default
101 // action in the child process."
102 // POSIX: "If POSIX_SPAWN_SETSIGDEF is set ... signals in sigdefault ... shall be set to
103 // their default actions in the child process."
104 if (sa.sa_handler != SIG_IGN || (use_sigdefault && sigismember(&sigdefault, s))) {
105 sa.sa_handler = SIG_DFL;
106 if (sigaction(s, &sa, nullptr) == -1) _exit(127);
107 }
108 }
109
110 if ((flags & POSIX_SPAWN_SETPGROUP) != 0 && setpgid(0, pgroup) == -1) _exit(127);
111 if ((flags & POSIX_SPAWN_SETSID) != 0 && setsid() == -1) _exit(127);
112
113 // POSIX_SPAWN_SETSCHEDULER overrides POSIX_SPAWN_SETSCHEDPARAM, but it is not an error
114 // to set both.
115 if ((flags & POSIX_SPAWN_SETSCHEDULER) != 0) {
116 if (sched_setscheduler(0, schedpolicy, &schedparam) == -1) _exit(127);
117 } else if ((flags & POSIX_SPAWN_SETSCHEDPARAM) != 0) {
118 if (sched_setparam(0, &schedparam) == -1) _exit(127);
119 }
120
121 if ((flags & POSIX_SPAWN_RESETIDS) != 0) {
122 if (seteuid(getuid()) == -1 || setegid(getgid()) == -1) _exit(127);
123 }
124
125 if ((flags & POSIX_SPAWN_SETSIGMASK) != 0) {
126 if (sigprocmask(SIG_SETMASK, &sigmask, nullptr)) _exit(127);
127 }
128 }
129};
130
131static int posix_spawn(pid_t* pid_ptr,
132 const char* path,
133 const posix_spawn_file_actions_t* actions,
134 const posix_spawnattr_t* attr,
135 char* const argv[],
136 char* const env[],
137 int exec_fn(const char* path, char* const argv[], char* const env[])) {
138 // See http://man7.org/linux/man-pages/man3/posix_spawn.3.html
139 // and http://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn.html
140
141 ScopedSignalBlocker ssb;
142
143 short flags = attr ? (*attr)->flags : 0;
144 bool use_vfork = ((flags & POSIX_SPAWN_USEVFORK) != 0) || (actions == nullptr && flags == 0);
145
146 pid_t pid = use_vfork ? vfork() : fork();
147 if (pid == -1) return errno;
148
149 if (pid == 0) {
150 // Child.
151 if (attr) (*attr)->Do();
152 if (actions) (*actions)->Do();
153 if ((flags & POSIX_SPAWN_SETSIGMASK) == 0) ssb.reset();
154 exec_fn(path, argv, env ? env : environ);
155 _exit(127);
156 }
157
158 // Parent.
159 if (pid_ptr) *pid_ptr = pid;
160 return 0;
161}
162
163int posix_spawn(pid_t* pid, const char* path, const posix_spawn_file_actions_t* actions,
164 const posix_spawnattr_t* attr, char* const argv[], char* const env[]) {
165 return posix_spawn(pid, path, actions, attr, argv, env, execve);
166}
167
168int posix_spawnp(pid_t* pid, const char* file, const posix_spawn_file_actions_t* actions,
169 const posix_spawnattr_t* attr, char* const argv[], char* const env[]) {
170 return posix_spawn(pid, file, actions, attr, argv, env, execvpe);
171}
172
173int posix_spawnattr_init(posix_spawnattr_t* attr) {
174 *attr = reinterpret_cast<__posix_spawnattr*>(calloc(1, sizeof(__posix_spawnattr)));
175 return (*attr == nullptr) ? errno : 0;
176}
177
178int posix_spawnattr_destroy(posix_spawnattr_t* attr) {
179 free(*attr);
180 *attr = nullptr;
181 return 0;
182}
183
184int posix_spawnattr_setflags(posix_spawnattr_t* attr, short flags) {
185 if ((flags & ~(POSIX_SPAWN_RESETIDS | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF |
186 POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER |
187 POSIX_SPAWN_USEVFORK | POSIX_SPAWN_SETSID)) != 0) {
188 return EINVAL;
189 }
190 (*attr)->flags = flags;
191 return 0;
192}
193
194int posix_spawnattr_getflags(const posix_spawnattr_t* attr, short* flags) {
195 *flags = (*attr)->flags;
196 return 0;
197}
198
199int posix_spawnattr_setpgroup(posix_spawnattr_t* attr, pid_t pgroup) {
200 (*attr)->pgroup = pgroup;
201 return 0;
202}
203
204int posix_spawnattr_getpgroup(const posix_spawnattr_t* attr, pid_t* pgroup) {
205 *pgroup = (*attr)->pgroup;
206 return 0;
207}
208
209int posix_spawnattr_setsigmask(posix_spawnattr_t* attr, const sigset_t* mask) {
210 (*attr)->sigmask = *mask;
211 return 0;
212}
213
214int posix_spawnattr_getsigmask(const posix_spawnattr_t* attr, sigset_t* mask) {
215 *mask = (*attr)->sigmask;
216 return 0;
217}
218
219int posix_spawnattr_setsigdefault(posix_spawnattr_t* attr, const sigset_t* mask) {
220 (*attr)->sigdefault = *mask;
221 return 0;
222}
223
224int posix_spawnattr_getsigdefault(const posix_spawnattr_t* attr, sigset_t* mask) {
225 *mask = (*attr)->sigdefault;
226 return 0;
227}
228
229int posix_spawnattr_setschedparam(posix_spawnattr_t* attr, const struct sched_param* param) {
230 (*attr)->schedparam = *param;
231 return 0;
232}
233
234int posix_spawnattr_getschedparam(const posix_spawnattr_t* attr, struct sched_param* param) {
235 *param = (*attr)->schedparam;
236 return 0;
237}
238
239int posix_spawnattr_setschedpolicy(posix_spawnattr_t* attr, int policy) {
240 (*attr)->schedpolicy = policy;
241 return 0;
242}
243
244int posix_spawnattr_getschedpolicy(const posix_spawnattr_t* attr, int* policy) {
245 *policy = (*attr)->schedpolicy;
246 return 0;
247}
248
249int posix_spawn_file_actions_init(posix_spawn_file_actions_t* actions) {
250 *actions = reinterpret_cast<__posix_spawn_file_actions*>(calloc(1, sizeof(**actions)));
251 return (*actions == nullptr) ? errno : 0;
252}
253
254int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t* actions) {
255 __posix_spawn_file_action* a = (*actions)->head;
256 while (a) {
257 __posix_spawn_file_action* last = a;
258 a = a->next;
259 free(last->path);
260 free(last);
261 }
262 free(*actions);
263 *actions = nullptr;
264 return 0;
265}
266
267static int posix_spawn_add_file_action(posix_spawn_file_actions_t* actions,
268 Action what,
269 int fd,
270 int new_fd,
271 const char* path,
272 int flags,
273 mode_t mode) {
274 __posix_spawn_file_action* action =
275 reinterpret_cast<__posix_spawn_file_action*>(malloc(sizeof(*action)));
276 if (action == nullptr) return errno;
277
278 action->next = nullptr;
279 if (path != nullptr) {
280 action->path = strdup(path);
281 if (action->path == nullptr) {
282 free(action);
283 return errno;
284 }
285 } else {
286 action->path = nullptr;
287 }
288 action->what = what;
289 action->fd = fd;
290 action->new_fd = new_fd;
291 action->flags = flags;
292 action->mode = mode;
293
294 if ((*actions)->head == nullptr) {
295 (*actions)->head = (*actions)->last = action;
296 } else {
297 (*actions)->last->next = action;
298 (*actions)->last = action;
299 }
300
301 return 0;
302}
303
304int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t* actions,
305 int fd, const char* path, int flags, mode_t mode) {
306 if (fd < 0) return EBADF;
307 return posix_spawn_add_file_action(actions, kOpen, -1, fd, path, flags, mode);
308}
309
310int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t* actions, int fd) {
311 if (fd < 0) return EBADF;
312 return posix_spawn_add_file_action(actions, kClose, fd, -1, nullptr, 0, 0);
313}
314
315int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t* actions, int fd, int new_fd) {
316 if (fd < 0 || new_fd < 0) return EBADF;
317 return posix_spawn_add_file_action(actions, kDup2, fd, new_fd, nullptr, 0, 0);
318}