The init file is a key component of the Android boot sequence. It is a program to initialize the elements of the Android system. Unlike Linux, Android uses its own initialization program.
This Android init program processes 2 files, and it executes the commands it finds in both programs. These programs are: ‘init.rc’ and ‘init<machine name>.rc’ (this machine name is the name of the hardware that Android is running on).
What does each program contain?:
init<machine name>.rc is imported by the init.rc program.
What is the syntax of these .rc files?
The android init language consists of 4 classes of statements:
Actions, Commands, Services and Options.
Actions and Services declare new sections. All the commands or options belong to the section most recently declared.
Actions and Services have to have unique names. If a second Action or Service has the same name of a previous one, it is ignored.
Actions
Actions are sequences of commands. They have a trigger which is used to determine when the action should occur.
Actions take following form.
On <trigger>
<command>
<command>
<command>…
Services
Services are programs which init launches and (optionally) restart when it exists
Services take the following form.
Service <name> <patchname> [argument]
<option>
<option>…
Options
Options are modifiers to services. These affect how and when init runs a service.
Critical
This is a device-critical service. If it exits more than four times in four minutes, the device will reboor into recovery mode.
Disabled
This service will not automatically start with its class. It must be explicitly started by name.
Setenv <name> <value>
Set the environment variable <name> to <value> in the launched process.
User <username>
Change to username before executing this service. Currently defaults to root.
Group <groupname> [<groupname>]
Change to groupname before executing this service.
Oneshot
Do not restart the service when it exists
Class <name>
Specify a class name for the service. All services in a named class may be started or stopped together.
Onrestart
Execute a command when service restarts
Triggers
Triggers are strings which can be used to match certain kinds of events and used to cause an action to occur.
Boot
This is the first trigger that will occur when init starts (after /init.conf is loaded)
Commands:
Exec <path> [<arguments>]
Fork and execute a program (<path>). This will block until the program completes execution.
Export <name> <value>
Set the environment variable <name> equal to <value> in the global environment.
Ifup <interface>
Bring the network interface <interface> online.
Import <filename>
Parse and init config file, extending the current configuration.
Hostname <name>
Set the host name
Chdir <directory>
Change working directory
Chmod <octal-dmoe> <path>
Change file access permissions
Chwon <owner> <group> <path>
Change file owner and group
Chroot <directory>
Change process root directory
Class_start <serviceclass>
Start all services of the specified class if they are not already running.
Class_stop <serviceclass>
Stop all services of the specified class if they are currently running.
Domainname <name>
Set the domain name
Enable <servicename>
Turns a disabled service into an enabled one.
Insmod <path>
Install the module at <path>
Mkdir <path>
Create a directory at <path>
Mount <type><device><dir>
Attempt to mount the named device at the directory.
Restorecon <path>
Restore the file named by <path> to the security context specified in the file_contexts configuration.
Setcon <securitycontext>
set the current process security context to the specified string.
Setenforce 0|1
Set the SELinux system wide enforcing status. 0 = permissive. 1 = enforcing.
Setprop <name><value>
Set system property <name> to <value>
Setrlimit <resource><cur><max>
Set the rlimit for a resource
Setsebool <name><value>
Set SELinux Boolean <name> to <value>
Start <service>
Start a service
Stop <service>
Stop a service
Symlink <target><path>
Create a symbolic link at <path> with the value <target>
Trigger <event>
Trigger an event. Used to queue an action from another action
Wait <path>
Poll for the existence of the given file and return when found.
Write <path> <string>
Open the file at <path> and write a string to it.
Examples
How to run a script:
service my_service /data/test
class main
oneshot
Here we are declaring the service named 'my service' with location in /data/test. It belongs to the main class and will start along with any other service that belongs with that class and we declare that the service wont restart when it exits (oneshot).
Change file access permissions:
chmod 0660 /sys/fs/cgroup/memory/tasks
Here we are changing access permissions in path /sys/fs/cgroup/memory/tasks
Write a string to a file in a path:
write /sys/fs/cgroup/memory/memory/memory.move_charge_at_immigrate 1
Create a symbolic link:
symlink /system/etc /etc
Here we are creating a symbolic link to /system/etc -> /etc
Set a specific density of the display:
setprop ro.sf.lcd_density 240
Here we are setting a system property of 240 to ro.sf.lcd_density
Set your watchdog timer to 30 seconds:
service watchdog /sbin/watchdogd 10 20
class core
We are petting the watchdog every 10 seconds to get a 20 second margin
Change file owner:
chown root system /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
The new owner being 'root' from the group 'system'
Dear All,
This post is very useful as functionality of Init.rc has been explained with example.
I was trying to access a specific file from "/sdcard" through a script to and run some system setting based on the content in the file on "boot complete" event. I could not success to do that even there was no AVC denial message or any error in dmesg. I also tried to execute same on "post fs" but result was same.
I could not understand why this was not happening while other command in same