mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-08-28 05:56:33 +02:00
Revert "Merge bitcoin/bitcoin#32343: common: Close non-std fds before exec in RunCommandJSON"
This reverts commit31d3eebfb9
, reversing changes made to4b26ca0e2f
.
This commit is contained in:
@@ -24,7 +24,7 @@ UniValue RunCommandParseJSON(const std::string& str_command, const std::string&
|
|||||||
|
|
||||||
if (str_command.empty()) return UniValue::VNULL;
|
if (str_command.empty()) return UniValue::VNULL;
|
||||||
|
|
||||||
auto c = sp::Popen(str_command, sp::input{sp::PIPE}, sp::output{sp::PIPE}, sp::error{sp::PIPE}, sp::close_fds{true});
|
auto c = sp::Popen(str_command, sp::input{sp::PIPE}, sp::output{sp::PIPE}, sp::error{sp::PIPE});
|
||||||
if (!str_std_in.empty()) {
|
if (!str_std_in.empty()) {
|
||||||
c.send(str_std_in);
|
c.send(str_std_in);
|
||||||
}
|
}
|
||||||
|
@@ -36,8 +36,6 @@ Documentation for C++ subprocessing library.
|
|||||||
#ifndef BITCOIN_UTIL_SUBPROCESS_H
|
#ifndef BITCOIN_UTIL_SUBPROCESS_H
|
||||||
#define BITCOIN_UTIL_SUBPROCESS_H
|
#define BITCOIN_UTIL_SUBPROCESS_H
|
||||||
|
|
||||||
#include <util/fs.h>
|
|
||||||
#include <util/strencodings.h>
|
|
||||||
#include <util/syserror.h>
|
#include <util/syserror.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@@ -538,20 +536,6 @@ namespace util
|
|||||||
* -------------------------------
|
* -------------------------------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*!
|
|
||||||
* Option to close all file descriptors
|
|
||||||
* when the child process is spawned.
|
|
||||||
* The close fd list does not include
|
|
||||||
* input/output/error if they are explicitly
|
|
||||||
* set as part of the Popen arguments.
|
|
||||||
*
|
|
||||||
* Default value is false.
|
|
||||||
*/
|
|
||||||
struct close_fds {
|
|
||||||
explicit close_fds(bool c): close_all(c) {}
|
|
||||||
bool close_all = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Base class for all arguments involving string value.
|
* Base class for all arguments involving string value.
|
||||||
*/
|
*/
|
||||||
@@ -749,7 +733,6 @@ struct ArgumentDeducer
|
|||||||
void set_option(input&& inp);
|
void set_option(input&& inp);
|
||||||
void set_option(output&& out);
|
void set_option(output&& out);
|
||||||
void set_option(error&& err);
|
void set_option(error&& err);
|
||||||
void set_option(close_fds&& cfds);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Popen* popen_ = nullptr;
|
Popen* popen_ = nullptr;
|
||||||
@@ -1043,8 +1026,6 @@ private:
|
|||||||
std::future<void> cleanup_future_;
|
std::future<void> cleanup_future_;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool close_fds_ = false;
|
|
||||||
|
|
||||||
std::string exe_name_;
|
std::string exe_name_;
|
||||||
|
|
||||||
// Command in string format
|
// Command in string format
|
||||||
@@ -1288,10 +1269,6 @@ namespace detail {
|
|||||||
if (err.rd_ch_ != -1) popen_->stream_.err_read_ = err.rd_ch_;
|
if (err.rd_ch_ != -1) popen_->stream_.err_read_ = err.rd_ch_;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void ArgumentDeducer::set_option(close_fds&& cfds) {
|
|
||||||
popen_->close_fds_ = cfds.close_all;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
inline void Child::execute_child() {
|
inline void Child::execute_child() {
|
||||||
#ifndef __USING_WINDOWS__
|
#ifndef __USING_WINDOWS__
|
||||||
@@ -1338,41 +1315,6 @@ namespace detail {
|
|||||||
if (stream.err_write_ != -1 && stream.err_write_ > 2)
|
if (stream.err_write_ != -1 && stream.err_write_ > 2)
|
||||||
subprocess_close(stream.err_write_);
|
subprocess_close(stream.err_write_);
|
||||||
|
|
||||||
// Close all the inherited fd's except the error write pipe
|
|
||||||
if (parent_->close_fds_) {
|
|
||||||
// If possible, try to get the list of open file descriptors from the
|
|
||||||
// operating system. This is more efficient, but not guaranteed to be
|
|
||||||
// available.
|
|
||||||
#ifdef __linux__
|
|
||||||
// For Linux, enumerate /proc/<pid>/fd.
|
|
||||||
try {
|
|
||||||
std::vector<int> fds_to_close;
|
|
||||||
for (const auto& it : fs::directory_iterator(strprintf("/proc/%d/fd", getpid()))) {
|
|
||||||
auto fd{ToIntegral<uint64_t>(it.path().filename().native())};
|
|
||||||
if (!fd || *fd > std::numeric_limits<int>::max()) continue;
|
|
||||||
if (*fd <= 2) continue; // leave std{in,out,err} alone
|
|
||||||
if (*fd == static_cast<uint64_t>(err_wr_pipe_)) continue;
|
|
||||||
fds_to_close.push_back(*fd);
|
|
||||||
}
|
|
||||||
for (const int fd : fds_to_close) {
|
|
||||||
close(fd);
|
|
||||||
}
|
|
||||||
} catch (const fs::filesystem_error &e) {
|
|
||||||
throw OSError("/proc/<pid>/fd iteration failed", e.code().value());
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
// On other operating systems, iterate over all file descriptor slots
|
|
||||||
// and try to close them all.
|
|
||||||
int max_fd = sysconf(_SC_OPEN_MAX);
|
|
||||||
if (max_fd == -1) throw OSError("sysconf failed", errno);
|
|
||||||
|
|
||||||
for (int i = 3; i < max_fd; i++) {
|
|
||||||
if (i == err_wr_pipe_) continue;
|
|
||||||
close(i);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replace the current image with the executable
|
// Replace the current image with the executable
|
||||||
sys_ret = execvp(parent_->exe_name_.c_str(), parent_->cargv_.data());
|
sys_ret = execvp(parent_->exe_name_.c_str(), parent_->cargv_.data());
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user