[launchd-dev] The MachService key

Quinn "The Eskimo!" eskimo1 at apple.com
Tue Nov 22 07:49:28 PST 2011

On 22 Nov 2011, at 12:42, Jerry Krinock wrote:

> Is  "Mach Service" maybe just another name for "Mach Port"?

Well, yes, and no.  Mach messaging is a capability-based, so the kernel has to explicitly grant you permission to access a particular Mach port.  There are various ways to do this, but the most common is for each service provider to register its port with the Mach bootstrap service (every process inherits the capability to address the bootstrap service from its parent).  An entry in the "MachServices" key tells launchd to create a Mach port and register it with the bootstrap service with the given string.  Clients can then ask the bootstrap service to look up that string and thus obtain the capability to send a message to that port.  When a client sends a message to that port, launchd starts the daemon and the daemon can then process the message.

>> If it is, I can explain how you can use XPC to talk to a launchd daemon via the MachServices key.
> Yes, please do.  That was going to be my next question.

The basic strategy is:

o In the server, create an XPC listening connection using xpc_connection_create_mach_service.

o Set an event handler using xpc_connection_set_event_handler.

o Call xpc_connection_resume to enable the connection.

o Call dispatch_main to have the server wait for incoming connections.

o When a connection comes in, XPC will call your event handler with an object of type XPC_TYPE_CONNECTION.  You should enable this connection by setting an event handler (xpc_connection_set_event_handler) and resuming it (xpc_connection_resume).  It's this event handler that will be called when the client sends you a message.

o In the client, create an XPC connection using xpc_connection_create_mach_service.  If the client is a normal app and the service is vended by a daemon, pass in XPC_CONNECTION_MACH_SERVICE_PRIVILEGED.

o From then on, in both the client and the server, following the standard XPC techniques.



Share and Enjoy 
Quinn "The Eskimo!"                    <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

More information about the launchd-dev mailing list