While the First Stage init focuses purely on mounting partitions from the ramdisk, the Second Stage init is responsible for launching the massive Android user space environment.
This phase is entirely driven by the complex event loop and property system managed by the init binary.
The Property Service
One of the most powerful features managed by init is the Android Property System (often accessed via getprop and setprop in the ADB shell).
The Property System is a globally accessible, shared memory dictionary of key-value pairs (e.g., ro.build.version.release = 14).
- Initialization: During the Second Stage,
initreads thebuild.propfiles located in/system,/vendor, and/productand securely loads them into memory. - Triggering Events: Properties are not just static text. They are dynamic triggers. An
init.rcscript can say: "Do not start the Wi-Fi daemon until the propertysys.boot_completedequals1."
# Changing a property to trigger an action (if you have root)
adb shell setprop ctl.start surfaceflinger
Actions and Services
The .rc scripts parsed by init contain two fundamental building blocks: Actions and Services.
1. Actions
Actions are sequences of commands (like mkdir, chown, write) that are executed when a specific "trigger" occurs.
on post-fs-data
# This action triggers after the userdata partition is mounted
mkdir /data/vendor/wifi 0770 wifi wifi
mkdir /data/misc/camera 0770 camera camera
2. Services
Services are executable programs that init will launch as background daemon processes.
service surfaceflinger /system/bin/surfaceflinger
class core
user system
group graphics drmrpc
onrestart restart zygote
This tells init to launch the surfaceflinger binary, securely assign it to the system user, and if it crashes, init must also force restart the zygote process.
The Trigger Sequence
init does not execute actions randomly. It follows a strictly ordered, hardcoded sequence of triggers in C++ to ensure dependencies are met.
early-init: Basic memory management and cgroups setup.init: General system setup.fs: Mounts the remaining file systems (if any).post-fs-data: Executed after the/datapartition is mounted and decrypted. This is when native daemons can finally create their private folders.boot: This trigger signals that the base native environment is ready, and it is time to start the massive Java framework processes (the Zygote).