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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
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
201
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
29 bool KvmFdTools::EnumerateOpenProcesses(process_proc_t process_proc) {
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
201
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
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
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
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
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
57 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
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
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
61 for (int i = 0; i < cnt; i++)
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
62 if (!open_file_proc({pid, kfile[i].kf_path}))
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
63 return false;
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
64 }
201
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
65
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
66 kvm_close(kernel);
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
67
8f6f8dd2eb23 dep/animia: finish kvm backend
paper@DavesDouble.local
parents: 169
diff changeset
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 }