comparison 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
comparison
equal deleted inserted replaced
169:e44b7c428d7c 201:8f6f8dd2eb23
5 ** 5 **
6 ** Much of this file is taken from the fstat source code in 6 ** Much of this file is taken from the fstat source code in
7 ** NetBSD. 7 ** NetBSD.
8 */ 8 */
9 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
10 #include <kvm.h> 23 #include <kvm.h>
24
25 #include <string>
11 26
12 namespace animia::internal::kvm { 27 namespace animia::internal::kvm {
13 28
14 static bool GetFilename(kvm_t* kvm, struct vnode *vp, std::string& name) { 29 bool KvmFdTools::EnumerateOpenProcesses(process_proc_t process_proc) {
15 struct vnode vn; 30 char errbuf[_POSIX2_LINE_MAX];
16 if (!kvm_read(kvm, vp, &vn, sizeof(*vn))) 31 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
17 return 0; 32 if (!kernel)
18
19 struct filestat fst;
20 const char* type = vfilestat(vn, &fst);
21 if (type == dead)
22 return false; 33 return false;
23 34
24 for (DEVS* d = devs; d != NULL; d = d->next) { 35 int entries = 0;
25 if (d->fsid == fst->fsid && d->ino == fst->fileid) { 36 struct kinfo_proc* kinfo = kvm_getprocs(kernel, KERN_PROC_ALL, 0, &entries);
26 name = d->name; 37 if (!kinfo)
27 break; 38 return false;
28 } 39
29 } 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);
30 45
31 return true; 46 return true;
32 } 47 }
33 48
34 static bool GetFilePath(kvm_t* kvm, fdfile_t* fp, std::string& path) { 49 bool KvmFdTools::EnumerateOpenFiles(std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
35 struct file file;
36 fdfile_t fdfile;
37
38 if (!kvm_read(kvm, fp, &fdfile, sizeof(fdfile)))
39 return false;
40
41 if (!fdfile.ff_file)
42 return false;
43
44 if (!kvm_read(fdfile.ff_file, &file, sizeof(file)))
45 return false;
46
47 if (file.f_type != DTYPE_VNODE)
48 return false;
49
50 return GetFilename(kvm, file.f_data, path);
51 }
52
53 static bool OpenFiles(kvm_t* kvm, struct kinfo_proc* p, open_file_proc_t open_file_proc) {
54 if (p->proc->p_fd == 0 || p->proc->p_cwdi == 0)
55 return false;
56
57 struct filedesc filed;
58 if (!kvm_read(kvm, p->proc->p_fd, &filed, sizeof(filed)))
59 return false;
60
61 if (filed.fd_lastfile == -1)
62 return false;
63
64 struct cwdinfo cwdi;
65 if (!kvm_read(kvm, p->proc->p_cwdi, &cwdi, sizeof(cwdi)))
66 return false;
67
68 struct fdtab dt;
69 if (!kvm_read(kvm, filed.fd_dt, &dt, sizeof(dt)))
70 return false;
71
72 /* check for corrupted files? */
73 if ((unsigned)filed.fd_lastfile >= dt.dt_nfiles || filed.fd_freefile > filed.fd_lastfile + 1)
74 return false;
75
76 /* open files */
77 std::unique_ptr<fdfile_t*[]> ofiles = nullptr;
78 {
79 ofiles.reset(malloc((filed.fd_lastfile + 1) * sizeof(fdfile_t*)));
80 if (!ofiles.get())
81 return false;
82 }
83
84 if (!kvm_read(kvm, &filed.fd_dt->dt_ff, ofiles.get(), filed.fd_lastfile + 1 * (sizeof(fdfile_t*))))
85 return false;
86
87 for (int i = 0; i <= filed.fd_lastfile; i++) {
88 if (!ofiles[i])
89 continue;
90 std::string name;
91 GetFilePath(kvm, ofiles[i], name);
92 if (!open_file_proc(p->proc->p_pid, name))
93 return false;
94 }
95
96 return true;
97 }
98
99 bool EnumerateOpenProcesses(process_proc_t process_proc) {
100 char errbuf[_POSIX2_LINE_MAX]; 50 char errbuf[_POSIX2_LINE_MAX];
101 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf); 51 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
102 if (!kernel)
103 return false;
104
105 int entries = 0;
106 struct kinfo_proc* kinfo = kvm_getprocs(kernel, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &nentries);
107 if (!kinfo)
108 return false;
109
110 for (int i = 0; i < entries; i++)
111 if (!process_proc({kinfo[i].p_pid, kinfo[i].p_comm}))
112 return false;
113
114 return true;
115 }
116
117 bool EnumerateOpenFiles(std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
118 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
119 if (!kernel) 52 if (!kernel)
120 return false; 53 return false;
121 54
122 for (const auto& pid : pids) { 55 for (const auto& pid : pids) {
123 int cnt; 56 int cnt;
124 struct kinfo_proc* kinfo = kvm_getprocs(kernel, KERN_PROC_PID, pid, &cnt); 57 struct kinfo_file* kfile = kvm_getfiles(kernel, KERN_FILE_BYPID, pid, &cnt);
125 if (!kinfo) 58 if (!kfile)
126 return false; 59 return false;
127 60
128 for (int i = 0; i < cnt; i++) { 61 for (int i = 0; i < cnt; i++)
129 OpenFiles(kernel, kinfo, open_file_proc); 62 if (!open_file_proc({pid, kfile[i].kf_path}))
130 } 63 return false;
131 } 64 }
65
66 kvm_close(kernel);
67
68 return true;
132 } 69 }
133 70
134 } 71 }