Mercurial > minori
annotate dep/animone/src/fd/freebsd.cc @ 345:d085c05247bd
build: fix windows build
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Thu, 20 Jun 2024 07:39:01 -0400 |
parents | f63dfa309380 |
children |
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/freebsd.cc: support for FreeBSD |
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 * somewhat tested... |
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 <libutil.h> |
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 #include <string> |
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 namespace animone::internal::freebsd { |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
24 |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
25 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
|
26 size_t s = name.find_last_of('/'); |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
27 |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
28 if (s == std::string::npos) |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
29 return name; |
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 return name.substr(s, name.size()); |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
32 } |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
33 |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
34 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
|
35 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
|
36 |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
37 struct kinfo_proc result; |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
38 |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
39 size_t length = 1; |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
40 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
|
41 return false; |
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 name = Basename(result.ki_comm); |
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 return true; |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
46 } |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
47 |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
48 bool EnumerateOpenProcesses(process_proc_t process_proc) { |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
49 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
|
50 size_t length = 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 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
|
53 |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
54 std::unique_ptr<struct kinfo_proc[]> result; |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
55 result.reset(new struct kinfo_proc[length]); |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
56 |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
57 if (!result.get()) |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
58 return false; |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
59 |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
60 /* actually get our results */ |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
61 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
|
62 return false; |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
63 |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
64 if (length < sizeof(struct kinfo_proc)) |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
65 return false; |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
66 |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
67 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
|
68 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
|
69 return false; |
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 return true; |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
72 } |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
73 |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
74 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
|
75 for (const auto& pid : pids) { |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
76 int cnt; |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
77 std::unique_ptr<struct kinfo_file[]> files(kinfo_getfile(pid, &cnt)); |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
78 if (!files) |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
79 return false; |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
80 |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
81 for (int i = 0; i < cnt; i++) { |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
82 const struct kinfo_file& current = files[i]; |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
83 if (current.kf_vnode_type != KF_VTYPE_VREG || current.kf_vnode_type != KF_VTYPE_VLNK) |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
84 continue; |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
85 |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
86 const int oflags = current.kf_flags & O_ACCMODE; |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
87 if (oflags == O_WRONLY || oflags == O_RDWR) |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
88 continue; |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
89 |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
90 if (!open_file_proc({pid, current.kf_path})) |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
91 return false; |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
92 } |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
93 } |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
94 |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
95 return true; |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
96 } |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
97 |
f63dfa309380
dep/animone: separate *BSD into separate files
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
98 } // namespace animone::internal::freebsd |