Mercurial > minori
diff dep/animia/src/bsd.cpp @ 62:4c6dd5999b39
*: update
1. updated animia
2. use widestrings for filesystem on Windows
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Sun, 01 Oct 2023 06:16:06 -0400 |
parents | 6ff7aabeb9d7 |
children | 1ce00c1c8ddc |
line wrap: on
line diff
--- a/dep/animia/src/bsd.cpp Fri Sep 29 15:52:31 2023 -0400 +++ b/dep/animia/src/bsd.cpp Sun Oct 01 06:16:06 2023 -0400 @@ -1,23 +1,23 @@ /** - * bsd.cpp + * bsd.cpp * - provides support for most* versions of BSD - * - this *should* also work for OS X + * - this also works for OS X :) * more technical details: this is essentially a wrapper * around the very C-like BSD system functions that are... * kind of unnatural to use in modern C++. -**/ -#include <vector> -#include <string> -#include <unordered_map> -#include <sys/types.h> -#include <sys/sysctl.h> -#include <sys/user.h> + **/ #include <fcntl.h> #include <iostream> +#include <string> +#include <sys/sysctl.h> +#include <sys/types.h> +#include <sys/user.h> +#include <unordered_map> +#include <vector> #ifdef __FreeBSD__ -#include <libutil.h> +# include <libutil.h> #elif defined(__APPLE__) -#include <libproc.h> +# include <libproc.h> #endif namespace Animia::Unix { @@ -26,77 +26,81 @@ ...anyway, what it essentially does is gets the size and stuff from sysctl() and reserves the space in a vector to store the PIDs */ std::vector<int> get_all_pids() { - std::vector<int> ret; - struct kinfo_proc* result = NULL; - size_t length = 0; - static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 }; + std::vector<int> ret; + 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() */ - sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, NULL, &length, NULL, 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>(); + result = (struct kinfo_proc*)malloc(length); + if (result == NULL) + return std::vector<int>(); - /* actually get our results */ - if (sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, result, &length, NULL, 0) == ENOMEM) { - assert(result != NULL); - free(result); - throw std::bad_alloc(); - } + /* actually get our results */ + 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 */ - ret.reserve(length/sizeof(*result)); - for (int i = 0; i < length/sizeof(*result); i++) - ret.push_back(result[i].kp_proc.p_pid); + /* add pids to our vector */ + ret.reserve(length / sizeof(*result)); + for (int i = 0; i < length / sizeof(*result); i++) + ret.push_back(result[i].kp_proc.p_pid); - return ret; + return ret; } std::string get_process_name(int pid) { - std::string ret; + std::string ret; #ifdef __FreeBSD__ - struct kinfo_proc* proc = kinfo_getproc(pid); - if (!proc) { - return ""; - ret = proc->ki_comm; - free(proc); + struct kinfo_proc* proc = kinfo_getproc(pid); + if (!proc) + return ""; + ret = proc->ki_comm; + free(proc); #elif defined(__APPLE__) - struct proc_bsdinfo proc; + struct proc_bsdinfo proc; - int st = proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &proc, PROC_PIDTBSDINFO_SIZE); - if (st != PROC_PIDTBSDINFO_SIZE) - return ""; - return proc.pbi_comm; + int st = proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &proc, PROC_PIDTBSDINFO_SIZE); + if (st != PROC_PIDTBSDINFO_SIZE) + return ""; + ret = proc.pbi_comm; #endif - return ret; + return ret; } std::vector<std::string> get_open_files(int pid) { - // initialize buffer - std::vector<std::string> ret; - int bufsz = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0); - struct proc_fdinfo *info = (struct proc_fdinfo *)malloc(bufsz); - proc_pidinfo(pid, PROC_PIDLISTFDS, 0, info, bufsz); + /* note: this is OS X only right now. eventually, I'll find a way + to do this in FreeBSD, OpenBSD and the like */ + std::vector<std::string> ret; + int bufsz = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0); + struct proc_fdinfo* info = (struct proc_fdinfo*)malloc(bufsz); + proc_pidinfo(pid, PROC_PIDLISTFDS, 0, info, bufsz); - // iterate over stuff - for (int i = 0; i < bufsz/sizeof(info[0]); i++) { - if (info[i].proc_fdtype == PROX_FDTYPE_VNODE) { - struct vnode_fdinfowithpath vnodeInfo; - proc_pidfdinfo(pid, info[i].proc_fd, PROC_PIDFDVNODEPATHINFO, &vnodeInfo, PROC_PIDFDVNODEPATHINFO_SIZE); - ret.push_back(vnodeInfo.pvip.vip_path); - } - } - return ret; + // iterate over stuff + ret.reserve(bufsz / sizeof(info[0])); + for (int i = 0; i < bufsz / sizeof(info[0]); i++) { + if (info[i].proc_fdtype == PROX_FDTYPE_VNODE) { + struct vnode_fdinfowithpath vnodeInfo; + proc_pidfdinfo(pid, info[i].proc_fd, PROC_PIDFDVNODEPATHINFO, &vnodeInfo, PROC_PIDFDVNODEPATHINFO_SIZE); + ret.push_back(vnodeInfo.pvip.vip_path); + } + } + return ret; } std::unordered_map<int, std::vector<std::string>> get_all_open_files() { - std::unordered_map<int, std::vector<std::string>> map; - std::vector<int> pids = get_all_pids(); - for (int i: pids) { - map[i] = get_open_files(i); - } - return map; + std::unordered_map<int, std::vector<std::string>> map; + std::vector<int> pids = get_all_pids(); + for (int i : pids) { + map[i] = get_open_files(i); + } + return map; } -} +} // namespace Animia::Unix