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

Piotr Panasewicz piotr.panasewicz at mac.com
Thu Jan 15 08:59:37 PST 2015


Hello Damien,

Is it possible to do that without XPC?

The problem is that code of the agent/service is rather multiplatform, it needs to work on Windows too, with the Windows version of the client. Of course we can separate implementation for each OS but right now the main difference of both is the way the service starts.

I need an agent that would start on demand (like it is doing now by creating a file when I need it to run and deleting it, when I want it to shutdown). The only problem is that is sometimes starts as the other user which is logged in via fast user switching or VNC.

Right now it works like this. I start my client app, the app creates the .txt file to start the agent and my app tells the agent to check if there are available updates to my client. Then the client closes and the agent starts the updater (the agent and the updater are a part of a redistributable package and on Mac they reside at fixed path: /Users/Shared/Company/redists/). The updater performs its task and hits back to the agent with a response that it finished and we can restart the client. The agent then starts the client. So as you see if the agent starts as a different user, the updater and the client will start for that user as well, not the one that initially clicked the client app.

Thanks.

The agent has a timeout implemented as well, so when it doesn’t receive any message for more than 30 seconds, it deletes the txt file and closes itself.

> On 15 Jan 2015, at 17:56, Damien Sorresso <dsorresso at apple.com> wrote:
> 
> Hi Piotr,
> 
> This sounds like a good use case for an XPC service. The service would ship in your app's bundle, and each instance of your app would have a private instance of the service that it talked to, and that instance would run with the same uid as your app. More here:
> 
> http://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/chapters/CreatingXPCServices.html
> -damien
> 
> On 15 Jan, 2015, at 04:25, Piotr Panasewicz <piotr.panasewicz at mac.com> wrote:
>> Hello,
>> 
>> I would like to create a Launch Agent that would start on demand when my bundle app requires it. I have it currently implemented like below which monitors a specific path for existence of a file. This works ok for one user logged in. If I there are more users logged via Fast User Switching the OS tends to open the service for different user, not the one that’s currently using my app. That’s because two users are now monitoring the same path and the OS isn’t sure in which user’s context the service should run.
>> 
>> Also my bundle app has LSMultipleInstancesProhibited set to true, so it isn’t allowed to run two instances at once. But if User A opens my app and there is a User B logged in, the service may open for User B (even the file from the plist was created by User A and User B hasn’t touched my app).
>> 
>> Therefore I need a solution that would allow my client app to open the service for the same user it runs as. And if I close the app for User A, switch to User B and start the client, the service would now open for User B.
>> 
>> Thanks,
>> Peter
>> 
>> <?xml version="1.0" encoding="UTF-8"?>
>> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
>> <plist version="1.0">
>> <dict>
>>   <key>KeepAlive</key>
>>   <dict>
>>       <key>PathState</key>
>>       <dict>
>>           <key>/Users/Shared/Company/Service.txt</key>
>>           <true/>
>>       </dict>
>>   </dict>
>> 	<key>Label</key>
>> 	<string>com.company.service</string>
>> 	<key>Program</key>
>> 	<string>/Users/Shared/Company/Service</string>
>> 	<key>MachServices</key>
>> 	<dict>
>> 		<key>com.company.service</key>
>> 		<true/>
>> 	</dict>
>> </dict>
>> </plist>
>> 
>> _______________________________________________
>> launchd-dev mailing list
>> launchd-dev at lists.macosforge.org
>> https://lists.macosforge.org/mailman/listinfo/launchd-dev
> 



More information about the launchd-dev mailing list