Launching agent installed with SMAppService

I am attempting to install and utilize an agent using the new(ish) SMAppService API with an existing app. The agent appears to install (no error is returned), but when I try to start the agent from Terminal, I get the following in the launchd.log:

2024-04-22 09:57:27.469039 (gui/502/com.redacted.service.agent) <Notice>: internal event: WILL_SPAWN, code = 0 2024-04-22 09:57:27.469080 (gui/502/com.redacted.service.agent) <Notice>: service state: spawn scheduled 2024-04-22 09:57:27.469081 (gui/502/com.redacted.service.agent) <Notice>: service state: spawning 2024-04-22 09:57:27.469100 (gui/502/com.redacted.service.agent) <Notice>: launching: one-shot 2024-04-22 09:57:27.469105 (gui/502/com.redacted.service.agent) <Notice>: Allowing non-reentrant proxy for resolving path 2024-04-22 09:57:27.469947 (gui/502/com.redacted.service.agent [71866]) <Notice>: xpcproxy spawned with pid 71866 2024-04-22 09:57:27.469960 (gui/502/com.redacted.service.agent [71866]) <Notice>: internal event: SPAWNED, code = 0 2024-04-22 09:57:27.469964 (gui/502/com.redacted.service.agent [71866]) <Notice>: service state: xpcproxy 2024-04-22 09:57:27.469997 (gui/502/com.redacted.service.agent [71866]) <Notice>: internal event: SOURCE_ATTACH, code = 0 2024-04-22 09:57:27.506283 (gui/502/com.redacted.service.agent [71866]) <Error>: Service could not initialize: posix_spawn(/Users/chrisf/Library/Developer/Xcode/DerivedData/Redacted-gttupgdyakodzddurpavhmscwabs/Build/Products/Debug/Redacted App.app/Contents/MacOS/Service Agent.app), error 0xd - Permission denied 2024-04-22 09:57:27.506306 (gui/502/com.redacted.service.agent [71866]) <Error>: initialization failure: 23E224: xpcproxy + 31420 [1098][A7EF179C-FBCC-349E-A7D2-09B2F1408413]: 0xd 2024-04-22 09:57:27.506309 (gui/502/com.redacted.service.agent [71866]) <Notice>: internal event: INIT, code = 13 2024-04-22 09:57:27.506313 (gui/502/com.redacted.service.agent [71866]) <Notice>: job state = spawn failed 2024-04-22 09:57:27.507148 (gui/502/com.redacted.service.agent [71866]) <Notice>: xpcproxy exited due to exit(78) 2024-04-22 09:57:27.507153 (gui/502/com.redacted.service.agent [71866]) <Notice>: exited due to exit(78) 2024-04-22 09:57:27.507162 (gui/502/com.redacted.service.agent [71866]) <Notice>: already handled failed init, ignoring 2024-04-22 09:57:27.507170 (gui/502/com.redacted.service.agent [71866]) <Notice>: service state: exited 2024-04-22 09:57:27.507186 (gui/502/com.redacted.service.agent [71866]) <Notice>: internal event: EXITED, code = 0

(tldr: error 0xd - Permission denied)

I'd also be curious how we are expected to launch agents once registered with SMAppService. Is it sufficient simply to make an XPC call to an exposed method?

Thanks!

Answered by DTS Engineer in 786012022
/Users/chrisf/Library/Developer/Xcode/DerivedData/Redacted-gttupgdyakodzddurpavhmscwabs/Build/Products/Debug/Redacted App.app/Contents/MacOS/Service Agent.app

It looks like you configured your launchd agent’s executable to point to your agent’s bundle rather than to the agent executable itself. I suspect you’ve set BundleProgram to Contents/MacOS/Service Agent.app rather than Contents/MacOS/Service Agent.app/Contents/MacOS/Service Agent.

IMPORTANT The agent doesn’t have to have its own bundle, so only apply this bundle structure if you need it for some other reason (bundled resources, a provisioning profile, and so on).

I'd also be curious how we are expected to launch agents once registered with SMAppService. Is it sufficient simply to make an XPC call to an exposed method?

An agent registered in this way behaves the same as an agent registered via a launchd property list file. You can start it on demand using XPC with MachServices, set RunAtLoad or KeepAlive, and so on. See the launchd.plist man page for details.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer
/Users/chrisf/Library/Developer/Xcode/DerivedData/Redacted-gttupgdyakodzddurpavhmscwabs/Build/Products/Debug/Redacted App.app/Contents/MacOS/Service Agent.app

It looks like you configured your launchd agent’s executable to point to your agent’s bundle rather than to the agent executable itself. I suspect you’ve set BundleProgram to Contents/MacOS/Service Agent.app rather than Contents/MacOS/Service Agent.app/Contents/MacOS/Service Agent.

IMPORTANT The agent doesn’t have to have its own bundle, so only apply this bundle structure if you need it for some other reason (bundled resources, a provisioning profile, and so on).

I'd also be curious how we are expected to launch agents once registered with SMAppService. Is it sufficient simply to make an XPC call to an exposed method?

An agent registered in this way behaves the same as an agent registered via a launchd property list file. You can start it on demand using XPC with MachServices, set RunAtLoad or KeepAlive, and so on. See the launchd.plist man page for details.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Launching agent installed with SMAppService
 
 
Q