本文爲 ANDROID_SOURCE/system/core/init/readme.txt 的譯文。
安卓初始化語言
安卓初始化語言包括四種類型的語句,它們是:
- 動作 Action
- 命令 Command
- 服務 Service
- 選項 Option
所有語句都是面向行的,以空格分割每行包含的若干token。C風格的反斜槓可以用於token中插入空格,雙引號同樣可以避免空格將文本分爲多個token。反斜槓是一行的最後一個字符時,將用於續行(PS:下一行也屬於該句)。
以#開頭的行(前面有空格也是允許的)是註釋。
Action和Service隱式定義了一個新的section(段),所有Command或Option屬於最近定義的section。在第一個section之前的Command或Option將被忽略。
Action和Service有唯一的名字。如果有第二個Action或Service定義爲和一個已存在的(Action或Service)同名,它將被作爲錯誤忽略掉。
動作 Action
Action是有名字的一系列的命令。Action有一個tirgger(觸發器),用於決定該Action應在何時執行。當一個事件發生並匹配了一個Action的trigger,相應的Action將被添加到即將執行(to-be-executed)隊列的尾部(除非她已經在隊列上了)。
每個action在隊列中順序排列,每個action中的command將會順序執行。init在執行command的過程中還有執行其他活動(設備節點的創建/銷燬,屬性設置,進程重啓)。
Action具有以下格式:
on <trigger>
<command>
<command>
<command>
服務 Service
Service是init加載的和是退出重啓的(可選)程序。Service具有如下格式:
service <name> <pathname> [ <argument> ]*
<option>
<option>
...
選項 Option
Option是Service的修改者。它們影響着init如何及何時運行Service。可用的Option有:
critical
這是十分關鍵的服務。如果在四分鐘內超過四次,手機將會重啓並進入recovery模式。
disabled
這種類型的服務不會自動啓動。它必須明確的使用名字啓動。
setenv <name> <value>
設置環境變量<name>=<value>在加載的進程中。
socket <name> <type> <perm> [ <user> [ <group> [ <context> ] ] ]
創建一個名爲/dev/socket/<name>的UNIX域socket並將fd傳遞到加載的進程中。
<type>必須是"dgram", "stream", "seqpacket"中的一種。
<user>和<group>默認爲0.
<context>是 SELinux socket 安全上下文,默認爲service安全級別,可以指定爲seclabel或根據service的可執行文件的安全級別計算。
user <username>
在執行該service前改變用戶名,默認爲root。如果你的進程請求Linux的特殊能力,就不要用這個命令。需以進入進程仍是root->請求特權->切換到你期望的uid來替換此法。
group <groupname> [ <groupname> ]*
在執行該service前改變組名。第一個以後的附加組名用於設定進程的附加組(通過setgroups())。當前默認是root。
seclabel <securitycontext>
在執行服務之前改變安全級別。主要用於從rootfs執行服務,比如ueventd, adbd. 在system分區上可以用基於文件安全級別的策略定義的transition,如果沒有指定且沒有定義策略的transition,默認是init上下文。
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 is defined in policy, defaults to the init context.
oneshot
退出不重啓服務(名副其實,一次性)。
class <name>
爲一service指定一個類名,所有有相同類名的service可以一同啓動或停止。如果沒有用class選項指定類名,該service屬於"default"。
onrestart
在service重啓的時候執行。
觸發器 Trigger
觸發器是可以用來匹配一些種類的事件和用來引發一個行爲發生的字符串。
boot
這是init啓動後最先被觸發的trigger(在Init.conf被加載以後)。
<name>=<value>
這種形式額trigger(觸發器)屬性<name>被設定爲<value>時被觸發。
device-added-<path>
device-removed-<path>
這種形式的Triggers(觸發器)會在設備節點<path>被添加或移除時觸發。
service-exited-<name>
這種形式的Triggers會在指定的service退出時被觸發。
命令 Command
exec <path> [ <argument> ]*
創建和執行程序(<path>). 這將會阻塞init,直到程序執行完成。由於它不是內置命令,應儘量避免使用exec,它可能會引起init卡死。
export <name> <value>
在全局環境變量中設在環境變量 <name>爲<value>。(這將會被所有在這命令之後運行的進程所繼承)
ifup <interface>
啓動網絡接口<interface>
import <filename>
解析一個init配置文件,擴展當前配置。
hostname <name>
設置主機名。
chdir <directory>
改變工作目錄。
chmod <octal-mode> <path>
更改文件訪問權限。
chown <owner> <group> <path>
更改文件的所有者和組。
chroot <directory>
改變進程的根目錄。
class_start <serviceclass>
啓動該類service所有尚未運行的服務。
class_stop <serviceclass>
停止所有該類正在運行的service。
domainname <name>
設置域名。
enable <servicename>
改變一個disable的service爲enabled,就像他們好像沒有明確的關閉一樣。如果該service已經應該運行,它會立刻啓動。典型的用法是,當bootloader設定一個表示特定的變量,service應該啓動。比如:
on property:ro.boot.myfancyhardware=1
enable my_fancy_service_for_my_fancy_hardware
insmod <path>
安裝位於<path>的模塊(PS:驅動)。
mkdir <path> [mode] [owner] [group]
在<path>創建一個目錄,(可選)使用給定的模式,所有者個組。如果沒有提供,該目錄將用755權限,所有者爲root用戶,組爲root。
mount <type> <device> <dir> [ <mountoption> ]*
嘗試掛載<device>到<dir>,<device>可能有mtd@name形式,以指定名爲name的mtd塊設備。
<mountoption>包括 "ro", "rw", "remount", "noatime", ...
restorecon <path> [ <path> ]*
恢復名爲<path>的文件在file_contexts中配置的的安全級別。自動被init標記正確,不需要用init.rc創建的目錄。
restorecon_recursive <path> [ <path> ]*
遞歸的恢復<path>指出的目錄樹中file_contexts配置指定的安全級別。 path不要用shell可寫或app可寫的目錄,如/data/locla/temp, /data/data,或者有類似前綴的(目錄)。
設置當前進程的security context爲特定的字符串。這是典型的僅用於所有進程啓動之前的early-init設置init context。
setenforce 0|1
設置SELinux系統範圍的enfoucing狀態。0 is permissive (i.e. log but do not deny), 1 is enforcing.
setkey
TBD
setprop <name> <value>
設置系統屬性<name>爲<value>.
setrlimit <resource> <cur> <max>
爲特定資源設置rlimit.
setsebool <name> <value>
設置SELinux的bool類型<name>爲<value>。 <value> may be 1|true|on or 0|false|off
start <service>
啓動一個服務(如果服務尚未啓動)。
stop <service>
停止服務(如果正在運行)。
symlink <target> <path>
創建一個符號連接,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>
觸發一個事件。一個動作將另一動作排隊。
wait <path> [ <timeout> ]
poll特定的<path>,出現後返回,或timeout到達。如果timeout沒有指定,默認爲5秒。
write <path> <string>
打開一個位於<path>的文件,寫入(不是追加)字符串<string>。
屬性 Properties
init進程會更新一些系統屬性,下面是一些具體的屬性:init.action
等於當前正在執行的action,或者""若果沒有的話。
init.command
等於當前正在執行的command,或者""如果沒有的話。
init.svc.<name>
一個有名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 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 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 will drop stdout and stderr into
/dev/null. To help with debugging, you can execute your program via the
Andoird program logwrapper. This will redirect stdout/stderr into the
Android logging system (accessed via logcat).
For example
service akmd /system/bin/logwrapper /sbin/akmd