Android Initialization language (init.rc syntax)

Source: Internet
Author: User
Tags unix domain socket

This article is a translation of android_source/system/core/init/readme.txt.


Android Init language

The Android initialization language consists of four types of statements, which are:

    • Actions action
    • Command commands
    • Services Service
    • Options option

All statements are row-oriented, with spaces separating several tokens contained in each row. C-style backslashes can be used to insert spaces in tokens, and double quotes can also avoid spaces to divide text into tokens. When the backslash is the last character of a line, it is used to continue the line (PS: The next line also belongs to the sentence).

Lines that begin with a # (preceded by a space are also allowed) are comments.

Action and service implicitly define a new section (segment), with all command or option belonging to the most recently defined sections. command or option before the first section is ignored.

The action and service have a unique name. If a second action or service is defined as having the same name as an existing (action or service), it will be ignored as an error.


Actions action

An action is a series of commands that have a name. The action has a tirgger (trigger) that determines when the action should be executed. When an event occurs and matches an action's trigger, the corresponding action is added to the end of the upcoming (to-be-executed) queue (unless she is already on the queue).

Each action is arranged sequentially in the queue, and the command in each action is executed sequentially. Init also performs other activities during command execution (creation/destruction of device nodes, property settings, Process restarts).

Action has the following format:

On <trigger>   <command>   <command>   <command>

Services Service

The service is the (optional) program that init loads and exits the restart. The service has the following format:

Service <name> <pathname> [<argument>]*   <option>   <option>   ...

Options option

option is the person who modified the service. They affect how and when Init runs the service. The available option is:

Critical
This is a very critical service. If more than four times within four minutes, the phone will reboot and enter recovery mode.

Disabled
This type of service does not start automatically. It must be explicitly started using the name.

Setenv <name> <value>
Set the environment variable <name>=<value> in the loaded process.

Sockets <name> <type> <perm> [<user> [<group>] [<context>]]
Create a UNIX domain socket named/dev/socket/<name> and pass FD to the loaded process.
<type> must be one of "Dgram", "Stream", "Seqpacket".
<user> and <group> default is 0.
<context> is the SELinux socket security context, which defaults to the service security level, which can be specified as Seclabel or calculated based on the security level of the service's executable file.

User <username>
Change the user name before executing the service, which defaults to root. If your process requests special Linux capabilities, do not use this command. To replace this method, you need to switch to the UID you expect, which is still root-> request privilege, as the entry process.

Group <groupname> [<groupname>]*
Change the group name before executing the service. The first additional group name is used to set the additional group for the process (via Setgroups ()). The current default is root.

Seclabel <securitycontext>
Change the security level before executing the service. Mainly used to perform services from Rootfs, such as Ueventd, ADBD. The transition can be defined on the system partition with a policy based on the file security level, and if no transition is specified and no policy is defined, the default is the init context.
Change to securitycontext before exec ' ing this service.
Primarily for use by services run from the rootfs, e.g Ueventd, adbd.
Services on the system partition can instead use policy-defined transitions
Based on their file security context.
If not specified and no transition are defined in policy, defaults to the init context.

OneShot
Exit does not restart the service (veritable, one-time).

Class <name>
Specify a class name for a service, and all service with the same class name can be started or stopped together. If the class name is not specified with the Classes option, the service belongs to "default".

Onrestart
Executes when the service is restarted.


Trigger Trigger

Triggers are strings that can be used to match some kind of event and to cause a behavior to occur.

Boot
This is the trigger that was first triggered after Init started (after init.conf was loaded).

<name>=<value>
This form trigger (trigger) attribute <name> is triggered when set to <value>.

Device-added-<path>
Device-removed-<path>
This form of triggers (trigger) is triggered when the device node <path> is added or removed.

Service-exited-<name>
This form of triggers is triggered when the specified service exits.


Command commands

exec <path> [<argument>]*
Create and execute programs (<path>). This will block Init until the program execution is complete. Since it is not a built-in command, you should try to avoid using exec, which may cause the Init card to die.

Export <name> <value>
In the Global environment variable, set the environment variable <name> to <value>. (This will be inherited by all processes running after this command)

Ifup <interface>
Start the network interface <interface>

Import <filename>
Parses an init configuration file, extending the current configuration.

Hostname <name>
Set the host name.

ChDir <directory>
Change the working directory.

chmod <octal-mode> <path>
Change file access permissions.

Chown <owner> <group> <path>
Change the owner and group of the file.

Chroot <directory>
Change the root directory of the process.

Class_start <serviceclass>
Start all services that are not yet running for that class of service.

Class_stop <serviceclass>
Stop all service that is running on this class.

DomainName <name>
Set the domain name.

Enable <servicename>
Change a disable service to Enabled, just as if they were not explicitly closed. If the service should already be running, it will start immediately. The typical usage is that when bootloader sets a specific variable to be represented, the service should start. Like what:

     On property:ro.boot.myfancyhardware=1        Enable My_fancy_service_for_my_fancy_hardware

Insmod <path>
Install the module (PS: drive) located in <path>.

mkdir <path> [mode] [owner] [group]
Create a directory in <path>, optionally using a given pattern, owner groups. If not provided, the directory will have 755 permissions, the owner is the root user, and the group is root.

Mount <type> <device> <dir> [<mountoption>]*
Attempt to mount <device> to <dir>,<device> may have [email protected] form to specify a MTD block device named name.
<mountoption> includes "Ro", "RW", "remount", "Noatime", ...

Restorecon <path> [<path>]*
Restores the security level that the file named <path> is configured in File_contexts. Automatically marked correctly by INIT, no directories created with INIT.RC are required.
Restorecon_recursive <path> [<path>]*

Recursive recovery <path> Indicates the security level specified in the FILE_CONTEXTS configuration of the directory tree. Path does not use a shell-writable or app-writable directory, such as/data/locla/temp,/data/data, or a prefix-like (directory).


Setcon <securitycontext>
Sets the security context for the current process to a specific string. This is typical only for all processes that start before the Early-init settings init context.

Setenforce 0|1
Sets the enfoucing state of the SELinux system range. 0 is permissive (i.e. log but does not deny), and 1 is enforcing.

Setkey
TBD

SetProp <name> <value>
Set System Properties <name> for <value>.

Setrlimit <resource> <cur> <max>
Set rlimit for a specific resource.

Setsebool <name> <value>
Set SELinux bool type <name> to <value>. <value> may 1|true|on or 0|false|off

Start <service>
Start a service (if the service is not already started).

Stop <service>
Stop the service (if it is running).

Symlink <target> <path>
Create a symbolic connection at <path> with the value <target>.

Sysclktz <mins_west_of_gmt>
Set the system clock base (0 if system clock ticks in GMT)

Trigger <event>
Triggers an event. One action queues another action.

Wait <path> [<timeout>]
Poll specific <path>, returns after it appears, or timeout arrives. If timeout is not specified, the default is 5 seconds.

Write <path> <string>
Open a file that is located in <path> and write (not append) the string <string>.


The properties of the Propertiesinit process update some system properties, and here are some specific properties:

Init.action
equals the action currently being executed, or "" if not.

Init.command
equals the command that is currently executing, or "" if not.

Init.svc.<name>
Status of a well-known service ("Stopped", "Running", "restarting")

Example init.conf-----------------

# not complete--just providing some examples of usage
#
On boot
Export Path/sbin:/system/sbin:/system/bin
Export Ld_library_path/system/lib

Mkdir/dev
Mkdir/proc
Mkdir/sys

Mount Tmpfs Tmpfs/dev
Mkdir/dev/pts
Mkdir/dev/socket
Mount Devpts devpts/dev/pts
Mount Proc Proc/proc
Mount Sysfs Sysfs/sys

Write/proc/cpu/alignment 4

Ifup Lo

hostname localhost
DomainName localhost

Mount YAFFS2 [email protected]/system
Mount YAFFS2 [email protected]/data

Import/system/etc/init.conf

Class_start Default

Service ADBD/SBIN/ADBD
User adb
Group ADB

Service Usbd/system/bin/usbd-r
User USBD
Group USBD
Socket USBD 666

Service Zygote/system/bin/app_process-xzygote/system/bin--zygote
Socket Zygote 666

Service Runtime/system/bin/runtime
User System
Group system

On Device-added-/dev/compass
Start AKMD

On Device-removed-/dev/compass
Stop AKMD

Service AKMD/SBIN/AKMD
Disabled
User AKMD
Group AKMD

Debugging Notes---------------
By default, programs executed by Init would drop stdout and stderr into
/dev/null. To help with debugging, you can execute your program via the
Andoird program Logwrapper. This would redirect Stdout/stderr into the
Android logging System (accessed via Logcat).

For example
Service AKMD/SYSTEM/BIN/LOGWRAPPER/SBIN/AKMD


Android Initialization language (init.rc syntax)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.