annotate dep/animia/src/fd/kvm.cc @ 191:0fc126d52de4

animia: multiple stylistic choices win.class_name is now used to store bundle IDs on OS X, add some little explanations here and there for dumb stuff
author Paper <mrpapersonic@gmail.com>
date Thu, 07 Dec 2023 01:48:04 -0500
parents e44b7c428d7c
children 8f6f8dd2eb23
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
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 #include <kvm.h>
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12 namespace animia::internal::kvm {
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14 static bool GetFilename(kvm_t* kvm, struct vnode *vp, std::string& name) {
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15 struct vnode vn;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16 if (!kvm_read(kvm, vp, &vn, sizeof(*vn)))
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17 return 0;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19 struct filestat fst;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
20 const char* type = vfilestat(vn, &fst);
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21 if (type == dead)
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22 return false;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24 for (DEVS* d = devs; d != NULL; d = d->next) {
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
25 if (d->fsid == fst->fsid && d->ino == fst->fileid) {
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
26 name = d->name;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27 break;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
28 }
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29 }
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31 return true;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32 }
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
33
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
34 static bool GetFilePath(kvm_t* kvm, fdfile_t* fp, std::string& path) {
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35 struct file file;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36 fdfile_t fdfile;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
38 if (!kvm_read(kvm, fp, &fdfile, sizeof(fdfile)))
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 return false;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41 if (!fdfile.ff_file)
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42 return false;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44 if (!kvm_read(fdfile.ff_file, &file, sizeof(file)))
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45 return false;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 if (file.f_type != DTYPE_VNODE)
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48 return false;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50 return GetFilename(kvm, file.f_data, path);
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51 }
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53 static bool OpenFiles(kvm_t* kvm, struct kinfo_proc* p, open_file_proc_t open_file_proc) {
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54 if (p->proc->p_fd == 0 || p->proc->p_cwdi == 0)
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 return false;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57 struct filedesc filed;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58 if (!kvm_read(kvm, p->proc->p_fd, &filed, sizeof(filed)))
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
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
61 if (filed.fd_lastfile == -1)
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62 return false;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
63
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
64 struct cwdinfo cwdi;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
65 if (!kvm_read(kvm, p->proc->p_cwdi, &cwdi, sizeof(cwdi)))
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
66 return false;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
67
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
68 struct fdtab dt;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
69 if (!kvm_read(kvm, filed.fd_dt, &dt, sizeof(dt)))
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
70 return false;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
71
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
72 /* check for corrupted files? */
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
73 if ((unsigned)filed.fd_lastfile >= dt.dt_nfiles || filed.fd_freefile > filed.fd_lastfile + 1)
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
74 return false;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
75
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
76 /* open files */
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
77 std::unique_ptr<fdfile_t*[]> ofiles = nullptr;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
78 {
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
79 ofiles.reset(malloc((filed.fd_lastfile + 1) * sizeof(fdfile_t*)));
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
80 if (!ofiles.get())
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
81 return false;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
82 }
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
83
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
84 if (!kvm_read(kvm, &filed.fd_dt->dt_ff, ofiles.get(), filed.fd_lastfile + 1 * (sizeof(fdfile_t*))))
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
85 return false;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
86
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
87 for (int i = 0; i <= filed.fd_lastfile; i++) {
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
88 if (!ofiles[i])
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
89 continue;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
90 std::string name;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
91 GetFilePath(kvm, ofiles[i], name);
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
92 if (!open_file_proc(p->proc->p_pid, name))
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
93 return false;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
94 }
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
95
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
96 return true;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
97 }
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
98
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
99 bool EnumerateOpenProcesses(process_proc_t process_proc) {
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
100 char errbuf[_POSIX2_LINE_MAX];
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
101 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
102 if (!kernel)
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
103 return false;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
104
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
105 int entries = 0;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
106 struct kinfo_proc* kinfo = kvm_getprocs(kernel, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &nentries);
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
107 if (!kinfo)
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
108 return false;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
109
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
110 for (int i = 0; i < entries; i++)
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
111 if (!process_proc({kinfo[i].p_pid, kinfo[i].p_comm}))
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
112 return false;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
113
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
114 return true;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
115 }
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
116
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
117 bool EnumerateOpenFiles(std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
118 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
119 if (!kernel)
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
120 return false;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
121
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
122 for (const auto& pid : pids) {
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
123 int cnt;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
124 struct kinfo_proc* kinfo = kvm_getprocs(kernel, KERN_PROC_PID, pid, &cnt);
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
125 if (!kinfo)
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
126 return false;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
127
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
128 for (int i = 0; i < cnt; i++) {
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
129 OpenFiles(kernel, kinfo, open_file_proc);
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
130 }
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
131 }
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
132 }
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
133
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
134 }