Mercurial > minori
comparison dep/animia/src/fd/linux.cc @ 152:8700806c2cc2
dep/animia: awesome new breaking changes!
I'm so tired
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Wed, 15 Nov 2023 02:34:59 -0500 |
parents | 54744a48a7d7 |
children | d43d68408d3c |
comparison
equal
deleted
inserted
replaced
151:54744a48a7d7 | 152:8700806c2cc2 |
---|---|
1 #include "animia/util.h" | 1 #include "animia/util.h" |
2 #include "animia/fd/linux.h" | 2 #include "animia/fd/linux.h" |
3 #include "animia.h" | |
3 | 4 |
4 #include <algorithm> | 5 #include <algorithm> |
5 #include <filesystem> | 6 #include <filesystem> |
6 #include <fstream> | 7 #include <fstream> |
7 #include <iostream> | |
8 #include <sstream> | 8 #include <sstream> |
9 #include <string> | 9 #include <string> |
10 #include <unordered_map> | 10 #include <unordered_map> |
11 #include <vector> | 11 #include <vector> |
12 #include <cstring> | 12 #include <cstring> |
97 } | 97 } |
98 | 98 |
99 return ret.c_str(); | 99 return ret.c_str(); |
100 } | 100 } |
101 | 101 |
102 bool LinuxFdTools::GetAllPids(std::set<pid_t>& pids) { | 102 static bool GetProcessName(pid_t pid, std::string& result) { |
103 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/comm"; | |
104 | |
105 if (!util::ReadFile(path, result)) | |
106 return false; | |
107 | |
108 result.erase(std::remove(result.begin(), result.end(), '\n'), result.end()); | |
109 return true; | |
110 } | |
111 | |
112 bool LinuxFdTools::EnumerateOpenProcesses(process_proc_t process_proc) { | |
103 bool success = false; | 113 bool success = false; |
104 for (const auto& dir : GetAllFilesInDir(PROC_LOCATION)) { | 114 for (const auto& dir : GetAllFilesInDir(PROC_LOCATION)) { |
105 pid_t pid; | 115 pid_t pid; |
106 try { | 116 try { |
107 pid = std::stoul(Basename(dir)); | 117 pid = std::stoul(Basename(dir)); |
108 success = true; | 118 success = true; |
109 } catch (std::invalid_argument) { | 119 } catch (std::invalid_argument) { |
110 continue; | 120 continue; |
111 } | 121 } |
112 pids.insert(pid); | 122 if (!process_proc({pid, GetProcessName(pid)})) |
123 return false; | |
113 } | 124 } |
114 return success; | 125 return success; |
115 } | 126 } |
116 | 127 |
117 bool LinuxFdTools::GetProcessName(pid_t pid, std::string& result) { | 128 bool LinuxFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) { |
118 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/comm"; | 129 if (!open_file_proc) |
119 | |
120 if (!util::ReadFile(path, result)) | |
121 return false; | 130 return false; |
122 | 131 |
123 result.erase(std::remove(result.begin(), result.end(), '\n'), result.end()); | |
124 return true; | |
125 } | |
126 | |
127 bool LinuxFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<OpenFile>& files) { | |
128 for (const auto& pid : pids) { | 132 for (const auto& pid : pids) { |
129 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fd"; | 133 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fd"; |
130 | 134 |
131 for (const auto& dir : GetAllFilesInDir(path)) { | 135 for (const auto& dir : GetAllFilesInDir(path)) { |
132 if (!AreFlagsOk(pid, std::stoi(Basename(dir)))) | 136 if (!AreFlagsOk(pid, std::stoi(Basename(dir)))) |
135 std::string name = GetFilenameFromFd(dir); | 139 std::string name = GetFilenameFromFd(dir); |
136 | 140 |
137 if (!IsRegularFile(name)) | 141 if (!IsRegularFile(name)) |
138 continue; | 142 continue; |
139 | 143 |
140 files.push_back({pid, name}); | 144 if (!open_file_proc({pid, name})) |
145 return false; | |
141 } | 146 } |
142 } | 147 } |
143 return true; | 148 return true; |
144 } | 149 } |
145 | 150 |