Init.rc

`init.rc` is a critical configuration file used in the Android operating system. It is part of the Android init system, which is responsible for initializing the system during boot. The `init.rc` file defines the services and daemons that should be started, the properties that should be set, and the actions that should be taken during various stages of the boot process.


Here is a breakdown of some key sections and commands found in an `init.rc` file:

 Key Sections of `init.rc`

1. Import Statements:   - Used to include other configuration files.
   ```plaintext
   import /init.${ro.hardware}.rc
   ```

2. Service Definitions:
   - Define services and daemons that should be started.
   ```plaintext
   service servicename /path/to/executable [args]
       class class_name
       user username
       group groupname
       disabled
       oneshot
   ```

3.On Statements:
   - Define actions to be taken when specific events occur.
   ```plaintext
   on boot
       # Actions to take on boot
       start servicename

   on property:ro.secure=1
       # Actions to take when a property is set
       stop servicename
   ```

4. Set Property Commands:
   - Set system properties.
   ```plaintext
   setprop ro.debuggable 1
   ```

5. Mount Commands:
   - Mount filesystems.
   ```plaintext
   mount ext4 /dev/block/mmcblk0p1 /system
   ```

Example `init.rc` File

Here is a simplified example of an `init.rc` file:

```plaintext
# Import other configuration files
import /init.environ.rc
import /init.usb.rc

 Define actions to be taken on boot
on boot
    # Start essential services
    start ueventd
    start logd

 Set properties
    setprop ro.debuggable 1

 Define actions for certain properties
on property:sys.boot_completed=1
    # Start additional services after boot is complete
    start netd

 Define a service
service ueventd /sbin/ueventd
    class core
    user root
    group root
    critical

service logd /system/bin/logd
    class main
    user logd
    group logd
    oneshot

 Define another service
service netd /system/bin/netd
    class main
    user root
    group root
    disabled
```

Explanation

- Import Statements:**
  - Include additional configuration files based on the device's hardware and environment.

- On Boot:*
  - Start essential services like `ueventd` and `logd`.
  - Set the `ro.debuggable` property to `1`.

- On Property:
  - When the `sys.boot_completed` property is set to `1`, indicating that the boot process is complete, start the `netd` service.

- Service Definitions:
  - Define the `ueventd` service, which is critical and runs as root.
  - Define the `logd` service, which runs once (`oneshot`).
  - Define the `netd` service, which is initially disabled and will be started later.

Key Commands and Their Descriptions

- import:** Includes other init configuration files.
- on <event>:* Specifies actions to be taken when the specified event occurs.
- service <name> <path> [args]:** Defines a service to be managed by init.
  - `class <name>`: Groups services for easier management.
  - `user <username>`: Specifies the user to run the service as.
  - `group <groupname>`: Specifies the group to run the service as.
  - `disabled`: The service will not start automatically.
  - `oneshot`: The service will not be restarted if it exits.
  - `critical`: Marks the service as critical, triggering a device reboot if it exits unexpectedly.
- setprop <name> <value>:* Sets a system property.
- start <service_name>:* Starts a specified service.
- stop <service_name>:* Stops a specified service.
- mount <filesystem> <device> <mount_point>:** Mounts a filesystem.

Understanding and configuring `init.rc` is crucial for customizing the Android boot process and managing services effectively.


Init.rc Flow chart





The Important funtions of Init.rc




Download the AOSP source File and go to the contents shown below





  The Important Files to be understand the functionalities of the Init.rc file are as follows:

  • init.cpp
  • main.cpp
  • property_service.cpp
  • first_stage_init.cpp
ROOT DIR : init.zygote64.rc


To understand various components initiated by init.rc file they are as follows:

*Actions
*Triggers
*Services

ACTIONS:

  • They are the start of a series of commands
  • They have a trigger

  • FORMAT of Actions

on <trigger> [&& <trigger>]*

<command1>

<command2>

TRIGGERS:

- Event or condition that initiates some action within an app

- They are subdivided into :

* event triggers * property triggers

  • FORMAT of Triggers

on boot && property:a=b

SERVICES:

- It is a program that starts with service and is started by the init process

- It runs in another child process of init

  • FORMAT of Services

service <name> <pathname> [<argument> ]*

<option>


For Any doubts, suggestions and queries , Reach me through the contact details mentioned within the blog. Thanks for reading!

Comments

Popular Posts