[launchd-dev] Programmatic interface to launchctl and some other questions, OS X 10.5

eveningnick eveningnick eveningnick at gmail.com
Mon Oct 25 18:01:18 PDT 2010


Hello
I am writing a Cocoa application, that installs and launches an agent
(and, if needed - stops that agent, and uninstalls it). This agent  is
supposed to watch for a launch of another certain application, and if
it encounters such an event, it launches some process itself.

I am wondering, how could i  programmatically register (launchctl's
load operation), start (launchctl's start), stop and unload the agent,
doing these operations from my Cocoa application?
The target operating systems are Mac OS X 10.5 and 10.6.

Having searched through the Internet i have found ServiceManagement.h
(that allows, as i understand, installing application into
/Library/LaunchAgents, even if the application that called
SMJobBless/SMJobSumbin was non-root).
I am not very "familiar" with OS X ideas. 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.
But unfortunately this ServiceManagement API is supported only since OS X 10.6

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?

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, but i am a little
confused about privileges - i won't be able to install an agent to
/Library/LaunchAgents, will I?.
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?

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?

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? 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"?
OS X 10.6's ServiceManagement.h API seems to support launching of
processes without first copying plist to the one of the "agents'"
directories, but again it's only OS X 10.6.

Sorry for so many questions :) And Thanks a lot for the response!


More information about the launchd-dev mailing list