Mercurial > minori
comparison dep/animia/src/fd/proc.cc @ 166:54c5d80a737e
dep/animia: add libutil method
I changed the "linux" method to be "proc", because it isn't exactly Linux specific
this commit also has some changes to the x11 stuff:
instead of enumerating over only top-level windows, we iterate over ALL of them
this is because many X11 apps actually use multiple windows
for some reason, I still can't get it to work with VLC, but it picks up Firefox...
| author | paper@DavesDouble.local |
|---|---|
| date | Sun, 19 Nov 2023 04:21:56 -0500 |
| parents | dep/animia/src/fd/linux.cc@99fdf5a90b0f |
| children | 5be17d636aee |
comparison
equal
deleted
inserted
replaced
| 165:8937fb7f2d66 | 166:54c5d80a737e |
|---|---|
| 1 #include "animia/fd/proc.h" | |
| 2 #include "animia.h" | |
| 3 #include "animia/util.h" | |
| 4 | |
| 5 #include <algorithm> | |
| 6 #include <cstring> | |
| 7 #include <filesystem> | |
| 8 #include <fstream> | |
| 9 #include <sstream> | |
| 10 #include <string> | |
| 11 #include <unordered_map> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include <dirent.h> | |
| 15 #include <fcntl.h> | |
| 16 #include <sys/stat.h> | |
| 17 #include <unistd.h> | |
| 18 | |
| 19 #ifdef FREEBSD | |
| 20 # include <sys/types.h> | |
| 21 # include <sys/user.h> | |
| 22 # include <libutil.h> | |
| 23 #endif | |
| 24 | |
| 25 #define PROC_LOCATION "/proc" | |
| 26 | |
| 27 namespace animia::internal::proc { | |
| 28 | |
| 29 /* this uses dirent instead of std::filesystem; it would make a bit | |
| 30 more sense to use the latter, but this is platform dependent already :) */ | |
| 31 static std::vector<std::string> GetAllFilesInDir(const std::string& _dir) { | |
| 32 std::vector<std::string> ret; | |
| 33 | |
| 34 DIR* dir = opendir(_dir.c_str()); | |
| 35 if (!dir) | |
| 36 return ret; | |
| 37 | |
| 38 struct dirent* dp; | |
| 39 while ((dp = readdir(dir)) != NULL) { | |
| 40 if (!(!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))) | |
| 41 ret.push_back(_dir + "/" + dp->d_name); | |
| 42 } | |
| 43 | |
| 44 closedir(dir); | |
| 45 return ret; | |
| 46 } | |
| 47 | |
| 48 static std::string Basename(const std::string& path) { | |
| 49 return path.substr(path.find_last_of("/") + 1, path.length()); | |
| 50 } | |
| 51 | |
| 52 static bool IsRegularFile(std::string link) { | |
| 53 struct stat sb; | |
| 54 if (stat(link.c_str(), &sb) == -1) | |
| 55 return false; | |
| 56 return S_ISREG(sb.st_mode); | |
| 57 } | |
| 58 | |
| 59 static bool AreFlagsOk(pid_t pid, int fd) { | |
| 60 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fdinfo/" + std::to_string(fd); | |
| 61 | |
| 62 std::ifstream file(path.c_str()); | |
| 63 if (!file) | |
| 64 return false; | |
| 65 | |
| 66 int flags = 0; | |
| 67 for (std::string line; std::getline(file, line); ) | |
| 68 if (line.find("flags:", 0) == 0) | |
| 69 flags = std::stoi(line.substr(line.find_last_not_of("0123456789") + 1)); | |
| 70 | |
| 71 if (flags & O_WRONLY || flags & O_RDWR) | |
| 72 return false; | |
| 73 return true; | |
| 74 } | |
| 75 | |
| 76 static bool GetFilenameFromFd(std::string link, std::string& out) { | |
| 77 /* gets around stupid linux limitation where /proc doesn't | |
| 78 give actual filesize readings */ | |
| 79 constexpr size_t OUT_MAX = (1 << 15); // 32KiB | |
| 80 out.resize(1024); | |
| 81 | |
| 82 for (ssize_t exe_used = 0; | |
| 83 out.length() < OUT_MAX && exe_used >= (ssize_t)(out.length() - 1); | |
| 84 out.resize(out.length() * 2)) { | |
| 85 exe_used = readlink(link.c_str(), &out.front(), out.length()); | |
| 86 if (exe_used == (ssize_t)-1 || exe_used < (ssize_t)1) | |
| 87 return false; | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 static std::string GetProcessName(pid_t pid) { | |
| 92 std::string result; | |
| 93 | |
| 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"; | |
| 102 | |
| 103 if (!util::ReadFile(path, result)) | |
| 104 return ""; | |
| 105 | |
| 106 result.erase(std::remove(result.begin(), result.end(), '\n'), result.end()); | |
| 107 #endif | |
| 108 | |
| 109 return result; | |
| 110 } | |
| 111 | |
| 112 bool ProcFdTools::EnumerateOpenProcesses(process_proc_t process_proc) { | |
| 113 bool success = false; | |
| 114 for (const auto& dir : GetAllFilesInDir(PROC_LOCATION)) { | |
| 115 pid_t pid; | |
| 116 try { | |
| 117 pid = std::stoul(Basename(dir)); | |
| 118 success = true; | |
| 119 } catch (std::invalid_argument) { | |
| 120 continue; | |
| 121 } | |
| 122 if (!process_proc({pid, GetProcessName(pid)})) | |
| 123 return false; | |
| 124 } | |
| 125 return success; | |
| 126 } | |
| 127 | |
| 128 bool ProcFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) { | |
| 129 if (!open_file_proc) | |
| 130 return false; | |
| 131 | |
| 132 for (const auto& pid : pids) { | |
| 133 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fd"; | |
| 134 | |
| 135 for (const auto& dir : GetAllFilesInDir(path)) { | |
| 136 if (!AreFlagsOk(pid, std::stoi(Basename(dir)))) | |
| 137 continue; | |
| 138 | |
| 139 std::string name = GetFilenameFromFd(dir); | |
| 140 | |
| 141 if (!IsRegularFile(name)) | |
| 142 continue; | |
| 143 | |
| 144 if (!open_file_proc({pid, name})) | |
| 145 return false; | |
| 146 } | |
| 147 } | |
| 148 return true; | |
| 149 } | |
| 150 | |
| 151 } // namespace animia::internal::linux |
