MID init.rc文法分析
By YWW
關於init.rc最原始的教程請參考Android源碼目錄中:system/core/init/readme.txt。
Android中的Init語言由四種類型的語句構成。如下:
· Actionn :行為
· Commands
· Services
· Options
文法(language syntax)結構如下:
每一個語句佔據一行,並且各個關鍵字被空格分開。c規範中的(如 \n)反斜線將被忽略(backslash escapes)而被認為是一個空格 ,雙引號用來保證空格不會把一個文字串分為多個關鍵字。行最後的反斜線用作續行。
· 由 # (前面允許有空格)開始的行都是注釋行(comment)
· 一個actions 或 services 的開始隱含聲明了一個新的段,所有commands 或 options 屬於最近的聲明。在第一個段之前的 commands 或 options 都會被忽略
· 每一個actions 和 services 都有不同的名字。後面與前面發生重名的,那麼這個後面重名的將被忽略或被認為是一個錯誤
以init.rc中的語句分析為例,分別解釋一下這四種語句。
1.Actions
Actions由一系列的命令組成,而且還有一個觸發器,用來表明這
些命令什麼時候被執行,當一個事件發生如果匹配action的觸發條件,那麼這個action將會被添加到預備執行隊列的尾部(除非它已經在隊列當中)。Actions的文法格式如下:
on <trigger>
<command>
<command>
Eg:
# adbd on at boot in emulator
on property:ro.kernel.qemu=1
start adbd
這裡trigger的分類有如下幾種:
Triggers
--------
Triggers are strings which can be used to match certain kinds of events and used to cause an action to occur.
u boot
This is the first trigger that will occur when init starts (after /init.conf is loaded)
u <name>=<value>
Triggers of this form occur when the property <name> is set to the specific value <value>.
u device-added-<path>
u device-removed-<path>
Triggers of these forms occur when a device node is added or removed.
u service-exited-<name>
Triggers of this form occur when the specified service exits.
2. Services
Services 是一些已經存在的初始化啟動程式。文法格式如下:
service <name> <pathname> [ <argument> ]*
<option>
<option>
Eg:
# adbd is controlled by the persist.service.adb.enable system property
service adbd /sbin/adbd
disabled
3. Options
Options是service的修飾符,用來告訴init進程應該何時、以何種方式來運行service。
u critical
This is a device-critical service. If it exits more than four times in four minutes, the device will reboot into recovery mode.
u disabled
This service will not automatically start with its class. It must be explicitly started by name.
u setenv <name> <value>
Set the environment variable <name> to <value> in the launched process.
u 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", "stream" or "seqpacket". User and group default to 0.
u user <username>
Change to username before exec'ing this service. Currently defaults to root. (??? probably should 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.
u group <groupname> [ <groupname> ]*
Change to groupname before exec'ing this service. Additional groupnames beyond the (required) first one are used to set the supplemental groups of the process (via setgroups()). Currently defaults to root. (??? probably should default to nobody)
u oneshot
Do not restart the service when it exits.
u class <name>
Specify a class name for the service. All services in a named class may be started or stopped together. A service is in the class "default" if one is not specified via the class option.
u onrestart
Execute a Command (see below) when service restarts.
4.Commands
u exec <path> [ <argument> ]*
Fork and execute a program (<path>). This will block until 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 should be a timeout?)
u export <name> <value>
Set the environment variable <name> equal to <value> in the global environment (which will be inherited by all processes started after this command is executed)
u ifup <interface>
Bring the network interface <interface> online.
u import <filename>
Parse an init config file, extending the current configuration.
u hostname <name>
Set the host name.
u chdir <directory>
Change working directory.
u chmod <octal-mode> <path>
Change file access permissions.
u chown <owner> <group> <path>
Change file owner and group.
u chroot <directory>
Change process root directory.
u class_start <serviceclass>
Start all services of the specified class if they are not already running.
u class_stop <serviceclass>
Stop all services of the specified class if they are currently running.
u domainname <name>
Set the domain name.
u insmod <path>
Install the module at <path>
u 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.
u 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", ...
u setkey
TBD
u setprop <name> <value>
Set system property <name> to <value>.
u setrlimit <resource> <cur> <max>
Set the rlimit for a resource.
u start <service>
Start a service running if it is not already running.
u stop <service>
Stop a service from running if it is currently running.
u symlink <target> <path>
Create a symbolic link at <path> with the value <target>
u sysclktz <mins_west_of_gmt>
Set the system clock base (0 if system clock ticks in GMT)
u trigger <event>
Trigger an event. Used to queue an action from another action.
u write <path> <string> [ <string> ]*
Open the file at <path> and write one or more strings to it with write(2)