comparison dep/animia/src/fd/xnu.cc @ 157:18c8eb5d1edc

x11: make it work
author Paper <mrpapersonic@gmail.com>
date Thu, 16 Nov 2023 16:51:34 -0500
parents cdf79282d647
children 900b5b530883
comparison
equal deleted inserted replaced
156:cdf79282d647 157:18c8eb5d1edc
17 #include <sys/user.h> 17 #include <sys/user.h>
18 18
19 namespace animia::internal::xnu { 19 namespace animia::internal::xnu {
20 20
21 static std::string GetProcessName(pid_t pid) { 21 static std::string GetProcessName(pid_t pid) {
22 struct proc_bsdinfo proc; 22 char name[2*MAXCOMLEN];
23 proc_name(pid, name, sizeof(name));
23 24
24 int st = proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &proc, PROC_PIDTBSDINFO_SIZE); 25 return name;
25 if (st != PROC_PIDTBSDINFO_SIZE)
26 return "";
27
28 return (proc.pbi_name[0]) ? proc.pbi_name : proc.pbi_comm;
29 } 26 }
30 27
31 bool XnuFdTools::EnumerateOpenProcesses(process_proc_t process_proc) { 28 bool XnuFdTools::EnumerateOpenProcesses(process_proc_t process_proc) {
32 std::vector<pid_t> pids; 29 std::vector<pid_t> pids;
33 pids.reserve(1024); 30 pids.reserve(1024);
36 returned_size = proc_listpids(PROC_ALL_PIDS, 0, pids.data(), pids.capacity() * sizeof(pid_t)); 33 returned_size = proc_listpids(PROC_ALL_PIDS, 0, pids.data(), pids.capacity() * sizeof(pid_t));
37 if (returned_size == -1) 34 if (returned_size == -1)
38 return false; 35 return false;
39 } 36 }
40 37
41 for (int i = 0; i < size / sizeof(*pids); i++) { 38 for (const auto& pid : pids) {
42 const pid_t pid = pids[i].kp_proc.p_pid;
43 if (!process_proc({pid, GetProcessName(pid)})) 39 if (!process_proc({pid, GetProcessName(pid)}))
44 return false; 40 return false;
45 } 41 }
46 42
47 return true; 43 return true;