[launchd-dev] Stopping and unloading an agent from root-privileged application

eveningnick eveningnick eveningnick at gmail.com
Fri Jan 7 06:02:56 PST 2011


>> Try this.
>>
>> % sudo -u <username|#uid> launchctl stop my.agent
>> % sudo -u <username|#uid> launchctl unload /Library/LaunchAgents/my.agent.plist
>
> Have you actually tried this?  It /might/ work on 10.6, but I'm almost certain it won't work on 10.5 because launchctl looks up the launchd to talk to via its Mach bootstrap namespace, and sudo doesn't switch namespace.

yes, i tried and it works on 10.6 and doesn't work on 10.5

>> How could i stop an agent from root-privileged application?
>
> There's no good solution to this.  I generally recommend that you use IPC between your daemon and agents to tell the agents to stop.

I found the solution to stop it both an agent and daemon. Luckily for
me, helper tools are launched with effective UID of root, while real
UID is the ordinary user's one. Which means that unless i execute
"setuid(0)" all calls to launchctl are delivered to a per-session
instance of launchd. Hence, i unload an agent, execute setuid(0) and
unload the daemon. Works both in 10.6 and 10.5 (strange: why? i dont
switch the boostrap context. But then, if i execute in Terminal "sudo
launchctl stop -w /Library/LaunchDaemons/mydaemon.plist", the daemon
stops in 10.5 as well).

Now i have a problem to launch an agent, since stopping works fine :)
I'd like to launch an agent from an installer script (which runs as
root) in 10.5, because as you mentioned launchctl uses boostrap
namespace.
The solution i've been suggested is to call from within a script

ps ex | grep loginwindow | cut -c 1-5

this will give me a list of the PIDs  of all the "sample" processes of
all the users which run in users namespace.
Then i have to call something like
launchctl bsexec $FOUNDPID "sudo -u $USERNAME; launchctl load -w
/Library/LaunchAgents/MyAgent.plist"

which will change both first bootstrap context, and then the username
so eventually launchctl will talk to the needed instance of launchd. I
don't know how "alive" this solution is in the "real environment".

Why didn't Apple make any libraries to link to and to be able to
control applications launchd/kextload programmatically?

>
> What's the high-level goal here?  Uninstall?

Installing an agent without reboot

thanks


More information about the launchd-dev mailing list