Mercurial > minori
annotate dep/animia/src/bsd.cpp @ 97:18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Thu, 02 Nov 2023 13:14:15 -0400 |
parents | c912128af0eb |
children | f5940a575d83 |
rev | line source |
---|---|
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
1 /** |
62 | 2 * bsd.cpp |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
3 * - provides support for most* versions of BSD |
62 | 4 * - this also works for OS X :) |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
5 * more technical details: this is essentially a wrapper |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
6 * around the very C-like BSD system functions that are... |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
7 * kind of unnatural to use in modern C++. |
62 | 8 **/ |
97
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
9 #include "bsd.h" |
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
10 #include "os.h" |
86 | 11 #include <assert.h> |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
12 #include <fcntl.h> |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
13 #include <iostream> |
62 | 14 #include <string> |
15 #include <sys/sysctl.h> | |
16 #include <sys/types.h> | |
17 #include <sys/user.h> | |
18 #include <unordered_map> | |
19 #include <vector> | |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
20 #ifdef __FreeBSD__ |
62 | 21 # include <libutil.h> |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
22 #elif defined(__APPLE__) |
62 | 23 # include <libproc.h> |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
24 #endif |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
25 |
84
eab9e623eb84
dep/animia: update from upstream
Paper <mrpapersonic@gmail.com>
parents:
78
diff
changeset
|
26 namespace Animia { namespace Unix { |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
27 |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
28 /* this is a cleaned up version of a function from... Apple? |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
29 ...anyway, what it essentially does is gets the size and stuff from |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
30 sysctl() and reserves the space in a vector to store the PIDs */ |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
31 std::vector<int> get_all_pids() { |
62 | 32 std::vector<int> ret; |
33 struct kinfo_proc* result = NULL; | |
34 size_t length = 0; | |
35 static const int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0}; | |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
36 |
62 | 37 /* get appropriate length from sysctl() |
38 note: the reason this isn't checked is actually because this will | |
39 *always* return an error on OS X (or... maybe I'm doing it wrong :) ) */ | |
40 sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, NULL, &length, NULL, 0); | |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
41 |
62 | 42 result = (struct kinfo_proc*)malloc(length); |
43 if (result == NULL) | |
44 return std::vector<int>(); | |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
45 |
62 | 46 /* actually get our results */ |
47 if (sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, result, &length, NULL, 0) == ENOMEM) { | |
48 assert(result != NULL); | |
49 free(result); | |
50 throw std::bad_alloc(); | |
51 } | |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
52 |
62 | 53 /* add pids to our vector */ |
54 ret.reserve(length / sizeof(*result)); | |
55 for (int i = 0; i < length / sizeof(*result); i++) | |
56 ret.push_back(result[i].kp_proc.p_pid); | |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
57 |
62 | 58 return ret; |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
59 } |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
60 |
97
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
61 std::string get_process_name(const int pid) { |
62 | 62 std::string ret; |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
63 #ifdef __FreeBSD__ |
62 | 64 struct kinfo_proc* proc = kinfo_getproc(pid); |
65 if (!proc) | |
66 return ""; | |
67 ret = proc->ki_comm; | |
68 free(proc); | |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
69 #elif defined(__APPLE__) |
62 | 70 struct proc_bsdinfo proc; |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
71 |
62 | 72 int st = proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &proc, PROC_PIDTBSDINFO_SIZE); |
73 if (st != PROC_PIDTBSDINFO_SIZE) | |
74 return ""; | |
75 ret = proc.pbi_comm; | |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
76 #endif |
62 | 77 return ret; |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
78 } |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
79 |
97
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
80 std::vector<std::string> get_open_files(const int pid) { |
62 | 81 /* note: this is OS X only right now. eventually, I'll find a way |
82 to do this in FreeBSD, OpenBSD and the like */ | |
83 std::vector<std::string> ret; | |
78
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
84 |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
85 if (pid == 0) |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
86 return ret; |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
87 |
62 | 88 int bufsz = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0); |
78
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
89 if (bufsz == -1) |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
90 return ret; |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
91 |
62 | 92 struct proc_fdinfo* info = (struct proc_fdinfo*)malloc(bufsz); |
78
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
93 if (!info) |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
94 return ret; |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
95 |
62 | 96 proc_pidinfo(pid, PROC_PIDLISTFDS, 0, info, bufsz); |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
97 |
62 | 98 // iterate over stuff |
99 ret.reserve(bufsz / sizeof(info[0])); | |
100 for (int i = 0; i < bufsz / sizeof(info[0]); i++) { | |
101 if (info[i].proc_fdtype == PROX_FDTYPE_VNODE) { | |
102 struct vnode_fdinfowithpath vnodeInfo; | |
78
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
103 |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
104 int sz = proc_pidfdinfo(pid, info[i].proc_fd, PROC_PIDFDVNODEPATHINFO, &vnodeInfo, PROC_PIDFDVNODEPATHINFO_SIZE); |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
105 if (sz != PROC_PIDFDVNODEPATHINFO_SIZE) |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
106 continue; |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
107 |
97
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
108 /* I *think* this is correct. For some reason, with VLC on macOS this thinks files are read/write, |
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
109 which is totally unnecessary and even harmful. */ |
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
110 if ((vnodeInfo.pfi.fi_status & O_ACCMODE) == O_WRONLY) |
78
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
111 continue; |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
112 |
62 | 113 ret.push_back(vnodeInfo.pvip.vip_path); |
114 } | |
115 } | |
116 return ret; | |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
117 } |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
118 |
97
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
119 std::vector<std::string> filter_system_files(const std::vector<std::string>& in) { |
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
120 #ifdef ON_OSX |
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
121 std::vector<std::string> ret; |
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
122 for (const auto& str : in) |
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
123 /* these are some places nobody would ever want to store media files. */ |
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
124 if (str.find("/Library") && str.find("/System") && str.find("/Applications") && |
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
125 str.find("/dev") && str.find("/private")) |
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
126 ret.push_back(str); |
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
127 return ret; |
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
128 #else |
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
129 return in; |
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
130 #endif |
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
131 } |
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
132 |
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
133 |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
134 std::unordered_map<int, std::vector<std::string>> get_all_open_files() { |
62 | 135 std::unordered_map<int, std::vector<std::string>> map; |
136 std::vector<int> pids = get_all_pids(); | |
137 for (int i : pids) { | |
138 map[i] = get_open_files(i); | |
139 } | |
140 return map; | |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
141 } |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
142 |
84
eab9e623eb84
dep/animia: update from upstream
Paper <mrpapersonic@gmail.com>
parents:
78
diff
changeset
|
143 } // namespace Unix |
eab9e623eb84
dep/animia: update from upstream
Paper <mrpapersonic@gmail.com>
parents:
78
diff
changeset
|
144 } // namespace Animia |