Software:launchd
Original author(s) | Dave Zarzycki |
---|---|
Developer(s) | Apple Inc. |
Initial release | April 29, 2005 |
Written in | C |
Operating system | macOS, FreeBSD, iOS, watchOS |
Type | Init daemon |
License | Proprietary software (previously APSL and later Apache License 2.0) |
launchd is an init and operating system service management daemon created by Apple Inc. as part of macOS to replace its BSD-style init and SystemStarter. There have been efforts to port launchd to FreeBSD and derived systems.
Components
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 macOS 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. launchd itself has no knowledge of these configuration files or any ability to read them - that is the responsibility of "launchctl".
launchctl is a command line application which talks to launchd using IPC and knows how to parse the property list files used to describe launchd jobs, serializing them using a specialized dictionary protocol that launchd understands. launchctl can be 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
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
, various scripts which scan through/System/Library/LaunchDaemons
and/Library/LaunchDaemons
, calling launchctl on the plists as needed, then launchd starts the login window.
In step 4, the startup scripts scan through a few different directories for jobs to run. There are two different directories that are scanned:
- The LaunchDaemons directories contain items that will run as root, generally background processes.
- The LaunchAgents directories 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 directories 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 launchctl scans through the job plists at boot time, it asks launchd to reserve and listen 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 no longer needed. 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.
Mac OS X Tiger, consequently, boots much faster than previous releases. The system only has to register the daemons that are to run and does not actually launch them until they are needed. In fact, the progress bar that appears during boot time is just a placebo application (named WaitingForLoginWindow[1]) 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 launchd dependencies on Tiger: IPC allows daemons to talk amongst themselves to work out dependencies, or daemons 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. SystemStarter was still supported up to OS X Mountain Lion, but was removed in OS X Yosemite.
launchctl
In 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 macOS.[2])
launchctl communicates with launchd via a Mach-specific IPC mechanism.
Property list
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 manual page for launchd.plist
.[3]
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 the user 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 subprocesses launched from a task launched by launchd will be killed when the task ends. Useful where a short-lived task starts a long-lived subtask, but may result in zombie processes. |
SessionCreate
|
Boolean (defaults to NO )
|
A boolean flag that defines whether a security session will be created for the task and its subprocesses. |
Socket activation protocol
The name of each key under Sockets will be placed into the environment of the job when it is run, and the file descriptor of that socket will be available in that environment variable. This differs from systemd's socket activation in that the name of a socket definition inside of the job configuration is hardcoded into the application. This protocol is less flexible, although it does not, as systemd does, require the daemon to hardcode a starting file descriptor (as of 2014, it is 3).[4]
History
The software was designed and coded by Dave Zarzycki at Apple. The company planned for all of the following to be superseded in OS X environments –
– and most of those things were superseded when launchd was introduced with Mac OS X v10.4 (Tiger).
In 2005, R. Tyler Croy ported launchd to FreeBSD as part of Google Summer of Code Project. It could not be run as PID 1 (only a session init), and it was not commonly used on that platform.[5]
In 2006, the Ubuntu Linux distribution considered using launchd. The option was rejected because the source code was subject to the Apple Public Source License – described as an "inescapable licence problem".[6] Ubuntu instead developed and switched to its own service management tool, Upstart.
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.[7] Most Linux distributions use systemd or Upstart, or continue with init, and the BSDs also continue with init.
In December 2013, R. Tyler Croy announced his intent to resume work on his port of launchd to FreeBSD, and his "openlaunchd" GitHub repo subsequently rose in activity.[8]
The last Wayback Machine capture of the Mac OS Forge area for launchd was in June 2012,[9] and the most recent open source version from Apple was 842.92.1 in code for OS X 10.9.5.
In 2014, with OS X 10.10 and iOS 8, Apple moved code for launchd to closed source libxpc.[10]
In August 2015 Jordan Hubbard and Kip Macy announced NextBSD, which is based on FreeBSD-CURRENT kernel while adding in Mach IPC, Libdispatch, notifyd, asld, launchd, and other components derived from Darwin, Apple's open-source code for OS X.
Apple open source release history
See also
- systemd: similar utility developed by Red Hat for use among various mainline Linux distributions
- Service Management Facility
- Super-server
- TCP Wrapper
- Operating system service management
References
- ↑ "Tiger Details". WaitingForLoginWindow. https://daringfireball.net/misc/2005/04/tiger_details.php#waitingforloginwindow.
- ↑ Mac OS X Manual Page For launchd.conf(5)
- ↑ Mac OS X Manual Page For launchd.plist(5)
- ↑ little-big-h (January 30, 2014). "node-launchd". https://www.npmjs.org/package/node-launchd.
- ↑ "Launchd". FreeBSD wiki. https://wiki.freebsd.org/launchd.
- ↑ "ReplacementInit". UbuntuWiki. https://wiki.ubuntu.com/ReplacementInit?action=show&redirect=upstart#head-43339edcdef26f39f7d6d0a93bb347e8ccac918d.
- ↑ Prabhakar, Ernest (August 7, 2006). "Apple Opens Up: Kernel, Mac OS Forge, iCal Server, Bonjour, Launchd". darwin-dev (Mailing list). Retrieved March 25, 2023.
- ↑ Croy, R Tyler. "The scratchiest neckbeard, or FreeBSD on my Thinkpad X200". unethicalblogger.com. http://unethicalblogger.com/2013/12/03/scratchiest-neckbeard-freebsd-x200.html.
- ↑ "launchd". Apple Inc.. http://launchd.macosforge.org/.
- ↑ Levin, Jonathan (2014). "Launchd – At Your Service!". p. 32. http://docs.macsysadmin.se/2014/pdf/Launchd_-_At_your_service.pdf. "… 10.10: moved to libxpc 559 (560 in iOS 8) – Source not available yet – and may not ever be – Libxpc is a closed source project …"
- ↑ "launchd 106 license file". https://opensource.apple.com/source/launchd/launchd-106/launchd/COPYING.
- ↑ "Mac OS X 10.4 - Source". https://opensource.apple.com/release/mac-os-x-104.html.
- ↑ "Mac OS X 10.4.1 - Source". https://opensource.apple.com/release/mac-os-x-1041.html.
- ↑ "launchd 106.3 license file". https://opensource.apple.com/source/launchd/launchd-106.3/launchd/COPYING.
- ↑ "Mac OS X 10.4.2 - Source". https://opensource.apple.com/release/mac-os-x-1042.html.
- ↑ "Mac OS X 10.4.3 - Source". https://opensource.apple.com/release/mac-os-x-1043.html.
- ↑ "Mac OS X 10.4.4.ppc - Source". https://opensource.apple.com/release/mac-os-x-1044ppc.html.
- ↑ "Mac OS X 10.4.5.ppc - Source". https://opensource.apple.com/release/mac-os-x-1045ppc.html.
- ↑ "launchd 106.10 license file". https://opensource.apple.com/source/launchd/launchd-106.10/launchd/COPYING.
- ↑ "Mac OS X 10.4.4.x86 - Source". https://opensource.apple.com/release/mac-os-x-1044x86.html.
- ↑ "Mac OS X 10.4.5.x86 - Source". https://opensource.apple.com/release/mac-os-x-1045x86.html.
- ↑ "launchd 106.13 license file". https://opensource.apple.com/source/launchd/launchd-106.13/launchd/COPYING.
- ↑ "Mac OS X 10.4.6.ppc - Source". https://opensource.apple.com/release/mac-os-x-1046ppc.html.
- ↑ "Mac OS X 10.4.6.x86 - Source". https://opensource.apple.com/release/mac-os-x-1046x86.html.
- ↑ "launchd 106.14 license file". https://opensource.apple.com/source/launchd/launchd-106.14/launchd/COPYING.
- ↑ "Mac OS X 10.4.7.ppc - Source". https://opensource.apple.com/release/mac-os-x-1047ppc.html.
- ↑ "Mac OS X 10.4.7.x86 - Source". https://opensource.apple.com/release/mac-os-x-1047x86.html.
- ↑ "Mac OS X 10.4.8.ppc - Source". https://opensource.apple.com/release/mac-os-x-1048ppc.html.
- ↑ "Mac OS X 10.4.9.ppc - Source". https://opensource.apple.com/release/mac-os-x-1049ppc.html.
- ↑ "Mac OS X 10.4.10.ppc - Source". https://opensource.apple.com/release/mac-os-x-10410ppc.html.
- ↑ "Mac OS X 10.4.11.ppc - Source". https://opensource.apple.com/release/mac-os-x-10411ppc.html.
- ↑ "launchd 106.20 license file". https://opensource.apple.com/source/launchd/launchd-106.20/launchd/COPYING.
- ↑ "Mac OS X 10.4.8.x86 - Source". https://opensource.apple.com/release/mac-os-x-1048x86.html.
- ↑ "Mac OS X 10.4.9.x86 - Source". https://opensource.apple.com/release/mac-os-x-1049x86.html.
- ↑ "Mac OS X 10.4.10.x86 - Source". https://opensource.apple.com/release/mac-os-x-10410x86.html.
- ↑ "Mac OS X 10.4.11.x86 - Source". https://opensource.apple.com/release/mac-os-x-10411x86.html.
- ↑ "Developer Tools 2.4 - Source". https://opensource.apple.com/release/developer-tools-24.html.
- ↑ "launchd 152 license file". https://opensource.apple.com/source/launchd/launchd-152/launchd/COPYING.
- ↑ "launchd 257 license file". https://opensource.apple.com/source/launchd/launchd-257/launchd/COPYING.
- ↑ "Mac OS X 10.5 - Source". https://opensource.apple.com/release/mac-os-x-105.html.
- ↑ "Mac OS X 10.5.1 - Source". https://opensource.apple.com/release/mac-os-x-1051.html.
- ↑ "Developer Tools 3.1 - Source". https://opensource.apple.com/release/developer-tools-31.html.
- ↑ "Developer Tools 3.1b - Source". https://opensource.apple.com/release/developer-tools-31b.html.
- ↑ "Developer Tools 3.1.1 - Source". https://opensource.apple.com/release/developer-tools-311.html.
- ↑ "Developer Tools 3.1.2 - Source". https://opensource.apple.com/release/developer-tools-312.html.
- ↑ "Developer Tools 3.1.3 - Source". https://opensource.apple.com/release/developer-tools-313.html.
- ↑ "Developer Tools 3.1.4 - Source". https://opensource.apple.com/release/developer-tools-314.html.
- ↑ "launchd 258.1 license file". https://opensource.apple.com/source/launchd/launchd-258.1/launchd/COPYING.
- ↑ "Mac OS X 10.5.2 - Source". https://opensource.apple.com/release/mac-os-x-1052.html.
- ↑ "launchd 258.12 license file". https://opensource.apple.com/source/launchd/launchd-258.12/launchd/COPYING.
- ↑ "Mac OS X 10.5.3 - Source". https://opensource.apple.com/release/mac-os-x-1053.html.
- ↑ "Mac OS X 10.5.4 - Source". https://opensource.apple.com/release/mac-os-x-1054.html.
- ↑ "launchd 258.18 license file". https://opensource.apple.com/source/launchd/launchd-258.18/launchd/COPYING.
- ↑ "Mac OS X 10.5.5 - Source". https://opensource.apple.com/release/mac-os-x-1055.html.
- ↑ "launchd 258.19 license file". https://opensource.apple.com/source/launchd/launchd-258.19/launchd/COPYING.
- ↑ "Mac OS X 10.5.6 - Source". https://opensource.apple.com/release/mac-os-x-1056.html.
- ↑ "launchd 258.22 license file". https://opensource.apple.com/source/launchd/launchd-258.22/launchd/COPYING.
- ↑ "Mac OS X 10.5.7 - Source". https://opensource.apple.com/release/mac-os-x-1057.html.
- ↑ "launchd 258.25 license file". https://opensource.apple.com/source/launchd/launchd-258.25/launchd/COPYING.
- ↑ "Mac OS X 10.5.8 - Source". https://opensource.apple.com/release/mac-os-x-1058.html.
- ↑ "launchd 328 license file". https://opensource.apple.com/source/launchd/launchd-328/launchd/COPYING.
- ↑ "Mac OS X 10.6 - Source". https://opensource.apple.com/release/mac-os-x-106.html.
- ↑ "Mac OS X 10.6.1 - Source". https://opensource.apple.com/release/mac-os-x-1061.html.
- ↑ "Mac OS X 10.6.2 - Source". https://opensource.apple.com/release/mac-os-x-1062.html.
- ↑ "Developer Tools 3.2.1 - Source". https://opensource.apple.com/release/developer-tools-321.html.
- ↑ "Developer Tools 3.2.2 - Source". https://opensource.apple.com/release/developer-tools-322.html.
- ↑ "Developer Tools 3.2.3 - Source". https://opensource.apple.com/release/developer-tools-323.html.
- ↑ "Developer Tools 3.2.4 - Source". https://opensource.apple.com/release/developer-tools-324.html.
- ↑ "launchd 329.3 license file". https://opensource.apple.com/source/launchd/launchd-329.3/launchd/COPYING.
- ↑ "Mac OS X 10.6.3 - Source". https://opensource.apple.com/release/mac-os-x-1063.html.
- ↑ "launchd 329.3.1 license file". https://opensource.apple.com/source/launchd/launchd-329.3.1/launchd/COPYING.
- ↑ "Mac OS X 10.6.4 - Source". https://opensource.apple.com/release/mac-os-x-1064.html.
- ↑ "Developer Tools 3.2.5 - Source". https://opensource.apple.com/release/developer-tools-325.html.
- ↑ "Developer Tools 3.2.6 - Source". https://opensource.apple.com/release/developer-tools-326.html.
- ↑ "launchd 329.3.2 license file". https://opensource.apple.com/source/launchd/launchd-329.3.2/launchd/COPYING.
- ↑ "Mac OS X 10.6.5 - Source". https://opensource.apple.com/release/mac-os-x-1065.html.
- ↑ "launchd 329.3.3 license file". https://opensource.apple.com/source/launchd/launchd-329.3.3/launchd/COPYING.
- ↑ "Mac OS X 10.6.6 - Source". https://opensource.apple.com/release/mac-os-x-1066.html.
- ↑ "Mac OS X 10.6.7 - Source". https://opensource.apple.com/release/mac-os-x-1067.html.
- ↑ "Mac OS X 10.6.8 - Source". https://opensource.apple.com/release/mac-os-x-1068.html.
- ↑ "launchd 392.18 license file". https://opensource.apple.com/source/launchd/launchd-392.18/launchd/COPYING.
- ↑ "Mac OS X 10.7 - Source". https://opensource.apple.com/release/mac-os-x-107.html.
- ↑ "Mac OS X 10.7.1 - Source". https://opensource.apple.com/release/mac-os-x-1071.html.
- ↑ "launchd 392.35 license file". https://opensource.apple.com/source/launchd/launchd-392.35/launchd/COPYING.
- ↑ "Mac OS X 10.7.2 - Source". https://opensource.apple.com/release/mac-os-x-1072.html.
- ↑ "launchd 392.36 license file". https://opensource.apple.com/source/launchd/launchd-392.36/launchd/COPYING.
- ↑ "Mac OS X 10.7.3 - Source". https://opensource.apple.com/release/mac-os-x-1073.html.
- ↑ "launchd 392.38 license file". https://opensource.apple.com/source/launchd/launchd-392.38/launchd/COPYING.
- ↑ "Mac OS X 10.7.4 - Source". https://opensource.apple.com/release/mac-os-x-1074.html.
- ↑ "launchd 392.39 license file". https://opensource.apple.com/source/launchd/launchd-392.39/launchd/COPYING.
- ↑ "Mac OS X 10.7.5 - Source". https://opensource.apple.com/release/mac-os-x-1075.html.
- ↑ "licence header in launchd 442.21 source code". https://opensource.apple.com/source/launchd/launchd-442.21/src/launchd.c.auto.html.
- ↑ "Mac OS X 10.8 - Source". https://opensource.apple.com/release/mac-os-x-108.html.
- ↑ "Mac OS X 10.8.1 - Source". https://opensource.apple.com/release/mac-os-x-1081.html.
- ↑ "license header in launchd 442.26.2 source code". https://opensource.apple.com/source/launchd/launchd-442.26.2/src/launchd.c.auto.html.
- ↑ "Mac OS X 10.8.2 - Source". https://opensource.apple.com/release/mac-os-x-1082.html.
- ↑ "Mac OS X 10.8.3 - Source". https://opensource.apple.com/release/mac-os-x-1083.html.
- ↑ "Mac OS X 10.8.4 - Source". https://opensource.apple.com/release/mac-os-x-1084.html.
- ↑ "Mac OS X 10.8.5 - Source". https://opensource.apple.com/release/mac-os-x-1085.html.
- ↑ "license header in launchd 842.1.4 source code". https://opensource.apple.com/source/launchd/launchd-842.1.4/src/launchd.c.auto.html.
- ↑ "OS X 10.9 - Source". https://opensource.apple.com/release/os-x-109.html.
- ↑ "OS X 10.9.1 - Source". https://opensource.apple.com/release/os-x-1091.html.
- ↑ "license header in launchd 842.90.1 source code". https://opensource.apple.com/source/launchd/launchd-842.90.1/src/launchd.c.auto.html.
- ↑ "OS X 10.9.2 - Source". https://opensource.apple.com/release/os-x-1092.html.
- ↑ "OS X 10.9.3 - Source". https://opensource.apple.com/release/os-x-1093.html.
- ↑ "license header in launchd 842.92.1 source code". https://opensource.apple.com/source/launchd/launchd-842.92.1/src/launchd.c.auto.html.
- ↑ "OS X 10.9.4 - Source". https://opensource.apple.com/release/os-x-1094.html.
- ↑ "OS X 10.9.5 - Source". https://opensource.apple.com/release/os-x-1095.html.
External links
Apple Developer:
- Mac Developer Library: Daemons and Services Programming Guide: Creating Launch Daemons and Agents
- Mac Developer Library: Technical Note TN2083: Daemons and Agents
Apple Developer Retired Documents Library:
- Darwin and macOS System Manager's Manual : System wide daemon and per-user agent manager –
- Darwin and macOS File Formats Manual –
- Darwin and macOS File Formats Manual –
- Darwin and macOS General Commands Manual –
Other links:
- Apple Developer Library:
- Launchd: One Program to Rule them All - YouTube – presentation by Dave Zarzycki uploaded October 8, 2007 by GoogleTechTalks
- wiki
.freebsd .org /launchd - developer
.apple .com /library /archive /documentation /MacOSX /Conceptual /BPSystemStartup /
Original source: https://en.wikipedia.org/wiki/Launchd.
Read more |