How to create an arbitrary XPC connection?

I keep getting hit over the head that I should stop using Mach ports for IPC switch to XPC. Until now, I've resisted because of the radical differences between the two communication models AND that I am still supporting OS X 10.6 (which has no XPC).


Well, OS X 10.6 is now on the chopping block so I started looking into XPC and immediately got stuck.


How can a process create advertise a named XPC service that other processes can connect to?


Right now, my application spawns multiple helper processes. Each helper process establishes itself as a service (with a uniquely named Mach service) that other processes then connect to. Some of those processes connect to control/direct a helper process; others simply connect to monitor and report on its progress. And just to complicate things, some of those helper processes are running in the user's bootstrap as a regular user process, while others are launched by a priviledged helper and run as root.


I can't figure out how to do this using XPC. launchd doesn't appear to support this kind of model and I can't find any function that will create a named XPC connection that other processes can use to connect with it. I initially thought that you would register a name and then bind an XPC connection to it, but I can't find any such facility.


So now I'm asking, is this even possible?

JamesB wrote:


> I can't find any function that will create a named XPC connection that other processes can use to connect with it.


While I can't really answer any of your questions, have you looked into either xpc_connection_create_mach_service() or the Anonymous Connections in the xpc_connection_create(3) man page?

Yes, I looked at both of those, as they seem to be the only ways to create an XPC connection (except for the odd-ball xpc_connection_create_from_endpoint(), which, as far as I can tell, is used to create a new connection from within an existing connection).


Specifically, xpc_connection_create() will create a connection to a helper bundled as part of your application package.


xpc_connection_create_mach_service() connects to a helper managed by launchd.


And those, it appears, are you only two choices for XPC: Talking to a helper that's bundled with your application or talking to a helper service controlled by launchd.

You're right that XPC applies a lot of constraints here. I suspect your best option is an XPC service to broker these requests. Have the app pass work to the service and then have the launched helpers connect to the service to request the details of that work. Once things are connected up the app and the helpers should be able to communicate directly via an XPC endpoint.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
How to create an arbitrary XPC connection?
 
 
Q