diff dep/animia/src/fd/xnu.cc @ 156:cdf79282d647

dep/animia: add VERY early x11 window stuff
author Paper <mrpapersonic@gmail.com>
date Wed, 15 Nov 2023 18:04:04 -0500
parents d2bbb5773616
children 18c8eb5d1edc
line wrap: on
line diff
--- a/dep/animia/src/fd/xnu.cc	Wed Nov 15 15:24:39 2023 -0500
+++ b/dep/animia/src/fd/xnu.cc	Wed Nov 15 18:04:04 2023 -0500
@@ -5,16 +5,16 @@
 #include "animia/fd/xnu.h"
 #include "animia.h"
 
+#include <cassert>
+#include <string>
 #include <unordered_map>
 #include <vector>
-#include <string>
-#include <cassert>
 
 #include <fcntl.h>
+#include <libproc.h>
 #include <sys/sysctl.h>
 #include <sys/types.h>
 #include <sys/user.h>
-#include <libproc.h>
 
 namespace animia::internal::xnu {
 
@@ -25,36 +25,21 @@
 	if (st != PROC_PIDTBSDINFO_SIZE)
 		return "";
 
-	return proc.pbi_name;
+	return (proc.pbi_name[0]) ? proc.pbi_name : proc.pbi_comm;
 }
 
-/* this is a cleaned up version of a function from... Apple?
-   ...anyway, what it essentially does is gets the size and stuff from
-   sysctl() and reserves the space in a vector to store the PIDs */
 bool XnuFdTools::EnumerateOpenProcesses(process_proc_t process_proc) {
-	struct kinfo_proc* result = NULL;
-	size_t length = 0;
-	static const int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
+	std::vector<pid_t> pids;
+	pids.reserve(1024);
 
-	/* get appropriate length from sysctl()
-	   note: the reason this isn't checked is actually because this will
-	   *always* return an error on OS X (or... maybe I'm doing it wrong :) ) */
-	sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, NULL, &length, NULL, 0);
-
-	result = (struct kinfo_proc*)malloc(length);
-	if (result == NULL)
-		return false;
-
-	/* TODO: this might actually return ENOMEM if the amount of file handles changes between the
-	   original sysctl() call and this one, which is technically possible */
-	if (sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, result, &length, NULL, 0) == ENOMEM) {
-		assert(result != NULL);
-		free(result);
-		return false;
+	for (int returned_size = pids.capacity(); pids.capacity() > returned_size; pids.reserve(pids.capacity() * 2)) {
+		returned_size = proc_listpids(PROC_ALL_PIDS, 0, pids.data(), pids.capacity() * sizeof(pid_t));
+		if (returned_size == -1)
+			return false;
 	}
 
-	for (int i = 0; i < length / sizeof(*result); i++) {
-		const pid_t pid = result[i].kp_proc.p_pid;
+	for (int i = 0; i < size / sizeof(*pids); i++) {
+		const pid_t pid = pids[i].kp_proc.p_pid;
 		if (!process_proc({pid, GetProcessName(pid)}))
 			return false;
 	}
@@ -81,13 +66,14 @@
 			if (info[i].proc_fdtype == PROX_FDTYPE_VNODE) {
 				struct vnode_fdinfowithpath vnodeInfo;
 
-				int sz = proc_pidfdinfo(pid, info[i].proc_fd, PROC_PIDFDVNODEPATHINFO, &vnodeInfo, PROC_PIDFDVNODEPATHINFO_SIZE);
+				int sz = proc_pidfdinfo(pid, info[i].proc_fd, PROC_PIDFDVNODEPATHINFO, &vnodeInfo,
+				                        PROC_PIDFDVNODEPATHINFO_SIZE);
 				if (sz != PROC_PIDFDVNODEPATHINFO_SIZE)
 					return false;
 
 				/* this doesn't work!
 				if (vnodeInfo.pfi.fi_openflags & O_WRONLY || vnodeInfo.pfi.fi_openflags & O_RDWR)
-					continue;
+				    continue;
 				*/
 
 				if (!open_file_proc({pid, vnodeInfo.pvip.vip_path}))
@@ -99,4 +85,4 @@
 	return true;
 }
 
-} // namespace animia::internal::unix
+} // namespace animia::internal::xnu