Mercurial > minori
comparison dep/animia/src/fd/linux.cc @ 144:e6668085e24d
dep/animia: fix many bugs in the linux code
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Sun, 12 Nov 2023 18:35:49 -0500 |
parents | 4e750f6545cf |
children | 8e9b71970bda |
comparison
equal
deleted
inserted
replaced
143:4e750f6545cf | 144:e6668085e24d |
---|---|
50 return S_ISREG(sb.st_mode); | 50 return S_ISREG(sb.st_mode); |
51 } | 51 } |
52 | 52 |
53 static bool AreFlagsOk(pid_t pid, int fd) { | 53 static bool AreFlagsOk(pid_t pid, int fd) { |
54 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fdinfo/" + std::to_string(fd); | 54 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fdinfo/" + std::to_string(fd); |
55 std::stringstream buffer(util::ReadFile(path)); | 55 std::stringstream buffer; |
56 { | |
57 std::string data; | |
58 if (!util::ReadFile(path, data)) | |
59 return false; | |
60 buffer << data; | |
61 } | |
56 | 62 |
57 int flags = 0; | 63 int flags = 0; |
58 for (std::string line; std::getline(buffer, line);) { | 64 for (std::string line; std::getline(buffer, line);) { |
59 /* FIXME: exception handling here!! */ | 65 /* FIXME: exception handling here!! */ |
60 if (line.rfind("flags:", 0) == 0) { | 66 if (line.rfind("flags:", 0) == 0) { |
92 | 98 |
93 return ret.c_str(); | 99 return ret.c_str(); |
94 } | 100 } |
95 | 101 |
96 bool LinuxFdTools::GetAllPids(std::set<pid_t>& pids) { | 102 bool LinuxFdTools::GetAllPids(std::set<pid_t>& pids) { |
97 for (const auto& dir : get_all_files_in_dir(PROC_LOCATION)) { | 103 bool success = false; |
104 for (const auto& dir : GetAllFilesInDir(PROC_LOCATION)) { | |
98 pid_t pid; | 105 pid_t pid; |
99 try { | 106 try { |
100 pid = std::stoul(basename(dir)); | 107 pid = std::stoul(Basename(dir)); |
108 success = true; | |
101 } catch (std::invalid_argument) { | 109 } catch (std::invalid_argument) { |
102 continue; | 110 continue; |
103 } | 111 } |
104 pids.push_back(pid); | 112 pids.insert(pid); |
105 } | 113 } |
114 return success; | |
106 } | 115 } |
107 | 116 |
108 bool LinuxFdTools::GetProcessName(pid_t pid, std::string& result) { | 117 bool LinuxFdTools::GetProcessName(pid_t pid, std::string& result) { |
109 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/comm"; | 118 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/comm"; |
110 | 119 |
111 std::string result = util::ReadFile(path); | 120 std::string result; |
121 if (!util::ReadFile(path, result)) | |
122 return false; | |
123 | |
112 result.erase(std::remove(result.begin(), result.end(), '\n'), result.end()); | 124 result.erase(std::remove(result.begin(), result.end(), '\n'), result.end()); |
125 return true; | |
113 } | 126 } |
114 | 127 |
115 bool LinuxFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::tuple<pid_t, std::string>>& files) { | 128 bool LinuxFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::tuple<pid_t, std::string>>& files) { |
116 for (const auto& pid : pids) { | 129 for (const auto& pid : pids) { |
117 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fd"; | 130 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fd"; |
118 | 131 |
119 for (const auto& dir : GetAllFilesInDir(path)) { | 132 for (const auto& dir : GetAllFilesInDir(path)) { |
120 if (!AreFlagsOk(pid, std::stoi(basename(dir)))) | 133 if (!AreFlagsOk(pid, std::stoi(Basename(dir)))) |
121 continue; | 134 continue; |
122 | 135 |
123 std::string name = GetFilenameFromFd(dir); | 136 std::string name = GetFilenameFromFd(dir); |
124 | 137 |
125 if (!IsRegularFile(name)) | 138 if (!IsRegularFile(name)) |