diff 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
line wrap: on
line diff
--- a/dep/animia/src/fd/linux.cc	Sun Nov 12 18:28:04 2023 -0500
+++ b/dep/animia/src/fd/linux.cc	Sun Nov 12 18:35:49 2023 -0500
@@ -52,7 +52,13 @@
 
 static bool AreFlagsOk(pid_t pid, int fd) {
 	const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fdinfo/" + std::to_string(fd);
-	std::stringstream buffer(util::ReadFile(path));
+	std::stringstream buffer;
+	{
+		std::string data;
+		if (!util::ReadFile(path, data))
+			return false;
+		buffer << data;
+	}	
 
 	int flags = 0;
 	for (std::string line; std::getline(buffer, line);) {
@@ -94,22 +100,29 @@
 }
 
 bool LinuxFdTools::GetAllPids(std::set<pid_t>& pids) {
-	for (const auto& dir : get_all_files_in_dir(PROC_LOCATION)) {
+	bool success = false;
+	for (const auto& dir : GetAllFilesInDir(PROC_LOCATION)) {
 		pid_t pid;
 		try {
-			pid = std::stoul(basename(dir));
+			pid = std::stoul(Basename(dir));
+			success = true;
 		} catch (std::invalid_argument) {
 			continue;
 		}
-		pids.push_back(pid);
+		pids.insert(pid);
 	}
+	return success;
 }
 
 bool LinuxFdTools::GetProcessName(pid_t pid, std::string& result) {
 	const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/comm";
 
-	std::string result = util::ReadFile(path);
+	std::string result;
+	if (!util::ReadFile(path, result))
+		return false;
+
 	result.erase(std::remove(result.begin(), result.end(), '\n'), result.end());
+	return true;
 }
 
 bool LinuxFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::tuple<pid_t, std::string>>& files) {
@@ -117,7 +130,7 @@
 		const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fd";
 
 		for (const auto& dir : GetAllFilesInDir(path)) {
-			if (!AreFlagsOk(pid, std::stoi(basename(dir))))
+			if (!AreFlagsOk(pid, std::stoi(Basename(dir))))
 				continue;
 
 			std::string name = GetFilenameFromFd(dir);