Mercurial > minori
annotate dep/animone/src/fd/bsd.cc @ 267:09c5bd74fe93
win32: make builds work again
| author | Paper <paper@paper.us.eu.org> |
|---|---|
| date | Thu, 11 Apr 2024 23:39:18 -0400 |
| parents | 1a6a5d3a94cd |
| children | 0718f538c5f9 |
| rev | line source |
|---|---|
|
266
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
1 #include "animone/fd/bsd.h" |
| 258 | 2 #include "animone.h" |
| 3 #include "animone/fd.h" | |
| 4 | |
| 5 #include <sys/file.h> | |
| 6 #include <sys/filedesc.h> | |
| 7 #include <sys/param.h> | |
| 8 #include <sys/queue.h> | |
| 9 #include <sys/sysctl.h> | |
| 10 #include <sys/types.h> | |
| 11 #include <sys/user.h> | |
| 12 #include <sys/vnode.h> | |
| 13 | |
|
266
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
14 #ifdef HAVE_KVM_GETFILES |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
15 # include <kvm.h> |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
16 #elif defined(LIBUTIL) |
| 258 | 17 # include <libutil.h> |
| 18 #endif | |
| 19 | |
| 20 #include <string> | |
|
266
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
21 #include <iostream> |
| 258 | 22 |
|
266
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
23 namespace animone::internal::bsd { |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
24 |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
25 static std::string Basename(const std::string& name) { |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
26 size_t s = name.find_last_of('/'); |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
27 |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
28 if (s == std::string::npos) |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
29 return name; |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
30 |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
31 return name.substr(s, name.size()); |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
32 } |
| 258 | 33 |
| 34 bool GetProcessName(pid_t pid, std::string& name) { | |
|
266
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
35 #ifdef HAVE_KVM_GETFILES |
| 258 | 36 char errbuf[_POSIX2_LINE_MAX]; |
| 37 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); | |
| 38 if (!kernel) | |
| 39 return false; | |
| 40 | |
| 41 int entries = 0; | |
| 42 struct kinfo_proc* kinfo = kvm_getprocs(kernel, KERN_PROC_PID, pid, &entries); | |
| 43 if (!kinfo) { | |
| 44 kvm_close(kernel); | |
| 45 return false; | |
| 46 } | |
| 47 | |
| 48 if (entries < 1) { | |
| 49 kvm_close(kernel); | |
| 50 return false; | |
| 51 } | |
| 52 | |
|
266
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
53 name = Basename(kinfo[0].ki_paddr->p_comm); |
| 258 | 54 |
| 55 return true; | |
|
266
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
56 #else |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
57 /* use sysctl as a fallback */ |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
58 static const int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid}; |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
59 |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
60 struct kinfo_proc result; |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
61 |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
62 size_t length = 1; |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
63 if (sysctl((int*)mib, (sizeof(mib) / sizeof(*mib)) - 1, &result, &length, NULL, 0) == -1) |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
64 return false; |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
65 |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
66 name = Basename(result.ki_comm); |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
67 |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
68 return true; |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
69 #endif |
| 258 | 70 } |
| 71 | |
| 72 /* Most of the BSDs share the common kvm library, | |
| 73 * so accessing this information can be trivial. | |
| 74 */ | |
| 75 bool EnumerateOpenProcesses(process_proc_t process_proc) { | |
|
266
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
76 #ifdef HAVE_KVM_GETFILES |
| 258 | 77 char errbuf[_POSIX2_LINE_MAX]; |
| 78 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); | |
| 79 if (!kernel) | |
| 80 return false; | |
| 81 | |
| 82 int entries = 0; | |
| 83 struct kinfo_proc* kinfo = kvm_getprocs(kernel, KERN_PROC_ALL, 0, &entries); | |
| 84 if (!kinfo) { | |
| 85 kvm_close(kernel); | |
| 86 return false; | |
| 87 } | |
| 88 | |
| 89 for (int i = 0; i < entries; i++) { | |
|
266
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
90 if (!process_proc({kinfo[i].ki_paddr->p_pid, Basename(kinfo[i].ki_paddr->p_comm)})) { |
| 258 | 91 kvm_close(kernel); |
| 92 return false; | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 kvm_close(kernel); | |
| 97 | |
| 98 return true; | |
|
266
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
99 #else |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
100 /* use sysctl as a fallback */ |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
101 static const int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0}; |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
102 size_t length = 0; |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
103 |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
104 sysctl((int*)mib, (sizeof(mib) / sizeof(*mib)) - 1, NULL, &length, NULL, 0); |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
105 |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
106 std::unique_ptr<struct kinfo_proc[]> result; |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
107 result.reset(new struct kinfo_proc[length]); |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
108 |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
109 if (!result.get()) |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
110 return false; |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
111 |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
112 /* actually get our results */ |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
113 if (sysctl((const int*)mib, (sizeof(mib) / sizeof(*mib)) - 1, result.get(), &length, NULL, 0) == ENOMEM) { |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
114 result.reset(); |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
115 throw std::bad_alloc(); |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
116 } |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
117 |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
118 if (length < sizeof(struct kinfo_proc)) |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
119 return false; |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
120 |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
121 for (int i = 0; i < length / sizeof(result[0]); i++) |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
122 if (!process_proc({result[i].ki_pid, result[i].ki_comm})) |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
123 return false; |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
124 |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
125 return true; |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
126 #endif |
| 258 | 127 } |
| 128 | |
|
266
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
129 bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) { |
|
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
130 #ifdef HAVE_KVM_GETFILES |
| 258 | 131 char errbuf[_POSIX2_LINE_MAX]; |
| 132 kvm_t* kernel = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); | |
| 133 if (!kernel) | |
| 134 return false; | |
| 135 | |
| 136 for (const auto& pid : pids) { | |
| 137 int cnt; | |
| 138 struct kinfo_file* kfile = kvm_getfiles(kernel, KERN_FILE_BYPID, pid, &cnt); | |
| 139 if (!kfile) { | |
| 140 kvm_close(kernel); | |
| 141 return false; | |
| 142 } | |
| 143 | |
| 144 for (int i = 0; i < cnt; i++) { | |
| 145 if (!open_file_proc({pid, kfile[i].kf_path})) { | |
| 146 kvm_close(kernel); | |
| 147 return false; | |
| 148 } | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 kvm_close(kernel); | |
| 153 | |
| 154 return true; | |
| 155 #elif defined(LIBUTIL) | |
| 156 /* does this code even work? */ | |
| 157 for (const auto& pid : pids) { | |
| 158 int cnt; | |
| 159 std::unique_ptr<struct kinfo_file[]> files(kinfo_getfile(pid, &cnt)); | |
| 160 if (!files) | |
| 161 return false; | |
| 162 | |
| 163 for (int i = 0; i < cnt; i++) { | |
| 164 const struct kinfo_file& current = files[i]; | |
| 165 if (current.kf_vnode_type != KF_VTYPE_VREG) | |
| 166 continue; | |
| 167 | |
| 168 if (!open_file_proc({pid, current.kf_path})) | |
| 169 return false; | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 return true; | |
|
266
1a6a5d3a94cd
dep/animone: make bsd.cc and x11.cc actually work
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
174 #else |
| 258 | 175 for (const auto& pid : pids) { |
| 176 int mib[6] = {CTL_KERN, KERN_FILE2, KERN_FILE_BYPID, pid, sizeof(struct kinfo_file), 0}; | |
| 177 | |
| 178 size_t len = 0; | |
| 179 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &len, NULL, 0) == -1) | |
| 180 return false; | |
| 181 | |
| 182 mib[5] = len / sizeof(struct kinfo_file); | |
| 183 | |
| 184 std::unique_ptr<struct kinfo_file[]> buf(new struct kinfo_file[mib[5]]); | |
| 185 if (!buf) | |
| 186 return false; | |
| 187 | |
| 188 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf.get(), &len, NULL, 0) == -1) | |
| 189 return false; | |
| 190 | |
| 191 /* TODO: check kfile[i].ki_ofileflags */ | |
| 192 for (size_t i = 0; i < mib[5]; i++) | |
| 193 if (!open_file_proc({pid, kfile[i].kf_path})) | |
| 194 return false; | |
| 195 } | |
| 196 | |
| 197 return true; | |
| 198 #endif | |
| 199 } | |
| 200 | |
| 201 } // namespace animone::internal::kvm |
