comparison dep/animone/src/fd/netbsd.cc @ 338:f63dfa309380

dep/animone: separate *BSD into separate files they are wholly different operating systems with very different kernels on the inside
author Paper <paper@paper.us.eu.org>
date Wed, 19 Jun 2024 13:06:10 -0400
parents
children
comparison
equal deleted inserted replaced
337:a7d4e5107531 338:f63dfa309380
1 /*
2 * fd/netbsd.cc: support for NetBSD
3 *
4 * as with OpenBSD, completely untested
5 */
6 #include "animone/fd/bsd.h"
7 #include "animone.h"
8 #include "animone/fd.h"
9
10 #include <sys/file.h>
11 #include <sys/filedesc.h>
12 #include <sys/param.h>
13 #include <sys/queue.h>
14 #include <sys/sysctl.h>
15 #include <sys/types.h>
16 #include <sys/user.h>
17 #include <sys/vnode.h>
18
19 #include <string>
20
21 namespace animone::internal::netbsd {
22
23 static std::string Basename(const std::string& name) {
24 size_t s = name.find_last_of('/');
25
26 if (s == std::string::npos)
27 return name;
28
29 return name.substr(s, name.size());
30 }
31
32 bool GetProcessName(pid_t pid, std::string& name) {
33 static const int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
34
35 struct kinfo_proc result;
36
37 size_t length = 1;
38 if (sysctl((int*)mib, (sizeof(mib) / sizeof(*mib)) - 1, &result, &length, NULL, 0) == -1)
39 return false;
40
41 name = Basename(result.ki_comm);
42
43 return true;
44 }
45
46 bool EnumerateOpenProcesses(process_proc_t process_proc) {
47 static const int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
48 size_t length = 0;
49
50 sysctl((int*)mib, (sizeof(mib) / sizeof(*mib)) - 1, NULL, &length, NULL, 0);
51
52 std::unique_ptr<struct kinfo_proc[]> result;
53 result.reset(new struct kinfo_proc[length]);
54
55 if (!result.get())
56 return false;
57
58 /* actually get our results */
59 if (sysctl((const int*)mib, (sizeof(mib) / sizeof(*mib)) - 1, result.get(), &length, NULL, 0) == ENOMEM)
60 return false;
61
62 if (length < sizeof(struct kinfo_proc))
63 return false;
64
65 for (int i = 0; i < length / sizeof(result[0]); i++)
66 if (!process_proc({.platform = ExecutablePlatform::Posix, .pid = result[i].ki_pid, .comm = result[i].ki_comm}))
67 return false;
68
69 return true;
70 }
71
72 bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
73 /* I don't think NetBSD actually provides a real API for this. */
74 return false;
75 }
76
77 } // namespace animone::internal::netbsd