android: v2.3.41. init.clinux核心起來後,init是android的第一個使用者進程system/core/init/init.cint main(int argc, char **argv){/******建立linux 根檔案系統的目錄***********/mkdir("/dev", 0755); mkdir("/proc", 0755); mkdir("/sys", 0755); mount("tmpfs", "/dev", "tmpfs", 0, "mode=0755"); mkdir("/dev/pts", 0755); mkdir("/dev/socket", 0755); mount("devpts", "/dev/pts", "devpts", 0, NULL); mount("proc", "/proc", "proc", 0, NULL); mount("sysfs", "/sys", "sysfs", 0, NULL);//開啟標準輸入,輸出,錯誤檔案描述符open_devnull_stdio();//讀取並解析init.rc init_parse_config_file("/init.rc");//通過/proc/cmdline得到啟動命令//從/proc/cmdline 中提取資訊核心啟動參數,並儲存到全域變數 import_kernel_cmdline(0);//通過/proc/cpuinfo 得到硬體名get_hardware_name(hardware, &revision);//讀取並且解析硬體相關的Init指令檔snprintf(tmp, sizeof(tmp), "/init.%s.rc", hardware); init_parse_config_file(tmp);/******觸發在init指令檔中名字為early-init的action並且執行命令*******/ action_for_each_trigger("early-init", action_add_queue_tail); //串口初始化 queue_builtin_action(console_init_action, "console_init");}static int console_init_action(int nargs, char **args){/***判斷是否有控制台,如果沒有就用預設的*********/ if (console[0]) { snprintf(tmp, sizeof(tmp), "/dev/%s", console); console_name = strdup(tmp); }/*****開啟console,如果cmdline中沒有指定console則開啟/dev/console fd = open(console_name, O_RDWR);/*****#define INIT_IMAGE_FILE"/initlogo.rle"@*讀取initlogo.rle.是放在根檔案系統下的一張565 rle壓縮的開機logo@*如果開啟成功則在/dev/graphics/fb0顯示logo,如果失敗,則開啟/dev/tty0顯示@*ANDROID 文本*/ if( load_565rle_image(INIT_IMAGE_FILE) ) { fd = open("/dev/tty0", O_WRONLY); if (fd >= 0) { const char *msg; msg = "\n" "\n" "\n" "\n" "\n" "\n" "\n" // console is 40 cols x 30 lines "\n" "\n" "\n" "\n" "\n" "\n" "\n" " A N D R O I D "; write(fd, msg, strlen(msg)); close(fd); } }}2. init指令碼語言Android中使用啟動指令碼init.rc,init啟動指令碼的路徑system/coer/rootdir/init.rcinit.rc被安裝到根檔案系統中,被init可執行程式解析init.rc指令碼的使用可以參考system/core/init/readme.txt所有的都是以行為單位,各種記號由空格來隔開反斜線號可用於在記號間插入空格1) Action 動作其實就是一系列的命令commands,Actions都有一個trigger觸發器,用來決定action的執行時間Actions take the form of:on <trigger> <command> <command> <command> on early-init on init on fs on post-fs on boot on property:ro.secure=0 on property:ro.kernel.qemu=1 on property:persist.service.adb.enable=1 這些觸發是由init.c裡的函數action_for_each_trigger來決定的 action_for_each_trigger("early-init", action_add_queue_tail); action_for_each_trigger("init", action_add_queue_tail); action_for_each_trigger("early-fs", action_add_queue_tail); action_for_each_trigger("fs", action_add_queue_tail); action_for_each_trigger("post-fs", action_add_queue_tail); action_for_each_trigger("early-boot", action_add_queue_tail); action_for_each_trigger("boot", action_add_queue_tail);2)services服務services表示啟動一個可執行程式,options選項是服務的附加內容用於配合服務使用service <name> <pathname> [ <argument> ]* <option> <option>name: 服務名pathname:當前服務對應的程式位置options:當前服務設定的選項3)options 選項Options are modifiers to services. They affect how and when initruns the service.critical:如果服務四分鐘內退出大於四次,系統將會重啟並進入復原模式service ueventd /sbin/ueventd criticaldisabled:服務不會同與它同trigger下的服務自動啟動,他必須被明確的按名稱啟動service adbd /sbin/adbd disabledsocket:建立通訊端service vold /system/bin/vold socket vold stream 0660 root mount ioprio be 2user:改變服務的使用者名稱service media /system/bin/mediaserver user mediagroup:改變服務的組名service bootanim /system/bin/bootanimation user graphics group graphicsoneshot:服務退出時不重啟service flash_recovery /system/etc/install-recovery.sh oneshotifup:啟動網路介面on boot ifup lo ifup usb0 hostname localhosthostname:設定主機名稱insmod:載入path中的模組mount:掛載裝置mount tmpfs tmpfs /mnt/asec mode=0755,gid=1000setprop:設定系統屬性init.rc中重要的服務service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server建立Dalvik Java虛擬機器,然後啟動SystemServer,啟動所有的Android服務,最終啟動Android系統service servicemanager /system/bin/servicemanager開啟binder驅動,binder是Android Service通訊機制的底層實現,該service裡封裝了binder的操作介面