Mercurial > minori
annotate dep/animia/src/fd/kvm.cc @ 202:71832ffe425a
animia: re-add kvm fd source
this is all being merged from my wildly out-of-date laptop. SORRY!
in other news, I edited the CI file to install the wayland client
as well, so the linux CI build might finally get wayland stuff.
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Tue, 02 Jan 2024 06:05:06 -0500 |
parents | 8f6f8dd2eb23 |
children |
rev | line source |
---|---|
202 | 1 /* kvm.cc: provides support for OpenBSD's libkvm |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
2 ** |
202 | 3 ** Theoretically, this code *should* work, but I haven't |
4 ** even tested it. | |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
5 ** |
202 | 6 ** This also contains some code to support NetBSD, although |
7 ** it calls the kernel instead of kvm. | |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
8 */ |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
9 |
201 | 10 #include "animia/fd/kvm.h" |
11 #include "animia/fd.h" | |
12 #include "animia.h" | |
13 | |
14 #include <sys/types.h> | |
15 #include <sys/user.h> | |
16 #include <sys/file.h> | |
17 #include <sys/filedesc.h> | |
18 #include <sys/param.h> | |
19 #include <sys/vnode.h> | |
20 #include <sys/queue.h> | |
21 #include <sys/sysctl.h> | |
22 | |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
23 #include <kvm.h> |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
24 |
201 | 25 #include <string> |
26 | |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
27 namespace animia::internal::kvm { |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
28 |
202 | 29 bool EnumerateOpenProcesses(process_proc_t process_proc) { |
201 | 30 char errbuf[_POSIX2_LINE_MAX]; |
31 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); | |
32 if (!kernel) | |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
33 return false; |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
34 |
201 | 35 int entries = 0; |
36 struct kinfo_proc* kinfo = kvm_getprocs(kernel, KERN_PROC_ALL, 0, &entries); | |
37 if (!kinfo) | |
38 return false; | |
39 | |
40 for (int i = 0; i < entries; i++) | |
41 if (!process_proc({kinfo[i].ki_paddr->p_pid, kinfo[i].ki_paddr->p_comm})) | |
42 return false; | |
43 | |
44 kvm_close(kernel); | |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
45 |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
46 return true; |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
47 } |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
48 |
202 | 49 bool EnumerateOpenFiles(std::set<pid_t>& pids, open_file_proc_t open_file_proc) { |
50 #ifdef HAVE_KVM_GETFILES | |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
51 char errbuf[_POSIX2_LINE_MAX]; |
201 | 52 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
53 if (!kernel) |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
54 return false; |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
55 |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
56 for (const auto& pid : pids) { |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
57 int cnt; |
201 | 58 struct kinfo_file* kfile = kvm_getfiles(kernel, KERN_FILE_BYPID, pid, &cnt); |
59 if (!kfile) | |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
60 return false; |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
61 |
201 | 62 for (int i = 0; i < cnt; i++) |
63 if (!open_file_proc({pid, kfile[i].kf_path})) | |
64 return false; | |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
65 } |
201 | 66 |
67 kvm_close(kernel); | |
68 | |
69 return true; | |
202 | 70 #else /* For NetBSD... I think */ |
71 for (const auto& pid : pids) { | |
72 int mib[6] = { | |
73 CTL_KERN, | |
74 KERN_FILE2, | |
75 KERN_FILE_BYPID, | |
76 pid, | |
77 sizeof(struct kinfo_file), | |
78 0 | |
79 }; | |
80 | |
81 size_t len = 0; | |
82 if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), NULL, &len, NULL, 0) == -1) | |
83 return false; | |
84 | |
85 mib[5] = len / sizeof(struct kinfo_file); | |
86 | |
87 std::unique_ptr<struct kinfo_file[]> buf(new struct kinfo_file[mib[5]]); | |
88 if (!buf) | |
89 return false; | |
90 | |
91 if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), buf.get(), &len, NULL, 0) == -1) | |
92 return false; | |
93 | |
94 for (size_t i = 0; i < cnt; i++) | |
95 if (!open_file_proc({pid, kfile[i].kf_path})) | |
96 return false; | |
97 } | |
98 | |
99 return true; | |
100 #endif | |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
101 } |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
102 |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
103 } |