Mercurial > minori
annotate dep/animia/src/fd/xnu.cc @ 164:99fdf5a90b0f
fd/linux: avoid reading buffers multiple times
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Sat, 18 Nov 2023 00:54:29 -0500 |
parents | 44c5e6dd9488 |
children | 54c5d80a737e |
rev | line source |
---|---|
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
1 /* |
162
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
160
diff
changeset
|
2 ** fd/xnu.cc |
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
3 ** - provides support for XNU (part of Darwin) |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
4 */ |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
5 #include "animia/fd/xnu.h" |
162
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
160
diff
changeset
|
6 #ifdef HAVE_COREFOUNDATION |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
160
diff
changeset
|
7 # include "animia/util/osx.h" |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
160
diff
changeset
|
8 #endif |
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
9 #include "animia.h" |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
10 |
156
cdf79282d647
dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
155
diff
changeset
|
11 #include <cassert> |
cdf79282d647
dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
155
diff
changeset
|
12 #include <string> |
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
13 #include <unordered_map> |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
14 #include <vector> |
160
900b5b530883
dep/animia: fd/xnu: use path args to get executable filename
Paper <mrpapersonic@gmail.com>
parents:
157
diff
changeset
|
15 #include <memory> |
153
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 #include <fcntl.h> |
156
cdf79282d647
dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
155
diff
changeset
|
18 #include <libproc.h> |
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
19 #include <sys/sysctl.h> |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
20 #include <sys/types.h> |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
21 #include <sys/user.h> |
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 #include <iostream> |
900b5b530883
dep/animia: fd/xnu: use path args to get executable filename
Paper <mrpapersonic@gmail.com>
parents:
157
diff
changeset
|
24 |
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
25 namespace animia::internal::xnu { |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
26 |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
27 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
|
28 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
|
29 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
|
30 |
160
900b5b530883
dep/animia: fd/xnu: use path args to get executable filename
Paper <mrpapersonic@gmail.com>
parents:
157
diff
changeset
|
31 int returned_size = 0; |
900b5b530883
dep/animia: fd/xnu: use path args to get executable filename
Paper <mrpapersonic@gmail.com>
parents:
157
diff
changeset
|
32 do { |
900b5b530883
dep/animia: fd/xnu: use path args to get executable filename
Paper <mrpapersonic@gmail.com>
parents:
157
diff
changeset
|
33 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
|
34 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
|
35 if (returned_size == -1) |
cdf79282d647
dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
155
diff
changeset
|
36 return false; |
160
900b5b530883
dep/animia: fd/xnu: use path args to get executable filename
Paper <mrpapersonic@gmail.com>
parents:
157
diff
changeset
|
37 } 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
|
38 |
160
900b5b530883
dep/animia: fd/xnu: use path args to get executable filename
Paper <mrpapersonic@gmail.com>
parents:
157
diff
changeset
|
39 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
|
40 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
|
41 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
|
42 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
|
43 return false; |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
44 } |
154 | 45 |
46 return true; | |
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
47 } |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
48 |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
49 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
|
50 if (!open_file_proc) |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
51 return false; |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
52 |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
53 for (const auto& pid : pids) { |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
54 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
|
55 if (bufsz == -1) |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
56 return false; |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
57 |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
58 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
|
59 if (!info) |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
60 return false; |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
61 |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
62 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
|
63 |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
64 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
|
65 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
|
66 struct vnode_fdinfowithpath vnodeInfo; |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
67 |
156
cdf79282d647
dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
155
diff
changeset
|
68 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
|
69 PROC_PIDFDVNODEPATHINFO_SIZE); |
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
70 if (sz != PROC_PIDFDVNODEPATHINFO_SIZE) |
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 /* this doesn't work! |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
74 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
|
75 continue; |
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
76 */ |
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 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
|
79 return false; |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
80 } |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
81 } |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
82 } |
154 | 83 |
153
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
84 return true; |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
85 } |
bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
86 |
156
cdf79282d647
dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
155
diff
changeset
|
87 } // namespace animia::internal::xnu |