Android init. RC notes (overview and init. RC syntax)

Source: Internet
Author: User
Tags unix domain socket

The root directory of Android contains a series of important configuration files: init. RC init. XXXXX. RC.

The code for parsing these configuration files in Android is in the system \ core \ init directory.

The core file is init. C. For more information about the syntax, see readme.txt.

This article briefly introduces the init. RC syntax. The init. c analysis will be performed next time.

 

The original readme.txt text and annotations are as follows:

Android init Language
---------------------

The android init language consists of four broad classes of statements,
Which are actions, commands, services, and options.

All of these are line-oriented, consisting of tokens separated
Whitespace. The C-style backslash escapes may be used to insert
Whitespace into a token. Double quotes may also be used to prevent
Whitespace from breaking text into multiple tokens. The backslash,
When it is the last character on a line, may be used for line-folding.

Lines which start with a # (Leading whitespace allowed) are comments.

Actions and Services implicitly declare a new section. All commands
Or options belong to the Section most recently declared. Commands
Or options before the first section are ignored.

Actions and Services have unique names. If a second action or service
Is declared with the same name as an existing one, it is ignored
An error .(??? Shocould we override instead)

The syntax of this language includes the following conventions:

• All types of statements are based on line-oriented. A statement contains several tokens, which are separated by space characters. if a token needs to contain space characters, it needs to be escaped by a backslash ('\') in the C language style, or the entire token is caused by double quotation marks. The backslash can also appear at the end of a row, indicating that the content of the next row still belongs to the current statement.
• The line starting with '#' is a comment line.
• The actions and services statements implicitly indicate the beginning of a new section. All commands (commands) and options belong to the most recent section above. The commands (commands) and options before the first paragraph are invalid.
• Actions and Services have unique names. If duplicate names occur, the subsequent definitions are ignored as errors.

Actions
-------
Actions are named sequences of commands. Actions have a trigger which
Is used to determine when the action shoshould occur. When an event
Occurs which matches an action's trigger, that action is added
The tail of a to-be-executed Queue (unless it is already on
Queue ).

Each action in the queue is dequeued in sequence and each command in
That action is executed in sequence. init handles other activities
(Device creation/destruction, Property setting, process restarting)
"Between" the execution of the commands in activities.

Actions take the form:

On <trigger>
<Command>
<Command>
<Command>

Services
--------
Services are programs which init launches and (optionally) restarts
When they exit. services take the form:

Service <Name> <pathname> [<argument>] *
<Option>
<Option>
...

Now we can see that init. RC is actually composed of two types of paragraphs:

1. A series of commands starting with on <trigger>

2. Start a service starting with service (parameters and options can be included)


Options
-------
Options are modifiers to services. They affect how and when init
Runs the service.

Critical
This is a device-critical service. If it exits more than four times in
Four minutes, the device will reboot into recovery mode.

Exit 4 times in 4 minutes and re-start to 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.

Socket <Name> <type> <perm> [<user> [<group>]
Create a Unix domain socket named/dev/socket/<Name> and pass
Its FD to the launched process. <type> must be "dgram" or "stream ".
User and group default to 0.

Create a socket in UNIX domain named/dev/socket/<Name> and pass its FD to the service process.

The type must be 'dgram' or 'stream'

User <username>
Change to username before exec 'ing this service.
Currently defaults to root .(??? Probably shoshould default to nobody)
Currently, if your process requires Linux capabilities then you cannot use
This command. You must instead request the capabilities in-process while
Still root, and then drop to your desired uid.

Group <groupname> [<groupname>] *
Change to groupname before exec 'ing this service. Additional
Groupnames beyond the (required) First one are used to set
Supplemental groups of the process (via setgroups ()).
Currently defaults to root .(??? Probably shoshould default to nobody)

Oneshot
Do not restart the service when it exits.

Do not restart after release. By default, the service is automatically restarted after exiting.

Class <Name>
Specify a class name for the service. All services in
Named class may be started or stopped together. A service
Is in the class "default" if one is not specified via
Class option.

Specify a class for the service. Start or stop a service in the same class.

The unspecified service is under the class 'default' by default.

Onrestart
Execute a command (see below) when service restarts.

When the service is restarted, run the command (before restart? After? To be investigated. It should be later .)

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)

In addition to the boot event, events commonly used in init. RC and their execution sequence are as follows:

Early-init, init, early-Fs, FS, post-Fs, early-boot, boot

<Name >=< value>
Triggers of this form occur when the property <Name> is set
To the specific value <value>.

Device-added-<path>
Device-removed-<path>
Triggers of these forms occur when a device node is added
Or removed.

Executed when a device node is added or removed

Service-Exited-<Name>
Triggers of this form occur when the specified service exits.

Commands
--------

Exec <path> [<argument>] *
Fork and execute a program (<path>). This will block
The program completes execution. It is best to avoid Exec
As unlike the builtin commands, it runs the risk of getting
Init "stuck ".(??? Maybe there shocould be a timeout ?)

Run the <path> specified program. It will block the current init.

Export <Name> <value>
Set the environment variable <Name> equal to <value> in
Global Environment (which will be inherited by all processes
Started after this command is executed)

IFUP <interface>
Bring the network interface <interface> online.

Import <FILENAME>
Parse an init config file, extending the current configuration.

Hostname <Name>
Set the host name.

Chdir <directory>
Change working directory.

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

Chown <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.

Start a service under a class. (If not started yet)

Class_stop <serviceclass>
Stop all services of the specified class if they are
Currently running.

Domainname <Name>
Set the domain name.

Insmod <path>
Install the module at <path>

Mkdir <path> [mode] [owner] [group]
Create a directory at <path>, optionally with the given mode, owner, and
Group. If not provided, the directory is created with permissions 755 and
Owned by the root user and root group.

Mount <type> <device> <dir> [<mountoption>] *
Attempt to mount the named device at the directory <dir>
<Device> may be of the form MTD @ name to specify a MTD Block
Device by name.
<Mountoption> S include "Ro", "RW", "remount", "noatime ",...

Setkey
TBD

Setprop <Name> <value>
Set system property <Name> to <value>.

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

Start <service>
Start a service running if it is not already running.

Start a service

Stop <service>
Stop a service from running if it is currently running.

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

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

Set Time Zone

Trigger <event>
Trigger an event. Used to queue an action from another
Action.

Write <path> <string> [<string>] *
Open the file at <path> and write one or more strings
To it with write (2)

Properties
----------
Init updates some system properties to provide some insight
What it's doing:

Init. Action
Equal to the name of the action currently being executed or "" If none

Init. Command
Equal to the command being executed or "" If none.

Init. SVC. <Name>
State of a named Service ("STOPPED", "running", "Restarting ")

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

# Not complete -- just providing some examples of Usage
#
On Boot
Export path/sbin:/system/bin
Export LD_LIBRARY_PATH/system/lib

Mkdir/dev
Mkdir/proc
Mkdir/sys

Mount tmpfs/dev
Mkdir/dev/PTS
Mkdir/dev/socket
Mount devpts/dev/PTS
Mount proc/proc
Mount sysfs/sys

Write/proc/CPU/alignment 4

IFUP Lo

Hostname localhost
Domainname localhost

Mount yaffs2 MTD @ system/System
Mount yaffs2 MTD @ userdata/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 zygot 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 will drop stdout and stderr
/Dev/null. To help with debugging, You can execute your program via
Andoird program logwrapper. This will redirect stdout/stderr into
Android logging system (accessed via logcat ).

For example
Service akmd/system/bin/logwrapper/sbin/akmd

When debugging, use the preceding command to start a service and view its output.

The reason is that android init forwards stdout and stderr to/dev/null.

 

Legacy problems:

1. How do I pass the socket in option to the service after it is created?

2. If a service is labeled disable in init. RC, how can it be started? Use start <service name>? In init. RC? Can it be started in C code?

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.