comparison 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
comparison
equal deleted inserted replaced
201:8f6f8dd2eb23 202:71832ffe425a
1 /* kvm.cc: provides support for libkvm in multiple BSDs 1 /* kvm.cc: provides support for OpenBSD's libkvm
2 ** 2 **
3 ** this is really the only way to get a thing that works on 3 ** Theoretically, this code *should* work, but I haven't
4 ** OpenBSD AND NetBSD. 4 ** even tested it.
5 ** 5 **
6 ** Much of this file is taken from the fstat source code in 6 ** This also contains some code to support NetBSD, although
7 ** NetBSD. 7 ** it calls the kernel instead of kvm.
8 */ 8 */
9 9
10 #include "animia/fd/kvm.h" 10 #include "animia/fd/kvm.h"
11 #include "animia/fd.h" 11 #include "animia/fd.h"
12 #include "animia.h" 12 #include "animia.h"
24 24
25 #include <string> 25 #include <string>
26 26
27 namespace animia::internal::kvm { 27 namespace animia::internal::kvm {
28 28
29 bool KvmFdTools::EnumerateOpenProcesses(process_proc_t process_proc) { 29 bool EnumerateOpenProcesses(process_proc_t process_proc) {
30 char errbuf[_POSIX2_LINE_MAX]; 30 char errbuf[_POSIX2_LINE_MAX];
31 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); 31 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
32 if (!kernel) 32 if (!kernel)
33 return false; 33 return false;
34 34
44 kvm_close(kernel); 44 kvm_close(kernel);
45 45
46 return true; 46 return true;
47 } 47 }
48 48
49 bool KvmFdTools::EnumerateOpenFiles(std::set<pid_t>& pids, open_file_proc_t open_file_proc) { 49 bool EnumerateOpenFiles(std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
50 #ifdef HAVE_KVM_GETFILES
50 char errbuf[_POSIX2_LINE_MAX]; 51 char errbuf[_POSIX2_LINE_MAX];
51 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); 52 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
52 if (!kernel) 53 if (!kernel)
53 return false; 54 return false;
54 55
64 } 65 }
65 66
66 kvm_close(kernel); 67 kvm_close(kernel);
67 68
68 return true; 69 return true;
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
69 } 101 }
70 102
71 } 103 }