Mercurial > minori
annotate dep/animia/src/fd/bsd.cc @ 146:d8a61e7e2a36
dep/animia: move fd stuff to a new fd.cc, don't force the user
to include windows.h
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Mon, 13 Nov 2023 13:52:58 -0500 |
parents | e6668085e24d |
children | aa4df5a84338 |
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 **/ |
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" |
4e750f6545cf
dep/animia: don't forget to include this stuff
Paper <mrpapersonic@gmail.com>
parents:
140
diff
changeset
|
7 |
140
1e696863b54c
dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents:
139
diff
changeset
|
8 #include <unordered_map> |
1e696863b54c
dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents:
139
diff
changeset
|
9 #include <vector> |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
10 #include <iostream> |
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 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
26 /* this is a cleaned up version of a function from... Apple? |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
27 ...anyway, what it essentially does is gets the size and stuff from |
146
d8a61e7e2a36
dep/animia: move fd stuff to a new fd.cc, don't force the user
Paper <mrpapersonic@gmail.com>
parents:
144
diff
changeset
|
28 sysctl() and reserves the space in a vector to store the PIDs |
d8a61e7e2a36
dep/animia: move fd stuff to a new fd.cc, don't force the user
Paper <mrpapersonic@gmail.com>
parents:
144
diff
changeset
|
29 |
d8a61e7e2a36
dep/animia: move fd stuff to a new fd.cc, don't force the user
Paper <mrpapersonic@gmail.com>
parents:
144
diff
changeset
|
30 TODO: https://kaashif.co.uk/2015/06/18/how-to-get-a-list-of-processes-on-openbsd-in-c/ */ |
139
478f3b366199
dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents:
138
diff
changeset
|
31 bool UnixFdTools::GetAllPids(std::set<pid_t>& pids) { |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
32 struct kinfo_proc* result = NULL; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
33 size_t length = 0; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
34 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
|
35 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
36 /* get appropriate length from sysctl() |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
37 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
|
38 *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
|
39 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
|
40 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
41 result = (struct kinfo_proc*)malloc(length); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
42 if (result == NULL) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
43 return std::vector<int>(); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
44 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
45 /* 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
|
46 original sysctl() call and this one, which is technically possible */ |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
47 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
|
48 assert(result != NULL); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
49 free(result); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
50 throw std::bad_alloc(); |
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 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
53 /* add pids to our vector */ |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
54 pids.reserve(length / sizeof(*result)); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
55 for (int i = 0; i < length / sizeof(*result); i++) |
144
e6668085e24d
dep/animia: fix many bugs in the linux code
Paper <mrpapersonic@gmail.com>
parents:
143
diff
changeset
|
56 pids.insert(result[i].kp_proc.p_pid); |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
57 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
58 |
139
478f3b366199
dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents:
138
diff
changeset
|
59 bool UnixFdTools::GetProcessName(pid_t pid, std::string& result) { |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
60 #ifdef __FreeBSD__ |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
61 struct kinfo_proc* proc = kinfo_getproc(pid); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
62 if (!proc) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
63 return false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
64 result = proc->ki_comm; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
65 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
66 /* FreeBSD manpage for kinfo_getproc(): |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
67 "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
|
68 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
|
69 free(proc); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
70 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
71 return true; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
72 #elif defined(__APPLE__) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
73 struct proc_bsdinfo proc; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
74 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
75 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
|
76 if (st != PROC_PIDTBSDINFO_SIZE) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
77 return false; |
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 /* 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
|
80 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
|
81 result = proc.pbi_name; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
82 return true; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
83 #endif |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
84 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
85 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
86 /* 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
|
87 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
|
88 for (const auto& pid : pids) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
89 int bufsz = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
90 if (bufsz == -1) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
91 return false; |
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 struct proc_fdinfo* info = (struct proc_fdinfo*)malloc(bufsz); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
94 if (!info) |
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 proc_pidinfo(pid, PROC_PIDLISTFDS, 0, info, bufsz); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
98 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
99 for (int i = 0; i < bufsz / sizeof(info[0]); i++) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
100 if (info[i].proc_fdtype == PROX_FDTYPE_VNODE) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
101 struct vnode_fdinfowithpath vnodeInfo; |
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 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
|
104 if (sz != PROC_PIDFDVNODEPATHINFO_SIZE) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
105 return false; |
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 /* this doesn't work! |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
108 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
|
109 continue; |
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 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
112 files.push_back({pid, vnodeInfo.pvip.vip_path}); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
113 } |
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 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
116 return true; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
117 } |
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 } // namespace animia::internal::unix |