Mercurial > minori
comparison dep/animia/src/fd/kvm.cc @ 169:e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Sun, 19 Nov 2023 17:30:38 -0500 |
parents | |
children | 8f6f8dd2eb23 |
comparison
equal
deleted
inserted
replaced
168:79a2a24453fa | 169:e44b7c428d7c |
---|---|
1 /* kvm.cc: provides support for libkvm in multiple BSDs | |
2 ** | |
3 ** this is really the only way to get a thing that works on | |
4 ** OpenBSD AND NetBSD. | |
5 ** | |
6 ** Much of this file is taken from the fstat source code in | |
7 ** NetBSD. | |
8 */ | |
9 | |
10 #include <kvm.h> | |
11 | |
12 namespace animia::internal::kvm { | |
13 | |
14 static bool GetFilename(kvm_t* kvm, struct vnode *vp, std::string& name) { | |
15 struct vnode vn; | |
16 if (!kvm_read(kvm, vp, &vn, sizeof(*vn))) | |
17 return 0; | |
18 | |
19 struct filestat fst; | |
20 const char* type = vfilestat(vn, &fst); | |
21 if (type == dead) | |
22 return false; | |
23 | |
24 for (DEVS* d = devs; d != NULL; d = d->next) { | |
25 if (d->fsid == fst->fsid && d->ino == fst->fileid) { | |
26 name = d->name; | |
27 break; | |
28 } | |
29 } | |
30 | |
31 return true; | |
32 } | |
33 | |
34 static bool GetFilePath(kvm_t* kvm, fdfile_t* fp, std::string& path) { | |
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]; | |
101 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, 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) | |
120 return false; | |
121 | |
122 for (const auto& pid : pids) { | |
123 int cnt; | |
124 struct kinfo_proc* kinfo = kvm_getprocs(kernel, KERN_PROC_PID, pid, &cnt); | |
125 if (!kinfo) | |
126 return false; | |
127 | |
128 for (int i = 0; i < cnt; i++) { | |
129 OpenFiles(kernel, kinfo, open_file_proc); | |
130 } | |
131 } | |
132 } | |
133 | |
134 } |