mirror of
https://github.com/Retropex/bitcoin.git
synced 2025-05-28 13:02:38 +02:00
cpp-subprocess: Iterate through /proc/self/fd for close_fds option
This commit is contained in:
parent
790e2f948a
commit
0cb1dc0be9
@ -36,6 +36,8 @@ Documentation for C++ subprocessing library.
|
||||
#ifndef BITCOIN_UTIL_SUBPROCESS_H
|
||||
#define BITCOIN_UTIL_SUBPROCESS_H
|
||||
|
||||
#include <util/fs.h>
|
||||
#include <util/strencodings.h>
|
||||
#include <util/syserror.h>
|
||||
|
||||
#include <algorithm>
|
||||
@ -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<int> 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!
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user