comparison dep/animone/src/fd/freebsd.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/freebsd.cc: support for FreeBSD
3 *
4 * somewhat tested...
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 <libutil.h>
20
21 #include <string>
22
23 namespace animone::internal::freebsd {
24
25 static std::string Basename(const std::string& name) {
26 size_t s = name.find_last_of('/');
27
28 if (s == std::string::npos)
29 return name;
30
31 return name.substr(s, name.size());
32 }
33
34 bool GetProcessName(pid_t pid, std::string& name) {
35 static const int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
36
37 struct kinfo_proc result;
38
39 size_t length = 1;
40 if (sysctl((int*)mib, (sizeof(mib) / sizeof(*mib)) - 1, &result, &length, NULL, 0) == -1)
41 return false;
42
43 name = Basename(result.ki_comm);
44
45 return true;
46 }
47
48 bool EnumerateOpenProcesses(process_proc_t process_proc) {
49 static const int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
50 size_t length = 0;
51
52 sysctl((int*)mib, (sizeof(mib) / sizeof(*mib)) - 1, NULL, &length, NULL, 0);
53
54 std::unique_ptr<struct kinfo_proc[]> result;
55 result.reset(new struct kinfo_proc[length]);
56
57 if (!result.get())
58 return false;
59
60 /* actually get our results */
61 if (sysctl((const int*)mib, (sizeof(mib) / sizeof(*mib)) - 1, result.get(), &length, NULL, 0) == ENOMEM)
62 return false;
63
64 if (length < sizeof(struct kinfo_proc))
65 return false;
66
67 for (int i = 0; i < length / sizeof(result[0]); i++)
68 if (!process_proc({.platform = ExecutablePlatform::Posix, .pid = result[i].ki_pid, .comm = result[i].ki_comm}))
69 return false;
70
71 return true;
72 }
73
74 bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
75 for (const auto& pid : pids) {
76 int cnt;
77 std::unique_ptr<struct kinfo_file[]> files(kinfo_getfile(pid, &cnt));
78 if (!files)
79 return false;
80
81 for (int i = 0; i < cnt; i++) {
82 const struct kinfo_file& current = files[i];
83 if (current.kf_vnode_type != KF_VTYPE_VREG || current.kf_vnode_type != KF_VTYPE_VLNK)
84 continue;
85
86 const int oflags = current.kf_flags & O_ACCMODE;
87 if (oflags == O_WRONLY || oflags == O_RDWR)
88 continue;
89
90 if (!open_file_proc({pid, current.kf_path}))
91 return false;
92 }
93 }
94
95 return true;
96 }
97
98 } // namespace animone::internal::freebsd