The Boot Process
From the moment a user turns on a Mac OS X system to beyond the time the login window appears, Mac OS X executes a boot sequence that readies the system for use. If you provide system services to all users, you might need to execute some code during this process. The following sections explain the basic boot sequence and the places where your code can tie into it.
BootROM
When the power to a Macintosh computer is turned on, the BootROM firmware is activated. BootROM (which is part of the computer’s hardware) has two primary responsibilities: it initializes system hardware and it selects an operating system to run. BootROM has two components to help it carry out these functions:
POST (Power-On Self Test) initializes some hardware interfaces and verifies that sufficient memory is available and in a good state.
For more information on how drivers are loaded, see I/O Kit Fundamentals.
Once the kernel and all drivers necessary for booting are loaded, the boot loader starts the kernel’s initialization procedure. At this point, enough drivers are loaded for the kernel to find the root device. Also from this point, on PowerPC-based Macintosh computers, Open Firmware is no longer accessible (quiesced).
Logout Responsibilities
The procedures for logging out, restarting the system, or shutting down the system have similar semantics. The foreground process usually initiates these procedures in response to the user choosing an item from the Apple menu; however, a process can also initiate the procedure programmatically by sending an appropriate Apple event to loginwindow. The loginwindow program carries out the procedure, posting alerts and notifying applications to give them a chance to clean up before closing.
Identifying the Scope of Processes
Although the launchd process owns every other process on the system, a distinction can still be made between user and system processes. Startup items, daemonized processes, and any processes run prior to loginwindow typically run in the root context. Processes in this context provide services to all users of the system.
Processes that run within the context of an authenticated user session are user processes. User processes are always associated with a particular user session and are usually children of the WindowServer or loginwindow processes associated with the user login.
Note: Not all user processes are children of the WindowServer process. Processes launched as root, and some special system processes, are owned by the user but are children of the launchd process. You can use the ActivityMonitor application to determine the owner and parent of any process on the system.
The Shutdown Process
At shutdown, Mac OS X first executes the service stop routines in any SystemStarter startup items such as those described in “Creating a Startup Item.”
Next, as with most UNIX-based and UNIX-like operating systems, Mac OS X sends a SIGTERM signal to all running processes prior to shutdown. Upon receiving this signal, your daemon should quickly make an orderly shutdown.
Every reasonable attempt will be made to wait for it to exit, but it is ultimately the responsibility of your daemon to keep the amount of unsaved state information to a level that can be reasonably written to disk in the time allotted.
A few seconds later, your daemon will receive a SIGKILL signal and will be terminated.