annotate dep/animia/src/fd/bsd.cc @ 139:478f3b366199

dep/animia: separate lots of things, use base class for OS stuff
author Paper <mrpapersonic@gmail.com>
date Sun, 12 Nov 2023 16:43:07 -0500
parents 28842a8d0c6b
children 1e696863b54c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
1 /**
139
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
2 * fd/bsd.cpp
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
3 * - this ONLY* supports OS X as of now
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
4 * (*there is some FreeBSD support code)
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 **/
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6 #include <fcntl.h>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7 #include <iostream>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 #include <string>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9 #include <sys/sysctl.h>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 #include <sys/types.h>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11 #include <sys/user.h>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12 #include <unordered_map>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13 #include <vector>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14 #ifdef __FreeBSD__
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15 # include <libutil.h>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16 #elif defined(__APPLE__)
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17 # include <libproc.h>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18 #endif
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
20 namespace animia::internal::unix {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22 /* this is a cleaned up version of a function from... Apple?
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23 ...anyway, what it essentially does is gets the size and stuff from
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24 sysctl() and reserves the space in a vector to store the PIDs */
139
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
25 bool UnixFdTools::GetAllPids(std::set<pid_t>& pids) {
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
26 struct kinfo_proc* result = NULL;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27 size_t length = 0;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
28 static const int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30 /* get appropriate length from sysctl()
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31 note: the reason this isn't checked is actually because this will
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32 *always* return an error on OS X (or... maybe I'm doing it wrong :) ) */
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
33 sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, NULL, &length, NULL, 0);
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
34
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35 result = (struct kinfo_proc*)malloc(length);
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36 if (result == NULL)
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37 return std::vector<int>();
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
38
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 /* TODO: this might actually return ENOMEM if the amount of file handles changes between the
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40 original sysctl() call and this one, which is technically possible */
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41 if (sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, result, &length, NULL, 0) == ENOMEM) {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42 assert(result != NULL);
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 free(result);
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44 throw std::bad_alloc();
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
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 /* add pids to our vector */
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48 pids.reserve(length / sizeof(*result));
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49 for (int i = 0; i < length / sizeof(*result); i++)
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50 pids.push_back(result[i].kp_proc.p_pid);
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
139
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
53 bool UnixFdTools::GetProcessName(pid_t pid, std::string& result) {
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54 #ifdef __FreeBSD__
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 struct kinfo_proc* proc = kinfo_getproc(pid);
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56 if (!proc)
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57 return false;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58 result = proc->ki_comm;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
59
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 /* FreeBSD manpage for kinfo_getproc():
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
61 "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
62 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
63 free(proc);
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
64
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
65 return true;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
66 #elif defined(__APPLE__)
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
67 struct proc_bsdinfo proc;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
68
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
69 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
70 if (st != PROC_PIDTBSDINFO_SIZE)
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
71 return false;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
72
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
73 /* 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
74 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
75 result = proc.pbi_name;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
76 return true;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
77 #endif
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
78 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
79
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
80 /* this only works on OS X :( */
139
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
81 bool UnixFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::tuple<pid_t, std::string>>& files) {
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
82 for (const auto& pid : pids) {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
83 int bufsz = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0);
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
84 if (bufsz == -1)
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
85 return false;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
86
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
87 struct proc_fdinfo* info = (struct proc_fdinfo*)malloc(bufsz);
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
88 if (!info)
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
89 return false;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
90
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
91 proc_pidinfo(pid, PROC_PIDLISTFDS, 0, info, bufsz);
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
92
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
93 for (int i = 0; i < bufsz / sizeof(info[0]); i++) {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
94 if (info[i].proc_fdtype == PROX_FDTYPE_VNODE) {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
95 struct vnode_fdinfowithpath vnodeInfo;
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 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
98 if (sz != PROC_PIDFDVNODEPATHINFO_SIZE)
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 /* this doesn't work!
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
102 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
103 continue;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
104 */
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
105
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
106 files.push_back({pid, vnodeInfo.pvip.vip_path});
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
107 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
108 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
109 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
110 return true;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
111 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
112
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
113 } // namespace animia::internal::unix