Send a file descriptor via Obj-C XPC API?

I have several processes built using the Objective-C/Swift XPC API (NSXPCConnection, et. al.). I have file descriptors that I need to share between these process. I can't find any high-level XPC methods for exchanging file descriptors.


The XPC C API has a several purpose-built functions for duplicating a file descriptor and exchanging them, but I can't seen to find any way of either (a) using the Obj-C API to exchange file descriptor or (b) using the high-level API to pass a low-level xpc_object_t.

Answered by DTS Engineer in 153607022

I have several processes built using the Objective-C/Swift XPC API (NSXPCConnection, et. al.).

Yay!

I have file descriptors that I need to share between these process.

I believe you can do this via NSFileHandle. Notably, it conforms to NSSecureCoding.

Share and Enjoy

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

let myEmail = "eskimo" + "1" + "@apple.com"
Accepted Answer

I have several processes built using the Objective-C/Swift XPC API (NSXPCConnection, et. al.).

Yay!

I have file descriptors that I need to share between these process.

I believe you can do this via NSFileHandle. Notably, it conforms to NSSecureCoding.

Share and Enjoy

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

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks, Quinn, NSFileHandle did the trick!


I did discover a caveat while testing this. For anyone else doing this, be aware that the the de-serialized NSFileHandle object that the XPC process receives owns its file descriptor, even if the original handle object does not.


So if you let the received copy go out of scope and get released, the file descriptor will be closed. Either dup() the received descriptor or somehow retain the NSFileHandle object to prevent it from closing.

Send a file descriptor via Obj-C XPC API?
 
 
Q