Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which PID listens on a given mach port

Tags:

macos

mach

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?

like image 221
user1119324 Avatar asked Feb 19 '12 08:02

user1119324


People also ask

How do I find the PID of a port on a Mac?

on OS X you can use the -v option for netstat to give the associated pid.

How do I find the PID of a port?

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.

How do you check which process is using a port Mac?

You can find out what is running on a specific port by running the command lsof -i with the port number, :<PortNumber> .


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

like image 84
steve Avatar answered Sep 19 '22 01:09

steve