annotate dep/animia/src/fd/libutil.cc @ 233:0a5b6a088886

dep/animia: fix proc readlink
author Paper <mrpapersonic@gmail.com>
date Mon, 15 Jan 2024 08:10:58 -0500
parents bc1ae1810855
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
166
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
1 /* This file uses the FreeBSD-specific libprocstat
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
2 */
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
3
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
4 #include "animia/fd/libutil.h"
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
5 #include "animia/fd.h"
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
6 #include "animia.h"
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
7
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
8 #include <sys/types.h>
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
9 #include <sys/sysctl.h>
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
10 #include <sys/user.h>
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
11 #include <libutil.h>
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
12
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
13 #include <memory>
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
14
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
15 namespace animia::internal::libutil {
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
16
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
17 static bool IsSystemFile(const std::string& file) {
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
18 return (!file.find("/usr") || !file.find("/lib") || !file.find("/dev") ||
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
19 !file.find("/proc"));
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
20 }
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
21
198
bc1ae1810855 dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents: 166
diff changeset
22 bool EnumerateOpenProcesses(process_proc_t process_proc) {
166
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
23 static const int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
24 size_t length = 0;
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
25
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
26 sysctl((int*)mib, (sizeof(mib) / sizeof(*mib)) - 1, NULL, &length, NULL, 0);
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
27
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
28 std::unique_ptr<struct kinfo_proc[]> result;
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
29 result.reset(new struct kinfo_proc[length]);
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
30
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
31 if (!result.get())
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
32 return false;
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
33
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
34 /* actually get our results */
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
35 if (sysctl((const int*)mib, (sizeof(mib) / sizeof(*mib)) - 1, result.get(), &length, NULL, 0) == ENOMEM) {
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
36 result.reset();
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
37 throw std::bad_alloc();
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
38 }
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
39
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
40 if (length < sizeof(struct kinfo_proc))
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
41 return false;
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
42
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
43 for (int i = 0; i < length / sizeof(result[0]); i++)
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
44 if (!process_proc({result[i].ki_pid, result[i].ki_comm}))
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
45 return false;
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
46
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
47 return true;
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
48 }
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
49
198
bc1ae1810855 dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents: 166
diff changeset
50 bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
166
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
51 for (const auto& pid : pids) {
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
52 int cnt;
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
53 std::unique_ptr<struct kinfo_file[]> files;
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
54 files.reset(kinfo_getfile(pid, &cnt));
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
55 if (!files.get())
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
56 return false;
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
57
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
58 for (int i = 0; i < cnt; i++) {
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
59 const struct kinfo_file& current = files[i];
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
60 if (current.kf_vnode_type != KF_VTYPE_VREG)
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
61 continue;
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
62
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
63 if (!open_file_proc({pid, current.kf_path}))
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
64 return false;
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
65 }
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
66 }
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
67
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
68 return true;
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
69 }
54c5d80a737e dep/animia: add libutil method
paper@DavesDouble.local
parents:
diff changeset
70
198
bc1ae1810855 dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents: 166
diff changeset
71 }