Mercurial > minori
comparison dep/animia/src/fd/bsd.cc @ 139:478f3b366199
dep/animia: separate lots of things, use base class for OS stuff
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Sun, 12 Nov 2023 16:43:07 -0500 |
parents | 28842a8d0c6b |
children | 1e696863b54c |
comparison
equal
deleted
inserted
replaced
138:28842a8d0c6b | 139:478f3b366199 |
---|---|
1 /** | 1 /** |
2 * bsd.cpp | 2 * fd/bsd.cpp |
3 * - provides support for most* versions of BSD | 3 * - this ONLY* supports OS X as of now |
4 * - this also works for OS X :) | 4 * (*there is some FreeBSD support code) |
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 **/ | 5 **/ |
9 #include <fcntl.h> | 6 #include <fcntl.h> |
10 #include <iostream> | 7 #include <iostream> |
11 #include <string> | 8 #include <string> |
12 #include <sys/sysctl.h> | 9 #include <sys/sysctl.h> |
23 namespace animia::internal::unix { | 20 namespace animia::internal::unix { |
24 | 21 |
25 /* this is a cleaned up version of a function from... Apple? | 22 /* this is a cleaned up version of a function from... Apple? |
26 ...anyway, what it essentially does is gets the size and stuff from | 23 ...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 */ | 24 sysctl() and reserves the space in a vector to store the PIDs */ |
28 bool GetAllPids(std::set<pid_t>& pids) { | 25 bool UnixFdTools::GetAllPids(std::set<pid_t>& pids) { |
29 struct kinfo_proc* result = NULL; | 26 struct kinfo_proc* result = NULL; |
30 size_t length = 0; | 27 size_t length = 0; |
31 static const int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0}; | 28 static const int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0}; |
32 | 29 |
33 /* get appropriate length from sysctl() | 30 /* get appropriate length from sysctl() |
51 pids.reserve(length / sizeof(*result)); | 48 pids.reserve(length / sizeof(*result)); |
52 for (int i = 0; i < length / sizeof(*result); i++) | 49 for (int i = 0; i < length / sizeof(*result); i++) |
53 pids.push_back(result[i].kp_proc.p_pid); | 50 pids.push_back(result[i].kp_proc.p_pid); |
54 } | 51 } |
55 | 52 |
56 bool GetProcessName(pid_t pid, std::string& result) { | 53 bool UnixFdTools::GetProcessName(pid_t pid, std::string& result) { |
57 #ifdef __FreeBSD__ | 54 #ifdef __FreeBSD__ |
58 struct kinfo_proc* proc = kinfo_getproc(pid); | 55 struct kinfo_proc* proc = kinfo_getproc(pid); |
59 if (!proc) | 56 if (!proc) |
60 return false; | 57 return false; |
61 result = proc->ki_comm; | 58 result = proc->ki_comm; |
79 return true; | 76 return true; |
80 #endif | 77 #endif |
81 } | 78 } |
82 | 79 |
83 /* this only works on OS X :( */ | 80 /* this only works on OS X :( */ |
84 bool EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::tuple<pid_t, std::string>>& files) { | 81 bool UnixFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::tuple<pid_t, std::string>>& files) { |
85 for (const auto& pid : pids) { | 82 for (const auto& pid : pids) { |
86 int bufsz = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0); | 83 int bufsz = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0); |
87 if (bufsz == -1) | 84 if (bufsz == -1) |
88 return false; | 85 return false; |
89 | 86 |