第
Android系统底层Reboot流程源码解读
目录Framework中Reboot流程Init中Reboot流程1,进程监控属性sys.powerctl的改变2,HandlePowerctlMessage()对属性sys.powerctl进行解析3,DoReboot()执行shutdown/reboot动作4,通过RebootSystem()执行系统Reboot调用内核中Reboot流程内核中的入口kernel_restart()完成重启Reboot和PowerOff的大致流程Reboot后的流程BCB主要的功能如下
Framework中Reboot流程
Reboot在Android系统中主要通过物理按键或UI菜单进行触发,最终由PowerManager执行Reboot流程。下图描述了Reboot执行时,Framework中相关线程的状态,最终将Reboot相关信息设置到属性sys.powerctl中。Framework中的具体流程本文不再描述。
Init中Reboot流程
Androidframework处理完Reboot流程后,更新了属性sys.powerctl。Init正是依靠该属性来执行底层Reboot动作。Init对Reboot的处理主要为以下几个方面:
1,进程监控属性sys.powerctl的改变
/system/core/init/init.cpp
voidPropertyChanged(conststd::stringname,conststd::stringvalue){
//Ifthepropertyissys.powerctl,webypasstheeventqueueandimmediatelyhandleit.
//Thisistoensurethatinitwillalwaysandimmediatelyshutdown/reboot,regardlessof
//ifthereareotherpendingeventstoprocessorifinitiswaitingonanexecserviceor
//waitingonaproperty.
//Innon-thermal-shutdowncase,shutdowntriggerwillbefiredtoletdevicespecific
//commandstobeexecuted.
//sys.powerctl做为特殊属性来处理,直接触发shutdown/reboot流程。
if(name==sys.powerctl){
trigger_shutdown(value);
if(property_triggers_enabled){
ActionManager::GetInstance().QueuePropertyChange(name,value);
WakeMainInitThread();
prop_waiter_state.CheckAndResetWait(name,value);
}
2,HandlePowerctlMessage()对属性sys.powerctl进行解析
真正shutdown/reboot的流程在HandlePowerctlMessage(),对属性sys.powerctl进行解析,并存储相关信息。
/system/core/init/reboot.cpp
voidHandlePowerctlMessage(conststd::stringcommand){
unsignedintcmd=0;
std::vectorstd::stringcmd_params=Split(command,,);
std::stringreboot_target=;
boolrun_fsck=false;
boolcommand_invalid=false;
booluserspace_reboot=false;
//解析shutdown参数
if(cmd_params[0]==shutdown){
cmd=ANDROID_RB_POWEROFF;
if(cmd_params.size(