From 8282369f45c92f01ed49107d159657a2f0b4fbec Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Mon, 1 Jul 2019 00:47:10 -0700 Subject: [PATCH] child_setup_process to stop passing the process child_setup_process only cares about whether we are in a forked child, not the entire process structure. Narrow the parameter. --- src/exec.cpp | 4 ++-- src/postfork.cpp | 5 ++--- src/postfork.h | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/exec.cpp b/src/exec.cpp index c6ac1b634..bd66ff308 100644 --- a/src/exec.cpp +++ b/src/exec.cpp @@ -300,7 +300,7 @@ void internal_exec(env_stack_t &vars, job_t *j, const io_chain_t &all_ios) { // commands in the pipeline will apply to exec. However, using exec in a pipeline doesn't // really make sense, so I'm not trying to fix it here. auto redirs = dup2_list_t::resolve_chain(all_ios); - if (redirs && !child_setup_process(nullptr, nullptr, *redirs)) { + if (redirs && !child_setup_process(nullptr, false, *redirs)) { // Decrement SHLVL as we're removing ourselves from the shell "stack". auto shlvl_var = vars.get(L"SHLVL", ENV_GLOBAL | ENV_EXPORT); wcstring shlvl_str = L"0"; @@ -445,7 +445,7 @@ static bool fork_child_for_process(const std::shared_ptr &job, process_t // stdout and stderr, and then exit. p->pid = getpid(); child_set_group(job.get(), p); - child_setup_process(job.get(), p, dup2s); + child_setup_process(job.get(), true, dup2s); child_action(); DIE("Child process returned control to fork_child lambda!"); } diff --git a/src/postfork.cpp b/src/postfork.cpp index 0008f9b98..566c1ed63 100644 --- a/src/postfork.cpp +++ b/src/postfork.cpp @@ -134,13 +134,12 @@ bool set_child_group(job_t *j, pid_t child_pid) { return true; } -int child_setup_process(const job_t *job, process_t *p, const dup2_list_t &dup2s) { +int child_setup_process(const job_t *job, bool is_forked, const dup2_list_t &dup2s) { // Note we are called in a forked child. for (const auto &act : dup2s.get_actions()) { int err = act.target < 0 ? close(act.src) : dup2(act.src, act.target); if (err < 0) { - // We have a null p if this is for the exec (non-fork) path. - if (p != nullptr) { + if (is_forked) { debug_safe(4, "redirect_in_child_after_fork failed in child_setup_process"); exit_without_destructors(1); } diff --git a/src/postfork.h b/src/postfork.h index f813ee268..997c5f07b 100644 --- a/src/postfork.h +++ b/src/postfork.h @@ -29,7 +29,7 @@ bool child_set_group(job_t *j, process_t *p); // called by child /// /// \return 0 on sucess, -1 on failiure. When this function returns, signals are always unblocked. /// On failiure, signal handlers, io redirections and process group of the process is undefined. -int child_setup_process(const job_t *job, process_t *p, const dup2_list_t &dup2s); +int child_setup_process(const job_t *job, bool is_forked, const dup2_list_t &dup2s); /// Call fork(), optionally waiting until we are no longer multithreaded. If the forked child /// doesn't do anything that could allocate memory, take a lock, etc. (like call exec), then it's