diff --git a/src/util/subprocess.h b/src/util/subprocess.h index 545f5879b0..91a51466c5 100644 --- a/src/util/subprocess.h +++ b/src/util/subprocess.h @@ -36,6 +36,8 @@ Documentation for C++ subprocessing library. #ifndef BITCOIN_UTIL_SUBPROCESS_H #define BITCOIN_UTIL_SUBPROCESS_H +#include +#include #include #include @@ -1302,12 +1304,20 @@ namespace detail { // Close all the inherited fd's except the error write pipe if (parent_->close_fds_) { - 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); + try { + std::vector fds_to_close; + for (const auto& it : fs::directory_iterator("/proc/self/fd")) { + int64_t fd; + if (!ParseInt64(it.path().filename().native(), &fd)) continue; + if (fd <= 2) continue; // leave std{in,out,err} alone + if (fd == err_wr_pipe_) continue; + fds_to_close.push_back(fd); + } + for (const int fd : fds_to_close) { + close(fd); + } + } catch (...) { + // TODO: maybe log this - but we're in a child process, so maybe non-trivial! } }