Mercurial > minori
view dep/animia/src/fd/xnu.cc @ 157:18c8eb5d1edc
x11: make it work
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Thu, 16 Nov 2023 16:51:34 -0500 |
parents | cdf79282d647 |
children | 900b5b530883 |
line wrap: on
line source
/* ** fd/xnu.cpp ** - provides support for XNU (part of Darwin) */ #include "animia/fd/xnu.h" #include "animia.h" #include <cassert> #include <string> #include <unordered_map> #include <vector> #include <fcntl.h> #include <libproc.h> #include <sys/sysctl.h> #include <sys/types.h> #include <sys/user.h> namespace animia::internal::xnu { static std::string GetProcessName(pid_t pid) { char name[2*MAXCOMLEN]; proc_name(pid, name, sizeof(name)); return name; } bool XnuFdTools::EnumerateOpenProcesses(process_proc_t process_proc) { std::vector<pid_t> pids; pids.reserve(1024); for (int returned_size = pids.capacity(); pids.capacity() > returned_size; pids.reserve(pids.capacity() * 2)) { returned_size = proc_listpids(PROC_ALL_PIDS, 0, pids.data(), pids.capacity() * sizeof(pid_t)); if (returned_size == -1) return false; } for (const auto& pid : pids) { if (!process_proc({pid, GetProcessName(pid)})) return false; } return true; } bool XnuFdTools::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) return false; struct proc_fdinfo* info = (struct proc_fdinfo*)malloc(bufsz); if (!info) return false; proc_pidinfo(pid, PROC_PIDLISTFDS, 0, info, bufsz); for (int i = 0; i < bufsz / sizeof(info[0]); i++) { if (info[i].proc_fdtype == PROX_FDTYPE_VNODE) { struct vnode_fdinfowithpath vnodeInfo; int sz = proc_pidfdinfo(pid, info[i].proc_fd, PROC_PIDFDVNODEPATHINFO, &vnodeInfo, PROC_PIDFDVNODEPATHINFO_SIZE); if (sz != PROC_PIDFDVNODEPATHINFO_SIZE) return false; /* this doesn't work! if (vnodeInfo.pfi.fi_openflags & O_WRONLY || vnodeInfo.pfi.fi_openflags & O_RDWR) continue; */ if (!open_file_proc({pid, vnodeInfo.pvip.vip_path})) return false; } } } return true; } } // namespace animia::internal::xnu