I was initially trying to use getpid()
in my kernel module for OS X/macOS, is there a way to get the PID (process ID) of the process in whose context my kext is running in the kernel? Is there an existing function or variable that I can use ?
To get the PID of the process with which the currently running kernel thread is associated, call the proc_selfpid()
function; you'll need to #include <sys/proc.h>
in your kext's code to get the prototype. The PID will of course only correspond to a user process if your code is running in the context of some kind of callback for a syscall.