Launchd - Startup Items Mac

launchd  - startup items mac

In computing, launchd, a unified service-management framework, starts, stops and manages daemons, applications, processes, and scripts.

launchd  - startup items mac
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):

  1. Open Firmware activates, initializes the hardware, and then loads BootX.
  2. BootX loads the kernel, spins the pinwheel cursor, and loads any needed kernel extensions (kexts).
  3. The kernel loads launchd.
  4. 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:

  1. The LaunchDaemons directories contain items that will run as root, generally background processes.
  2. 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 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) 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 lead to cleaner and quicker startups. SystemStarter was still supported up to OS X Mountain Lion, but was removed in OS X Yosemite.

launchctl

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 macOS.)

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 manpage for launchd.plist.

launchd  - startup items mac
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).

launchd  - startup items mac
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 â€"

  • init
  • rc
  • init.d script
  • rc.d script
  • SystemStarter
  • inetd / xinetd
  • crond / atd
  • watchdogd

â€" 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.

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". 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. 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.

The last Wayback Machine capture of the Mac OS Forge area for launchd was in June 2012, 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.

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.

0 komentar: