Mercurial > minori
annotate 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 |
rev | line source |
---|---|
166 | 1 #include "animia/fd/proc.h" |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
2 #include "animia.h" |
156
cdf79282d647
dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
154
diff
changeset
|
3 #include "animia/util.h" |
140
1e696863b54c
dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents:
139
diff
changeset
|
4 |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
5 #include <algorithm> |
156
cdf79282d647
dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
154
diff
changeset
|
6 #include <cstring> |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
7 #include <filesystem> |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
8 #include <fstream> |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
9 #include <sstream> |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
10 #include <string> |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
11 #include <unordered_map> |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
12 #include <vector> |
140
1e696863b54c
dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents:
139
diff
changeset
|
13 |
156
cdf79282d647
dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
154
diff
changeset
|
14 #include <dirent.h> |
140
1e696863b54c
dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents:
139
diff
changeset
|
15 #include <fcntl.h> |
1e696863b54c
dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents:
139
diff
changeset
|
16 #include <sys/stat.h> |
1e696863b54c
dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents:
139
diff
changeset
|
17 #include <unistd.h> |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
18 |
166 | 19 #ifdef FREEBSD |
20 # include <sys/types.h> | |
21 # include <sys/user.h> | |
22 # include <libutil.h> | |
23 #endif | |
24 | |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
25 #define PROC_LOCATION "/proc" |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
26 |
166 | 27 namespace animia::internal::proc { |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
28 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
29 /* this uses dirent instead of std::filesystem; it would make a bit |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
30 more sense to use the latter, but this is platform dependent already :) */ |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
156
diff
changeset
|
31 static std::vector<std::string> GetAllFilesInDir(const std::string& _dir) { |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
32 std::vector<std::string> ret; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
33 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
34 DIR* dir = opendir(_dir.c_str()); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
35 if (!dir) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
36 return ret; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
37 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
38 struct dirent* dp; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
39 while ((dp = readdir(dir)) != NULL) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
40 if (!(!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
41 ret.push_back(_dir + "/" + dp->d_name); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
42 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
43 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
44 closedir(dir); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
45 return ret; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
46 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
47 |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
156
diff
changeset
|
48 static std::string Basename(const std::string& path) { |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
49 return path.substr(path.find_last_of("/") + 1, path.length()); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
50 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
51 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
52 static bool IsRegularFile(std::string link) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
53 struct stat sb; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
54 if (stat(link.c_str(), &sb) == -1) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
55 return false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
56 return S_ISREG(sb.st_mode); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
57 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
58 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
59 static bool AreFlagsOk(pid_t pid, int fd) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
60 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fdinfo/" + std::to_string(fd); |
164
99fdf5a90b0f
fd/linux: avoid reading buffers multiple times
Paper <mrpapersonic@gmail.com>
parents:
163
diff
changeset
|
61 |
99fdf5a90b0f
fd/linux: avoid reading buffers multiple times
Paper <mrpapersonic@gmail.com>
parents:
163
diff
changeset
|
62 std::ifstream file(path.c_str()); |
99fdf5a90b0f
fd/linux: avoid reading buffers multiple times
Paper <mrpapersonic@gmail.com>
parents:
163
diff
changeset
|
63 if (!file) |
99fdf5a90b0f
fd/linux: avoid reading buffers multiple times
Paper <mrpapersonic@gmail.com>
parents:
163
diff
changeset
|
64 return false; |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
65 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
66 int flags = 0; |
164
99fdf5a90b0f
fd/linux: avoid reading buffers multiple times
Paper <mrpapersonic@gmail.com>
parents:
163
diff
changeset
|
67 for (std::string line; std::getline(file, line); ) |
99fdf5a90b0f
fd/linux: avoid reading buffers multiple times
Paper <mrpapersonic@gmail.com>
parents:
163
diff
changeset
|
68 if (line.find("flags:", 0) == 0) |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
69 flags = std::stoi(line.substr(line.find_last_not_of("0123456789") + 1)); |
164
99fdf5a90b0f
fd/linux: avoid reading buffers multiple times
Paper <mrpapersonic@gmail.com>
parents:
163
diff
changeset
|
70 |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
71 if (flags & O_WRONLY || flags & O_RDWR) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
72 return false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
73 return true; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
74 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
75 |
166 | 76 static bool GetFilenameFromFd(std::string link, std::string& out) { |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
77 /* gets around stupid linux limitation where /proc doesn't |
180
5be17d636aee
deps/animia/fd/proc: patch for new filename getter
Paper <mrpapersonic@gmail.com>
parents:
166
diff
changeset
|
78 * give actual size readings of the string |
5be17d636aee
deps/animia/fd/proc: patch for new filename getter
Paper <mrpapersonic@gmail.com>
parents:
166
diff
changeset
|
79 */ |
166 | 80 constexpr size_t OUT_MAX = (1 << 15); // 32KiB |
81 out.resize(1024); | |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
82 |
166 | 83 for (ssize_t exe_used = 0; |
84 out.length() < OUT_MAX && exe_used >= (ssize_t)(out.length() - 1); | |
85 out.resize(out.length() * 2)) { | |
86 exe_used = readlink(link.c_str(), &out.front(), out.length()); | |
87 if (exe_used == (ssize_t)-1 || exe_used < (ssize_t)1) | |
180
5be17d636aee
deps/animia/fd/proc: patch for new filename getter
Paper <mrpapersonic@gmail.com>
parents:
166
diff
changeset
|
88 return false; // we got a bad result, i think |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
89 } |
180
5be17d636aee
deps/animia/fd/proc: patch for new filename getter
Paper <mrpapersonic@gmail.com>
parents:
166
diff
changeset
|
90 |
5be17d636aee
deps/animia/fd/proc: patch for new filename getter
Paper <mrpapersonic@gmail.com>
parents:
166
diff
changeset
|
91 return true; |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
92 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
93 |
154 | 94 static std::string GetProcessName(pid_t pid) { |
166 | 95 std::string result; |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
96 |
166 | 97 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/comm"; |
154 | 98 |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
99 if (!util::ReadFile(path, result)) |
154 | 100 return ""; |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
101 |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
102 result.erase(std::remove(result.begin(), result.end(), '\n'), result.end()); |
154 | 103 |
104 return result; | |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
105 } |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
106 |
166 | 107 bool ProcFdTools::EnumerateOpenProcesses(process_proc_t process_proc) { |
144
e6668085e24d
dep/animia: fix many bugs in the linux code
Paper <mrpapersonic@gmail.com>
parents:
143
diff
changeset
|
108 bool success = false; |
e6668085e24d
dep/animia: fix many bugs in the linux code
Paper <mrpapersonic@gmail.com>
parents:
143
diff
changeset
|
109 for (const auto& dir : GetAllFilesInDir(PROC_LOCATION)) { |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
110 pid_t pid; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
111 try { |
144
e6668085e24d
dep/animia: fix many bugs in the linux code
Paper <mrpapersonic@gmail.com>
parents:
143
diff
changeset
|
112 pid = std::stoul(Basename(dir)); |
e6668085e24d
dep/animia: fix many bugs in the linux code
Paper <mrpapersonic@gmail.com>
parents:
143
diff
changeset
|
113 success = true; |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
114 } catch (std::invalid_argument) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
115 continue; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
116 } |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
117 if (!process_proc({pid, GetProcessName(pid)})) |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
118 return false; |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
119 } |
144
e6668085e24d
dep/animia: fix many bugs in the linux code
Paper <mrpapersonic@gmail.com>
parents:
143
diff
changeset
|
120 return success; |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
121 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
122 |
166 | 123 bool ProcFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) { |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
124 if (!open_file_proc) |
144
e6668085e24d
dep/animia: fix many bugs in the linux code
Paper <mrpapersonic@gmail.com>
parents:
143
diff
changeset
|
125 return false; |
e6668085e24d
dep/animia: fix many bugs in the linux code
Paper <mrpapersonic@gmail.com>
parents:
143
diff
changeset
|
126 |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
127 for (const auto& pid : pids) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
128 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fd"; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
129 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
130 for (const auto& dir : GetAllFilesInDir(path)) { |
144
e6668085e24d
dep/animia: fix many bugs in the linux code
Paper <mrpapersonic@gmail.com>
parents:
143
diff
changeset
|
131 if (!AreFlagsOk(pid, std::stoi(Basename(dir)))) |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
132 continue; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
133 |
180
5be17d636aee
deps/animia/fd/proc: patch for new filename getter
Paper <mrpapersonic@gmail.com>
parents:
166
diff
changeset
|
134 std::string name; |
5be17d636aee
deps/animia/fd/proc: patch for new filename getter
Paper <mrpapersonic@gmail.com>
parents:
166
diff
changeset
|
135 if (!GetFilenameFromFd(dir, name)) |
5be17d636aee
deps/animia/fd/proc: patch for new filename getter
Paper <mrpapersonic@gmail.com>
parents:
166
diff
changeset
|
136 continue; |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
137 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
138 if (!IsRegularFile(name)) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
139 continue; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
140 |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
141 if (!open_file_proc({pid, name})) |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
142 return false; |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
143 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
144 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
145 return true; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
146 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
147 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
148 } // namespace animia::internal::linux |