From 4c0b6a6add0c361c962b0b8f96b29e1b5d7a107c Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Tue, 29 Jan 2019 00:34:38 -0800 Subject: [PATCH] Use dup2_list_t in posix_spawn This simplifies the posix_spawn path and unifies it with the fork execution path. --- src/exec.cpp | 3 +-- src/postfork.cpp | 58 ++++++++---------------------------------------- src/postfork.h | 5 ++--- 3 files changed, 12 insertions(+), 54 deletions(-) diff --git a/src/exec.cpp b/src/exec.cpp index 3d51914b2..bf978345f 100644 --- a/src/exec.cpp +++ b/src/exec.cpp @@ -691,8 +691,7 @@ static bool exec_external_command(env_stack_t &vars, const std::shared_ptr io = io_chain.at(idx); - - if (io->io_mode == io_mode_t::fd) { - const io_fd_t *io_fd = static_cast(io.get()); - if (io->fd == io_fd->old_fd) continue; - } - - switch (io->io_mode) { - case io_mode_t::close: { - if (!err) err = posix_spawn_file_actions_addclose(actions, io->fd); - break; - } - - case io_mode_t::file: { - const io_file_t *io_file = static_cast(io.get()); - if (!err) - err = posix_spawn_file_actions_addopen(actions, io->fd, io_file->filename_cstr, - io_file->flags /* mode */, OPEN_MASK); - break; - } - - case io_mode_t::fd: { - const io_fd_t *io_fd = static_cast(io.get()); - if (!err) - err = posix_spawn_file_actions_adddup2(actions, io_fd->old_fd /* from */, - io->fd /* to */); - break; - } - - case io_mode_t::buffer: - case io_mode_t::pipe: { - const io_pipe_t *io_pipe = static_cast(io.get()); - unsigned int write_pipe_idx = (io_pipe->is_input ? 0 : 1); - int from_fd = io_pipe->pipe_fd[write_pipe_idx]; - int to_fd = io->fd; - if (!err) err = posix_spawn_file_actions_adddup2(actions, from_fd, to_fd); - - if (write_pipe_idx > 0) { - if (!err) err = posix_spawn_file_actions_addclose(actions, io_pipe->pipe_fd[0]); - if (!err) err = posix_spawn_file_actions_addclose(actions, io_pipe->pipe_fd[1]); - } else { - if (!err) err = posix_spawn_file_actions_addclose(actions, io_pipe->pipe_fd[0]); - } - break; - } + // Apply our dup2s. + for (const auto &act : dup2s.get_actions()) { + if (err) break; + if (act.target < 0) { + err = posix_spawn_file_actions_addclose(actions, act.src); + } else { + err = posix_spawn_file_actions_adddup2(actions, act.src, act.target); } } diff --git a/src/postfork.h b/src/postfork.h index 8ac8e82a3..d91d335cf 100644 --- a/src/postfork.h +++ b/src/postfork.h @@ -15,7 +15,6 @@ #endif class dup2_list_t; -class io_chain_t; class job_t; class process_t; @@ -56,8 +55,8 @@ void run_as_keepalive(pid_t parent_pid); /// Initializes and fills in a posix_spawnattr_t; on success, the caller should destroy it via /// posix_spawnattr_destroy. bool fork_actions_make_spawn_properties(posix_spawnattr_t *attr, - posix_spawn_file_actions_t *actions, job_t *j, process_t *p, - const io_chain_t &io_chain); + posix_spawn_file_actions_t *actions, const job_t *j, + const dup2_list_t &dup2s); #endif #endif