comparison dep/animone/src/fd/xnu.cc @ 270:0718f538c5f9

dep/animone: filter open files by access mode
author Paper <paper@paper.us.eu.org>
date Fri, 12 Apr 2024 19:13:50 -0400
parents 862d0d8619f6
children 246017a7907a
comparison
equal deleted inserted replaced
269:3efac0541151 270:0718f538c5f9
11 #include <fcntl.h> 11 #include <fcntl.h>
12 #include <libproc.h> 12 #include <libproc.h>
13 #include <sys/sysctl.h> 13 #include <sys/sysctl.h>
14 #include <sys/types.h> 14 #include <sys/types.h>
15 #include <sys/user.h> 15 #include <sys/user.h>
16
17 /* you may be asking: WTF is FWRITE?
18 * well, from bsd/sys/fcntl.h in the XNU kernel:
19 *
20 * Kernel encoding of open mode; separate read and write bits that are
21 * independently testable: 1 greater than [O_RDONLY and O_WRONLY].
22 *
23 * It's just how the kernel defines write mode.
24 */
25 #ifndef FWRITE
26 #define FWRITE 0x0002
27 #endif
16 28
17 namespace animone::internal::xnu { 29 namespace animone::internal::xnu {
18 30
19 bool EnumerateOpenProcesses(process_proc_t process_proc) { 31 bool EnumerateOpenProcesses(process_proc_t process_proc) {
20 size_t pids_size = 256; 32 size_t pids_size = 256;
64 int sz = proc_pidfdinfo(pid, info[i].proc_fd, PROC_PIDFDVNODEPATHINFO, &vnodeInfo, 76 int sz = proc_pidfdinfo(pid, info[i].proc_fd, PROC_PIDFDVNODEPATHINFO, &vnodeInfo,
65 PROC_PIDFDVNODEPATHINFO_SIZE); 77 PROC_PIDFDVNODEPATHINFO_SIZE);
66 if (sz != PROC_PIDFDVNODEPATHINFO_SIZE) 78 if (sz != PROC_PIDFDVNODEPATHINFO_SIZE)
67 return false; 79 return false;
68 80
69 // This doesn't work (for unknown reasons). I assume somethings fucked up with 81 /* why would a media player open a file in write mode? */
70 // my assumptions; I don't care enough to look into it tbh 82 if (vnodeInfo.pfi.fi_openflags & FWRITE)
71 // 83 continue;
72 // if (vnodeInfo.pfi.fi_openflags & O_WRONLY || vnodeInfo.pfi.fi_openflags & O_RDWR)
73 // continue;
74 84
75 if (!open_file_proc({pid, vnodeInfo.pvip.vip_path})) 85 if (!open_file_proc({pid, vnodeInfo.pvip.vip_path}))
76 return false; 86 return false;
77 } 87 }
78 } 88 }