1 /* 2 * Copyright 2017, Jérôme Duval, jerome.Duval@gmail.com 3 * Distributed under the terms of the MIT license. 4 */ 5 6 7 #include <spawn.h> 8 9 #include <errno.h> 10 #include <signal.h> 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <string.h> 14 #include <unistd.h> 15 16 #include <signal_defs.h> 17 #include <syscalls.h> 18 19 20 enum action_type { 21 file_action_open, 22 file_action_close, 23 file_action_dup2 24 }; 25 26 struct _file_action { 27 enum action_type type; 28 int fd; 29 union { 30 struct { 31 char* path; 32 int oflag; 33 mode_t mode; 34 } open_action; 35 struct { 36 int srcfd; 37 } dup2_action; 38 } action; 39 }; 40 41 struct _posix_spawnattr { 42 short flags; 43 pid_t pgroup; 44 sigset_t sigdefault; 45 sigset_t sigmask; 46 }; 47 48 struct _posix_spawn_file_actions { 49 int size; 50 int count; 51 _file_action *actions; 52 }; 53 54 55 static int 56 posix_spawn_file_actions_extend(struct _posix_spawn_file_actions *actions) 57 { 58 int newsize = actions->size + 4; 59 void *newactions = realloc(actions->actions, 60 newsize * sizeof(struct _file_action)); 61 if (newactions == NULL) 62 return ENOMEM; 63 actions->actions = (struct _file_action*)newactions; 64 actions->size = newsize; 65 return 0; 66 } 67 68 69 int 70 posix_spawn_file_actions_init(posix_spawn_file_actions_t *_file_actions) 71 { 72 posix_spawn_file_actions_t actions = (posix_spawn_file_actions_t)malloc( 73 sizeof(struct _posix_spawn_file_actions)); 74 75 if (actions == NULL) 76 return ENOMEM; 77 78 memset(actions, 0, sizeof(*actions)); 79 *_file_actions = actions; 80 81 return 0; 82 } 83 84 85 int 86 posix_spawn_file_actions_destroy(posix_spawn_file_actions_t *_actions) 87 { 88 struct _posix_spawn_file_actions* actions = _actions != NULL ? *_actions : NULL; 89 90 if (actions == NULL) 91 return EINVAL; 92 93 free(actions); 94 95 return 0; 96 } 97 98 99 int 100 posix_spawn_file_actions_addopen(posix_spawn_file_actions_t *_actions, 101 int fildes, const char *path, int oflag, mode_t mode) 102 { 103 struct _posix_spawn_file_actions* actions = _actions != NULL ? *_actions : NULL; 104 105 if (actions == NULL) 106 return EINVAL; 107 108 if (fildes < 0 || fildes >= sysconf(_SC_OPEN_MAX)) 109 return EBADF; 110 111 char* npath = strdup(path); 112 if (npath == NULL) 113 return ENOMEM; 114 if (actions->count == actions->size 115 && posix_spawn_file_actions_extend(actions) != 0) { 116 free(npath); 117 return ENOMEM; 118 } 119 120 struct _file_action *action = &actions->actions[actions->count]; 121 action->type = file_action_open; 122 action->fd = fildes; 123 action->action.open_action.path = npath; 124 action->action.open_action.oflag = oflag; 125 action->action.open_action.mode = mode; 126 actions->count++; 127 return 0; 128 } 129 130 131 int 132 posix_spawn_file_actions_addclose(posix_spawn_file_actions_t *_actions, 133 int fildes) 134 { 135 struct _posix_spawn_file_actions* actions = _actions != NULL ? *_actions : NULL; 136 137 if (actions == NULL) 138 return EINVAL; 139 140 if (fildes < 0 || fildes >= sysconf(_SC_OPEN_MAX)) 141 return EBADF; 142 143 if (actions->count == actions->size 144 && posix_spawn_file_actions_extend(actions) != 0) { 145 return ENOMEM; 146 } 147 148 struct _file_action *action = &actions->actions[actions->count]; 149 action->type = file_action_close; 150 action->fd = fildes; 151 actions->count++; 152 return 0; 153 } 154 155 156 int 157 posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *_actions, 158 int fildes, int newfildes) 159 { 160 struct _posix_spawn_file_actions* actions = _actions != NULL ? *_actions : NULL; 161 162 if (actions == NULL) 163 return EINVAL; 164 165 if (fildes < 0 || fildes >= sysconf(_SC_OPEN_MAX)) 166 return EBADF; 167 168 if (actions->count == actions->size 169 && posix_spawn_file_actions_extend(actions) != 0) { 170 return ENOMEM; 171 } 172 173 struct _file_action *action = &actions->actions[actions->count]; 174 action->type = file_action_dup2; 175 action->fd = newfildes; 176 action->action.dup2_action.srcfd = fildes; 177 actions->count++; 178 return 0; 179 } 180 181 182 int 183 posix_spawnattr_init(posix_spawnattr_t *_attr) 184 { 185 posix_spawnattr_t attr = (posix_spawnattr_t)malloc( 186 sizeof(struct _posix_spawnattr)); 187 188 if (attr == NULL) 189 return ENOMEM; 190 191 memset(attr, 0, sizeof(*attr)); 192 *_attr = attr; 193 194 return 0; 195 } 196 197 198 int 199 posix_spawnattr_destroy(posix_spawnattr_t *_attr) 200 { 201 struct _posix_spawnattr* attr = _attr != NULL ? *_attr : NULL; 202 203 if (attr == NULL) 204 return EINVAL; 205 206 free(attr); 207 208 return 0; 209 } 210 211 212 int 213 posix_spawnattr_getflags(const posix_spawnattr_t *_attr, short *flags) 214 { 215 struct _posix_spawnattr *attr; 216 217 if (_attr == NULL || (attr = *_attr) == NULL || flags == NULL) 218 return EINVAL; 219 220 *flags = attr->flags; 221 222 return 0; 223 } 224 225 226 int 227 posix_spawnattr_setflags(posix_spawnattr_t *_attr, short flags) 228 { 229 struct _posix_spawnattr *attr; 230 231 if (_attr == NULL || (attr = *_attr) == NULL) 232 return EINVAL; 233 234 if ((flags & ~(POSIX_SPAWN_RESETIDS | POSIX_SPAWN_SETPGROUP 235 | POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK)) != 0) { 236 return EINVAL; 237 } 238 239 attr->flags = flags; 240 241 return 0; 242 } 243 244 245 int 246 posix_spawnattr_getpgroup(const posix_spawnattr_t *_attr, pid_t *pgroup) 247 { 248 struct _posix_spawnattr *attr; 249 250 if (_attr == NULL || (attr = *_attr) == NULL || pgroup == NULL) 251 return EINVAL; 252 253 *pgroup = attr->pgroup; 254 255 return 0; 256 } 257 258 259 int 260 posix_spawnattr_setpgroup(posix_spawnattr_t *_attr, pid_t pgroup) 261 { 262 struct _posix_spawnattr *attr; 263 264 if (_attr == NULL || (attr = *_attr) == NULL) 265 return EINVAL; 266 267 attr->pgroup = pgroup; 268 269 return 0; 270 } 271 272 273 int 274 posix_spawnattr_getsigdefault(const posix_spawnattr_t *_attr, sigset_t *sigdefault) 275 { 276 struct _posix_spawnattr *attr; 277 278 if (_attr == NULL || (attr = *_attr) == NULL || sigdefault == NULL) 279 return EINVAL; 280 281 memcpy(sigdefault, &attr->sigdefault, sizeof(sigset_t)); 282 283 return 0; 284 } 285 286 287 int 288 posix_spawnattr_setsigdefault(posix_spawnattr_t *_attr, 289 const sigset_t *sigdefault) 290 { 291 struct _posix_spawnattr *attr; 292 293 if (_attr == NULL || (attr = *_attr) == NULL || sigdefault == NULL) 294 return EINVAL; 295 296 memcpy(&attr->sigdefault, sigdefault, sizeof(sigset_t)); 297 298 return 0; 299 } 300 301 302 int 303 posix_spawnattr_getsigmask(const posix_spawnattr_t *_attr, sigset_t *sigmask) 304 { 305 struct _posix_spawnattr *attr; 306 307 if (_attr == NULL || (attr = *_attr) == NULL || sigmask == NULL) 308 return EINVAL; 309 310 memcpy(sigmask, &attr->sigmask, sizeof(sigset_t)); 311 312 return 0; 313 } 314 315 316 int 317 posix_spawnattr_setsigmask(posix_spawnattr_t *_attr, const sigset_t *sigmask) 318 { 319 struct _posix_spawnattr *attr; 320 321 if (_attr == NULL || (attr = *_attr) == NULL || sigmask == NULL) 322 return EINVAL; 323 324 memcpy(&attr->sigmask, sigmask, sizeof(sigset_t)); 325 326 return 0; 327 } 328 329 330 static int 331 process_spawnattr(const posix_spawnattr_t *_attr) 332 { 333 if (_attr == NULL) 334 return 0; 335 336 struct _posix_spawnattr *attr = *_attr; 337 if (attr == NULL) 338 return EINVAL; 339 340 if ((attr->flags & POSIX_SPAWN_SETSIGMASK) != 0) 341 sigprocmask(SIG_SETMASK, &attr->sigmask, NULL); 342 343 if ((attr->flags & POSIX_SPAWN_SETSIGDEF) != 0) { 344 struct sigaction action; 345 action.sa_handler = SIG_DFL; 346 action.sa_flags = 0; 347 action.sa_userdata = NULL; 348 sigemptyset(&action.sa_mask); 349 for (int i = 1; i <= MAX_SIGNAL_NUMBER; i++) { 350 if (sigismember(&attr->sigdefault, i) == 1 351 && sigaction(i, &action, NULL) != 0) { 352 return errno; 353 } 354 } 355 } 356 357 if ((attr->flags & POSIX_SPAWN_RESETIDS) != 0) { 358 if (setegid(getgid()) != 0) 359 return errno; 360 if (seteuid(getuid()) != 0) 361 return errno; 362 } 363 364 if ((attr->flags & POSIX_SPAWN_SETPGROUP) != 0) { 365 if (setpgid(0, attr->pgroup) != 0) 366 return errno; 367 } 368 369 return 0; 370 } 371 372 373 static int 374 process_file_actions(const posix_spawn_file_actions_t *_actions, int *errfd) 375 { 376 if (_actions == NULL) 377 return 0; 378 379 struct _posix_spawn_file_actions* actions = *_actions; 380 if (actions == NULL) 381 return EINVAL; 382 383 for (int i = 0; i < actions->count; i++) { 384 struct _file_action *action = &actions->actions[i]; 385 386 if (action->fd == *errfd) { 387 int newfd = dup(action->fd); 388 if (newfd == -1) 389 return errno; 390 close(action->fd); 391 fcntl(newfd, F_SETFD, FD_CLOEXEC); 392 *errfd = newfd; 393 } 394 395 if (action->type == file_action_close) { 396 if (close(action->fd) != 0) 397 return errno; 398 } else if (action->type == file_action_open) { 399 int fd = open(action->action.open_action.path, 400 action->action.open_action.oflag, 401 action->action.open_action.mode); 402 if (fd == -1) 403 return errno; 404 if (fd != action->fd) { 405 if (dup2(action->fd, fd) != 0) 406 return errno; 407 if (close(fd) != 0) 408 return errno; 409 } 410 } else if (action->type == file_action_dup2) { 411 if (dup2(action->action.dup2_action.srcfd, action->fd) != 0) 412 return errno; 413 } 414 } 415 416 return 0; 417 } 418 419 420 static int 421 do_posix_spawn(pid_t *_pid, const char *path, 422 const posix_spawn_file_actions_t *actions, 423 const posix_spawnattr_t *attrp, char *const argv[], char *const envp[], 424 bool envpath) 425 { 426 int err = 0; 427 int fds[2]; 428 pid_t pid; 429 430 if (pipe(fds) != 0) 431 return errno; 432 if (fcntl(fds[0], F_SETFD, FD_CLOEXEC) != 0 433 || fcntl(fds[1], F_SETFD, FD_CLOEXEC) != 0) { 434 goto fail; 435 } 436 pid = vfork(); 437 if (pid == -1) 438 goto fail; 439 440 if (pid != 0) { 441 // parent 442 if (_pid != NULL) 443 *_pid = pid; 444 close(fds[1]); 445 read(fds[0], &err, sizeof(err)); 446 if (err != 0) 447 waitpid(pid, NULL, WNOHANG); 448 close(fds[0]); 449 return err; 450 } 451 452 // child 453 close(fds[0]); 454 455 err = process_spawnattr(attrp); 456 if (err == 0) 457 err = process_file_actions(actions, &fds[1]); 458 if (err != 0) 459 goto fail_child; 460 461 if (envpath) 462 execvpe(path, argv, envp != NULL ? envp : environ); 463 else 464 execve(path, argv, envp != NULL ? envp : environ); 465 466 err = errno; 467 468 fail_child: 469 write(fds[1], &err, sizeof(err)); 470 close(fds[1]); 471 _exit(127); 472 473 fail: 474 err = errno; 475 close(fds[0]); 476 close(fds[1]); 477 return err; 478 } 479 480 481 int 482 posix_spawn(pid_t *pid, const char *path, 483 const posix_spawn_file_actions_t *file_actions, 484 const posix_spawnattr_t *attrp, char *const argv[], char *const envp[]) 485 { 486 return do_posix_spawn(pid, path, file_actions, attrp, argv, envp, false); 487 } 488 489 490 int 491 posix_spawnp(pid_t *pid, const char *file, 492 const posix_spawn_file_actions_t *file_actions, 493 const posix_spawnattr_t *attrp, char *const argv[], 494 char *const envp[]) 495 { 496 return do_posix_spawn(pid, file, file_actions, attrp, argv, envp, true); 497 } 498 499