# HG changeset patch # User Paper # Date 1699832149 18000 # Node ID e6668085e24d75a3573ce636ae82353c0252f451 # Parent 4e750f6545cf208d67c2c8000fa9da2e55086241 dep/animia: fix many bugs in the linux code diff -r 4e750f6545cf -r e6668085e24d dep/animia/src/fd/bsd.cc --- a/dep/animia/src/fd/bsd.cc Sun Nov 12 18:28:04 2023 -0500 +++ b/dep/animia/src/fd/bsd.cc Sun Nov 12 18:35:49 2023 -0500 @@ -51,7 +51,7 @@ /* add pids to our vector */ pids.reserve(length / sizeof(*result)); for (int i = 0; i < length / sizeof(*result); i++) - pids.push_back(result[i].kp_proc.p_pid); + pids.insert(result[i].kp_proc.p_pid); } bool UnixFdTools::GetProcessName(pid_t pid, std::string& result) { diff -r 4e750f6545cf -r e6668085e24d dep/animia/src/fd/linux.cc --- a/dep/animia/src/fd/linux.cc Sun Nov 12 18:28:04 2023 -0500 +++ b/dep/animia/src/fd/linux.cc Sun Nov 12 18:35:49 2023 -0500 @@ -52,7 +52,13 @@ static bool AreFlagsOk(pid_t pid, int fd) { const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fdinfo/" + std::to_string(fd); - std::stringstream buffer(util::ReadFile(path)); + std::stringstream buffer; + { + std::string data; + if (!util::ReadFile(path, data)) + return false; + buffer << data; + } int flags = 0; for (std::string line; std::getline(buffer, line);) { @@ -94,22 +100,29 @@ } bool LinuxFdTools::GetAllPids(std::set& pids) { - for (const auto& dir : get_all_files_in_dir(PROC_LOCATION)) { + bool success = false; + for (const auto& dir : GetAllFilesInDir(PROC_LOCATION)) { pid_t pid; try { - pid = std::stoul(basename(dir)); + pid = std::stoul(Basename(dir)); + success = true; } catch (std::invalid_argument) { continue; } - pids.push_back(pid); + pids.insert(pid); } + return success; } bool LinuxFdTools::GetProcessName(pid_t pid, std::string& result) { const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/comm"; - std::string result = util::ReadFile(path); + std::string result; + if (!util::ReadFile(path, result)) + return false; + result.erase(std::remove(result.begin(), result.end(), '\n'), result.end()); + return true; } bool LinuxFdTools::EnumerateOpenFiles(const std::set& pids, std::vector>& files) { @@ -117,7 +130,7 @@ const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fd"; for (const auto& dir : GetAllFilesInDir(path)) { - if (!AreFlagsOk(pid, std::stoi(basename(dir)))) + if (!AreFlagsOk(pid, std::stoi(Basename(dir)))) continue; std::string name = GetFilenameFromFd(dir);