Mercurial > minori
comparison dep/animia/src/fd/proc.cc @ 181:d26cd2c00270
dep/animia/fd/proc: use constexpr std::string_view for proc location
this is probably a little slower, but it works Well Enough for what we need it for
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Mon, 04 Dec 2023 12:14:30 -0500 |
parents | 5be17d636aee |
children | c413e475f496 |
comparison
equal
deleted
inserted
replaced
180:5be17d636aee | 181:d26cd2c00270 |
---|---|
14 #include <dirent.h> | 14 #include <dirent.h> |
15 #include <fcntl.h> | 15 #include <fcntl.h> |
16 #include <sys/stat.h> | 16 #include <sys/stat.h> |
17 #include <unistd.h> | 17 #include <unistd.h> |
18 | 18 |
19 #ifdef FREEBSD | 19 static constexpr std::string_view PROC_LOCATION = "/proc"; |
20 # include <sys/types.h> | |
21 # include <sys/user.h> | |
22 # include <libutil.h> | |
23 #endif | |
24 | |
25 #define PROC_LOCATION "/proc" | |
26 | 20 |
27 namespace animia::internal::proc { | 21 namespace animia::internal::proc { |
28 | 22 |
29 /* this uses dirent instead of std::filesystem; it would make a bit | 23 /* 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 :) */ | 24 more sense to use the latter, but this is platform dependent already :) */ |
55 return false; | 49 return false; |
56 return S_ISREG(sb.st_mode); | 50 return S_ISREG(sb.st_mode); |
57 } | 51 } |
58 | 52 |
59 static bool AreFlagsOk(pid_t pid, int fd) { | 53 static bool AreFlagsOk(pid_t pid, int fd) { |
60 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fdinfo/" + std::to_string(fd); | 54 const std::string path = std::string(PROC_LOCATION) + "/" + std::to_string(pid) + "/fdinfo/" + std::to_string(fd); |
61 | 55 |
62 std::ifstream file(path.c_str()); | 56 std::ifstream file(path.c_str()); |
63 if (!file) | 57 if (!file) |
64 return false; | 58 return false; |
65 | 59 |
92 } | 86 } |
93 | 87 |
94 static std::string GetProcessName(pid_t pid) { | 88 static std::string GetProcessName(pid_t pid) { |
95 std::string result; | 89 std::string result; |
96 | 90 |
97 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/comm"; | 91 const std::string path = std::string(PROC_LOCATION) + "/" + std::to_string(pid) + "/comm"; |
98 | 92 |
99 if (!util::ReadFile(path, result)) | 93 if (!util::ReadFile(path, result)) |
100 return ""; | 94 return ""; |
101 | 95 |
102 result.erase(std::remove(result.begin(), result.end(), '\n'), result.end()); | 96 result.erase(std::remove(result.begin(), result.end(), '\n'), result.end()); |
104 return result; | 98 return result; |
105 } | 99 } |
106 | 100 |
107 bool ProcFdTools::EnumerateOpenProcesses(process_proc_t process_proc) { | 101 bool ProcFdTools::EnumerateOpenProcesses(process_proc_t process_proc) { |
108 bool success = false; | 102 bool success = false; |
109 for (const auto& dir : GetAllFilesInDir(PROC_LOCATION)) { | 103 for (const auto& dir : GetAllFilesInDir(std::string(PROC_LOCATION))) { |
110 pid_t pid; | 104 pid_t pid; |
111 try { | 105 try { |
112 pid = std::stoul(Basename(dir)); | 106 pid = std::stoul(Basename(dir)); |
113 success = true; | 107 success = true; |
114 } catch (std::invalid_argument) { | 108 } catch (std::invalid_argument) { |
123 bool ProcFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) { | 117 bool ProcFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) { |
124 if (!open_file_proc) | 118 if (!open_file_proc) |
125 return false; | 119 return false; |
126 | 120 |
127 for (const auto& pid : pids) { | 121 for (const auto& pid : pids) { |
128 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fd"; | 122 const std::string path = std::string(PROC_LOCATION) + "/" + std::to_string(pid) + "/fd"; |
129 | 123 |
130 for (const auto& dir : GetAllFilesInDir(path)) { | 124 for (const auto& dir : GetAllFilesInDir(path)) { |
131 if (!AreFlagsOk(pid, std::stoi(Basename(dir)))) | 125 if (!AreFlagsOk(pid, std::stoi(Basename(dir)))) |
132 continue; | 126 continue; |
133 | 127 |