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