annotate dep/animone/src/fd/xnu.cc @ 299:246017a7907a

dep/animone: clean up OS X code GetProcessName() really belongs in fd.cc after removing the stupid unnecessary LaunchServices code that was stolen from... some library :)
author Paper <paper@paper.us.eu.org>
date Mon, 13 May 2024 14:15:47 -0400
parents 0718f538c5f9
children b1f625b0227c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1 #include "animone/fd/xnu.h"
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2 #include "animone.h"
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
3 #include "animone/util/osx.h"
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
4
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
5 #include <cassert>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
6 #include <memory>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
7 #include <string>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
8 #include <unordered_map>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
9 #include <vector>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
10
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
11 #include <fcntl.h>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
12 #include <libproc.h>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
13 #include <sys/sysctl.h>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
14 #include <sys/types.h>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
15 #include <sys/user.h>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
16
270
0718f538c5f9 dep/animone: filter open files by access mode
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
17 /* you may be asking: WTF is FWRITE?
0718f538c5f9 dep/animone: filter open files by access mode
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
18 * well, from bsd/sys/fcntl.h in the XNU kernel:
0718f538c5f9 dep/animone: filter open files by access mode
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
19 *
0718f538c5f9 dep/animone: filter open files by access mode
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
20 * Kernel encoding of open mode; separate read and write bits that are
0718f538c5f9 dep/animone: filter open files by access mode
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
21 * independently testable: 1 greater than [O_RDONLY and O_WRONLY].
0718f538c5f9 dep/animone: filter open files by access mode
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
22 *
0718f538c5f9 dep/animone: filter open files by access mode
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
23 * It's just how the kernel defines write mode.
0718f538c5f9 dep/animone: filter open files by access mode
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
24 */
0718f538c5f9 dep/animone: filter open files by access mode
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
25 #ifndef FWRITE
0718f538c5f9 dep/animone: filter open files by access mode
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
26 #define FWRITE 0x0002
0718f538c5f9 dep/animone: filter open files by access mode
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
27 #endif
0718f538c5f9 dep/animone: filter open files by access mode
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
28
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
29 namespace animone::internal::xnu {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
30
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
31 bool EnumerateOpenProcesses(process_proc_t process_proc) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
32 size_t pids_size = 256;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
33 std::unique_ptr<pid_t[]> pids;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
34
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
35 int returned_size = 0;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
36 do {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
37 pids.reset(new pid_t[pids_size *= 2]);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
38 returned_size = proc_listpids(PROC_ALL_PIDS, 0, pids.get(), pids_size * sizeof(pid_t));
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
39 if (returned_size == -1)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
40 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
41 } while ((pids_size * sizeof(size_t)) < returned_size);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
42
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
43 for (int i = 0; i < pids_size; i++) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
44 std::string result;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
45 osx::util::GetProcessName(pids[i], result);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
46 if (!process_proc({pids[i], result}))
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
47 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
48 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
49
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
50 return true;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
51 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
52
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
53 bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
54 if (!open_file_proc)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
55 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
56
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
57 for (const auto& pid : pids) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
58 const int bufsz = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
59 if (bufsz < 0)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
60 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
61
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
62 const size_t info_len = bufsz / sizeof(struct proc_fdinfo);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
63 if (info_len < 1)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
64 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
65
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
66 std::unique_ptr<struct proc_fdinfo[]> info(new struct proc_fdinfo[info_len]);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
67 if (!info)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
68 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
69
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
70 proc_pidinfo(pid, PROC_PIDLISTFDS, 0, info.get(), bufsz);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
71
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
72 for (size_t i = 0; i < info_len; i++) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
73 if (info[i].proc_fdtype == PROX_FDTYPE_VNODE) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
74 struct vnode_fdinfowithpath vnodeInfo;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
75
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
76 int sz = proc_pidfdinfo(pid, info[i].proc_fd, PROC_PIDFDVNODEPATHINFO, &vnodeInfo,
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
77 PROC_PIDFDVNODEPATHINFO_SIZE);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
78 if (sz != PROC_PIDFDVNODEPATHINFO_SIZE)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
79 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
80
270
0718f538c5f9 dep/animone: filter open files by access mode
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
81 /* why would a media player open a file in write mode? */
0718f538c5f9 dep/animone: filter open files by access mode
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
82 if (vnodeInfo.pfi.fi_openflags & FWRITE)
0718f538c5f9 dep/animone: filter open files by access mode
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
83 continue;
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
84
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
85 if (!open_file_proc({pid, vnodeInfo.pvip.vip_path}))
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
86 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
87 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
88 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
89 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
90
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
91 return true;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
92 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
93
299
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
94 static bool GetProcessNameFromProcPidPath(pid_t pid, std::string& result) {
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
95 result.assign(PROC_PIDPATHINFO_MAXSIZE, '\0');
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
96
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
97 int ret = proc_pidpath(pid, result.data(), result.size() * sizeof(char));
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
98 if (ret <= 0)
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
99 return false;
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
100
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
101 /* find the last slash, if there's none, we're done here */
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
102 size_t last_slash = result.rfind('/');
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
103 if (last_slash == std::string::npos)
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
104 return true;
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
105
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
106 result.erase(0, last_slash + 1);
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
107 return true;
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
108 }
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
109
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
110 static bool GetProcessNameFromProcName(pid_t pid, std::string& result) {
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
111 result.assign(2 * MAXCOMLEN, '\0');
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
112
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
113 int size = proc_name(pid, &result.front(), result.length());
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
114
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
115 /* if size is MAXCOMLEN or 2 * MAXCOMLEN, assume
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
116 * this method won't work and our result is truncated */
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
117 if (size <= 0 || size == MAXCOMLEN || size == 2 * MAXCOMLEN)
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
118 return false;
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
119
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
120 result.resize(size);
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
121 return true;
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
122 }
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
123
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
124 bool GetProcessName(pid_t pid, std::string& result) {
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
125 if (GetProcessNameFromProcName(pid, result))
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
126 return true;
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
127
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
128 if (GetProcessNameFromProcPidPath(pid, result))
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
129 return true;
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
130
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
131 return false;
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
132 }
246017a7907a dep/animone: clean up OS X code
Paper <paper@paper.us.eu.org>
parents: 270
diff changeset
133
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
134 } // namespace animone::internal::xnu