Zygote进程启动System Server进程的过程

Android系统启动时,Init进程会把Zygote进程启动起来,而Zygote进程会把System Server进程启动起来。

Zygote进程启动System Server进程的过程_第1张图片



App_main.cpp
int main(int argc, char* const argv[])
{
   //创建一个AppRuntime对象runtime,通过它进一步启动Zygote进程
    AppRuntime runtime(argv[0], computeArgBlockSize(argc, argv));
    
    // Parse runtime arguments.  Stop at first unrecognized option.
    bool zygote = false;
  
		    //参数列表中有--zygote,变量zygote置为ture
            zygote = true;
           
			//参数列表中增加start-system-server,表示Zygote进程启动完成后会启动System进程
            args.add(String8("start-system-server"));
   
		//调用类AppRuntime的成员函数start,注意传进去的字符串是"com.android.internal.os.ZygoteInit"
        runtime.start("com.android.internal.os.ZygoteInit", args);
    
}


//注意AppRuntime继承AndroidRuntime
class AppRuntime : public AndroidRuntime
{
public:
    AppRuntime(char* argBlockStart, const size_t argBlockLength)
		//会创建AndroidRuntime
        : AndroidRuntime(argBlockStart, argBlockLength)

void AndroidRuntime::start(const char* className, const Vector<String8>& options)
{
	//启动虚拟机实例
    if (startVm(&mJavaVM, &env) != 0) {

	//注册JNI方法
    if (startReg(env) < 0) {

		//调用com.android.internal.os.ZygoteInit类的静态成员函数main来进一步启动Zygote进程
        	//注意这时候从cpp转到了java
            env->CallStaticVoidMethod(startClass, startMeth, strArray);
 
}

ZygoteInit.java
    public static void main(String argv[]) {

			//创建Server端Socket
            registerZygoteSocket(socketName);

				//启动系统关键服务进程System进程
                startSystemServer(abiList, socketName);

			//循环等待Activity管理服务ActivityManagerService请求Zygote进行创建新的应用程序进程
            runSelectLoop(abiList);
	}

    private static void registerZygoteSocket(String socketName) {
				//获取名称为"ANDROID_SOCKET_zygote"的环境变量的值
                String env = System.getenv(fullSocketName);
				//转化为文件描述符
                fileDesc = Integer.parseInt(env);

				//创建Server端Socket
                sServerSocket = new LocalServerSocket(
                        createFileDescriptor(fileDesc));
            
    }

   private static boolean startSystemServer(String abiList, String socketName)
            throws MethodAndArgsCaller, RuntimeException {
			//调用类Zygote的成员函数forkSystemServer来fork System Server进程
            pid = Zygote.forkSystemServer(
        
		//在System Server子进程中
        if (pid == 0) {

			//运行System Server进程
            handleSystemServerProcess(parsedArgs);

    }

	static JNINativeMethod gMethods[] = {
	//nativeForkSystemServer对应com_android_internal_os_Zygote_nativeForkSystemServer
    { "nativeForkSystemServer", "(II[II[[IJJ)I",
      (void *) com_android_internal_os_Zygote_nativeForkSystemServer }
};

static jint com_android_internal_os_Zygote_nativeForkSystemServer(
        JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
        jint debug_flags, jobjectArray rlimits, jlong permittedCapabilities,
        jlong effectiveCapabilities) {
	//还没fork,估计也快了
  pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids,
                                      debug_flags, rlimits,
                                      permittedCapabilities, effectiveCapabilities,
                                      MOUNT_EXTERNAL_NONE, NULL, NULL, true, NULL,
                                      NULL, NULL);
}



static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids,
                                     jint debug_flags, jobjectArray javaRlimits,
                                     jlong permittedCapabilities, jlong effectiveCapabilities,
                                     jint mount_external,
                                     jstring java_se_info, jstring java_se_name,
                                     bool is_system_server, jintArray fdsToClose,
                                     jstring instructionSet, jstring dataDir) {
  
 //System Server 进程就是在这里fork的
  pid_t pid = fork();
}

 private static void handleSystemServerProcess(
            ZygoteConnection.Arguments parsedArgs)
            throws ZygoteInit.MethodAndArgsCaller {
			//还没运行System Server进程
            RuntimeInit.zygoteInit(parsedArgs.targetSdkVersion, parsedArgs.remainingArgs, cl);
  
    }

 public static final void zygoteInit(int targetSdkVersion, String[] argv, ClassLoader classLoader)
            throws ZygoteInit.MethodAndArgsCaller {
        if (DEBUG) Slog.d(TAG, "RuntimeInit: Starting application from zygote");


        nativeZygoteInit();
		
		//还没运行System Server进程,估计也差不多了吧
        applicationInit(targetSdkVersion, argv, classLoader);
    }
	

private static void applicationInit(int targetSdkVersion, String[] argv, ClassLoader classLoader)
            throws ZygoteInit.MethodAndArgsCaller {
        //还没运行System Server进程
        invokeStaticMain(args.startClass, args.startArgs, classLoader);
    }

 private static void invokeStaticMain(String className, String[] argv, ClassLoader classLoader)
            throws ZygoteInit.MethodAndArgsCaller {
      

        /*
         * This throw gets caught in ZygoteInit.main(), which responds
         * by invoking the exception's run() method. This arrangement
         * clears up all the stack frames that were required in setting
         * up the process.
         */
		 //没有直接运行System Server进程而是抛出一个异常,原因见注释,过程太曲折,层层调用函数
        throw new ZygoteInit.MethodAndArgsCaller(m, argv);
    }

public static class MethodAndArgsCaller extends Exception
            implements Runnable {
      
        public void run() {
            //异常在这里补抓到,调用invoke启动System Server进程,运行的函数是类SystemServer.java的静态main函数
                mMethod.invoke(null, new Object[] { mArgs });
            
    }


你可能感兴趣的:(android,server,源代码,System,启动,Zygote)