android init.rc Syntax

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 and surfaceflinger are defined as services.
  • They are started during the boot event using the start 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值