annotate dep/animia/src/fd/kvm.cc @ 236:4d461ef7d424

HUGE UPDATE: convert build system to autotools why? because cmake sucks :)
author Paper <mrpapersonic@gmail.com>
date Fri, 19 Jan 2024 00:24:02 -0500
parents 71832ffe425a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
202
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
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
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
3 ** Theoretically, this code *should* work, but I haven't
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
4 ** even tested it.
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 **
202
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
6 ** This also contains some code to support NetBSD, although
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
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
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
10 #include "animia/fd/kvm.h"
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
11 #include "animia/fd.h"
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
12 #include "animia.h"
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
13
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
14 #include <sys/types.h>
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
15 #include <sys/user.h>
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
16 #include <sys/file.h>
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
17 #include <sys/filedesc.h>
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
18 #include <sys/param.h>
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
19 #include <sys/vnode.h>
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
20 #include <sys/queue.h>
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
21 #include <sys/sysctl.h>
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
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
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
25 #include <string>
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
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
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
29 bool EnumerateOpenProcesses(process_proc_t process_proc) {
201
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
30 char errbuf[_POSIX2_LINE_MAX];
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
31 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
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
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
35 int entries = 0;
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
36 struct kinfo_proc* kinfo = kvm_getprocs(kernel, KERN_PROC_ALL, 0, &entries);
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
37 if (!kinfo)
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
38 return false;
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
39
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
40 for (int i = 0; i < entries; i++)
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
41 if (!process_proc({kinfo[i].ki_paddr->p_pid, kinfo[i].ki_paddr->p_comm}))
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
42 return false;
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
43
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
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
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
49 bool EnumerateOpenFiles(std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
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
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
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
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
58 struct kinfo_file* kfile = kvm_getfiles(kernel, KERN_FILE_BYPID, pid, &cnt);
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
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
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
62 for (int i = 0; i < cnt; i++)
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
63 if (!open_file_proc({pid, kfile[i].kf_path}))
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
64 return false;
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
65 }
201
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
66
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
67 kvm_close(kernel);
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
68
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
69 return true;
202
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
70 #else /* For NetBSD... I think */
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
71 for (const auto& pid : pids) {
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
72 int mib[6] = {
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
73 CTL_KERN,
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
74 KERN_FILE2,
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
75 KERN_FILE_BYPID,
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
76 pid,
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
77 sizeof(struct kinfo_file),
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
78 0
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
79 };
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
80
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
81 size_t len = 0;
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
82 if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), NULL, &len, NULL, 0) == -1)
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
83 return false;
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
84
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
85 mib[5] = len / sizeof(struct kinfo_file);
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
86
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
87 std::unique_ptr<struct kinfo_file[]> buf(new struct kinfo_file[mib[5]]);
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
88 if (!buf)
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
89 return false;
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
90
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
91 if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), buf.get(), &len, NULL, 0) == -1)
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
92 return false;
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
93
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
94 for (size_t i = 0; i < cnt; i++)
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
95 if (!open_file_proc({pid, kfile[i].kf_path}))
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
96 return false;
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
97 }
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
98
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
99 return true;
71832ffe425a animia: re-add kvm fd source
Paper <paper@paper.us.eu.org>
parents: 201
diff changeset
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 }