Mercurial > minori
annotate dep/animia/src/fd/bsd.cc @ 152:8700806c2cc2
dep/animia: awesome new breaking changes!
I'm so tired
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Wed, 15 Nov 2023 02:34:59 -0500 |
parents | 54744a48a7d7 |
children |
rev | line source |
---|---|
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
1 /* |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
2 ** fd/bsd.cpp |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
3 ** - this ONLY* supports OS X as of now |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
4 ** (*there is some FreeBSD support code) |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
5 */ |
143
4e750f6545cf
dep/animia: don't forget to include this stuff
Paper <mrpapersonic@gmail.com>
parents:
140
diff
changeset
|
6 #include "animia/fd/bsd.h" |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
7 #include "animia.h" |
143
4e750f6545cf
dep/animia: don't forget to include this stuff
Paper <mrpapersonic@gmail.com>
parents:
140
diff
changeset
|
8 |
140
1e696863b54c
dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents:
139
diff
changeset
|
9 #include <unordered_map> |
1e696863b54c
dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents:
139
diff
changeset
|
10 #include <vector> |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
11 #include <string> |
140
1e696863b54c
dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents:
139
diff
changeset
|
12 |
1e696863b54c
dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents:
139
diff
changeset
|
13 #include <fcntl.h> |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
14 #include <sys/sysctl.h> |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
15 #include <sys/types.h> |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
16 #include <sys/user.h> |
140
1e696863b54c
dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents:
139
diff
changeset
|
17 |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
18 #ifdef __FreeBSD__ |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
19 # include <libutil.h> |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
20 #elif defined(__APPLE__) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
21 # include <libproc.h> |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
22 #endif |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
23 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
24 namespace animia::internal::unix { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
25 |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
26 static bool GetProcessName(pid_t pid, std::string& result) { |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
27 #ifdef __FreeBSD__ |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
28 struct kinfo_proc* proc = kinfo_getproc(pid); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
29 if (!proc) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
30 return false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
31 result = proc->ki_comm; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
32 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
33 /* FreeBSD manpage for kinfo_getproc(): |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
34 "The pointer was obtained by an internal call to malloc(3) and |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
35 must be freed by the caller with a call to free(3)." */ |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
36 free(proc); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
37 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
38 return true; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
39 #elif defined(__APPLE__) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
40 struct proc_bsdinfo proc; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
41 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
42 int st = proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &proc, PROC_PIDTBSDINFO_SIZE); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
43 if (st != PROC_PIDTBSDINFO_SIZE) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
44 return false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
45 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
46 /* fixme: is this right? pbi_comm is an alternative, but it reduces the string size to |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
47 16 chars. does pbi_name do the same, or is it different? */ |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
48 result = proc.pbi_name; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
49 return true; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
50 #endif |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
51 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
52 |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
53 /* this is a cleaned up version of a function from... Apple? |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
54 ...anyway, what it essentially does is gets the size and stuff from |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
55 sysctl() and reserves the space in a vector to store the PIDs |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
56 |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
57 TODO: https://kaashif.co.uk/2015/06/18/how-to-get-a-list-of-processes-on-openbsd-in-c/ */ |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
58 bool UnixFdTools::EnumerateOpenProcesses(process_proc_t process_proc) { |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
59 struct kinfo_proc* result = NULL; |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
60 size_t length = 0; |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
61 static const int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0}; |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
62 |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
63 /* get appropriate length from sysctl() |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
64 note: the reason this isn't checked is actually because this will |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
65 *always* return an error on OS X (or... maybe I'm doing it wrong :) ) */ |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
66 sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, NULL, &length, NULL, 0); |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
67 |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
68 result = (struct kinfo_proc*)malloc(length); |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
69 if (result == NULL) |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
70 return std::vector<int>(); |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
71 |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
72 /* TODO: this might actually return ENOMEM if the amount of file handles changes between the |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
73 original sysctl() call and this one, which is technically possible */ |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
74 if (sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, result, &length, NULL, 0) == ENOMEM) { |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
75 assert(result != NULL); |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
76 free(result); |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
77 throw std::bad_alloc(); |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
78 } |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
79 |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
80 for (int i = 0; i < length / sizeof(*result); i++) { |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
81 const pid_t pid = result[i].kp_proc.p_pid; |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
82 if (!process_proc({pid, GetProcessName(pid)})) |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
83 return false; |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
84 } |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
85 } |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
86 |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
87 /* this only works on OS X :( */ |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
88 bool UnixFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) { |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
89 if (!open_file_proc) |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
90 return false; |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
91 |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
92 for (const auto& pid : pids) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
93 int bufsz = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
94 if (bufsz == -1) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
95 return false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
96 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
97 struct proc_fdinfo* info = (struct proc_fdinfo*)malloc(bufsz); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
98 if (!info) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
99 return false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
100 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
101 proc_pidinfo(pid, PROC_PIDLISTFDS, 0, info, bufsz); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
102 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
103 for (int i = 0; i < bufsz / sizeof(info[0]); i++) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
104 if (info[i].proc_fdtype == PROX_FDTYPE_VNODE) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
105 struct vnode_fdinfowithpath vnodeInfo; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
106 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
107 int sz = proc_pidfdinfo(pid, info[i].proc_fd, PROC_PIDFDVNODEPATHINFO, &vnodeInfo, PROC_PIDFDVNODEPATHINFO_SIZE); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
108 if (sz != PROC_PIDFDVNODEPATHINFO_SIZE) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
109 return false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
110 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
111 /* this doesn't work! |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
112 if (vnodeInfo.pfi.fi_openflags & O_WRONLY || vnodeInfo.pfi.fi_openflags & O_RDWR) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
113 continue; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
114 */ |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
115 |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
116 if (!open_file_proc({pid, vnodeInfo.pvip.vip_path})) |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
117 return false; |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
118 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
119 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
120 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
121 return true; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
122 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
123 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
124 } // namespace animia::internal::unix |