標籤:android init.rc 啟動
本文為 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>可能有[email protected]形式,以指定名為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,或者有類似首碼的(目錄)。
setcon <securitycontext>
設定當前進程的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>。
屬性 Propertiesinit進程會更新一些系統屬性,下面是一些具體的屬性:
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 [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 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
Android初始化語言(init.rc文法)