Mercurial > minori
comparison dep/animia/src/fd/proc.cc @ 180:5be17d636aee
deps/animia/fd/proc: patch for new filename getter
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Mon, 04 Dec 2023 12:08:29 -0500 |
| parents | 54c5d80a737e |
| children | d26cd2c00270 |
comparison
equal
deleted
inserted
replaced
| 179:9c4645100fec | 180:5be17d636aee |
|---|---|
| 73 return true; | 73 return true; |
| 74 } | 74 } |
| 75 | 75 |
| 76 static bool GetFilenameFromFd(std::string link, std::string& out) { | 76 static bool GetFilenameFromFd(std::string link, std::string& out) { |
| 77 /* gets around stupid linux limitation where /proc doesn't | 77 /* gets around stupid linux limitation where /proc doesn't |
| 78 give actual filesize readings */ | 78 * give actual size readings of the string |
| 79 */ | |
| 79 constexpr size_t OUT_MAX = (1 << 15); // 32KiB | 80 constexpr size_t OUT_MAX = (1 << 15); // 32KiB |
| 80 out.resize(1024); | 81 out.resize(1024); |
| 81 | 82 |
| 82 for (ssize_t exe_used = 0; | 83 for (ssize_t exe_used = 0; |
| 83 out.length() < OUT_MAX && exe_used >= (ssize_t)(out.length() - 1); | 84 out.length() < OUT_MAX && exe_used >= (ssize_t)(out.length() - 1); |
| 84 out.resize(out.length() * 2)) { | 85 out.resize(out.length() * 2)) { |
| 85 exe_used = readlink(link.c_str(), &out.front(), out.length()); | 86 exe_used = readlink(link.c_str(), &out.front(), out.length()); |
| 86 if (exe_used == (ssize_t)-1 || exe_used < (ssize_t)1) | 87 if (exe_used == (ssize_t)-1 || exe_used < (ssize_t)1) |
| 87 return false; | 88 return false; // we got a bad result, i think |
| 88 } | 89 } |
| 90 | |
| 91 return true; | |
| 89 } | 92 } |
| 90 | 93 |
| 91 static std::string GetProcessName(pid_t pid) { | 94 static std::string GetProcessName(pid_t pid) { |
| 92 std::string result; | 95 std::string result; |
| 93 | 96 |
| 94 #ifdef FREEBSD | |
| 95 struct kinfo_proc* proc = kinfo_getproc(pid); | |
| 96 if (!proc) | |
| 97 return ""; | |
| 98 result = proc->ki_comm; | |
| 99 free(proc); | |
| 100 #elif defined(LINUX) | |
| 101 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/comm"; | 97 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/comm"; |
| 102 | 98 |
| 103 if (!util::ReadFile(path, result)) | 99 if (!util::ReadFile(path, result)) |
| 104 return ""; | 100 return ""; |
| 105 | 101 |
| 106 result.erase(std::remove(result.begin(), result.end(), '\n'), result.end()); | 102 result.erase(std::remove(result.begin(), result.end(), '\n'), result.end()); |
| 107 #endif | |
| 108 | 103 |
| 109 return result; | 104 return result; |
| 110 } | 105 } |
| 111 | 106 |
| 112 bool ProcFdTools::EnumerateOpenProcesses(process_proc_t process_proc) { | 107 bool ProcFdTools::EnumerateOpenProcesses(process_proc_t process_proc) { |
| 134 | 129 |
| 135 for (const auto& dir : GetAllFilesInDir(path)) { | 130 for (const auto& dir : GetAllFilesInDir(path)) { |
| 136 if (!AreFlagsOk(pid, std::stoi(Basename(dir)))) | 131 if (!AreFlagsOk(pid, std::stoi(Basename(dir)))) |
| 137 continue; | 132 continue; |
| 138 | 133 |
| 139 std::string name = GetFilenameFromFd(dir); | 134 std::string name; |
| 135 if (!GetFilenameFromFd(dir, name)) | |
| 136 continue; | |
| 140 | 137 |
| 141 if (!IsRegularFile(name)) | 138 if (!IsRegularFile(name)) |
| 142 continue; | 139 continue; |
| 143 | 140 |
| 144 if (!open_file_proc({pid, name})) | 141 if (!open_file_proc({pid, name})) |
