annotate dep/animia/src/fd/proc.cc @ 215:031a257ee019

dep/animia: fd/proc: use std::filesystem, etc. changes i.e. don't use std::stoi so we don't get exceptions...
author Paper <mrpapersonic@gmail.com>
date Sun, 07 Jan 2024 11:44:40 -0500
parents bc1ae1810855
children 0a5b6a088886
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
166
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents: 164
diff changeset
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 <filesystem>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6 #include <fstream>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7 #include <sstream>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 #include <string>
140
1e696863b54c dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents: 139
diff changeset
9
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
10 #include <sys/stat.h>
140
1e696863b54c dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents: 139
diff changeset
11 #include <fcntl.h>
1e696863b54c dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents: 139
diff changeset
12 #include <unistd.h>
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13
181
d26cd2c00270 dep/animia/fd/proc: use constexpr std::string_view for proc location
Paper <mrpapersonic@gmail.com>
parents: 180
diff changeset
14 static constexpr std::string_view PROC_LOCATION = "/proc";
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15
166
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents: 164
diff changeset
16 namespace animia::internal::proc {
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
18 template<typename T = int>
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
19 static T StringToInt(const std::string& str, T def = 0) {
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
20 std::istringstream s(str);
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
21 s >> def;
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
22 return def;
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
25 static bool IsRegularFile(std::string link) {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
26 struct stat sb;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27 if (stat(link.c_str(), &sb) == -1)
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
28 return false;
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
29
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30 return S_ISREG(sb.st_mode);
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
33 static bool AreFlagsOk(pid_t pid, int fd) {
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
34 const std::filesystem::path path = std::filesystem::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
35
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
36 std::ifstream file(path);
164
99fdf5a90b0f fd/linux: avoid reading buffers multiple times
Paper <mrpapersonic@gmail.com>
parents: 163
diff changeset
37 if (!file)
99fdf5a90b0f fd/linux: avoid reading buffers multiple times
Paper <mrpapersonic@gmail.com>
parents: 163
diff changeset
38 return false;
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40 int flags = 0;
164
99fdf5a90b0f fd/linux: avoid reading buffers multiple times
Paper <mrpapersonic@gmail.com>
parents: 163
diff changeset
41 for (std::string line; std::getline(file, line); )
99fdf5a90b0f fd/linux: avoid reading buffers multiple times
Paper <mrpapersonic@gmail.com>
parents: 163
diff changeset
42 if (line.find("flags:", 0) == 0)
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
43 flags = StringToInt(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
44
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45 if (flags & O_WRONLY || flags & O_RDWR)
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46 return false;
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
47
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48 return true;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50
166
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents: 164
diff changeset
51 static bool GetFilenameFromFd(std::string link, std::string& out) {
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52 /* 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
53 * 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
54 */
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
55 constexpr size_t OUT_MAX = (1ul << 15); // 32KiB
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
56 out.resize(32);
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
58 ssize_t exe_used = 0;
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
59 while (out.length() < OUT_MAX && exe_used >= static_cast<ssize_t>(out.length())) {
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
60 out.resize(out.length() * 2);
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
61
166
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents: 164
diff changeset
62 exe_used = readlink(link.c_str(), &out.front(), out.length());
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents: 164
diff changeset
63 if (exe_used == (ssize_t)-1 || exe_used < (ssize_t)1)
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
64 return false; // we got a bad result. SAD!
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
65 }
180
5be17d636aee deps/animia/fd/proc: patch for new filename getter
Paper <mrpapersonic@gmail.com>
parents: 166
diff changeset
66
182
c413e475f496 dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 181
diff changeset
67 out.resize(out.find('\0'));
c413e475f496 dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 181
diff changeset
68
180
5be17d636aee deps/animia/fd/proc: patch for new filename getter
Paper <mrpapersonic@gmail.com>
parents: 166
diff changeset
69 return true;
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
70 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
71
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
72 static bool GetProcessName(pid_t pid, std::string& result) {
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
73 const std::filesystem::path path = std::filesystem::path(PROC_LOCATION) / std::to_string(pid) / "comm";
154
d43d68408d3c dep/animia: fix XnuFdTools
Paper <mrpapersonic@gmail.com>
parents: 152
diff changeset
74
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
75 if (!util::ReadFile(path, result))
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
76 return false;
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
77
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
78 result.erase(std::remove(result.begin(), result.end(), '\n'), result.end());
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
79 return true;
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
80 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
81
198
bc1ae1810855 dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
82 bool 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
83 bool success = false;
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
84
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
85 for (const auto& dir : std::filesystem::directory_iterator{PROC_LOCATION}) {
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
86 Process proc;
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
87
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
88 try {
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
89 proc.pid = StringToInt(dir.path().stem());
144
e6668085e24d dep/animia: fix many bugs in the linux code
Paper <mrpapersonic@gmail.com>
parents: 143
diff changeset
90 success = true;
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
91 } catch (std::invalid_argument const& ex) {
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
92 continue;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
93 }
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
94
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
95 if (!GetProcessName(proc.pid, proc.name))
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
96 continue;
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
97
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
98 if (!process_proc(proc))
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
99 return false;
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
100 }
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
101
144
e6668085e24d dep/animia: fix many bugs in the linux code
Paper <mrpapersonic@gmail.com>
parents: 143
diff changeset
102 return success;
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
103 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
104
198
bc1ae1810855 dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
105 bool 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
106 if (!open_file_proc)
144
e6668085e24d dep/animia: fix many bugs in the linux code
Paper <mrpapersonic@gmail.com>
parents: 143
diff changeset
107 return false;
e6668085e24d dep/animia: fix many bugs in the linux code
Paper <mrpapersonic@gmail.com>
parents: 143
diff changeset
108
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
109 for (const auto& pid : pids) {
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
110 const std::filesystem::path path = std::filesystem::path(PROC_LOCATION) / std::to_string(pid) / "fd";
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
111
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
112 for (const auto& dir : std::filesystem::directory_iterator{path}) {
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
113 if (!AreFlagsOk(pid, StringToInt(dir.path().stem())))
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
114 continue;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
115
180
5be17d636aee deps/animia/fd/proc: patch for new filename getter
Paper <mrpapersonic@gmail.com>
parents: 166
diff changeset
116 std::string name;
215
031a257ee019 dep/animia: fd/proc: use std::filesystem, etc. changes
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
117 if (!GetFilenameFromFd(dir.path(), name))
180
5be17d636aee deps/animia/fd/proc: patch for new filename getter
Paper <mrpapersonic@gmail.com>
parents: 166
diff changeset
118 continue;
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
119
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
120 if (!IsRegularFile(name))
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
121 continue;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
122
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
123 if (!open_file_proc({pid, name}))
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
124 return false;
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
125 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
126 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
127 return true;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
128 }
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 } // namespace animia::internal::linux