Advanced AOSP Subsystems
2 min read

init.rc Language

Deep Dive: init.rc Language

Android's init process uses a custom, declarative scripting language defined in .rc files. These files dictate how the system boots, what services are launched, and how permissions are configured at an OS level.

Actions: Triggers and Commands

An Action is a sequence of commands that execute when a specific trigger condition is met. Actions form the procedural backbone of the boot sequence.

  • Trigger: Defines when the action runs (e.g., on boot, on property:sys.boot_completed=1).
  • Commands: Executed sequentially when the trigger fires (e.g., mkdir, chown, setprop).
on post-fs-data
    # Create the directory for Wi-Fi data
    mkdir /data/vendor/wifi 0770 wifi wifi
    chown wifi wifi /data/vendor/wifi

Services: Name, Binary Path, and Arguments

A Service defines a background process managed by the init daemon. If a service crashes, init can automatically restart it based on its configuration.

A service declaration includes:

  • Name: A unique identifier for the service.
  • Binary Path: The absolute path to the executable.
  • Arguments: Command-line arguments passed to the executable.
service logd /system/bin/logd
    socket logd stream 0666 logd logd
    socket logdr seqpacket 0666 logd logd
    socket logdw dgram+passcred 0222 logd logd
    file /proc/kmsg r
    file /dev/kmsg w
    user logd
    group logd system package_info readproc
    capabilities SYSLOG AUDIT_CONTROL setgid setuid setpcap

Service Options

Options modify how and when a service runs:

  • class <name>: Assigns the service to a class (e.g., core, main). Services in the same class can be started/stopped together.
  • user <username>: Changes the user identity before executing the binary.
  • group <groupname>: Changes the group identity.
  • seclabel <context>: Assigns a specific SELinux context to the service.
  • capabilities: Grants specific Linux capabilities (e.g., NET_ADMIN, SYS_ADMIN) without requiring full root access.
  • oneshot: Ensures the service runs only once and is not restarted if it exits.

The import Directive

To keep the boot configuration modular, init.rc files can include other .rc files using the import directive.

import /init.environ.rc
import /init.usb.rc
import /vendor/etc/init/hw/init.${ro.hardware}.rc

This allows silicon vendors and OEMs to maintain their own hardware-specific .rc files without modifying the core system init.rc.