aboutsummaryrefslogtreecommitdiff
path: root/spawn.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2021-02-26 08:15:16 +0000
committerDaniel Eklöf <daniel@ekloef.se>2021-02-26 08:15:16 +0000
commitebf80702447d6482903387b2871b3536944fb604 (patch)
treeb7939683ad73881f99bb6be63d31882a503033fe /spawn.c
parentgrid: reflow_uri_ranges(): URI end point is *inclusive* (diff)
parentspawn: call setsid() in child process (diff)
Merge branch 'url-launcher-redirect-stdout-stderr'
Diffstat (limited to 'spawn.c')
-rw-r--r--spawn.c35
1 files changed, 23 insertions, 12 deletions
diff --git a/spawn.c b/spawn.c
index 3f8a8fd3..99f1dc7e 100644
--- a/spawn.c
+++ b/spawn.c
@@ -35,28 +35,39 @@ spawn(struct reaper *reaper, const char *cwd, char *const argv[],
/* Child */
close(pipe_fds[0]);
+ if (setsid() < 0)
+ goto child_err;
+
/* Clear signal mask */
sigset_t mask;
sigemptyset(&mask);
- if (sigprocmask(SIG_SETMASK, &mask, NULL) < 0) {
- const int errno_copy = errno;
- LOG_ERRNO("failed to restore signals");
- (void)!write(pipe_fds[1], &errno_copy, sizeof(errno_copy));
- _exit(errno_copy);
- }
+ if (sigprocmask(SIG_SETMASK, &mask, NULL) < 0)
+ goto child_err;
+
+ bool close_stderr = stderr_fd >= 0;
+ bool close_stdout = stdout_fd >= 0 && stdout_fd != stderr_fd;
+ bool close_stdin = stdin_fd >= 0 && stdin_fd != stdout_fd && stdin_fd != stderr_fd;
- if ((stdin_fd >= 0 && (dup2(stdin_fd, STDIN_FILENO) < 0 || close(stdin_fd) < 0)) ||
- (stdout_fd >= 0 && (dup2(stdout_fd, STDOUT_FILENO) < 0 || close(stdout_fd) < 0)) ||
- (stderr_fd >= 0 && (dup2(stderr_fd, STDERR_FILENO) < 0 || close(stderr_fd) < 0)) ||
+ if ((stdin_fd >= 0 && (dup2(stdin_fd, STDIN_FILENO) < 0
+ || (close_stdin && close(stdin_fd) < 0))) ||
+ (stdout_fd >= 0 && (dup2(stdout_fd, STDOUT_FILENO) < 0
+ || (close_stdout && close(stdout_fd) < 0))) ||
+ (stderr_fd >= 0 && (dup2(stderr_fd, STDERR_FILENO) < 0
+ || (close_stderr && close(stderr_fd) < 0))) ||
(cwd != NULL && chdir(cwd) < 0) ||
execvp(argv[0], argv) < 0)
{
- const int errno_copy = errno;
- (void)!write(pipe_fds[1], &errno_copy, sizeof(errno_copy));
- _exit(errno_copy);
+ goto child_err;
}
+
xassert(false);
_exit(errno);
+
+ child_err:
+ ;
+ const int errno_copy = errno;
+ (void)!write(pipe_fds[1], &errno_copy, sizeof(errno_copy));
+ _exit(errno_copy);
}
/* Parent */