annotate dep/animia/src/fd/xnu.cc @ 155:d2bbb5773616

dep/animia: add quartz backend for windows
author Paper <mrpapersonic@gmail.com>
date Wed, 15 Nov 2023 15:24:39 -0500
parents d43d68408d3c
children cdf79282d647
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
153
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
1 /*
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
2 ** fd/xnu.cpp
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"
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6 #include "animia.h"
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 #include <unordered_map>
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9 #include <vector>
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 #include <string>
154
d43d68408d3c dep/animia: fix XnuFdTools
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
11 #include <cassert>
153
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13 #include <fcntl.h>
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14 #include <sys/sysctl.h>
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15 #include <sys/types.h>
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16 #include <sys/user.h>
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17 #include <libproc.h>
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 namespace animia::internal::xnu {
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
20
154
d43d68408d3c dep/animia: fix XnuFdTools
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
21 static std::string GetProcessName(pid_t pid) {
153
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22 struct proc_bsdinfo proc;
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24 int st = proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &proc, PROC_PIDTBSDINFO_SIZE);
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
25 if (st != PROC_PIDTBSDINFO_SIZE)
154
d43d68408d3c dep/animia: fix XnuFdTools
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
26 return "";
153
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27
154
d43d68408d3c dep/animia: fix XnuFdTools
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
28 return proc.pbi_name;
153
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29 }
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31 /* this is a cleaned up version of a function from... Apple?
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32 ...anyway, what it essentially does is gets the size and stuff from
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
33 sysctl() and reserves the space in a vector to store the PIDs */
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
34 bool XnuFdTools::EnumerateOpenProcesses(process_proc_t process_proc) {
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35 struct kinfo_proc* result = NULL;
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36 size_t length = 0;
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37 static const int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
38
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 /* get appropriate length from sysctl()
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40 note: the reason this isn't checked is actually because this will
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41 *always* return an error on OS X (or... maybe I'm doing it wrong :) ) */
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42 sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, NULL, &length, NULL, 0);
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44 result = (struct kinfo_proc*)malloc(length);
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45 if (result == NULL)
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46 return false;
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 /* TODO: this might actually return ENOMEM if the amount of file handles changes between the
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49 original sysctl() call and this one, which is technically possible */
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50 if (sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, result, &length, NULL, 0) == ENOMEM) {
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51 assert(result != NULL);
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52 free(result);
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53 return false;
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54 }
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 < length / sizeof(*result); i++) {
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57 const pid_t pid = result[i].kp_proc.p_pid;
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58 if (!process_proc({pid, GetProcessName(pid)}))
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
59 return false;
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 }
154
d43d68408d3c dep/animia: fix XnuFdTools
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
61
d43d68408d3c dep/animia: fix XnuFdTools
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
62 return true;
153
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
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
65 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
66 if (!open_file_proc)
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
67 return false;
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 for (const auto& pid : pids) {
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
70 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
71 if (bufsz == -1)
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
72 return false;
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 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
75 if (!info)
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
76 return false;
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 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
79
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
80 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
81 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
82 struct vnode_fdinfowithpath vnodeInfo;
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
83
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
84 int sz = proc_pidfdinfo(pid, info[i].proc_fd, PROC_PIDFDVNODEPATHINFO, &vnodeInfo, PROC_PIDFDVNODEPATHINFO_SIZE);
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
85 if (sz != PROC_PIDFDVNODEPATHINFO_SIZE)
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
86 return false;
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
87
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
88 /* this doesn't work!
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
89 if (vnodeInfo.pfi.fi_openflags & O_WRONLY || vnodeInfo.pfi.fi_openflags & O_RDWR)
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
90 continue;
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
91 */
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
92
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
93 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
94 return false;
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
95 }
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
96 }
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
97 }
154
d43d68408d3c dep/animia: fix XnuFdTools
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
98
153
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
99 return true;
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
100 }
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
101
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
102 } // namespace animia::internal::unix