mach_port_t vs mach_port_name_t

Questions and Answers about all things *OS (macOS, iOS, tvOS, watchOS)

mach_port_t vs mach_port_name_t

Postby scknight » Mon Oct 08, 2018 2:39 pm

This might be obvious but I can't seem to wrap my head around it. What's the difference between mach_port_t and mach_port_name_t? Both seem to ultimately define to unsigned int. But I don't understand when a function like this:

Code: Select all
mach_port_allocate(ipc_space_t task, mach_port_right_t right, mach_port_name_t *name)


Takes a mach_port_name_t but I often see code doing this:

Code: Select all
int ret;
mach_port_t port = MACH_PORT_NULL;

ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port);


So is mach_port_t and mach_port_name_t essentially the same thing?
scknight
 
Posts: 39
Joined: Thu Nov 10, 2016 1:01 pm

Re: mach_port_t vs mach_port_name_t

Postby Siguza » Mon Oct 08, 2018 5:09 pm

So is mach_port_t and mach_port_name_t essentially the same thing?


In userland yes, in the kernel no.
Think of mach ports like file descriptors. In userland you get a simple number, which means nothing when you take it out of context, e.g. by passing it to another process.
It only means something when you pass it to APIs that then perform a lookup in some data structure attached to your process.
And in the kernel, a mach_port_name_t is still a simple number, but a mach_port_t is then a pointer to an actual ipc_port struct.

In essence, "mach_port_name_t" denotes a port handle in some ipc space, whereas "mach_port_t" denotes one in yours.
User avatar
Siguza
Unicorn
 
Posts: 203
Joined: Thu Jan 28, 2016 10:38 am

Re: mach_port_t vs mach_port_name_t

Postby scknight » Mon Oct 08, 2018 6:58 pm

Thanks for the clarification! That makes perfect sense.
scknight
 
Posts: 39
Joined: Thu Nov 10, 2016 1:01 pm


Return to Questions and Answers

Who is online

Users browsing this forum: No registered users and 3 guests