rdhindsa updated this revision to Diff 358429.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D105732/new/
https://reviews.llvm.org/D105732
Files:
lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
Index: lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
===================================================================
--- lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
+++ lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
@@ -16,6 +16,7 @@
#include "llvm/Support/Errno.h"
#include <climits>
+#include <filesystem>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <unistd.h>
@@ -143,10 +144,30 @@
// Close everything besides stdin, stdout, and stderr that has no file
// action to avoid leaking. Only do this when debugging, as elsewhere we
// actually rely on passing open descriptors to child processes.
- for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
- if (!info.GetFileActionForFD(fd) && fd != error_fd)
- close(fd);
+ std::string proc_fd_path = "/proc/self/fd";
+ std::filesystem::path fp(proc_fd_path);
+ if (std::filesystem::is_directory(fp)) {
+ std::vector<int> files_to_close;
+ // Directory iterator doesn't ensure any sequence.
+ for (auto &entry : std::filesystem::directory_iterator(proc_fd_path)) {
+ int fd =
+ std::stoi(entry.path().string().substr(proc_fd_path.size() + 1));
+
+ // Don't close first three entries since they are
+ // stdin/stdout/stderr
+ if ((fd > 2) && !info.GetFileActionForFD(fd) && fd != error_fd)
+ files_to_close.push_back(fd);
+ }
+ for (auto &file_to_close : files_to_close)
+ close(file_to_close);
+ } else {
+ // /proc/self/fd didn't work - trying the slow way instead
+ for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
+ if (!info.GetFileActionForFD(fd) && fd != error_fd) {
+ close(fd);
+ }
+ }
// Start tracing this child that is about to exec.
if (ptrace(PT_TRACE_ME, 0, nullptr, 0) == -1)
ExitWithError(error_fd, "ptrace");
Index: lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
===================================================================
--- lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
+++ lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
@@ -16,6 +16,7 @@
#include "llvm/Support/Errno.h"
#include <climits>
+#include <filesystem>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <unistd.h>
@@ -143,10 +144,30 @@
// Close everything besides stdin, stdout, and stderr that has no file
// action to avoid leaking. Only do this when debugging, as elsewhere we
// actually rely on passing open descriptors to child processes.
- for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
- if (!info.GetFileActionForFD(fd) && fd != error_fd)
- close(fd);
+ std::string proc_fd_path = "/proc/self/fd";
+ std::filesystem::path fp(proc_fd_path);
+ if (std::filesystem::is_directory(fp)) {
+ std::vector<int> files_to_close;
+ // Directory iterator doesn't ensure any sequence.
+ for (auto &entry : std::filesystem::directory_iterator(proc_fd_path)) {
+ int fd =
+ std::stoi(entry.path().string().substr(proc_fd_path.size() + 1));
+
+ // Don't close first three entries since they are
+ // stdin/stdout/stderr
+ if ((fd > 2) && !info.GetFileActionForFD(fd) && fd != error_fd)
+ files_to_close.push_back(fd);
+ }
+ for (auto &file_to_close : files_to_close)
+ close(file_to_close);
+ } else {
+ // /proc/self/fd didn't work - trying the slow way instead
+ for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
+ if (!info.GetFileActionForFD(fd) && fd != error_fd) {
+ close(fd);
+ }
+ }
// Start tracing this child that is about to exec.
if (ptrace(PT_TRACE_ME, 0, nullptr, 0) == -1)
ExitWithError(error_fd, "ptrace");
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits