launchd
| Original author(s) | Apple Computer |
|---|---|
| Initial release | 29 April 2005 |
| Development status | Active |
| Written in | C |
| Operating system | Mac OS X |
| Type | System Startup software |
| License | Apache License 2.0 |
| Website | launchd.macosforge.org[dead link] |
launchd is a unified, open-source service management framework for starting, stopping and managing daemons, applications, processes, and scripts. Written and designed by Dave Zarzycki at Apple, it was introduced with Mac OS X Tiger and is licensed under the Apache License.
The launchd daemon is essentially a replacement for:
Apple has stated that it intends to eliminate all of the aforementioned services in favor of launchd.[1]
For Mac OS X v10.4 Apple moved most of the processes handled by the previously mentioned daemons to launchd. By consolidating all the launch services into one program, launchd significantly shortens boot time on slow computers.[citation needed]
Contents |
Components [edit]
There are two main programs in the launchd system: launchd and launchctl.
launchd manages the daemons at both a system and user level. Similar to xinetd, launchd can start daemons on demand. Similar to watchdogd, launchd can monitor daemons to make sure that they keep running. launchd also has replaced init as PID 1 on Mac OS X and as a result it is responsible for starting the system at boot time.
Configuration files define the parameters of services run by launchd. Stored in the LaunchAgents and LaunchDaemons subdirectories of the Library folders, the property list-based files have approximately thirty different keys that can be set.
launchctl is a command line application used to load and unload daemons, start and stop launchd controlled jobs, get system utilization statistics for launchd and its child processes, and set environment settings.
launchd [edit]
launchd has two main tasks. The first is to boot the system, and the second is to load and maintain services.
Here is a simplified view of the Mac OS X Tiger system startup on a PowerPC Mac (on an Intel Mac, EFI replaces Open Firmware and boot.efi replaces BootX):
- Open Firmware activates, initializes the hardware, and then loads BootX.
- BootX loads the kernel, spins the pinwheel cursor, and loads any needed kernel extensions (kexts).
- The kernel loads launchd.
- launchd runs
/etc/rc, scans through/System/Library/LaunchAgentsand/Library/LaunchDaemonsand acts on the plists as needed, and starts the login window.
In step 4, launchd scans through a few different directories for jobs to run. There are two different folders that are scanned. The LaunchDaemons folders contain items that will run as root, generally background processes. The LaunchAgents folders contain jobs, called agent applications, that will run as a user or in the context of userland. These may be scripts or other foreground items, and they can even include a user interface. These directories are all kept in the typical Library folders of Mac OS X.
Launchd is very different from SystemStarter in that it may not actually launch all the daemons at boot time. Key to launchd, and similar to xinetd, is the idea of launch on demand daemons. When launchd scans through the job plists at boot time it reserves and listens on all of the ports requested by those jobs. If so indicated in the plist by the "OnDemand" key, the daemon is not actually loaded at the time. Rather, launchd will listen on the port, start the daemon when needed, and shut it down when it is not. After a daemon is loaded, launchd will keep track of it and make sure it is running if needed. In this way it is like watchdogd, and shares watchdogd's requirement that processes do not attempt to fork or daemonize on their own. If a process goes into the background launchd will lose track of it and attempt to relaunch it.
Consequently, Mac OS X Tiger boots much faster than previous releases. The system only has to register the daemons that are to run, not actually launch them. In fact, the progress bar that appears during boot time is just a placebo application (named WaitingForLoginWindow[2]) that does not really show anything other than the passage of time.
The hardest part to manage during a launchd boot is dependencies. SystemStarter had a very simple system of dependencies that used the "Uses", "Requires", and "Provides" keys in the plist of a startup item. There are two main strategies when creating launch dependencies on Tiger. Using IPC will allow the daemons to talk amongst themselves to work it out, or you can watch files or paths for changes. Using IPC is much more subtle than the SystemStarter's keys and requires more work from the developer, but it may[citation needed] lead to cleaner and quicker startups. The SystemStarter is an option that is still supported at this time, but it has been reduced in importance in Mac OS X Tiger; it may not be available in future OS X versions.
launchctl [edit]
With the other daemon starting facilities, mechanisms for service control are strewn across the OS with no central way to manage them. With launchd, control of services is centralized in the launchctl application.
On its own, launchctl can take commands from the command line, from standard in, or operate in interactive mode. With superuser privileges, launchctl can be used to make changes on a global scale. A set of launchctl commands can be made permanent when stored in /etc/launchd.conf. (A per-user ~/.launchd.conf file appears to have been considered, but is not supported in any existing version of OS X.[3])
Property list [edit]
A property list (plist) is a type of file that launchd uses for program configuration. When launchd scans a folder, or a job is submitted with launchctl, it reads a plist file that describes how the program is to be run.
A list of often used keys follows below. All keys are optional unless otherwise noted. For a full list, see Apple's manpage for launchd.plist.[4]
| Key | Type | Description |
|---|---|---|
Label |
String | The name of the job. By convention, the job label is the same as the plist file name, without the .plist extension. Required. |
Program |
String | A path to an executable. Useful for simple launches. At least one of Program or ProgramArguments is required. |
ProgramArguments |
Array of strings | An array of strings representing a UNIX command. The first string is generally a path to an executable, while latter strings contain options or parameters. At least one of Program or ProgramArguments is required. |
UserName |
String (defaults to root or current user) |
The job will be run as the given user, who may (or may not) be who submitted it to launchd. |
OnDemand
(Deprecated since 10.5) |
Boolean (defaults to YES) |
Deprecated as of 10.5 with the more powerful KeepAlive option. A boolean flag that defines if a job runs continuously or not. |
RunAtLoad |
Boolean (defaults to NO) |
A boolean flag that defines if a task is launched immediately when the job is loaded into launchd. |
StartOnMount |
Boolean (defaults to NO) |
A boolean flag that defines if a task is launched when a new filesystem is mounted. |
QueueDirectories |
Array of strings | Watch a directory for new files. The directory must be empty to begin with, and must be returned to an empty state before QueueDirectories will launch its task again. |
WatchPaths |
Array of strings | Watch a filesystem path for changes. Can be a file or folder. |
StartInterval |
Integer | Schedules job to run on a repeating schedule. Indicates number of seconds to wait between runs. |
StartCalendarInterval |
Dictionary of integers or Array of dictionaries of integers |
Job scheduling. The syntax is similar to cron. |
RootDirectory |
String | The job will be chrooted into this directory before execution. |
WorkingDirectory |
String | The job will be chdired into this directory before execution. |
|
|
String | Keys to determine files for input and output for the launched process. |
LowPriorityIO |
Boolean | Tells the kernel that this task is of a low priority when doing filesystem I/O. |
AbandonProcessGroup |
Boolean (defaults to NO) |
A boolean flag that defines whether subprocess launched from tasks launched by launchd will be killed when the tasks end. Useful where a short-lived task starts a long-lived subtask, but may result in zombie processes. |
Use outside Mac OS X [edit]
A port to FreeBSD was done as part of Google Summer of Code Project in 2005. However, it is not commonly used on that platform.
The Ubuntu Linux distribution considered using launchd in 2006. However, launchd was rejected as an option because it was released under the Apple Public Source License – which at the time was described as an "inescapable licence problem".[5] Ubuntu instead developed and switched to Upstart, its own service management tool.
In August 2006, Apple relicensed launchd under the Apache License, Version 2.0 in an effort to make adoption by other open source developers easier.[6] However, most common open source operating systems use systemd or continue with init instead.
See also [edit]
- Daemontools
- Runit
- eINIT
- Initng
- Operating System service management
- Super-server
- TCP Wrapper
- Upstart
- Service Management Facility
- systemd
References [edit]
- ^ "Mac OS X Manual Page For launchd(8)". Retrieved 2008-01-08.
- ^ Daring Fireball: Tiger Details
- ^ Mac OS X Manual Page For launchd.conf(5)
- ^ Mac OS X Manual Page For launchd.plist(5)
- ^ "ReplacementInit". UbuntuWiki. Retrieved 2007-07-02.
- ^ Prabhakar, Ernest (2006-08-07). "Apple Opens Up: Kernel, Mac OS Forge, iCal Server, Bonjour, Launchd". Retrieved 2007-07-02.
External links [edit]
- System Startup Programming Topics: Creating launchd Daemons and Agents at developer.apple.com
- Technical Note TN2083: Daemons and Agents at developer.apple.com
- Getting Started with launchd cache on web.archive.org originally at developer.apple.com
- launchd in Depth from AFP548, published July 8, 2005. The basis for much of this article.
- launchd: One Program to Rule Them All – Google Tech talk video presentation by the developer of launchd.
- All About launchd Items (and How To Make One Yourself): The creation of property lists for launchd
- launchd site at macosforge.org contains the official subversion repository for the launchd source.
- launchd's source code at Mac OS Forge (click on "trunk", then "Zip Archive" to get the full source)
- launchd - FreeBSD-Wiki, launchd ported to FreeBSD
Online Unix Manual References [edit]
- : System wide daemon and per-user agent manager – Darwin and Mac OS X System Manager's Manual
- – Darwin and Mac OS X File Formats Manual
- – Darwin and Mac OS X File Formats Manual
- – Darwin and Mac OS X General Commands Manual