comparison 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
comparison
equal deleted inserted replaced
96:bd68e4393e6f 97:18979b066284
4 * - this also works for OS X :) 4 * - this also works for OS X :)
5 * more technical details: this is essentially a wrapper 5 * more technical details: this is essentially a wrapper
6 * around the very C-like BSD system functions that are... 6 * around the very C-like BSD system functions that are...
7 * kind of unnatural to use in modern C++. 7 * kind of unnatural to use in modern C++.
8 **/ 8 **/
9 #include "bsd.h"
10 #include "os.h"
9 #include <assert.h> 11 #include <assert.h>
10 #include <fcntl.h> 12 #include <fcntl.h>
11 #include <iostream> 13 #include <iostream>
12 #include <string> 14 #include <string>
13 #include <sys/sysctl.h> 15 #include <sys/sysctl.h>
54 ret.push_back(result[i].kp_proc.p_pid); 56 ret.push_back(result[i].kp_proc.p_pid);
55 57
56 return ret; 58 return ret;
57 } 59 }
58 60
59 std::string get_process_name(int pid) { 61 std::string get_process_name(const int pid) {
60 std::string ret; 62 std::string ret;
61 #ifdef __FreeBSD__ 63 #ifdef __FreeBSD__
62 struct kinfo_proc* proc = kinfo_getproc(pid); 64 struct kinfo_proc* proc = kinfo_getproc(pid);
63 if (!proc) 65 if (!proc)
64 return ""; 66 return "";
73 ret = proc.pbi_comm; 75 ret = proc.pbi_comm;
74 #endif 76 #endif
75 return ret; 77 return ret;
76 } 78 }
77 79
78 std::vector<std::string> get_open_files(int pid) { 80 std::vector<std::string> get_open_files(const int pid) {
79 /* note: this is OS X only right now. eventually, I'll find a way 81 /* note: this is OS X only right now. eventually, I'll find a way
80 to do this in FreeBSD, OpenBSD and the like */ 82 to do this in FreeBSD, OpenBSD and the like */
81 std::vector<std::string> ret; 83 std::vector<std::string> ret;
82 84
83 if (pid == 0) 85 if (pid == 0)
101 103
102 int sz = proc_pidfdinfo(pid, info[i].proc_fd, PROC_PIDFDVNODEPATHINFO, &vnodeInfo, PROC_PIDFDVNODEPATHINFO_SIZE); 104 int sz = proc_pidfdinfo(pid, info[i].proc_fd, PROC_PIDFDVNODEPATHINFO, &vnodeInfo, PROC_PIDFDVNODEPATHINFO_SIZE);
103 if (sz != PROC_PIDFDVNODEPATHINFO_SIZE) 105 if (sz != PROC_PIDFDVNODEPATHINFO_SIZE)
104 continue; 106 continue;
105 107
106 if (vnodeInfo.pfi.fi_openflags & O_WRONLY || vnodeInfo.pfi.fi_openflags & O_RDWR) 108 /* I *think* this is correct. For some reason, with VLC on macOS this thinks files are read/write,
109 which is totally unnecessary and even harmful. */
110 if ((vnodeInfo.pfi.fi_status & O_ACCMODE) == O_WRONLY)
107 continue; 111 continue;
108 112
109 ret.push_back(vnodeInfo.pvip.vip_path); 113 ret.push_back(vnodeInfo.pvip.vip_path);
110 } 114 }
111 } 115 }
112 return ret; 116 return ret;
113 } 117 }
118
119 std::vector<std::string> filter_system_files(const std::vector<std::string>& in) {
120 #ifdef ON_OSX
121 std::vector<std::string> ret;
122 for (const auto& str : in)
123 /* these are some places nobody would ever want to store media files. */
124 if (str.find("/Library") && str.find("/System") && str.find("/Applications") &&
125 str.find("/dev") && str.find("/private"))
126 ret.push_back(str);
127 return ret;
128 #else
129 return in;
130 #endif
131 }
132
114 133
115 std::unordered_map<int, std::vector<std::string>> get_all_open_files() { 134 std::unordered_map<int, std::vector<std::string>> get_all_open_files() {
116 std::unordered_map<int, std::vector<std::string>> map; 135 std::unordered_map<int, std::vector<std::string>> map;
117 std::vector<int> pids = get_all_pids(); 136 std::vector<int> pids = get_all_pids();
118 for (int i : pids) { 137 for (int i : pids) {