一、Android init進程啟動
還是從Linux的啟動開始吧。Linux被bootloader載入到了記憶體之後,開始運行,在初始化完Linux運行環境之後,掛載ramdisk.img根檔案系統映像,運行裡面的init程式,這也是Linux的第一個使用者程式,其pid為1。下面的文章是作者關於init進程啟動的描述。
二、Android本地服務的啟動
init進程啟動完之後,開始初始化並啟動Dalvik虛擬機器,在Dalvik虛擬機器啟動之前做了一些工作,請看下面一篇文章。
Android啟動總結:
init進程在執行過程中可以分為以下幾個階段:
Ø 啟動準備:建立檔案系統的基本目錄、開啟標準輸入、標準輸出、標準錯誤,初始化log日誌功能等
Ø 解析init.rc和init.hardware.rc檔案:將rc檔案逐行解析成Action或Service。解析出來的Action和Service分別存放在action_list和service_list鏈表裡,每個Action都對應一個或多個Commands,每個依附於Action的Commands也由一個鏈表維護。www.2cto.com
Ø 將early-initAction添加到action_queue隊列裡,等待執行
Ø 將init Action添加到action_queue隊列裡,等待執行
Ø 添加其它條件的Action到action_queue隊列裡
Ø 進入死迴圈
o 從action_queue隊列裡依次取出每個Action,執行其維護的Commands鏈表裡的命令
o 重新啟動service_list中標記為SVC_RESTARTING服務
o 監聽系統屬性狀態變化事件、子進程訊號、Keychord組合按鍵事件
註:在代碼裡沒有明顯運行service_list裡服務的代碼,每個服務都有一個class屬性,該屬性決定了服務的分類,在init.rc檔案的on boot Action最後有兩個命令:
on boot
…
...
class_start core
class_start main
class_start命令是指運行某一類的服務,先啟動了class為core的服務,然後再啟動了class為main的服務。
init.rc中class為core的服務有:
Service名
對應程式及參數
ueventd
/sbin/ueventd
console
/system/bin/sh
adbd
/sbin/adbd
servicemanager
/system/bin/servicemanager
vold
/system/bin/vold
class為main的服務有:
Service名
對應程式及參數
netd
/system/bin/netd
debuggerd
/system/bin/debuggerd
ril-daemon
/system/bin/rild
surfaceflinger
/system/bin/surfaceflinger
zygote
/system/bin/app_process -Xzygote /system/bin --zygote--start-system-server
drm
/system/bin/drmserver
media
/system/bin/mediaserver
bootanim
/system/bin/bootanimation
dbus
/system/bin/dbus-daemon --system --nofork
bluetoothd
/system/bin/bluetoothd -n
installd
/system/bin/installd
flash_recovery
/system/etc/install-recovery.sh
racoon
/system/bin/racoon
mtpd
/system/bin/mtpd
keystore
/system/bin/keystore /data/misc/keystore
dumpstate
/system/bin/dumpstate -s