Mercurial > minori
comparison dep/animia/src/bsd.cpp @ 56:6ff7aabeb9d7
deps: add animia for open files detection
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Thu, 28 Sep 2023 12:35:21 -0400 |
| parents | |
| children | 4c6dd5999b39 |
comparison
equal
deleted
inserted
replaced
| 54:466ac9870df9 | 56:6ff7aabeb9d7 |
|---|---|
| 1 /** | |
| 2 * bsd.cpp | |
| 3 * - provides support for most* versions of BSD | |
| 4 * - this *should* also work for OS X | |
| 5 * more technical details: this is essentially a wrapper | |
| 6 * around the very C-like BSD system functions that are... | |
| 7 * kind of unnatural to use in modern C++. | |
| 8 **/ | |
| 9 #include <vector> | |
| 10 #include <string> | |
| 11 #include <unordered_map> | |
| 12 #include <sys/types.h> | |
| 13 #include <sys/sysctl.h> | |
| 14 #include <sys/user.h> | |
| 15 #include <fcntl.h> | |
| 16 #include <iostream> | |
| 17 #ifdef __FreeBSD__ | |
| 18 #include <libutil.h> | |
| 19 #elif defined(__APPLE__) | |
| 20 #include <libproc.h> | |
| 21 #endif | |
| 22 | |
| 23 namespace Animia::Unix { | |
| 24 | |
| 25 /* this is a cleaned up version of a function from... Apple? | |
| 26 ...anyway, what it essentially does is gets the size and stuff from | |
| 27 sysctl() and reserves the space in a vector to store the PIDs */ | |
| 28 std::vector<int> get_all_pids() { | |
| 29 std::vector<int> ret; | |
| 30 struct kinfo_proc* result = NULL; | |
| 31 size_t length = 0; | |
| 32 static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 }; | |
| 33 | |
| 34 /* get appropriate length from sysctl() */ | |
| 35 sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, NULL, &length, NULL, 0); | |
| 36 | |
| 37 result = (struct kinfo_proc*)malloc(length); | |
| 38 if (result == NULL) | |
| 39 return std::vector<int>(); | |
| 40 | |
| 41 /* actually get our results */ | |
| 42 if (sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, result, &length, NULL, 0) == ENOMEM) { | |
| 43 assert(result != NULL); | |
| 44 free(result); | |
| 45 throw std::bad_alloc(); | |
| 46 } | |
| 47 | |
| 48 /* add pids to our vector */ | |
| 49 ret.reserve(length/sizeof(*result)); | |
| 50 for (int i = 0; i < length/sizeof(*result); i++) | |
| 51 ret.push_back(result[i].kp_proc.p_pid); | |
| 52 | |
| 53 return ret; | |
| 54 } | |
| 55 | |
| 56 std::string get_process_name(int pid) { | |
| 57 std::string ret; | |
| 58 #ifdef __FreeBSD__ | |
| 59 struct kinfo_proc* proc = kinfo_getproc(pid); | |
| 60 if (!proc) { | |
| 61 return ""; | |
| 62 ret = proc->ki_comm; | |
| 63 free(proc); | |
| 64 #elif defined(__APPLE__) | |
| 65 struct proc_bsdinfo proc; | |
| 66 | |
| 67 int st = proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &proc, PROC_PIDTBSDINFO_SIZE); | |
| 68 if (st != PROC_PIDTBSDINFO_SIZE) | |
| 69 return ""; | |
| 70 return proc.pbi_comm; | |
| 71 #endif | |
| 72 return ret; | |
| 73 } | |
| 74 | |
| 75 std::vector<std::string> get_open_files(int pid) { | |
| 76 // initialize buffer | |
| 77 std::vector<std::string> ret; | |
| 78 int bufsz = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0); | |
| 79 struct proc_fdinfo *info = (struct proc_fdinfo *)malloc(bufsz); | |
| 80 proc_pidinfo(pid, PROC_PIDLISTFDS, 0, info, bufsz); | |
| 81 | |
| 82 // iterate over stuff | |
| 83 for (int i = 0; i < bufsz/sizeof(info[0]); i++) { | |
| 84 if (info[i].proc_fdtype == PROX_FDTYPE_VNODE) { | |
| 85 struct vnode_fdinfowithpath vnodeInfo; | |
| 86 proc_pidfdinfo(pid, info[i].proc_fd, PROC_PIDFDVNODEPATHINFO, &vnodeInfo, PROC_PIDFDVNODEPATHINFO_SIZE); | |
| 87 ret.push_back(vnodeInfo.pvip.vip_path); | |
| 88 } | |
| 89 } | |
| 90 return ret; | |
| 91 } | |
| 92 | |
| 93 std::unordered_map<int, std::vector<std::string>> get_all_open_files() { | |
| 94 std::unordered_map<int, std::vector<std::string>> map; | |
| 95 std::vector<int> pids = get_all_pids(); | |
| 96 for (int i: pids) { | |
| 97 map[i] = get_open_files(i); | |
| 98 } | |
| 99 return map; | |
| 100 } | |
| 101 | |
| 102 } |
