My application receives mach IPC messages and return answers for the callers. I have mach caller port(msgh_remote_port)
and I want to know PID
of the caller.
Can I find on OSX by the mach port a PID
which listen for specific mach port?

- 3,541
- 28
- 38
- 38

- 151
- 1
- 3
3 Answers
The mach port is not directly associated with a process, but instead with a task. The task is then associated with the bsd process structure. To query the ports of a task you can use the mach_port_names function. To get all the open mach ports iterate over all the tasks and use the above mentioned function.
A different approach is to use the procfs filesystem. The procfs filesystem is implemented on top of the fuse filesystem and needs to be manually installed on a system. It is a open source solution. Once the procfs filesystem is installed you can query the ports of a task by accessing the file /proc/proc-id/task/ports. Have a look at Link.

- 21,988
- 13
- 81
- 109

- 5,870
- 1
- 21
- 22
Three ways you can do this without a kext:
- launchctl print system (or another domain)
- lsmp -a (no port names, just ids)
procexp all ports | grep the_service_name_you_want
(procexp is an add on tool from http://NewOSXBook.com/tools/procexp.html)

- 7,674
- 25
- 36
Thanks, I found here the way to do it - https://web.archive.org/web/20120907161143/http://robert.sesek.com/thoughts/2012/1/debugging_mach_ports.html

- 891
- 7
- 20

- 151
- 1
- 3