Thursday, June 15, 2006

launchd and plists

Mac OS X replaces init, xinetd and some other things with "launchd". Launchd is capable of starting daemons and restarting them if they die. Launchd can be controlled by the command 'launchctl". OS X has man pages for both launchd and launchctl. If you're familiar with xinetd, launchd doesn't look too alien. In xinetd, services are configured in files under /etc/xinetd.d, and each file contains parameters that describe the service: what program to run, what port it listens on, whether to start it automatically, etc.. Launchd uses OS X "Property List" files in a similar way to describe services.

"Property List" (or "plist") files can be either ASCII XML or a binary format derived from the XML. The command "plutil" is able to convert from one format to the other. With the "-lint" qualifier, you can also use plutil to check the syntax of a plist file.

Here's an example of what an ASCII plist file looks like. In this case, it's for the telnet service:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "
;http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Disabled</key>
<true/>
<key>Label</key>
<string>com.apple.telnetd</string>
<key>ProgramArguments</key>
<array>
<string>/usr/libexec/telnetd</string>
</array>
<key>inetdCompatibility</key>
<dict>
<key>Wait</key>
<false/>
</dict>
<key>Sockets</key>
<dict>
<key>Listeners</key>
<dict>
<key>SockServiceName</key>
<string>telnet</string>
<key>Bonjour</key>
<true/>
</dict>
</dict>
</dict>
</plist>


Launchd is run at boot time, as the first process (PID = 1) started by the kernel (like "init" in Linux systems). Launchd then fires up (and maintains) other processes, as described in its configuration files. These are plist files, and they live in several different places. The launchd man page gives the following breakdown:

~/Library/LaunchAgents Per-user agents provided by the user.
/Library/LaunchAgents Per-user agents provided by the adminis-
trator.
/Library/LaunchDaemons System wide daemons provided by the admin-
istrator.
/System/Library/LaunchAgents Mac OS X Per-user agents.
/System/Library/LaunchDaemons Mac OS X System wide daemons.

The "Daemons" items are executed as root. The "Agents" items are executed as some designated user.

The list of allowable properties in launchd plist files can be found in the launchd.plist man page. A good source of general information about launchd is this wikipedia article.

In the next article, I'll give an example of a plist file for adding a simple service to be started at boot time.

No comments: