comparison dep/animia/src/fd/linux.cc @ 138:28842a8d0c6b

dep/animia: huge refactor (again...) but this time, it actually compiles! and it WORKS! (on win32... not sure about other platforms...) configuring players is still not supported: at some point I'll prune something up...
author Paper <mrpapersonic@gmail.com>
date Sun, 12 Nov 2023 04:53:19 -0500
parents
children 478f3b366199
comparison
equal deleted inserted replaced
137:69db40272acd 138:28842a8d0c6b
1 #include <algorithm>
2 #include <fcntl.h>
3 #include <filesystem>
4 #include <fstream>
5 #include <iostream>
6 #include <sstream>
7 #include <string>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #include <unordered_map>
11 #include <vector>
12 #include <cstring>
13 #include <dirent.h>
14
15 #include "animia/util.h"
16
17 #define PROC_LOCATION "/proc"
18
19 namespace animia::internal::linux {
20
21 /* this uses dirent instead of std::filesystem; it would make a bit
22 more sense to use the latter, but this is platform dependent already :) */
23 std::vector<std::string> GetAllFilesInDir(const std::string& _dir) {
24 std::vector<std::string> ret;
25
26 DIR* dir = opendir(_dir.c_str());
27 if (!dir)
28 return ret;
29
30 struct dirent* dp;
31 while ((dp = readdir(dir)) != NULL) {
32 if (!(!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")))
33 ret.push_back(_dir + "/" + dp->d_name);
34 }
35
36 closedir(dir);
37 return ret;
38 }
39
40 std::string Basename(const std::string& path) {
41 return path.substr(path.find_last_of("/") + 1, path.length());
42 }
43
44 static bool IsRegularFile(std::string link) {
45 struct stat sb;
46 if (stat(link.c_str(), &sb) == -1)
47 return false;
48 return S_ISREG(sb.st_mode);
49 }
50
51 static bool AreFlagsOk(pid_t pid, int fd) {
52 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fdinfo/" + std::to_string(fd);
53 std::stringstream buffer(util::ReadFile(path));
54
55 int flags = 0;
56 for (std::string line; std::getline(buffer, line);) {
57 /* FIXME: exception handling here!! */
58 if (line.rfind("flags:", 0) == 0) {
59 flags = std::stoi(line.substr(line.find_last_not_of("0123456789") + 1));
60 }
61 }
62 if (flags & O_WRONLY || flags & O_RDWR)
63 return false;
64 return true;
65 }
66
67 static std::string GetFilenameFromFd(std::string link) {
68 /* gets around stupid linux limitation where /proc doesn't
69 give actual filesize readings */
70 size_t exe_size = 1024;
71 ssize_t exe_used;
72 std::string ret;
73
74 while (1) {
75 ret = std::string(exe_size, '\0');
76 exe_used = readlink(link.c_str(), &ret.front(), ret.length());
77 if (exe_used == (ssize_t)-1)
78 return NULL;
79
80 if (exe_used < (ssize_t)1) {
81 errno = ENOENT;
82 return NULL;
83 }
84
85 if (exe_used < (ssize_t)(exe_size - 1))
86 break;
87
88 exe_size += 1024;
89 }
90
91 return ret.c_str();
92 }
93
94 bool GetAllPids(std::set<pid_t>& pids) {
95 for (const auto& dir : get_all_files_in_dir(PROC_LOCATION)) {
96 pid_t pid;
97 try {
98 pid = std::stoul(basename(dir));
99 } catch (std::invalid_argument) {
100 continue;
101 }
102 pids.push_back(pid);
103 }
104 }
105
106 bool GetProcessName(pid_t pid, std::string& result) {
107 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/comm";
108
109 std::string result = util::ReadFile(path);
110 result.erase(std::remove(result.begin(), result.end(), '\n'), result.end());
111 }
112
113 bool EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::tuple<pid_t, std::string>>& files) {
114 for (const auto& pid : pids) {
115 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fd";
116
117 for (const auto& dir : GetAllFilesInDir(path)) {
118 if (!AreFlagsOk(pid, std::stoi(basename(dir))))
119 continue;
120
121 std::string name = GetFilenameFromFd(dir);
122
123 if (!IsRegularFile(name))
124 continue;
125
126 files.push_back({pid, name});
127 }
128 }
129 return true;
130 }
131
132 } // namespace animia::internal::linux