System Server 分析

SystemServer Android JAVA 层的系统服务模块,这个模块主要功能就是管理供 Android 应用开发的 system service.


1.SystemServer 类是如何启动的

 

    让我们从 Android 的启动过程看起,查看 init.rc 文件,就会发现下面这一行

service zygote /system/bin/app_process -Xzygote /system/bin --zygote –start-system-server

我们知道 zygote 进程是整个 Android 的孵化器进程,所有的 Activity 进程均是通过它来生成的。我们发现在 zygote 进程启动过程中指定了这么一个参数“– start-system-server” ,这个参数就是在 zygote 进程启动的同时启动 SystemServer

     那么 SystemServer 是以什么样的形式启动的呢?是单独的一个进程还是线程,分析一下 zygote 进程的启动过程就明白了。在 Android zygote 进程启动其实就是启动 /system/bin/app_process 这个进程,这个进程的源代码在 frameworks/base/cmds/app_process/app_main.cpp 中。



 

  1. if (0 == strcmp("--zygote", arg)) {  
  2.             bool startSystemServer = (i < argc) ?  
  3.                     strcmp(argv[i], "--start-system-server") == 0 : false;  
  4.             setArgv0(argv0, "zygote");  
  5.             set_process_name("zygote");  
  6.             runtime.start("com.android.internal.os.ZygoteInit",  
  7.                 startSystemServer);  
  8.         } else {  
  9.             set_process_name(argv0);  
  10.             runtime.mClassName = arg;  
  11.             // Remainder of args get passed to startup class main()  
  12.             runtime.mArgC = argc-i;  
  13.             runtime.mArgV = argv+i;  
  14.             LOGV("App process is starting with pid=%d, class=%s./n",  
  15.                  getpid(), runtime.getClassName());  
  16.             runtime.start();  
  17.         }     


     由于 zygote 进程启动过程有“ --zygote” 这个参数,所以走的是下面这步


         runtime.start("com.android.internal.os.ZygoteInit",startSystemServer);


     查看对象 runtime 的类型的定义,

          class AppRuntime : public AndroidRuntime


     因此查看 AndroidRuntime start 方法中的一段代码 (frameworks/base/core/jni/AndroidRuntime.cpp)


 

  1. startClass = env->FindClass(slashClassName);  
  2.     if (startClass == NULL) {  
  3.         LOGE("JavaVM unable to locate class '%s'/n", slashClassName);  
  4.         /* keep going */  
  5.     } else {  
  6.         startMeth = env->GetStaticMethodID(startClass, "main",  
  7.             "([Ljava/lang/String;)V");  
  8.         if (startMeth == NULL) {  
  9.             LOGE("JavaVM unable to find main() in '%s'/n", className);  
  10.             /* keep going */  
  11.         } else {  
  12.             env->CallStaticVoidMethod(startClass, startMeth, strArray);  
  13. #if 0  
  14.             if (env->ExceptionCheck())  
  15.                 threadExitUncaughtException(env);  
  16. #endif  
  17.         }  
  18.     }  


    其中 startClass 即为 "com.android.internal.os.ZygoteInit" ,这段代码调用了 com.android.internal.os.ZygoteInit main 函数。


    那么再往下看 ZygoteInit 类的 main 函数,其中的一段 MethodAndArgsCaller 代码为



  1. if (argv[1].equals("true")) {  
  2.     startSystemServer();  
  3. else if (!argv[1].equals("false")) {  
  4.     throw new RuntimeException(argv[0] + USAGE_STRING);  
  5. }  

 

调用了 startSystemServer () ,这个函数启动了一个子进程来作为 SystemServer 的载体,

1. 它首先指定 SystemServer 进程的参数 ;

2. 根据指定的参数来创建 SystemServer 进程;

3. 调用 handleSystemServerProcess 启动第一步指定进程参数过程中指定的类,此时为“ com.android.server.SystemServer ,启动的这个进程在 ps 查看后显示为” system_server



 

  1. private static boolean startSystemServer()  
  2.             throws MethodAndArgsCaller, RuntimeException {  
  3.         /* Hardcoded command line to start the system server */  
  4.         String args[];  
  5.         String ashmem_size = System.getProperty("gralloc.ashmem_size");  
  6.         if ((null != ashmem_size) && (0 != ashmem_size.length())) {  
  7.           args = new String[] {  
  8.             "--setuid=1000",  
  9.             "--setgid=1000",  
  10.             "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,3001,3002,3003",  
  11.             "--capabilities=130104352,130104352",  
  12.             "--rlimit=8,",  
  13.             "--runtime-init",  
  14.             "--nice-name=system_server",  
  15.             "com.android.server.SystemServer",  
  16.           };  
  17.           args[4] = args[4].concat(ashmem_size);  
  18.           args[4] = args[4].concat(",");  
  19.           args[4] = args[4].concat(ashmem_size);  
  20.         } else {  
  21.           args = new String[] {  
  22.             "--setuid=1000",  
  23.             "--setgid=1000",  
  24.             "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,3001,3002,3003",  
  25.             "--capabilities=130104352,130104352",  
  26.             "--runtime-init",  
  27.             "--nice-name=system_server",  
  28.             "com.android.server.SystemServer",  
  29.           };  
  30.         }  
  31.         ZygoteConnection.Arguments parsedArgs = null;  
  32.         int pid;  
  33.         try {  
  34.             parsedArgs = new ZygoteConnection.Arguments(args);  
  35.             /* 
  36.              * Enable debugging of the system process if *either* the command line flags 
  37.              * indicate it should be debuggable or the ro.debuggable system property 
  38.              * is set to "1" 
  39.              */  
  40.             int debugFlags = parsedArgs.debugFlags;  
  41.             if ("1".equals(SystemProperties.get("ro.debuggable")))  
  42.                 debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER;  
  43.             int[][] rlimits = new int[0][0];  
  44.             if (parsedArgs.rlimits != null) {  
  45.               rlimits = parsedArgs.rlimits.toArray(rlimits);  
  46.             }  
  47.             /* Request to fork the system server process */  
  48.             pid = Zygote.forkSystemServer(  
  49.                     parsedArgs.uid, parsedArgs.gid,  
  50.                     parsedArgs.gids, debugFlags, rlimits,  
  51.                     parsedArgs.permittedCapabilities,  
  52.                     parsedArgs.effectiveCapabilities);  
  53.         } catch (IllegalArgumentException ex) {  
  54.             throw new RuntimeException(ex);  
  55.         }  
  56.         /* For child process */  
  57.         if (pid == 0) {  
  58.             handleSystemServerProcess(parsedArgs);  
  59.         }  
  60.         return true;  
  61.     }  


 

     通过以上过程就会调用到 SystemServer 类,且是运行在一个名为“ system_server” 的进程中,这个进程为 zygote 的子进程。

     虽然 SystemServer 类是运行在 system_server 中的,但是它并不运行在 system_server 的主线程中。

     上代码

 

  1. private static void handleSystemServerProcess(  
  2.             ZygoteConnection.Arguments parsedArgs)  
  3.             throws ZygoteInit.MethodAndArgsCaller {  
  4.         closeServerSocket();  
  5.         /* 
  6.          * Pass the remaining arguments to SystemServer. 
  7.          * "--nice-name=system_server com.android.server.SystemServer" 
  8.          */  
  9.         RuntimeInit.zygoteInit(parsedArgs.remainingArgs);ZygoteInit  
  10.         /* should never reach here */  
  11.     }  
  12.     public static final void zygo用teInit(String[] argv)  
  13.             throws ZygoteInit.MethodAndArgsCaller {  
  14.         // TODO: Doing this here works, but it seems kind of arbitrary. Find  
  15.         // a better place. The goal is to set it up for applications, but not  
  16.         // tools like am.  
  17.         System.setOut(new AndroidPrintStream(Log.INFO, "System.out"));  
  18.         System.setErr(new AndroidPrintStream(Log.WARN, "System.err"));  
  19.         commonInit();  
  20.         zygoteInitNative();  
  21.         int curArg = 0;  
  22.         for ( /* curArg */ ; curArg < argv.length; curArg++) {  
  23.             String arg = argv[curArg];  
  24.             if (arg.equals("--")) {  
  25.                 curArg++;  
  26.                 break;  
  27.             } else if (!arg.startsWith("--")) {  
  28.                 break;  
  29.             } else if (arg.startsWith("--nice-name=")) {  
  30.                 String niceName = arg.substring(arg.indexOf('=') + 1);  
  31.                 Process.setArgV0(niceName);  
  32.             }  
  33.         }  
  34.         if (curArg == argv.length) {  
  35.             Slog.e(TAG, "Missing classname argument to RuntimeInit!");  
  36.             // let the process exit  
  37.             return;  
  38.         }  
  39.         // Remaining arguments are passed to the start class's static main  
  40.         String startClass = argv[curArg++];  
  41.         String[] startArgs = new String[argv.length - curArg];  
  42.         System.arraycopy(argv, curArg, startArgs, 0, startArgs.length);  
  43.         invokeStaticMain(startClass, startArgs);  
  44.     }  
  45.    private static void invokeStaticMain(String className, String[] argv)  
  46.             throws ZygoteInit.MethodAndArgsCaller {  
  47. 用  
  48.         // We want to be fairly aggressive about heap utilization, to avoid  
  49.         // holding on to a lot of memory that isn't needed.  
  50.         VMRuntime.getRuntime().setTargetHeapUtilization(0.75f);  
  51.         Class<?> cl;  
  52.         try {  
  53.             cl = Class.forName(className);  
  54.         } catch (ClassNotFoundException ex) {  
  55.             throw new RuntimeException(  
  56.                     "Missing class when invoking static main " + className,  
  57.                     ex);  
  58.         }  
  59.         Method m;  
  60.         try {  
  61.             m = cl.getMethod("main"new Class[] { String[].class });  
  62.         } catch (NoSuchMethodException ex) {  
  63.             throw new RuntimeException(  
  64.                     "Missing static main on " + className, ex);  
  65.         } catch (SecurityException ex) {  
  66.             throw new RuntimeException(  
  67.                     "Problem getting static main on " + className, ex);  
  68.         }  
  69.         int modifiers = m.getModifiers();用  
  70.         if (! (Modifier.isStatic(modif用iers) && Modifier.isPublic(modifiers))) {  
  71.             throw new RuntimeException(  
  72.                     "Main method is not用 public and static on " + className);  
  73.         }  
  74.         /* 
  75.          * This throw gets caught in ZygoteInit.main(), which responds 
  76.          * by invoking the exception's run() method. This arrangement 
  77.          * clears up all the stack frames that were required in setting 
  78.          * up the process. 
  79.          */  
  80.         throw new ZygoteInit.MethodAndArgsCaller(m, argv);  
  81.     }  


 

由代码可以看出,在调用函数 invokeStaticMain 的最后会抛出一个异常。

   

  1. public static class MethodAndArgsCaller extends Exception implements Runnable   

回过头再看一下 ZygoteInit 类的 main 函数中的一段代码

 

  1. catch (MethodAndArgsCaller caller) {   
  2.   
  3.     caller.run();   
  4.   
  5. }   

其实就是在这个抛出的异常中启动了 SystemServer 类,且这个异常为一个 Runnable 类型,因此 SystemServer 类就运行在 system_server 进程中的一个新的线程里。


2.SystemServer 执行内容


 

1. 执行 frameworks/base/cmds/system_server/library/system_init.cpp 中的 system_init 函数,启动当前进程,也就是进程 system_server 的 pool thread ,以便执行 Binder IPC 操作。

2. 向 SM(Service Manager) 添加系统服务。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值