版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、android开机启动流程简单分析android启动当引导程序启动Linux内核后,会加载各种驱动和数据结构,当有了驱动以后,开始启动Android系统同时会加载用户级别的第一个进程init(systemcoreinitinit.cpp)代码如下:int main(int argc, char* argv) . /创建文件夹,挂载 / Get the basic filesystem setup we need put together in the initramdisk / on / and then we'll let the rc file figure out the res
2、t. if (is_first_stage) mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755"); mkdir("/dev/pts", 0755); mkdir("/dev/socket", 0755); mount("devpts", "/dev/pts", "devpts", 0, NULL); #define MAKE_STR(x) _STRING(x
3、) mount("proc", "/proc", "proc", 0, "hidepid=2,gid=" MAKE_STR(AID_READPROC); mount("sysfs", "/sys", "sysfs", 0, NULL); . /打印日志,设置log的级别 klog_init(); klog_set_level(KLOG_NOTICE_LEVEL); . Parser& parser = Parser:GetInstance(); p
4、arser.AddSectionParser("service",std:make_unique<ServiceParser>(); parser.AddSectionParser("on", std:make_unique<ActionParser>(); parser.AddSectionParser("import", std:make_unique<ImportParser>(); / 加载init.rc配置文件 parser.ParseConfig("/init.rc"
5、);加载init.rc文件,会启动一个Zygote进程,此进程是Android系统的一个母进程,用来启动Android的其他服务进程,代码:从Android L开始,在 /system/core/rootdir 目录中有 4 个 zygote 相关的启动脚本如下图:在init.rc文件中,有如下代码:import /init.environ.rcimport /init.usb.rcimport /init.$ro.hardware.rcimport /init.usb.configfs.rcimport /init.$ro.zygote.rc注意到上面的代码import /init.$ro.
6、zygote.rc,这里会读取ro.zygote这个属性,导入相应的init.rc文件。 ro.zygote的属性可为:zygote32、zygote64、zygote32_64、zygote64_32。对于这个属性的解释如下。init.zygote32.rc:zygote 进程对应的执行程序是 app_process (纯 32bit 模式)init.zygote64.rc:zygote 进程对应的执行程序是 app_process64 (纯 64bit 模式)init.zygote32_64.rc:启动两个 zygote 进程 (名为 zygote 和 zygote_secondary),
7、对应的执行程序分别是 app_process32 (主模式)、app_process64。init.zygote64_32.rc:启动两个 zygote 进程 (名为 zygote 和 zygote_secondary),对应的执行程序分别是 app_process64 (主模式)、app_process32主流的机型属性都为zygote64_32,我们来看看init.zygote64_32.rc文件:service zygote /system/bin/app_process64 -Xzygote /system/bin -zygote -start-system-server -socke
8、t-name=zygote class main socket zygote stream 660 root system onrestart write /sys/android_power/request_state wake onrestart write /sys/power/state on onrestart restart audioserver onrestart restart cameraserver onrestart restart media onrestart restart netd writepid /dev/cpuset/foreground/tasksser
9、vice zygote_secondary /system/bin/app_process32 -Xzygote /system/bin -zygote -socket-name=zygote_secondary class main socket zygote_secondary stream 660 root system onrestart restart zygote writepid /dev/cpuset/foreground/tasks在 Linux 系统中,service 通常是一种被称为守护进程 (daemon) 的程序。它通常在系统启动时启动,并一直运行于后台,直到系统关闭
10、时终止。这里会以service方式来启动zygote进程,app_process的代码位于/frameworks/base/cmds/app_process/路径下,该路径下有一个文件app_main.cpp,入口是main函数。接下来看app_main.cpp函数,这里实现从c+代码调到java代码:int main(int argc, char* const argv) . /android运行时环境 AppRuntime runtime(argv0, computeArgBlockSize(argc, argv); / Process command line arguments / i
11、gnore argv0 argc-; argv+; . if (zygote) /启动java代码 runtime.start("ernal.os.ZygoteInit", args, zygote); else if (className) runtime.start("ernal.os.RuntimeInit", args, zygote); else fprintf(stderr, "Error: no class name or -zygote supplied.n"
12、); app_usage(); LOG_ALWAYS_FATAL("app_process: no class name or -zygote supplied."); return 10; ZygoteInit.java 代码:/frameworks/base/core/Java/com/android/internal/os/ZygoteInit.java public static void main(String argv) . try . boolean startSystemServer = false; String socketName = "zy
13、gote" String abiList = null; for (int i = 1; i < argv.length; i+) if ("start-system-server".equals(argvi) startSystemServer = true; else if (argvi.startsWith(ABI_LIST_ARG) abiList = argvi.substring(ABI_LIST_ARG.length(); else if (argvi.startsWith(SOCKET_NAME_ARG) socketName = argvi
14、.substring(SOCKET_NAME_ARG.length(); else throw new RuntimeException("Unknown command line argument: " + argvi); . /预加载android依赖的文件 preload(); . if (startSystemServer) /启动系统服务 startSystemServer(abiList, socketName); . catch (MethodAndArgsCaller caller) caller.run(); catch (Throwable ex) Lo
15、g.e(TAG, "Zygote died with exception", ex); closeServerSocket(); throw ex; 看这个类的main方法,最上面根据传入的参数判断是否将startSystemServer这个标记设为true,接着预加载android依赖的文件,最后根据上面设置的标记来判断是否能启动系统服务。接着看ZygoteInit.java中main方法在最后调用startSystemServer方法具体内容 private static boolean startSystemServer(String abiList, String
16、socketName) throws MethodAndArgsCaller, RuntimeException . /* Hardcoded command line to start the system server */ String args = "-setuid=1000", "-setgid=1000", "-setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,1021,1032,3001,3002,3003,3006,3007,3009,3010&qu
17、ot;, "-capabilities=" + capabilities + "," + capabilities, "-nice-name=system_server", "-runtime-args", "com.android.server.SystemServer", ; . try parsedArgs = new ZygoteConnection.Arguments(args); ZygoteConnection.applyDebuggerSystemProperty(parsedA
18、rgs); ZygoteConnection.applyInvokeWithSystemProperty(parsedArgs); /Zygote进程开始fork子进程,启动SystemServer /* Request to fork the system server process */ pid = Zygote.forkSystemServer( parsedArgs.uid, parsedArgs.gid, parsedArgs.gids, parsedArgs.debugFlags, null, parsedArgs.permittedCapabilities, parsedArg
19、s.effectiveCapabilities); catch (IllegalArgumentException ex) throw new RuntimeException(ex); . SystemServer.java 代码/frameworks/base/services/java/com/android/server/SystemServer.java /* * The main entry point from zygote. */ public static void main(String args) new SystemServer().run(); private voi
20、d run() . / Start services. try Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartServices"); startBootstrapServices(); /启动核心Service startCoreServices(); /启动其他Service startOtherServices(); catch (Throwable ex) Slog.e("System", "*"); Slog.e("System", "
21、* Failure starting system services", ex); throw ex; finally Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER); / Loop forever. Looper.loop(); throw new RuntimeException("Main thread loop unexpectedly exited"); 启动SystemServer.java的startOtherServices方法/* * Starts a miscellaneous grab bag of
22、 stuff that has yet to be refactored * and organized. */private void startOtherServices() . / We now tell the activity manager it is okay to run third party / code. It will call back into us once it has gotten to the state / where third party code can really run (but before it has actually / started
23、 launching the initial applications), for us to complete our / initialization. mActivityManagerService.systemReady(new Runnable() Override public void run() Slog.i(TAG, "Making services ready"); . .ActivityManagerService.java中的mStackSupervisor.resumeFocusedStackTopActivityLocked() public v
24、oid systemReady(final Runnable goingCallback) . Slog.i(TAG, "System now ready"); EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY, SystemClock.uptimeMillis(); . synchronized (this) / Only start up encryption-aware persistent apps; once user is / unlocked we'll come back around
25、and start unaware apps startPersistentApps(PackageManager.MATCH_DIRECT_BOOT_AWARE); . /调用ActivityStackSupervisor中的resumeFocusedStackTopActivityLocked方法 mStackSupervisor.resumeFocusedStackTopActivityLocked(); mUserController.sendUserSwitchBroadcastsLocked(-1, currentUserId); ActivityStackSupervisor.j
26、ava中的resumeFocusedStackTopActivityLocked方法 boolean resumeFocusedStackTopActivityLocked() return resumeFocusedStackTopActivityLocked(null, null, null); boolean resumeFocusedStackTopActivityLocked( ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) if (targetStack != ull
27、&& isFocusedStack(targetStack) return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions); final ActivityRecord r = mFocusedStack.topRunningActivityLocked(); if (r = null | r.state != RESUMED) /调用ActivityStack中的resumeTopActivityUncheckedLocked来保证栈中的顶层Activity被重新启动 mFocusedSta
28、ck.resumeTopActivityUncheckedLocked(null, null); return false; ActivityStack.java中的resumeTopActivityUncheckedLocked方法 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) . boolean result = false; try / Protect against recursion. mStackSupervisor.inResumeTopActivity
29、 = true; if (mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_LEAVING) mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN; mService.updateSleepIfNeededLocked(); result = resumeTopActivityInnerLocked(prev, options); finally mStackSupervisor.inResumeTopActivity = false
30、; return result; private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) if (DEBUG_LOCKSCREEN) mService.logLockScreen(""); if (!mService.mBooting && !mService.mBooted) / Not ready yet! return false; . if (next = null) / There are no more activitie
31、s! final String reason = "noMoreActivities" final int returnTaskType = prevTask = null | !prevTask.isOverHomeStack() ? HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo(); if (!mFullscreen && adjustFocusToNextFocusableStackLocked(returnTaskType, reason) / Try to move focus to the next visible stack with a running activity if this / stack is not covering the entire screen. return mStackSupervisor.resumeFocusedStackTopActivityLocked( mStackSupervisor.getFocusedStack(), prev, null); / Let's just start up the Launcher.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2134电大成本会计历年真题及答案
- 彩妆知识培训课件图片
- 2024食品配料研发实验室员工保密条款合同范本3篇
- 2025年度企业IT运维外包服务及数据备份恢复合同3篇
- 福建省南平市岚下中学2020年高三英语期末试卷含解析
- 2024瓷砖经销商墙地砖销售合同
- 2024年小班区域教案
- 2025年度交通安全责任协议范本3篇
- 2024铝材行业信息化建设与数据共享合同3篇
- 2024高层次人才聘用协议书
- 申论公务员考试试题与参考答案(2024年)
- DB4101T 9.1-2023 反恐怖防范管理规范 第1部分:通则
- 2024-2030年中国公安信息化建设与IT应用行业竞争策略及投资模式分析报告
- 2024年加油站场地出租协议
- 南宁房地产市场月报2024年08月
- 2024年金融理财-担保公司考试近5年真题附答案
- 高中语文古代文学课件:先秦文学
- 人教版五年级上册递等式计算100道及答案
- 文娱产业政策与发展趋势
- 集团公司2024年度内控评价工作方案
- 20以内加减法口算练习题带括号填空135
评论
0/150
提交评论