mirror of
https://github.com/Retropex/bitcoin.git
synced 2025-06-02 07:22:33 +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
|
#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>
|
||||||
@ -1302,12 +1304,20 @@ namespace detail {
|
|||||||
|
|
||||||
// Close all the inherited fd's except the error write pipe
|
// Close all the inherited fd's except the error write pipe
|
||||||
if (parent_->close_fds_) {
|
if (parent_->close_fds_) {
|
||||||
int max_fd = sysconf(_SC_OPEN_MAX);
|
try {
|
||||||
if (max_fd == -1) throw OSError("sysconf failed", errno);
|
std::vector<int> fds_to_close;
|
||||||
|
for (const auto& it : fs::directory_iterator("/proc/self/fd")) {
|
||||||
for (int i = 3; i < max_fd; i++) {
|
int64_t fd;
|
||||||
if (i == err_wr_pipe_) continue;
|
if (!ParseInt64(it.path().filename().native(), &fd)) continue;
|
||||||
close(i);
|
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