blob: e5075f54e8e1ad03c1bc17c5c57a7339eaba9dbb [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;
Elliott Hughes14e3ff92017-10-06 16:58:36 -070092};
93
Elliott Hughes7bfacaa2017-11-28 19:58:00 -080094static void ApplyAttrs(short flags, const posix_spawnattr_t* attr) {
95 // POSIX: "If POSIX_SPAWN_SETSIGDEF is set ... signals in sigdefault ...
96 // shall be set to their default actions in the child process."
97 // POSIX: "Signals set to be caught by the calling process shall be
98 // set to the default action in the child process."
99 bool use_sigdefault = ((flags & POSIX_SPAWN_SETSIGDEF) != 0);
100 const struct sigaction default_sa = { .sa_handler = SIG_DFL };
101 for (int s = 1; s < _NSIG; ++s) {
102 bool reset = false;
103 if (use_sigdefault && sigismember(&(*attr)->sigdefault, s)) {
104 reset = true;
105 } else {
106 struct sigaction current;
107 if (sigaction(s, nullptr, &current) == -1) _exit(127);
108 reset = (current.sa_handler != SIG_IGN && current.sa_handler != SIG_DFL);
109 }
110 if (reset && sigaction(s, &default_sa, nullptr) == -1) _exit(127);
111 }
112
113 if ((flags & POSIX_SPAWN_SETPGROUP) != 0 && setpgid(0, (*attr)->pgroup) == -1) _exit(127);
114 if ((flags & POSIX_SPAWN_SETSID) != 0 && setsid() == -1) _exit(127);
115
116 // POSIX_SPAWN_SETSCHEDULER overrides POSIX_SPAWN_SETSCHEDPARAM, but it is not an error
117 // to set both.
118 if ((flags & POSIX_SPAWN_SETSCHEDULER) != 0) {
119 if (sched_setscheduler(0, (*attr)->schedpolicy, &(*attr)->schedparam) == -1) _exit(127);
120 } else if ((flags & POSIX_SPAWN_SETSCHEDPARAM) != 0) {
121 if (sched_setparam(0, &(*attr)->schedparam) == -1) _exit(127);
122 }
123
124 if ((flags & POSIX_SPAWN_RESETIDS) != 0) {
125 if (seteuid(getuid()) == -1 || setegid(getgid()) == -1) _exit(127);
126 }
127
128 if ((flags & POSIX_SPAWN_SETSIGMASK) != 0) {
129 if (sigprocmask(SIG_SETMASK, &(*attr)->sigmask, nullptr)) _exit(127);
130 }
131}
132
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700133static int posix_spawn(pid_t* pid_ptr,
134 const char* path,
135 const posix_spawn_file_actions_t* actions,
136 const posix_spawnattr_t* attr,
137 char* const argv[],
138 char* const env[],
139 int exec_fn(const char* path, char* const argv[], char* const env[])) {
140 // See http://man7.org/linux/man-pages/man3/posix_spawn.3.html
141 // and http://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn.html
142
143 ScopedSignalBlocker ssb;
144
145 short flags = attr ? (*attr)->flags : 0;
146 bool use_vfork = ((flags & POSIX_SPAWN_USEVFORK) != 0) || (actions == nullptr && flags == 0);
147
148 pid_t pid = use_vfork ? vfork() : fork();
149 if (pid == -1) return errno;
150
151 if (pid == 0) {
152 // Child.
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800153 ApplyAttrs(flags, attr);
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700154 if (actions) (*actions)->Do();
155 if ((flags & POSIX_SPAWN_SETSIGMASK) == 0) ssb.reset();
156 exec_fn(path, argv, env ? env : environ);
157 _exit(127);
158 }
159
160 // Parent.
161 if (pid_ptr) *pid_ptr = pid;
162 return 0;
163}
164
165int posix_spawn(pid_t* pid, const char* path, const posix_spawn_file_actions_t* actions,
166 const posix_spawnattr_t* attr, char* const argv[], char* const env[]) {
167 return posix_spawn(pid, path, actions, attr, argv, env, execve);
168}
169
170int posix_spawnp(pid_t* pid, const char* file, const posix_spawn_file_actions_t* actions,
171 const posix_spawnattr_t* attr, char* const argv[], char* const env[]) {
172 return posix_spawn(pid, file, actions, attr, argv, env, execvpe);
173}
174
175int posix_spawnattr_init(posix_spawnattr_t* attr) {
176 *attr = reinterpret_cast<__posix_spawnattr*>(calloc(1, sizeof(__posix_spawnattr)));
177 return (*attr == nullptr) ? errno : 0;
178}
179
180int posix_spawnattr_destroy(posix_spawnattr_t* attr) {
181 free(*attr);
182 *attr = nullptr;
183 return 0;
184}
185
186int posix_spawnattr_setflags(posix_spawnattr_t* attr, short flags) {
187 if ((flags & ~(POSIX_SPAWN_RESETIDS | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF |
188 POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER |
189 POSIX_SPAWN_USEVFORK | POSIX_SPAWN_SETSID)) != 0) {
190 return EINVAL;
191 }
192 (*attr)->flags = flags;
193 return 0;
194}
195
196int posix_spawnattr_getflags(const posix_spawnattr_t* attr, short* flags) {
197 *flags = (*attr)->flags;
198 return 0;
199}
200
201int posix_spawnattr_setpgroup(posix_spawnattr_t* attr, pid_t pgroup) {
202 (*attr)->pgroup = pgroup;
203 return 0;
204}
205
206int posix_spawnattr_getpgroup(const posix_spawnattr_t* attr, pid_t* pgroup) {
207 *pgroup = (*attr)->pgroup;
208 return 0;
209}
210
211int posix_spawnattr_setsigmask(posix_spawnattr_t* attr, const sigset_t* mask) {
212 (*attr)->sigmask = *mask;
213 return 0;
214}
215
216int posix_spawnattr_getsigmask(const posix_spawnattr_t* attr, sigset_t* mask) {
217 *mask = (*attr)->sigmask;
218 return 0;
219}
220
221int posix_spawnattr_setsigdefault(posix_spawnattr_t* attr, const sigset_t* mask) {
222 (*attr)->sigdefault = *mask;
223 return 0;
224}
225
226int posix_spawnattr_getsigdefault(const posix_spawnattr_t* attr, sigset_t* mask) {
227 *mask = (*attr)->sigdefault;
228 return 0;
229}
230
231int posix_spawnattr_setschedparam(posix_spawnattr_t* attr, const struct sched_param* param) {
232 (*attr)->schedparam = *param;
233 return 0;
234}
235
236int posix_spawnattr_getschedparam(const posix_spawnattr_t* attr, struct sched_param* param) {
237 *param = (*attr)->schedparam;
238 return 0;
239}
240
241int posix_spawnattr_setschedpolicy(posix_spawnattr_t* attr, int policy) {
242 (*attr)->schedpolicy = policy;
243 return 0;
244}
245
246int posix_spawnattr_getschedpolicy(const posix_spawnattr_t* attr, int* policy) {
247 *policy = (*attr)->schedpolicy;
248 return 0;
249}
250
251int posix_spawn_file_actions_init(posix_spawn_file_actions_t* actions) {
252 *actions = reinterpret_cast<__posix_spawn_file_actions*>(calloc(1, sizeof(**actions)));
253 return (*actions == nullptr) ? errno : 0;
254}
255
256int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t* actions) {
257 __posix_spawn_file_action* a = (*actions)->head;
258 while (a) {
259 __posix_spawn_file_action* last = a;
260 a = a->next;
261 free(last->path);
262 free(last);
263 }
264 free(*actions);
265 *actions = nullptr;
266 return 0;
267}
268
269static int posix_spawn_add_file_action(posix_spawn_file_actions_t* actions,
270 Action what,
271 int fd,
272 int new_fd,
273 const char* path,
274 int flags,
275 mode_t mode) {
276 __posix_spawn_file_action* action =
277 reinterpret_cast<__posix_spawn_file_action*>(malloc(sizeof(*action)));
278 if (action == nullptr) return errno;
279
280 action->next = nullptr;
281 if (path != nullptr) {
282 action->path = strdup(path);
283 if (action->path == nullptr) {
284 free(action);
285 return errno;
286 }
287 } else {
288 action->path = nullptr;
289 }
290 action->what = what;
291 action->fd = fd;
292 action->new_fd = new_fd;
293 action->flags = flags;
294 action->mode = mode;
295
296 if ((*actions)->head == nullptr) {
297 (*actions)->head = (*actions)->last = action;
298 } else {
299 (*actions)->last->next = action;
300 (*actions)->last = action;
301 }
302
303 return 0;
304}
305
306int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t* actions,
307 int fd, const char* path, int flags, mode_t mode) {
308 if (fd < 0) return EBADF;
309 return posix_spawn_add_file_action(actions, kOpen, -1, fd, path, flags, mode);
310}
311
312int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t* actions, int fd) {
313 if (fd < 0) return EBADF;
314 return posix_spawn_add_file_action(actions, kClose, fd, -1, nullptr, 0, 0);
315}
316
317int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t* actions, int fd, int new_fd) {
318 if (fd < 0 || new_fd < 0) return EBADF;
319 return posix_spawn_add_file_action(actions, kDup2, fd, new_fd, nullptr, 0, 0);
320}