Mercurial > minori
comparison dep/animia/src/fd/kvm.cc @ 212:6b08fbd7f206
chore: merge branches
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Sun, 07 Jan 2024 09:54:50 -0500 |
| parents | 71832ffe425a |
| children |
comparison
equal
deleted
inserted
replaced
| 211:7cf53145de11 | 212:6b08fbd7f206 |
|---|---|
| 1 /* kvm.cc: provides support for OpenBSD's libkvm | |
| 2 ** | |
| 3 ** Theoretically, this code *should* work, but I haven't | |
| 4 ** even tested it. | |
| 5 ** | |
| 6 ** This also contains some code to support NetBSD, although | |
| 7 ** it calls the kernel instead of kvm. | |
| 8 */ | |
| 9 | |
| 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 | |
| 23 #include <kvm.h> | |
| 24 | |
| 25 #include <string> | |
| 26 | |
| 27 namespace animia::internal::kvm { | |
| 28 | |
| 29 bool EnumerateOpenProcesses(process_proc_t process_proc) { | |
| 30 char errbuf[_POSIX2_LINE_MAX]; | |
| 31 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); | |
| 32 if (!kernel) | |
| 33 return false; | |
| 34 | |
| 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); | |
| 45 | |
| 46 return true; | |
| 47 } | |
| 48 | |
| 49 bool EnumerateOpenFiles(std::set<pid_t>& pids, open_file_proc_t open_file_proc) { | |
| 50 #ifdef HAVE_KVM_GETFILES | |
| 51 char errbuf[_POSIX2_LINE_MAX]; | |
| 52 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); | |
| 53 if (!kernel) | |
| 54 return false; | |
| 55 | |
| 56 for (const auto& pid : pids) { | |
| 57 int cnt; | |
| 58 struct kinfo_file* kfile = kvm_getfiles(kernel, KERN_FILE_BYPID, pid, &cnt); | |
| 59 if (!kfile) | |
| 60 return false; | |
| 61 | |
| 62 for (int i = 0; i < cnt; i++) | |
| 63 if (!open_file_proc({pid, kfile[i].kf_path})) | |
| 64 return false; | |
| 65 } | |
| 66 | |
| 67 kvm_close(kernel); | |
| 68 | |
| 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 | |
| 101 } | |
| 102 | |
| 103 } |
