Mercurial > minori
comparison dep/animone/src/fd/kvm.cc @ 258:862d0d8619f6
*: HUUUGE changes
animia has been renamed to animone, so instead of thinking of a
health condition, you think of a beautiful flower :)
I've also edited some of the code for animone, but I have no idea
if it even works or not because I don't have a mac or windows
machine lying around. whoops!
... anyway, all of the changes divergent from Anisthesia are now
licensed under BSD. it's possible that I could even rewrite most
of the code to where I don't even have to keep the MIT license,
but that's thinking too far into the future
I've been slacking off on implementing the anime seasons page,
mostly out of laziness. I think I'd have to create another db file
specifically for the seasons
anyway, this code is being pushed *primarily* because the hard drive
it's on is failing! yay :)
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Mon, 01 Apr 2024 02:43:44 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
257:699a20c57dc8 | 258:862d0d8619f6 |
---|---|
1 /* kvm.cc: provides support for *BSD. | |
2 */ | |
3 | |
4 #include "animone/fd/kvm.h" | |
5 #include "animone.h" | |
6 #include "animone/fd.h" | |
7 | |
8 #include <sys/file.h> | |
9 #include <sys/filedesc.h> | |
10 #include <sys/param.h> | |
11 #include <sys/queue.h> | |
12 #include <sys/sysctl.h> | |
13 #include <sys/types.h> | |
14 #include <sys/user.h> | |
15 #include <sys/vnode.h> | |
16 | |
17 #include <kvm.h> | |
18 #ifdef LIBUTIL | |
19 # include <libutil.h> | |
20 #endif | |
21 | |
22 #include <string> | |
23 | |
24 namespace animone::internal::kvm { | |
25 | |
26 bool GetProcessName(pid_t pid, std::string& name) { | |
27 char errbuf[_POSIX2_LINE_MAX]; | |
28 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); | |
29 if (!kernel) | |
30 return false; | |
31 | |
32 int entries = 0; | |
33 struct kinfo_proc* kinfo = kvm_getprocs(kernel, KERN_PROC_PID, pid, &entries); | |
34 if (!kinfo) { | |
35 kvm_close(kernel); | |
36 return false; | |
37 } | |
38 | |
39 if (entries < 1) { | |
40 kvm_close(kernel); | |
41 return false; | |
42 } | |
43 | |
44 name = kinfo[0].ki_paddr->p_comm; | |
45 | |
46 return true; | |
47 } | |
48 | |
49 /* Most of the BSDs share the common kvm library, | |
50 * so accessing this information can be trivial. | |
51 */ | |
52 bool EnumerateOpenProcesses(process_proc_t process_proc) { | |
53 char errbuf[_POSIX2_LINE_MAX]; | |
54 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); | |
55 if (!kernel) | |
56 return false; | |
57 | |
58 int entries = 0; | |
59 struct kinfo_proc* kinfo = kvm_getprocs(kernel, KERN_PROC_ALL, 0, &entries); | |
60 if (!kinfo) { | |
61 kvm_close(kernel); | |
62 return false; | |
63 } | |
64 | |
65 for (int i = 0; i < entries; i++) { | |
66 if (!process_proc({kinfo[i].ki_paddr->p_pid, kinfo[i].ki_paddr->p_comm})) { | |
67 kvm_close(kernel); | |
68 return false; | |
69 } | |
70 } | |
71 | |
72 kvm_close(kernel); | |
73 | |
74 return true; | |
75 } | |
76 | |
77 bool EnumerateOpenFiles(std::set<pid_t>& pids, open_file_proc_t open_file_proc) { | |
78 #ifdef __OpenBSD__ | |
79 char errbuf[_POSIX2_LINE_MAX]; | |
80 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); | |
81 if (!kernel) | |
82 return false; | |
83 | |
84 for (const auto& pid : pids) { | |
85 int cnt; | |
86 struct kinfo_file* kfile = kvm_getfiles(kernel, KERN_FILE_BYPID, pid, &cnt); | |
87 if (!kfile) { | |
88 kvm_close(kernel); | |
89 return false; | |
90 } | |
91 | |
92 for (int i = 0; i < cnt; i++) { | |
93 if (!open_file_proc({pid, kfile[i].kf_path})) { | |
94 kvm_close(kernel); | |
95 return false; | |
96 } | |
97 } | |
98 } | |
99 | |
100 kvm_close(kernel); | |
101 | |
102 return true; | |
103 #elif defined(LIBUTIL) | |
104 /* does this code even work? */ | |
105 for (const auto& pid : pids) { | |
106 int cnt; | |
107 std::unique_ptr<struct kinfo_file[]> files(kinfo_getfile(pid, &cnt)); | |
108 if (!files) | |
109 return false; | |
110 | |
111 for (int i = 0; i < cnt; i++) { | |
112 const struct kinfo_file& current = files[i]; | |
113 if (current.kf_vnode_type != KF_VTYPE_VREG) | |
114 continue; | |
115 | |
116 if (!open_file_proc({pid, current.kf_path})) | |
117 return false; | |
118 } | |
119 } | |
120 | |
121 return true; | |
122 #elif defined(__NetBSD__) | |
123 for (const auto& pid : pids) { | |
124 int mib[6] = {CTL_KERN, KERN_FILE2, KERN_FILE_BYPID, pid, sizeof(struct kinfo_file), 0}; | |
125 | |
126 size_t len = 0; | |
127 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &len, NULL, 0) == -1) | |
128 return false; | |
129 | |
130 mib[5] = len / sizeof(struct kinfo_file); | |
131 | |
132 std::unique_ptr<struct kinfo_file[]> buf(new struct kinfo_file[mib[5]]); | |
133 if (!buf) | |
134 return false; | |
135 | |
136 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf.get(), &len, NULL, 0) == -1) | |
137 return false; | |
138 | |
139 /* TODO: check kfile[i].ki_ofileflags */ | |
140 for (size_t i = 0; i < mib[5]; i++) | |
141 if (!open_file_proc({pid, kfile[i].kf_path})) | |
142 return false; | |
143 } | |
144 | |
145 return true; | |
146 #else | |
147 return false; | |
148 #endif | |
149 } | |
150 | |
151 } // namespace animone::internal::kvm |