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?
on OS X you can use the -v option for netstat to give the associated pid.
Using Netstat command: Open a CMD prompt. Type in the command: netstat -ano -p tcp. You'll get an output similar to this one. Look-out for the TCP port in the Local Address list and note the corresponding PID number.
You can find out what is running on a specific port by running the command lsof -i with the port number, :<PortNumber> .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With