[launchd-dev] Creating a Launch Agent (per user) on demand when client app is open

Per Olofsson per.olofsson at gu.se
Mon Jan 19 06:19:05 PST 2015

16 jan 2015 kl. 13:21 skrev Piotr Panasewicz <piotr.panasewicz at mac.com>:
> Seems I will encounter the same issues I am having now with listening on localhost or socket port option.
> In my current implementation the agent sometimes starts as the second logged in user via fast user switching (so both sessions are GUI sessions and active at the time). So if I have an agent (with plist inside /Library/LaunchAgents, so it will work for each user logged in) monitoring localhost or the specific socket port, for which user the agent will run? Because both sessions are active and have GUI. How will I communicate with the one I want if the IPC between my app and the agent is base on one socket port 9977? If I run my app as User A I want the service to run only for the User A, not User B, or both.

A unix domain socket listener can identify the UID of the connecting peer by using getpeereid() or LOCAL_PEERCRED. You can use this with a privileged listener which forks and executes tasks on behalf of the connecting peer, but I would not recommend going down that route unless you're comfortable writing privileged code. NSXPCConnection is a lot less work and secure by default.

Per Olofsson, IT-service, University of Gothenburg

More information about the launchd-dev mailing list