view dep/animone/src/fd/xnu.cc @ 278:c41c14ff8c67

dep/animone: x11: remove debugging comment oops
author Paper <paper@paper.us.eu.org>
date Mon, 22 Apr 2024 19:11:31 -0400
parents 0718f538c5f9
children 246017a7907a
line wrap: on
line source

#include "animone/fd/xnu.h"
#include "animone.h"
#include "animone/util/osx.h"

#include <cassert>
#include <memory>
#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>

/* you may be asking: WTF is FWRITE?
 * well, from bsd/sys/fcntl.h in the XNU kernel:
 *
 *    Kernel encoding of open mode; separate read and write bits that are
 *    independently testable: 1 greater than [O_RDONLY and O_WRONLY].
 *
 * It's just how the kernel defines write mode.
*/
#ifndef FWRITE
#define FWRITE	0x0002
#endif

namespace animone::internal::xnu {

bool EnumerateOpenProcesses(process_proc_t process_proc) {
	size_t pids_size = 256;
	std::unique_ptr<pid_t[]> pids;

	int returned_size = 0;
	do {
		pids.reset(new pid_t[pids_size *= 2]);
		returned_size = proc_listpids(PROC_ALL_PIDS, 0, pids.get(), pids_size * sizeof(pid_t));
		if (returned_size == -1)
			return false;
	} while ((pids_size * sizeof(size_t)) < returned_size);

	for (int i = 0; i < pids_size; i++) {
		std::string result;
		osx::util::GetProcessName(pids[i], result);
		if (!process_proc({pids[i], result}))
			return false;
	}

	return true;
}

bool 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) {
		const int bufsz = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0);
		if (bufsz < 0)
			return false;

		const size_t info_len = bufsz / sizeof(struct proc_fdinfo);
		if (info_len < 1)
			return false;

		std::unique_ptr<struct proc_fdinfo[]> info(new struct proc_fdinfo[info_len]);
		if (!info)
			return false;

		proc_pidinfo(pid, PROC_PIDLISTFDS, 0, info.get(), bufsz);

		for (size_t i = 0; i < info_len; 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;

				/* why would a media player open a file in write mode? */
				if (vnodeInfo.pfi.fi_openflags & FWRITE)
					continue;

				if (!open_file_proc({pid, vnodeInfo.pvip.vip_path}))
					return false;
			}
		}
	}

	return true;
}

} // namespace animone::internal::xnu