LaunchAgent as XPC server

I'm having trouble finding working examples of using the xpc.h (c lib) API. There are a number of examples showing NSXPC but I'm trying to use the xpc.h interface.


I have a CUPS backend (Unix command line) program that needs to open a url in the user's browser. This is no longer allowed within a CUPS backed starting with Yosemite, so DTS advised that I look into building a Launch Agent (also Unix command line) which can open the url and then have the CUPS backend contact it via XPC.


I tried using the following but it seg faults.

https://eminisnp500.wordpress.com/2012/01/31/using-mac-os-xpc-service-with-launchd/


Also, since launchd needs to run my XPC server, how does one go about debugging this? I didn't think I could start my XPC server from xcode because only launchd could bind the Mach service. Is there a preferred way to build/debug a LaunchAgent that hosts an XPC Mach service?


Finally, do these programs need to be signed with my Developer ID (can you even sign command line programs)?


Thanks

William

wjens wrote


> Also, since launchd needs to run my XPC server, how does one go about debugging this? I didn't think I could start my XPC server from xcode because only launchd could bind the Mach service. Is there a preferred way to build/debug a LaunchAgent that hosts an XPC Mach service?


While I couldn't get Xcode to attach to the XPC Mach service I'm working on through the method described this this post by ktam2, you can, however, also attempt to attach the lldb debugger to the XPC service Launch Agent at the command line with:


launchctl attach -s gui/UID_ofLoggedInUser/labelOfXPCLaunchAgent


Refer to the launchctl(1) man page. Also, the WaitForDebugger key is mentioned in the launchd.plist(5) man page.


> Finally, do these programs need to be signed with my Developer ID (can you even sign command line programs)?


You can Code Sign and Sandbox a single file executable by embedding an info.plist into it. In the Target’s Build Settings in the Other Linker Flags field, add the following to get the info.plist embedded into the executable:


-sectcreate __TEXT __info_plist path_to_infoPlist


Refer to the Adding an Info.plist to Single-File Tools section of the Code Signing Guide.


To Sandbox the command line program, you also have to manually create an Entitlements file for it, as there's no corresponding Capabilities tab present for such a Target, and manually configure the command line program's Build Settings in Code Signing -> Code Sighing Entitlements to point to sub-path in the subdirectory within the Xcode Project directory where the Entitlements file resides. e.g.- CommandLineProgramTargetName/CommandLineProgram.entitlements.

There are a number of examples showing NSXPC but I'm trying to use the xpc.h interface.

Why? In my experience the C XPC API is substantially more work than NSXPCConnection, so I recommend you use the latter unless you have a hard requirement that forces you to use the former.

Share and Enjoy

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

let myEmail = "eskimo" + "1" + "@apple.com"
LaunchAgent as XPC server
 
 
Q