diff dep/animia/src/fd/proc.cc @ 202:71832ffe425a

animia: re-add kvm fd source this is all being merged from my wildly out-of-date laptop. SORRY! in other news, I edited the CI file to install the wayland client as well, so the linux CI build might finally get wayland stuff.
author Paper <paper@paper.us.eu.org>
date Tue, 02 Jan 2024 06:05:06 -0500
parents bc1ae1810855
children 031a257ee019
line wrap: on
line diff
--- a/dep/animia/src/fd/proc.cc	Sun Nov 19 19:13:28 2023 -0500
+++ b/dep/animia/src/fd/proc.cc	Tue Jan 02 06:05:06 2024 -0500
@@ -16,13 +16,7 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
-#ifdef FREEBSD
-#	include <sys/types.h>
-#	include <sys/user.h>
-#	include <libutil.h>
-#endif
-
-#define PROC_LOCATION "/proc"
+static constexpr std::string_view PROC_LOCATION = "/proc";
 
 namespace animia::internal::proc {
 
@@ -57,7 +51,7 @@
 }
 
 static bool AreFlagsOk(pid_t pid, int fd) {
-	const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fdinfo/" + std::to_string(fd);
+	const std::string path = std::string(PROC_LOCATION) + "/" + std::to_string(pid) + "/fdinfo/" + std::to_string(fd);
 
 	std::ifstream file(path.c_str());
 	if (!file)
@@ -75,7 +69,8 @@
 
 static bool GetFilenameFromFd(std::string link, std::string& out) {
 	/* gets around stupid linux limitation where /proc doesn't
-	   give actual filesize readings */
+	 * give actual size readings of the string
+	*/
 	constexpr size_t OUT_MAX = (1 << 15); // 32KiB
 	out.resize(1024);
 
@@ -84,34 +79,30 @@
 		 out.resize(out.length() * 2)) {
 		exe_used = readlink(link.c_str(), &out.front(), out.length());
 		if (exe_used == (ssize_t)-1 || exe_used < (ssize_t)1)
-			return false;
+			return false; // we got a bad result, i think
 	}
+
+	out.resize(out.find('\0'));
+
+	return true;
 }
 
 static std::string GetProcessName(pid_t pid) {
 	std::string result;
 
-#ifdef FREEBSD
-	struct kinfo_proc* proc = kinfo_getproc(pid);
-	if (!proc)
-		return "";
-	result = proc->ki_comm;
-	free(proc);
-#elif defined(LINUX)
-	const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/comm";
+	const std::string path = std::string(PROC_LOCATION) + "/" + std::to_string(pid) + "/comm";
 
 	if (!util::ReadFile(path, result))
 		return "";
 
 	result.erase(std::remove(result.begin(), result.end(), '\n'), result.end());
-#endif
 
 	return result;
 }
 
-bool ProcFdTools::EnumerateOpenProcesses(process_proc_t process_proc) {
+bool EnumerateOpenProcesses(process_proc_t process_proc) {
 	bool success = false;
-	for (const auto& dir : GetAllFilesInDir(PROC_LOCATION)) {
+	for (const auto& dir : GetAllFilesInDir(std::string(PROC_LOCATION))) {
 		pid_t pid;
 		try {
 			pid = std::stoul(Basename(dir));
@@ -125,18 +116,20 @@
 	return success;
 }
 
-bool ProcFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
+bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
 	if (!open_file_proc)
 		return false;
 
 	for (const auto& pid : pids) {
-		const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fd";
+		const std::string path = std::string(PROC_LOCATION) + "/" + std::to_string(pid) + "/fd";
 
 		for (const auto& dir : GetAllFilesInDir(path)) {
 			if (!AreFlagsOk(pid, std::stoi(Basename(dir))))
 				continue;
 
-			std::string name = GetFilenameFromFd(dir);
+			std::string name;
+			if (!GetFilenameFromFd(dir, name))
+				continue;
 
 			if (!IsRegularFile(name))
 				continue;