Mercurial > minori
diff dep/animia/src/fd/bsd.cc @ 152:8700806c2cc2
dep/animia: awesome new breaking changes!
I'm so tired
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Wed, 15 Nov 2023 02:34:59 -0500 |
parents | 54744a48a7d7 |
children |
line wrap: on
line diff
--- a/dep/animia/src/fd/bsd.cc Tue Nov 14 16:31:21 2023 -0500 +++ b/dep/animia/src/fd/bsd.cc Wed Nov 15 02:34:59 2023 -0500 @@ -1,13 +1,13 @@ -/** - * fd/bsd.cpp - * - this ONLY* supports OS X as of now - * (*there is some FreeBSD support code) - **/ +/* +** fd/bsd.cpp +** - this ONLY* supports OS X as of now +** (*there is some FreeBSD support code) +*/ #include "animia/fd/bsd.h" +#include "animia.h" #include <unordered_map> #include <vector> -#include <iostream> #include <string> #include <fcntl.h> @@ -23,40 +23,7 @@ namespace animia::internal::unix { -/* this is a cleaned up version of a function from... Apple? - ...anyway, what it essentially does is gets the size and stuff from - sysctl() and reserves the space in a vector to store the PIDs - - TODO: https://kaashif.co.uk/2015/06/18/how-to-get-a-list-of-processes-on-openbsd-in-c/ */ -bool UnixFdTools::GetAllPids(std::set<pid_t>& pids) { - struct kinfo_proc* result = NULL; - size_t length = 0; - static const int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0}; - - /* get appropriate length from sysctl() - note: the reason this isn't checked is actually because this will - *always* return an error on OS X (or... maybe I'm doing it wrong :) ) */ - sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, NULL, &length, NULL, 0); - - result = (struct kinfo_proc*)malloc(length); - if (result == NULL) - return std::vector<int>(); - - /* TODO: this might actually return ENOMEM if the amount of file handles changes between the - original sysctl() call and this one, which is technically possible */ - if (sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, result, &length, NULL, 0) == ENOMEM) { - assert(result != NULL); - free(result); - throw std::bad_alloc(); - } - - /* add pids to our vector */ - pids.reserve(length / sizeof(*result)); - for (int i = 0; i < length / sizeof(*result); i++) - pids.insert(result[i].kp_proc.p_pid); -} - -bool UnixFdTools::GetProcessName(pid_t pid, std::string& result) { +static bool GetProcessName(pid_t pid, std::string& result) { #ifdef __FreeBSD__ struct kinfo_proc* proc = kinfo_getproc(pid); if (!proc) @@ -83,8 +50,45 @@ #endif } +/* this is a cleaned up version of a function from... Apple? + ...anyway, what it essentially does is gets the size and stuff from + sysctl() and reserves the space in a vector to store the PIDs + + TODO: https://kaashif.co.uk/2015/06/18/how-to-get-a-list-of-processes-on-openbsd-in-c/ */ +bool UnixFdTools::EnumerateOpenProcesses(process_proc_t process_proc) { + struct kinfo_proc* result = NULL; + size_t length = 0; + static const int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0}; + + /* get appropriate length from sysctl() + note: the reason this isn't checked is actually because this will + *always* return an error on OS X (or... maybe I'm doing it wrong :) ) */ + sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, NULL, &length, NULL, 0); + + result = (struct kinfo_proc*)malloc(length); + if (result == NULL) + return std::vector<int>(); + + /* TODO: this might actually return ENOMEM if the amount of file handles changes between the + original sysctl() call and this one, which is technically possible */ + if (sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, result, &length, NULL, 0) == ENOMEM) { + assert(result != NULL); + free(result); + throw std::bad_alloc(); + } + + for (int i = 0; i < length / sizeof(*result); i++) { + const pid_t pid = result[i].kp_proc.p_pid; + if (!process_proc({pid, GetProcessName(pid)})) + return false; + } +} + /* this only works on OS X :( */ -bool UnixFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::pair<pid_t, std::string>>& files) { +bool UnixFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) { + if (!open_file_proc) + return false; + for (const auto& pid : pids) { int bufsz = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0); if (bufsz == -1) @@ -109,7 +113,8 @@ continue; */ - files.push_back({pid, vnodeInfo.pvip.vip_path}); + if (!open_file_proc({pid, vnodeInfo.pvip.vip_path})) + return false; } } }