aboutsummaryrefslogtreecommitdiff
path: root/spawn.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2021-02-24 20:29:34 +0000
committerDaniel Eklöf <daniel@ekloef.se>2021-02-24 20:29:34 +0000
commit9cb8f6e3b7549b1f279f6de34a97c0e568f4d148 (patch)
treec6d3ea0dfb43a1c3d031abd685e320e3f4fd3da8 /spawn.c
parentchangelog: change "control- and escape sequences" to "escape sequences" (diff)
spawn: handle custom stdin/stdout/stderr fds being the same
Allow the caller to pass the same FD (for example, a single /dev/null FD) to spawn(). All we need to do to handle this correctly is ensure we don’t try to close the same FD multiple times.
Diffstat (limited to 'spawn.c')
-rw-r--r--spawn.c13
1 files changed, 10 insertions, 3 deletions
diff --git a/spawn.c b/spawn.c
index 3f8a8fd3..d200ed12 100644
--- a/spawn.c
+++ b/spawn.c
@@ -45,9 +45,16 @@ spawn(struct reaper *reaper, const char *cwd, char *const argv[],
_exit(errno_copy);
}
- 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)) ||
+ 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 && 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)
{