annotate dep/animone/src/fd/netbsd.cc @ 362:433eeaca3baf

CI/linux: revert change to linuxdeployqt; add qt plugin turns out I'm stupid and linuxdeployqt is insanely broken
author Paper <paper@paper.us.eu.org>
date Mon, 15 Jul 2024 01:28:57 -0400
parents f63dfa309380
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
338
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1 /*
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2 * fd/netbsd.cc: support for NetBSD
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
3 *
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
4 * as with OpenBSD, completely untested
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
5 */
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
6 #include "animone/fd/bsd.h"
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
7 #include "animone.h"
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
8 #include "animone/fd.h"
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
9
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
10 #include <sys/file.h>
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
11 #include <sys/filedesc.h>
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
12 #include <sys/param.h>
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
13 #include <sys/queue.h>
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
14 #include <sys/sysctl.h>
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
15 #include <sys/types.h>
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
16 #include <sys/user.h>
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
17 #include <sys/vnode.h>
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
18
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
19 #include <string>
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
20
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
21 namespace animone::internal::netbsd {
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
22
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
23 static std::string Basename(const std::string& name) {
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
24 size_t s = name.find_last_of('/');
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
25
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
26 if (s == std::string::npos)
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
27 return name;
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
28
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
29 return name.substr(s, name.size());
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
30 }
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
31
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
32 bool GetProcessName(pid_t pid, std::string& name) {
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
33 static const int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
34
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
35 struct kinfo_proc result;
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
36
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
37 size_t length = 1;
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
38 if (sysctl((int*)mib, (sizeof(mib) / sizeof(*mib)) - 1, &result, &length, NULL, 0) == -1)
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
39 return false;
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
40
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
41 name = Basename(result.ki_comm);
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
42
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
43 return true;
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
44 }
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
45
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
46 bool EnumerateOpenProcesses(process_proc_t process_proc) {
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
47 static const int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
48 size_t length = 0;
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
49
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
50 sysctl((int*)mib, (sizeof(mib) / sizeof(*mib)) - 1, NULL, &length, NULL, 0);
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
51
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
52 std::unique_ptr<struct kinfo_proc[]> result;
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
53 result.reset(new struct kinfo_proc[length]);
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
54
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
55 if (!result.get())
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
56 return false;
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
57
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
58 /* actually get our results */
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
59 if (sysctl((const int*)mib, (sizeof(mib) / sizeof(*mib)) - 1, result.get(), &length, NULL, 0) == ENOMEM)
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
60 return false;
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
61
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
62 if (length < sizeof(struct kinfo_proc))
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
63 return false;
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
64
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
65 for (int i = 0; i < length / sizeof(result[0]); i++)
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
66 if (!process_proc({.platform = ExecutablePlatform::Posix, .pid = result[i].ki_pid, .comm = result[i].ki_comm}))
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
67 return false;
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
68
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
69 return true;
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
70 }
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
71
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
72 bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
73 /* I don't think NetBSD actually provides a real API for this. */
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
74 return false;
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
75 }
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
76
f63dfa309380 dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff changeset
77 } // namespace animone::internal::netbsd