Search code examples
macoskernelpidkernel-extensionxnu

How do you get the current process ID in Mac kernel extension code?


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 ?


Solution

  • 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.