The Android Init language contains four main types of statements: actions, commands, services, and options.
All of these are line-oriented and consist of tags separated by Spaces. C-style backslash escapes can be used to insert Spaces into tags. You can also use double quotes to prevent Spaces from splitting text into multiple tags. A backslash can be used for line folding when it is the last character in a line.
Lines starting with # are comments.
Action and Service implicitly declare a new Section. All commands or options belong to the most recently declared Section. Commands or options before the first Section are ignored.
Action and Service have unique names. If the second Action or Service is declared with the same name as an existing Action or Service, it is ignored as an error.
A, the Action
Action is called a sequence of commands. Actions have triggers that determine when the Action should be executed. When an event matching the trigger of an Action occurs, that Action is added to the end of the queue to execute (unless it is already in the queue). Each Action in the queue is enqueued sequentially, and each command in that Action is executed sequentially. The Init process also handles other activities (device creation/destruction, property setting, and process restart) between command execution.
The Action format is as follows:
on <trigger>
<command>
<command>
<command>
Copy the code
Second, the Service
Services are programs used for Init startup, and they are (optionally) restarted on exit.
service <name> <pathname> [ <argument> ]*
<option>
<option>
...
Copy the code
Third, the Option
Options are modifiers for the service. They affect how and when Init runs the service.
3.1 critical
This is a critical equipment service. If you exit the device more than four times within four minutes, the device will restart and enter recovery mode.
3.2 disabled
The service does not automatically start with its classes. Must be started explicitly by name.
3.3 the setenv < name > < value >
During startup, set the environment variable to.
3.4 Socket <name> <type> <perm> [<user> [<group> [<seclabel>]]]
Create a Unix domain socket named /dev/socket/<name> and pass its FD to the boot process.
<type> must be “dgram”, “stream”, and “seqpacket”.
The default user and group values are 0.
“Seclabel” is the SELinux security context for a socket. It defaults to a service security context such as secLabel or computations based on a service executable security context.
3.5 the user < username >
Before running this service, change it to username <username>.
The current default is root.
Currently, you cannot use this command if your process requires Linux functionality. Instead, you must request in-process functionality while still root and then bring it down to the desired UID.
3.6 Group <groupname> [<groupname>]*
Before performing this service, switch the group to < groupName >. Group names other than the first group (required) are used to set supplementary groups for the process (via setGroups ()).
The current default is root.
3.7 seclabel < seclabel >
Change to < secLabel > before executing this service.
Mainly used for services running from RootFS, such as UeventD and ADBD. Services on system partitions can use transformations defined by policies based on their file security context. If not specified and the transformation is not defined in the policy, the default is the Init context.
3.8 oneshot
Do not restart the service when you exit it.
3.9 class < name >
Specify the class name of the service. All services in a named class can be started or stopped together. If the service class name is not specified with the class option, the service is in the “Default” class.
3.10 onrestart
This command is executed when the service restarts.
3.11 writepid < file… >
Fork writes the pid of the child process to the given file.
Fourth, the Trigger
A trigger is a string that can be used to match certain types of events and to cause an action to occur.
4.1 the boot
This is the first trigger that will happen when Init starts (after loading /init.conf).
4.2 < name > = < value >
This form of trigger occurs when the attribute <name> is set to a specific value <value>.
You can also test multiple properties to execute a set of commands. Such as:
on property:test.a=1 && property:test.b=1
setprop test.c 1
Copy the code
The above fragment sets test.c to 1 only if test.a = 1 and test.b = 1.
Fifth, the Command
5.1 bootchart_init
If configured, start the boot diagram. This is included in the default init.rc.
5.2 chmod < octal mode – > < path >
Change file access permissions.
5.3 chown <owner> <group> <path>
Change the file owner and group.
5.4 class_start < serviceclass >
Starts all services for the specified class if they are not already running.
5.5 class_stop < serviceclass >
If currently running, stops and disables all services for the specified class.
5.6 class_reset < serviceclass >
If all services of the specified class are currently running, stop them without disabling them. You can restart them later using class_start.
5.7 copy < SRC > < DST >
Copy files. Similar to writing, but useful for binary/large amounts of data.
5.8 domainname < name >
Set the domain name.
5.9 enable < servicename >
Turn a disabled service into an enabled service as if the service were not specified to be disabled.
If the service is assumed to be running, it starts immediately.
Typically used when the boot loader sets a variable indicating that a particular service should be started when needed. Such as:
on property:ro.boot.myfancyhardware=1
enable my_fancy_service_for_my_fancy_hardware
Copy the code
5.10 exec [< secLabel > [<user> [<group>]*]] — <command> [<argument>]*
Fork with the given argument and execute the command. The command starts after “-” to provide optional security contexts, users, and supplementary groups. No other commands will be run until this command is completed. < secLabel > can be “-” to indicate the default value.
5.11 the export < name > < value >
Set the environment variable <name> to equal <value> in the global environment (to be inherited by all processes started after executing this command).
5.12 the hostname < name >
Set the host name.
5.13 ifup < interface >
Bring the network interface <interface> online.
5.14 the import < filename >
Parse an init configuration file to extend the current configuration.
5.15 insmod < path >
Install the module at <path>.
5.16 load_all_props
Load attributes from /system, /vendor, etc. This is included in the default init.rc.
5.17 load_persist_props
After decrypting /data, the persistent properties are loaded. This is included in the default init.rc.
5.18 loglevel < level >
Set the kernel logging level to level. Property is expanded in <level>.
5.19 mkdir <path> [mode] [owner] [group]
Create a directory in <path>, optionally using the given schema, owner, and group. If no, use permission 755 to create a directory that is owned by user root and root group. If the directory already exists, the schema, owner, and group are updated.
5.20 mount_all < fstab >
Call fs_mgr_mount_all on the given fs_mgr format fstab.
5.21 Mount <type> <device> <dir> [< flag>]* [<options>]
Try to mount the named device in the directory <dir>
<device> specifies the MTD block device name in the form mtd@name.
<flag> Includes ro, RW, remount, noatime…
<options> Includes barrier=1, noauto_da_alloc, discard… Comma-separated strings, for example: barrier=1,noauto_da_alloc
5.22 powerctl
The internal implementation details are used in response to changes to the “sys.powerctl” system property for the restart.
5.23 restart < service >
Like stop, but can’t help service.
5.24 restorecon <path> [<path>]*
Restore the file named <path> to the security context specified in the File_Contexts configuration. Directories created by init.rc are not needed because they are automatically marked correctly by init.
5.25 restorecon_recursive <path> []*
Recurse the directory tree named <path> to the security context specified in the File_Contexts configuration.
5.26 the rm < path >
Call unlink(2) on the given path. You might want to use “exec — rm…” Instead (if the system partition is already installed).
5.27 rmdir < path >
Call RMdir (2) on the given path.
5.28 setprop < name > < value >
Set the system property <name> to <value>.
5.29 setrLimit <resource> <cur> < Max >
Sets the Rlimit for a resource.
5.23 start < service >
If the service is not already running, start it.
5.24 stop < service >
If the service is currently running, stop it.
5.25 swapon_all < fstab >
Call fs_mgr_swapon_all on the given fstab file.
5.26 symlink < target > < path >
Create a symbolic link at <path> with a value of <target>
5.27 sysclktz < mins_west_of_gmt >
Set the system clock base (0 if the system clock is timed in GMT)
5.28 the trigger < event >
Trigger event. Use to queue an Action within another Action.
5.29 verity_load_state
Internal implementation details for loading dM-Verity state.
5.30 verity_update_state < mount_point >
Internal implementation details for updating dM-Verity status and setting partitions. Since fs_Mgr itself cannot set them directly, the <mount_point>.verified attribute used by ADB is re-installed.
5.31 Wait <path> [<timeout>]
Polls for the existence of a given file and returns if a file is found or times out. If timeout is not specified, the current default is five seconds.
5.32 write < path > < content >
Open the file in, and write a string to it using write(2). If the file does not exist, it will be created. If it does exist, it will be truncated.
Six, the init. Zygote32. Rc
system/core/rootdir/init.zygote32.rc
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
class main
socket zygote stream 660 root system
onrestart write /sys/android_power/request_state wake
onrestart write /sys/power/state on
onrestart restart media
onrestart restart netd
writepid /dev/cpuset/foreground/tasks
Copy the code
The name of the service is zygote and the pathname is zygote. /system/bin/app_process, using -xzygote, /system/bin, -zygote, and -start-system-server respectively.
Specify that the class name of the service is main, which centrally controls the start and stop of the service.
Start a socket named zygote, the type of the socket is Stream, and the permission is 660. It indicates that the owner has read and write permissions, and the user in the owner group also has read and write permissions. The user in the root group is system.
Run the following command when the service is restarted:
write /sys/android_power/request_state wake
write /sys/power/state on
restart media
restart netd
Open the file in /sys/android_power/request_state and write “wake” to it using write(2);
Open the file in /sys/power/state and write “on” to it using write(2);
Restart the media service.
Restart the Netd service.
writepid /dev/cpuset/foreground/tasks
Fork when writing the child process pid in/dev/cpuset/foreground/tasks.