Mercurial > minori
annotate dep/animia/src/fd/xnu.cc @ 187:9613d72b097e
*: multiple performance improvements
like marking `static const` when it makes sense...
date: change old stupid heap-based method to a structure which should
make copying the thing actually make a copy.
also many performance-based changes, like removing the std::tie
dependency and forward-declaring nlohmann json
*: replace every instance of QString::fromUtf8 to Strings::ToQString.
the main difference is that our function will always convert exactly
what is in the string, while some other times it would only convert
up to the nearest NUL byte
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Wed, 06 Dec 2023 13:43:54 -0500 |
parents | e44b7c428d7c |
children | bc1ae1810855 |
rev | line source |
---|---|
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
1 #include "animia/fd/xnu.h" |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
166
diff
changeset
|
2 #include "animia/util/osx.h" |
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
3 #include "animia.h" |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
4 |
156
cdf79282d647
dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
155
diff
changeset
|
5 #include <cassert> |
cdf79282d647
dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
155
diff
changeset
|
6 #include <string> |
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
7 #include <unordered_map> |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
8 #include <vector> |
160
900b5b530883
dep/animia: fd/xnu: use path args to get executable filename
Paper <mrpapersonic@gmail.com>
parents:
157
diff
changeset
|
9 #include <memory> |
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
10 |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
11 #include <fcntl.h> |
156
cdf79282d647
dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
155
diff
changeset
|
12 #include <libproc.h> |
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
13 #include <sys/sysctl.h> |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
14 #include <sys/types.h> |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
15 #include <sys/user.h> |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
16 |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
17 namespace animia::internal::xnu { |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
18 |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
19 bool XnuFdTools::EnumerateOpenProcesses(process_proc_t process_proc) { |
160
900b5b530883
dep/animia: fd/xnu: use path args to get executable filename
Paper <mrpapersonic@gmail.com>
parents:
157
diff
changeset
|
20 size_t pids_size = 512; |
900b5b530883
dep/animia: fd/xnu: use path args to get executable filename
Paper <mrpapersonic@gmail.com>
parents:
157
diff
changeset
|
21 std::unique_ptr<pid_t[]> pids; |
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
22 |
160
900b5b530883
dep/animia: fd/xnu: use path args to get executable filename
Paper <mrpapersonic@gmail.com>
parents:
157
diff
changeset
|
23 int returned_size = 0; |
900b5b530883
dep/animia: fd/xnu: use path args to get executable filename
Paper <mrpapersonic@gmail.com>
parents:
157
diff
changeset
|
24 do { |
900b5b530883
dep/animia: fd/xnu: use path args to get executable filename
Paper <mrpapersonic@gmail.com>
parents:
157
diff
changeset
|
25 pids.reset(new pid_t[pids_size]); |
900b5b530883
dep/animia: fd/xnu: use path args to get executable filename
Paper <mrpapersonic@gmail.com>
parents:
157
diff
changeset
|
26 returned_size = proc_listpids(PROC_ALL_PIDS, 0, pids.get(), pids_size * sizeof(pid_t)); |
156
cdf79282d647
dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
155
diff
changeset
|
27 if (returned_size == -1) |
cdf79282d647
dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
155
diff
changeset
|
28 return false; |
160
900b5b530883
dep/animia: fd/xnu: use path args to get executable filename
Paper <mrpapersonic@gmail.com>
parents:
157
diff
changeset
|
29 } while ((pids_size * sizeof(size_t)) < returned_size); |
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
30 |
160
900b5b530883
dep/animia: fd/xnu: use path args to get executable filename
Paper <mrpapersonic@gmail.com>
parents:
157
diff
changeset
|
31 for (int i = 0; i < pids_size; i++) { |
900b5b530883
dep/animia: fd/xnu: use path args to get executable filename
Paper <mrpapersonic@gmail.com>
parents:
157
diff
changeset
|
32 std::string result; |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
33 osx::util::GetProcessName(pids[i], result); |
160
900b5b530883
dep/animia: fd/xnu: use path args to get executable filename
Paper <mrpapersonic@gmail.com>
parents:
157
diff
changeset
|
34 if (!process_proc({pids[i], result})) |
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
35 return false; |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
36 } |
154 | 37 |
38 return true; | |
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
39 } |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
40 |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
41 bool XnuFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) { |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
42 if (!open_file_proc) |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
43 return false; |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
44 |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
45 for (const auto& pid : pids) { |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
46 int bufsz = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0); |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
47 if (bufsz == -1) |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
48 return false; |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
49 |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
50 struct proc_fdinfo* info = (struct proc_fdinfo*)malloc(bufsz); |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
51 if (!info) |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
52 return false; |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
53 |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
54 proc_pidinfo(pid, PROC_PIDLISTFDS, 0, info, bufsz); |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
55 |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
56 for (int i = 0; i < bufsz / sizeof(info[0]); i++) { |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
57 if (info[i].proc_fdtype == PROX_FDTYPE_VNODE) { |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
58 struct vnode_fdinfowithpath vnodeInfo; |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
59 |
156
cdf79282d647
dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
155
diff
changeset
|
60 int sz = proc_pidfdinfo(pid, info[i].proc_fd, PROC_PIDFDVNODEPATHINFO, &vnodeInfo, |
cdf79282d647
dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
155
diff
changeset
|
61 PROC_PIDFDVNODEPATHINFO_SIZE); |
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
62 if (sz != PROC_PIDFDVNODEPATHINFO_SIZE) |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
63 return false; |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
64 |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
65 /* this doesn't work! |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
66 if (vnodeInfo.pfi.fi_openflags & O_WRONLY || vnodeInfo.pfi.fi_openflags & O_RDWR) |
156
cdf79282d647
dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
155
diff
changeset
|
67 continue; |
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
68 */ |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
69 |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
70 if (!open_file_proc({pid, vnodeInfo.pvip.vip_path})) |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
71 return false; |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
72 } |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
73 } |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
74 } |
154 | 75 |
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
76 return true; |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
77 } |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
78 |
156
cdf79282d647
dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
155
diff
changeset
|
79 } // namespace animia::internal::xnu |