Mercurial > minori
annotate dep/animia/src/fd/xnu.cc @ 215:031a257ee019
dep/animia: fd/proc: use std::filesystem, etc. changes
i.e. don't use std::stoi so we don't get exceptions...
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Sun, 07 Jan 2024 11:44:40 -0500 |
parents | bc1ae1810855 |
children | 2f5a9247e501 |
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 |
198
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
169
diff
changeset
|
19 bool 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 |
198
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
169
diff
changeset
|
41 bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) { |
153
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 |