diff --git a/lldb/source/Host/posix/ProcessLauncherPosixFork.cpp b/lldb/source/Host/posix/ProcessLauncherPosixFork.cpp index 635dbb14a0271692d21b7105152f23f3b965ab85..298e468c15500619820db5eb103e1116b937a416 100644 --- a/lldb/source/Host/posix/ProcessLauncherPosixFork.cpp +++ b/lldb/source/Host/posix/ProcessLauncherPosixFork.cpp @@ -172,15 +172,26 @@ struct ForkLaunchInfo { ec = llvm::sys::fs::is_directory(proc_fd_path, result); if (result) { std::vector files_to_close; + // get proc_fd_path for the process(/proc/pid/fd) + char path[PATH_MAX]; + lldb::pid_t pid = getpid(); + std::string dir_name = "/proc/" + std::to_string(pid) + "/fd"; // Directory iterator doesn't ensure any sequence. for (llvm::sys::fs::directory_iterator iter(proc_fd_path, ec), file_end; iter != file_end && !ec; iter.increment(ec)) { int fd = std::stoi(iter->path().substr(proc_fd_path.size() + 1)); - // Don't close first three entries since they are stdin, stdout and // stderr. - if (fd > 2 && !info.has_action(fd) && fd != error_fd) + if (fd > 2 && !info.has_action(fd) && fd != error_fd) { + ssize_t len = readlink(iter->path().c_str(), path, sizeof(path) - 1); + if (len != -1) { + path[len] = '\0'; + } + if (dir_name.compare(std::string(path)) == 0) + continue; + files_to_close.push_back(fd); + } } for (int file_to_close : files_to_close) close(file_to_close);