Mercurial > minori
annotate dep/animia/src/fd/kvm.cc @ 201:8f6f8dd2eb23
dep/animia: finish kvm backend
dep/animia: cmake: don't use kvm on unsupported systems, use private and public includes properly
cmake: why are we defining target include directories twice?
author | paper@DavesDouble.local |
---|---|
date | Sun, 19 Nov 2023 19:13:28 -0500 |
parents | e44b7c428d7c |
children | 71832ffe425a |
rev | line source |
---|---|
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
1 /* kvm.cc: provides support for libkvm in multiple BSDs |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
2 ** |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
3 ** this is really the only way to get a thing that works on |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
4 ** OpenBSD AND NetBSD. |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
5 ** |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
6 ** Much of this file is taken from the fstat source code in |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
7 ** NetBSD. |
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 |
201 | 29 bool KvmFdTools::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) | |
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 |
201 | 49 bool KvmFdTools::EnumerateOpenFiles(std::set<pid_t>& pids, open_file_proc_t open_file_proc) { |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
50 char errbuf[_POSIX2_LINE_MAX]; |
201 | 51 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
|
52 if (!kernel) |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
53 return false; |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
54 |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
55 for (const auto& pid : pids) { |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
56 int cnt; |
201 | 57 struct kinfo_file* kfile = kvm_getfiles(kernel, KERN_FILE_BYPID, pid, &cnt); |
58 if (!kfile) | |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
59 return false; |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
60 |
201 | 61 for (int i = 0; i < cnt; i++) |
62 if (!open_file_proc({pid, kfile[i].kf_path})) | |
63 return false; | |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
64 } |
201 | 65 |
66 kvm_close(kernel); | |
67 | |
68 return true; | |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
69 } |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
70 |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
71 } |