# Copyright (C) 2012 The Android Open Source Project # # IMPORTANT: Do not create world writable files or directories. # This is a common source of Android security bugs. #
执行了 /system/bin/app_process64,并且传入几个参数,注意这里的 --socket-name 参数,并不是给 Zygote 自己监听用的,而是后续传递给 system server 的时候用到的。我们这里先对应找到 app_process 源码文件 app_main.cpp 中的 main 函数
Vector<String8> args; if (!className.isEmpty()) { ... } else { // We're in zygote mode. maybeCreateDalvikCache();
...
// In zygote mode, pass all remaining arguments to the zygote // main() method. for (; i < argc; ++i) { args.add(String8(argv[i])); } }
if (!niceName.isEmpty()) { runtime.setArgv0(niceName.string(), true/* setProcName */); }
if (zygote) { runtime.start("com.android.internal.os.ZygoteInit", args, zygote); } elseif (!className.isEmpty()) { 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."); } }
/* * Start the Android runtime. This involves starting the virtual machine * and calling the "static void main(String[] args)" method in the class * named by "className". * * Passes the main function two arguments, the class name and the specified * options string. */ voidAndroidRuntime::start(constchar* className, const Vector<String8>& options, bool zygote) { ...
/* * Start VM. This thread becomes the main thread of the VM, and will * not return until the VM exits. */ char* slashClassName = toSlashClassName(className != NULL ? className : ""); jclass startClass = env->FindClass(slashClassName); if (startClass == NULL) { ALOGE("JavaVM unable to locate class '%s'\n", slashClassName); /* keep going */ } else { jmethodID startMeth = env->GetStaticMethodID(startClass, "main", "([Ljava/lang/String;)V"); if (startMeth == NULL) { ALOGE("JavaVM unable to find main() in '%s'\n", className); /* keep going */ } else { env->CallStaticVoidMethod(startClass, startMeth, strArray); } }
... }
其中反射调用了主类的 main 函数,对于 Zygote 来说,就是 com.android.internal.os.ZygoteInit,至此,Zygote 启动正式进入 Java Framework 层。ZygoteInit 的 main 方法比复杂,我们一点一点来看:
classZygoteServer{ /** * Runs the zygote process's select loop. Accepts new connections as * they happen, and reads commands from connections one spawn-request's * worth at a time. * @param abiList list of ABIs supported by this zygote. */ Runnable runSelectLoop(String abiList){ ... while (true) { ...
/** * Prepare the arguments and forks for the system server process. * * @return A {@code Runnable} that provides an entrypoint into system_server code in the child * process; {@code null} in the parent. */ privatestatic Runnable forkSystemServer(String abiList, String socketName, ZygoteServer zygoteServer){ ...
/* 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,1023," + "1024,1032,1065,3001,3002,3003,3005,3006,3007,3009,3010,3011,3012", "--capabilities=" + capabilities + "," + capabilities, "--nice-name=system_server", "--runtime-args", "--target-sdk-version=" + VMRuntime.SDK_VERSION_CUR_DEVELOPMENT, "com.android.server.SystemServer", }; ZygoteArguments parsedArgs;
int pid;
try { ZygoteCommandBuffer commandBuffer = new ZygoteCommandBuffer(args); ...
privatestatic Runnable handleSystemServerProcess(ZygoteArguments parsedArgs){ // set umask to 0077 so new files and directories will default to owner-only permissions. Os.umask(S_IRWXG | S_IRWXO); // ^1
if (parsedArgs.mNiceName != null) { Process.setArgV0(parsedArgs.mNiceName); }
final String systemServerClasspath = Os.getenv("SYSTEMSERVERCLASSPATH");