[launchd-dev] launchd keeps re-launching my binary in /Library/PrivilegedHelperTools

Anshul Kothari ans_nsit at yahoo.co.in
Fri Nov 1 04:29:48 PDT 2013


Hi,

I am installing a privileged helper tool binary using
*SMJobBless*<https://developer.apple.com/library/mac/samplecode/SMJobBless/Listings/ReadMe_txt.html>that
will be used to update my application in background without asking for
password.



Now, the problem is, once the helper tool is installed, it keeps getting
launched again and again even if it completes its job successfully. I have
tried various things to stop it from launching again like making it sleep
for at least 15 seconds before quitting; toggling RunAtLoad,
KeepAlive/SuccessfulExit keys in its plist but in vain.



I have got a way by which it works i.e. setting KeepAlive with
PathState<https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html>key
in the plist (see below); the launcher program can create a file at
pre-determined path so that launchd launches helper tool and helper tool
can remove that file before quitting so that launchd doesn’t launch it
again.



But I am not happy with it as it looks little hacky to me. I would rather
call an [NSConnection connectionWithRegisteredName: host:] to launch it but
with that the above problem of launching again and again starts occurring.




My binary always returns 0.

The launchd .plist is as below:



<?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">

      <key>Label</key>

            <string>com.ak.SMJobBlessHelper</string>

      <key>MachServices</key>

      <dict>

            <key>com.ak.SMJobBlessHelper.mach</key>

            <true/>

      </dict>

      <key>ProgramArguments</key>

            <array>


<string>/Library/PrivilegedHelperTools/com.ak.SMJobBlessHelper</string>

            </array>

      <key>RunAtLoad</key>

      <true/>

</plist>



I further tried the below but in vain:

1.       Tried to add KeepAlive to false in the plist.

2.       Tried to add KeepAlive as a dictionary with the key SuccessfulExit
to false.

3.       Tried to make my app sleep for occult times before quitting like
sleeping for 5 minutes, 10 minutes etc. but it still gets launched again.





The one way I have got it to work is by adding “KeepAlive with PathState”:



<dict>
<key>PathState</key>

<dict>

<key>/tmp/*helper.txt*</key>

<true/>
</dict>

      </dict>



So, whenever my launcher creates the “helper.txt” file, launchd launches my
binary (com.ak.SMJobBlessHelper) and then my binary deletes “helper.txt”
before quitting so that launchd doesn’t launch it again. But I am not sure
if it is a good way to do this.





More info:


This is OSX and it is a LaunchDaemon. I have not tried by removing Mach
Services as I use them to launch it on demand.

Actually this is a binary used by the updater and not the updater itself.

I don’t care about the RunAtLoad value as I want to call it on demand using
the mach services – I had put it there initially so that the thing launches
right after installation. However, I have tried by removing RunAtLoad but
it still does not work and it keeps re-launching.



The approaches of StartInterval/StartCalendarInterval are good (that binary
automatically starts after some time) but neither of the approaches works
for me as I want to launch it on demand (I will have to change my design
quite a bit if I want to start using this approach).




It would be great if some could tell why it may be happening and if it
could be launched on demand and doesn’t keep re-launching.


Thanks,

Anshul
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/launchd-dev/attachments/20131101/096242d2/attachment.html>


More information about the launchd-dev mailing list