本文分析Android中init进程的执行过程,只是分析init进程启动的流水,具体细节在今后的各个专题中再分别详细分析。本文虽是后面各个专题的基础,读者初看可能理解不深,可以在阅读后面各个专题的时候,结合本文的整体流程会有更清晰的理解。
Init进程从 /system/core/init/init.c里的main()函数开始
1. mkdir && mount
2. import_kernel_cmdline
从内核中通过/proc/cmdline导入下列命令行参数,这些参数会分别被设置到各个属性值中:
[plain]
androidboot.console // 设置console
androidboot.mode // 设置属性ro.factorytest
androidboot.serialno // 设置属性ro.serialno
androidboot.baseband // 设置属性ro.baseband
androidboot.carrier // 设置属性ro.carrier
androidboot.bootloader // 设置属性ro.bootloader
androidboot.hardware // 设置属性ro.hardware
androidboot.bsp // 根据这个设置与否,选择不同的init.rc版本
androidboot.console // 设置console
androidboot.mode // 设置属性ro.factorytest
androidboot.serialno // 设置属性ro.serialno
androidboot.baseband // 设置属性ro.baseband
androidboot.carrier // 设置属性ro.carrier
androidboot.bootloader // 设置属性ro.bootloader
androidboot.hardware // 设置属性ro.hardware
androidboot.bsp // 根据这个设置与否,选择不同的init.rc版本注意:以上说的设置属性还未真正设置,property机制还未工作。
3. init_parse_config_file()
解析init.rc或者init_bsp.rc(看步骤2导入的参数“androidboot.bsp”是否设置)
Init.rc的解析,参见《Android中init.rc文件的解析》
4. get_hardware_name()
从/proc/cpuinfo中获取“Hardware”字段信息写入<hw>;“Reversion” 字段信息写入<reversion>
5. init_parse_config_file()
解析init.<hw>.rc或者init_bsp.<hw>.rc(看步骤2导入的参数“androidboot.bsp”是否设置)
Init.rc的解析,参见《Android中init.rc文件的解析》
6. action_for_each_trigger("early-init",action_add_queue_tail);
对init???.rc中解析出的early-initsection里action,执行action_add_queue_tail操作,也就是把act->qlist加入到action_queue的列尾
注:此时并未真正执行,只是挂在队列尾。
7. 把一些初始化操作加入到action_queue列表中
[cpp]
queue_builtin_action(wait_for_coldboot_done_action,"wait_for_coldboot_done");
queue_builtin_action(property_init_action,"property_init");
queue_builtin_action(keychord_init_action,"keychord_init");
queue_builtin_action(console_init_action,"console_init");
queue_builtin_action(set_init_properties_action,"set_init_properties");
queue_builtin_action(wait_for_coldboot_done_action,"wait_for_coldboot_done");
queue_builtin_action(property_init_action,"property_init");
queue_builtin_action(keychord_init_action,"keychord_init");
queue_builtin_action(console_init_action,"console_init");
queue_builtin_action(set_init_properties_action,"set_init_properties");queue_builtin_action(int (*func)(int nargs,char **args), char *name)是以name形成action,挂在action_list上;以func和name组成command,挂在action的commands上。然后加入到action_queue的队尾。
8. 对其他section内的action,加入到action_queue列表中
另外一些初始操作也加入到action_queue列表中
[cpp]
/* execute all the boot actions to getus started */
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);
queue_builtin_action(property_service_init_action,"property_service_init");
queue_builtin_action(signal_init_action,"signal_init");
queue_builtin_action(check_startup_action,"check_startup");
/* execute all the boot actions to get usstarted */
action_for_each_trigger("early-boot", action_add_queue_tail);
action_for_each_trigger("boot",action_add_queue_tail);
/* run all property triggers based oncurrent state of the properties */
queue_builtin_action(queue_property_triggers_action,"queue_propety_triggers");
/* execute all the boot actions to getus started */
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);
queue_builtin_action(property_service_init_action,"property_service_init");
queue_builtin_action(signal_init_action,"signal_init");
queue_builtin_action(check_startup_action,"check_startup");
/* execute all the boot actions to get usstarted */
action_for_each_trigger("early-boot", action_add_queue_tail);
action_for_each_trigger("boot",action_add_queue_tail);
/* run all property triggers based oncurrent state of the properties */
queue_builtin_action(queue_property_triggers_action,"queue_propety_triggers");
9. 进入无限循环中for(;;)
9.1 execute_one_command():[system/core/init/init.c]