On 2010 Oct 25, at 18:01, eveningnick eveningnick wrote:
As i understand, when a user doubleclicks my Cocoa application's bundle, the application is launched with non-root privileges, which means i can't "launchctl load" my agent into /Library/LaunchAgents - i can only do that with /Users/username/Library/LaunchAgents directory.
That is correct.
Also there's a /usr/include/launch.h file, which provides some kind of API, but i've no idea how to use it, and i am not sure if it gives any means to load/start/stop/unload agents. Maybe i could use it somehow?
Anyone know what he's talking about?
Most suggestions about programmatical management of agents are about calling "[NSTask launchProcess:@"launchctl {operation} {label}"]". If there is no other better choice, i'd take it
That's how I do it. It is sad, and a chore.
but i am a little confused about privileges - i won't be able to install an agent to /Library/LaunchAgents, will I?.
That is correct. You'd have to write a Privileged Helper Tool based on Better Authorization Sample to do that, which is a *really* big chore. You might find it a little easier if you use my framework… http://sheepsystems.com/sourceCode/authTasksCocoa.html
I'm sorry if i'm saying nonsense, but if i call "[NSTask launchProcess:@"sudo launchctl {operation} {label}"]" will it count as a launch from root, or this trick works only if its done in a terminal?
Nice try :) But that sudo won't work because there is no UI for the user to type in their credential. Sorry, you need a Privileged Helper Tool.
how can i determine, if my agent is running or not, from my Cocoa application? and how could i determine if it's registered (loaded, in terms of launchctl) or not? Calling [NSTask launchProcess:@"launchctl list | grep com.mycompany.myagent"] and then parsing the returned string seems wrong to me :/. Is it really the only choice?
That's the only choice that I know of. Again, this is quite sad.
I was also searching for the info on how to launch a process, that is a console one (Gui-less). Most suggestions are about using launchd instead of fork/exec-ing it. How is it done usually?
Well, yes. But launchd is *normally* used in this way, to launch gui-less tools. The path to your tool is the first element in the ProgramArguments array. If you wanted to launch an *app* with launchd, you'd need to use /usr/bin/open.
Should i necessarily create and save that *.plist to the /Users/username/Library/LaunchAgents/ directory and then call that "[NSTask launchProcess:@"sudo launchctl {operation} {label}"]"?, first with "launchctl load" and second with "launchctl start"?
Yes, but again, that sudo ain't going to work, but if you are launching an agent from ~/Library/LaunchAgents, you don't need sudo. Also, I use "launchctl load" but I've never even heard of "launchctl start". Sounds like it might be the same as putting RunAtLoad into the plist? That's what I do. You should appreciate that to use system-domain agents (/Library/LaunchAgents) instead of user-domain agents (~/Library/LaunchAgents), you are entering the realm of security and privileges, and greatly complicating your life. However, if you really need to do this, address questions in these areas to apple-cdsa@lists.apple.com.