Spawn a new process executing PATH with the attributes describes in *ATTRP. Before running the process perform the actions described in FILE-ACTIONS. */
| 36 | /* Spawn a new process executing PATH with the attributes describes in *ATTRP. |
| 37 | Before running the process perform the actions described in FILE-ACTIONS. */ |
| 38 | int |
| 39 | dmtcp_spawn(pid_t *pid, |
| 40 | const char *file, |
| 41 | const posix_spawn_file_actions_t *file_actions, |
| 42 | const posix_spawnattr_t *attrp, |
| 43 | char *const argv[], |
| 44 | char *const envp[]) |
| 45 | { |
| 46 | /* Do this once. */ |
| 47 | short int flags = attrp == NULL ? 0 : attrp->__flags; |
| 48 | |
| 49 | // TODO(kapil): Add support for POSIX_SPAWN_USEVFORK. |
| 50 | pid_t new_pid = fork(); |
| 51 | |
| 52 | // Parent. |
| 53 | if (new_pid != 0) { |
| 54 | if (new_pid < 0) { |
| 55 | return errno; |
| 56 | } |
| 57 | |
| 58 | /* The call was successful. Store the PID if necessary. */ |
| 59 | if (pid != NULL) { |
| 60 | *pid = new_pid; |
| 61 | } |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | /* Set signal mask. */ |
| 67 | if ((flags & POSIX_SPAWN_SETSIGMASK) != 0 && |
| 68 | sigprocmask(SIG_SETMASK, &attrp->__ss, NULL) != 0) { |
| 69 | exit(SPAWN_ERROR); |
| 70 | } |
| 71 | |
| 72 | /* Set signal default action. */ |
| 73 | if ((flags & POSIX_SPAWN_SETSIGDEF) != 0) { |
| 74 | struct sigaction sa = {}; |
| 75 | sa.sa_handler = SIG_DFL; |
| 76 | |
| 77 | int ckptSig = dmtcp_get_ckpt_signal(); |
| 78 | for (int sig = 1; sig < _NSIG; ++sig) { |
| 79 | if (sig != ckptSig && sigismember(&attrp->__sd, sig) != 0 && |
| 80 | sigaction(sig, &sa, NULL) != 0) { |
| 81 | JTRACE("sigaction failed"); |
| 82 | exit(SPAWN_ERROR); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /* Set the process group ID. */ |
| 88 | if ((flags & POSIX_SPAWN_SETPGROUP) != 0 && setpgid(0, attrp->__pgrp) != 0) { |
| 89 | JTRACE("setpgid failed"); |
| 90 | exit(SPAWN_ERROR); |
| 91 | } |
| 92 | |
| 93 | /* Set the effective user and group IDs. */ |
| 94 | if ((flags & POSIX_SPAWN_RESETIDS) != 0 && |
| 95 | (seteuid(getuid()) != 0 || setegid(getgid()) != 0)) { |
no test coverage detected