intSecondStageMain(int argc, char** argv){ ... // Don't mount filesystems or start core system services in charger mode. std::string bootmode = GetProperty("ro.bootmode", ""); if (bootmode == "charger") { am.QueueEventTrigger("charger"); } else { am.QueueEventTrigger("late-init"); }
在 SecondsStageMain 中加入了 late-init的 trigger
system/core/rootdir/init.rc
1 2 3 4 5
# Mount filesystems and start core system services. on late-init ... # Now we can start zygote for devices with file based encryption trigger zygote-start
# It is recommended to put unnecessary data/ initialization from post-fs-data # to start-zygote in device's init.rc to unblock zygote start. on zygote-start && property:ro.crypto.state=unencrypted # A/B update verifier that marks a successful boot. exec_start update_verifier_nonencrypted start statsd start netd start zygote start zygote_secondary on zygote-start && property:ro.crypto.state=unsupported # A/B update verifier that marks a successful boot. exec_start update_verifier_nonencrypted start statsd start netd start zygote start zygote_secondary on zygote-start && property:ro.crypto.state=encrypted && property:ro.crypto.type=file # A/B update verifier that marks a successful boot. exec_start update_verifier_nonencrypted start statsd start netd start zygote start zygote_secondary
Vector<String8> args; if (!className.isEmpty()) { // We're not in zygote mode, the only argument we need to pass // to RuntimeInit is the application argument. // // The Remainder of args get passed to startup class main(). Make // copies of them before we overwrite them with the process name. args.add(application ? String8("application") : String8("tool")); ... } else { ... if (startSystemServer) { args.add(String8("start-system-server")); } ... String8 abiFlag("--abi-list="); abiFlag.append(prop); args.add(abiFlag);
// In zygote mode, pass all remaining arguments to the zygote // main() method. for (; i < argc; ++i) { args.add(String8(argv[i])); } }
args 是后面调用的 runtime.start() 函数的参数;
2.4 调用 runtime.start 启动 Android 运行时
在 main 函数的最后,调用 runtime.start() 函数启动 android 运行时环境
1 2 3 4 5 6 7 8 9 10
... if (zygote) { runtime.start("com.android.internal.os.ZygoteInit", args, zygote); } elseif (className) { runtime.start("com.android.internal.os.RuntimeInit", args, zygote); } else { fprintf(stderr, "Error: no class name or --zygote supplied.\n"); app_usage(); LOG_ALWAYS_FATAL("app_process: no class name or --zygote supplied."); }
/* * Register android functions. */ if (startReg(env) < 0) { // 注册 JNI ALOGE("Unable to register all android natives\n"); return; } /* * We want to call main() with a String array with arguments in it. * At present we have two arguments, the class name and an option string. * Create an array to hold them. */ jclass stringClass; jobjectArray strArray; jstring classNameStr;
... for (inti=1; i < argv.length; i++) { if ("start-system-server".equals(argv[i])) { startSystemServer = true; ...
// In some configurations, we avoid preloading resources and classes eagerly. // In such cases, we will preload things prior to our first fork. if (!enableLazyPreload) { bootTimingsTraceLog.traceBegin("ZygotePreload"); EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START, SystemClock.uptimeMillis()); preload(bootTimingsTraceLog); // 1.预加载资源 EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END, SystemClock.uptimeMillis()); bootTimingsTraceLog.traceEnd(); // ZygotePreload }
// The select loop returns early in the child process after a fork and // loops forever in the zygote. caller = zygoteServer.runSelectLoop(abiList); // 4.zygote 进入无限循环 } catch (Throwable ex) { ... }
ZygoteInit 的 main 函数主要做了四件事:preload(), new ZygoteServer(), forkSystemServer(), runSelectLoop()。