The init.rc
file in Android is a crucial configuration file that defines how the Android system initializes during boot. It uses a specific syntax to define services, actions, dependencies, and other initialization tasks. Below, I’ll outline the syntax elements of init.rc
with clear examples for each:
service
Directive
The service
directive defines a service that should be started and managed by the Android Init system.
Syntax:
service <name> <binary_path>
<property1>
<property2>
...
Properties:
class
: Specifies the class of the service (main
,graphics
,radio
, etc.).user
: Specifies the user under which the service runs (system
,radio
,graphics
, etc.).group
: Specifies the group under which the service runs (system
,radio
,graphics
, etc.).critical
: Indicates that the service is critical for system operation.oneshot
: Indicates that the service should be started only once.restart
: Specifies conditions under which the service should be restarted.
Example:
service myservice /system/bin/myservice
class main
user system
group system
oneshot
on
Directive
The on
directive specifies actions to be performed in response to specific system events.
Syntax:
on <event_name>
<action1>
<action2>
...
Events:
boot
: Triggered when the system boots up.early-init
: Triggered during the early initialization phase.late-init
: Triggered during the late initialization phase.shutdown
: Triggered when the system shuts down.
Example:
on boot
start myservice
action
Directive
The action
directive specifies a command or action to be executed.
Syntax:
action <action_name> <command>
Example:
action mount_partitions exec /system/bin/mount_partitions.sh
start
Directive
The start
directive starts a service explicitly.
Syntax:
start <service_name>
Example:
start myservice
stop
Directive
The stop
directive stops a service explicitly.
Syntax:
stop <service_name>
Example:
stop myservice
restart
Directive
The restart
directive restarts a service explicitly.
Syntax:
restart <service_name>
Example:
restart myservice
Full Example init.rc
Here’s a simplified example of an init.rc
file that combines these directives:
# Example init.rc
service servicemanager /system/bin/servicemanager
class main
user system
group system
critical
service surfaceflinger /system/bin/surfaceflinger
class main
user system
group graphics
on boot
start servicemanager
start surfaceflinger
In this example:
servicemanager
andsurfaceflinger
are defined as services.- They are started during the
boot
event using thestart
directive. servicemanager
is marked as critical (critical
property).
Notes:
- Ensure proper indentation in
init.rc
for clarity and consistency, although indentation is not strictly required for syntax. - Understand dependencies (
requires
,requires_mounts_for
) when defining services to ensure correct initialization order. - After modifying
init.rc
, it may require rebooting the device or reloading the configuration to take effect.
The init.rc
file is fundamental in Android for defining how system components and services are managed during the boot process, contributing significantly to the stability and functionality of the operating system.