开篇 本篇以android-11.0.0_r25作为基础解析
上一篇文章Android源码分析 - Zygote进程 ,我们分析了Android Zygote
进程的启动和之后是如何接收消息创建App进程的
在上一章中,我们说了,Zygote
的一大作用就是启动SystemServer
,那么SystemServer
是怎么启动的呢?启动后又做了些什么呢?我们分上下两篇来分析,本篇介绍SystemServer
是如何启动的
介绍 SystemServer
主要是用来创建系统服务的,譬如我们熟知的ActivityManagerService
,PackageManagerService
都是由它创建的
启动SystemServer 我们从上一篇文章的ZygoteInit
开始,ZygoteInit
类的源码路径为frameworks/base/core/java/com/android/internal/os/ZygoteInit.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public static void main (String argv[]) { ... boolean startSystemServer = false ; ... for (int i = 1 ; i < argv.length; i++) { if ("start-system-server" .equals(argv[i])) { startSystemServer = true ; } ... } ... if (startSystemServer) { Runnable r = forkSystemServer(abiList, zygoteSocketName, zygoteServer); if (r != null ) { r.run(); return ; } } }
之前在c++代码中JNI调用Java函数的时候,带了参数start-system-server
,在这里就会通过这个参数判断是否启动SystemServer
,接下来调用forkSystemServer
方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 private static Runnable forkSystemServer (String abiList, String socketName, ZygoteServer zygoteServer) { long capabilities = posixCapabilitiesAsBits( OsConstants.CAP_IPC_LOCK, OsConstants.CAP_KILL, OsConstants.CAP_NET_ADMIN, OsConstants.CAP_NET_BIND_SERVICE, OsConstants.CAP_NET_BROADCAST, OsConstants.CAP_NET_RAW, OsConstants.CAP_SYS_MODULE, OsConstants.CAP_SYS_NICE, OsConstants.CAP_SYS_PTRACE, OsConstants.CAP_SYS_TIME, OsConstants.CAP_SYS_TTY_CONFIG, OsConstants.CAP_WAKE_ALARM, OsConstants.CAP_BLOCK_SUSPEND ); StructCapUserHeader header = new StructCapUserHeader( OsConstants._LINUX_CAPABILITY_VERSION_3, 0 ); StructCapUserData[] data; try { data = Os.capget(header); } catch (ErrnoException ex) { throw new RuntimeException("Failed to capget()" , ex); } capabilities &= ((long ) data[0 ].effective) | (((long ) data[1 ].effective) << 32 ); 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,3006,3007,3009,3010,3011" , "--capabilities=" + capabilities + "," + capabilities, "--nice-name=system_server" , "--runtime-args" , "--target-sdk-version=" + VMRuntime.SDK_VERSION_CUR_DEVELOPMENT, "com.android.server.SystemServer" , }; ZygoteArguments parsedArgs = null ; int pid; try { parsedArgs = new ZygoteArguments(args); ... pid = Zygote.forkSystemServer( parsedArgs.mUid, parsedArgs.mGid, parsedArgs.mGids, parsedArgs.mRuntimeFlags, null , parsedArgs.mPermittedCapabilities, parsedArgs.mEffectiveCapabilities); } catch (IllegalArgumentException ex) { throw new RuntimeException(ex); } if (pid == 0 ) { if (hasSecondZygote(abiList)) { waitForSecondaryZygote(socketName); } zygoteServer.closeServerSocket(); return handleSystemServerProcess(parsedArgs); } return null ; }
Capabilities 这里需要先了解一下Linux Capabilities机制:Linux Capabilities机制
这里先定义了SystemServer
进程的Permitted
和Effective
能力集合
1 2 3 4 5 6 7 8 9 10 11 12 private static long posixCapabilitiesAsBits (int ... capabilities) { long result = 0 ; for (int capability : capabilities) { if ((capability < 0 ) || (capability > OsConstants.CAP_LAST_CAP)) { throw new IllegalArgumentException(String.valueOf(capability)); } result |= (1L << capability); } return result; }
检查一下有无非法capability
,然后做位或运算,构建出一个capabilities
集合
然后通过Os.capget
方法获取当前线程的capabilities
集合,上一篇文章中我们已经分析过了Os的作用,最终通过Linux_capget
JNI函数调用Linuxcapget
函数,通过返回回来的值,剔除一些当前线程不支持的特权
Fork 接着设置一些fork参数,通过ZygoteArguments
去解析它
然后调用Zygote.forkSystemServer
方法,这个和上一章里说的fork App的过程差不多
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 static int forkSystemServer (int uid, int gid, int [] gids, int runtimeFlags, int [][] rlimits, long permittedCapabilities, long effectiveCapabilities) { ZygoteHooks.preFork(); int pid = nativeForkSystemServer( uid, gid, gids, runtimeFlags, rlimits, permittedCapabilities, effectiveCapabilities); Thread.currentThread().setPriority(Thread.NORM_PRIORITY); ZygoteHooks.postForkCommon(); return pid; }
先把子线程都停止掉,fork完后再恢复,调用native函数nativeForkSystemServer
,路径为frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 static jint com_android_internal_os_Zygote_nativeForkSystemServer ( JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids, jint runtime_flags, jobjectArray rlimits, jlong permitted_capabilities, jlong effective_capabilities) { ... pid_t pid = ForkCommon(env, true , fds_to_close, fds_to_ignore, true ); if (pid == 0 ) { SpecializeCommon(env, uid, gid, gids, runtime_flags, rlimits, permitted_capabilities, effective_capabilities, MOUNT_EXTERNAL_DEFAULT, nullptr , nullptr , true , false , nullptr , nullptr , false , nullptr , nullptr , false , false ); } else if (pid > 0 ) { ... gSystemServerPid = pid; int status; if (waitpid(pid, &status, WNOHANG) == pid) { ALOGE("System server process %d has died. Restarting Zygote!" , pid); RuntimeAbort(env, __LINE__, "System server process has died. Restarting Zygote!" ); } if (UsePerAppMemcg()) { if (!SetTaskProfiles(pid, std ::vector <std ::string >{"SystemMemoryProcess" })) { ALOGE("couldn't add process %d into system memcg group" , pid); } } } return pid; }
ForkCommon 我们先看ForkCommon
函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 static pid_t ForkCommon (JNIEnv* env, bool is_system_server, const std::vector<int >& fds_to_close, const std::vector<int >& fds_to_ignore, bool is_priority_fork) { SetSignalHandlers(); auto fail_fn = std::bind(ZygoteFailure, env, is_system_server ? "system_server" : "zygote" , nullptr, _1); BlockSignal(SIGCHLD, fail_fn); __android_log_close(); AStatsSocket_close(); if (gOpenFdTable == nullptr) { gOpenFdTable = FileDescriptorTable::Create(fds_to_ignore, fail_fn); } else { gOpenFdTable->Restat(fds_to_ignore, fail_fn); } android_fdsan_error_level fdsan_error_level = android_fdsan_get_error_level(); mallopt(M_PURGE, 0 ); pid_t pid = fork(); if (pid == 0 ) { if (is_priority_fork) { setpriority(PRIO_PROCESS, 0 , PROCESS_PRIORITY_MAX); } else { setpriority(PRIO_PROCESS, 0 , PROCESS_PRIORITY_MIN); } PreApplicationInit(); DetachDescriptors(env, fds_to_close, fail_fn); ClearUsapTable(); gOpenFdTable->ReopenOrDetach(fail_fn); android_fdsan_set_error_level(fdsan_error_level); gSystemServerSocketFd = -1 ; } else { ALOGD("Forked child process %d" , pid); } UnblockSignal(SIGCHLD, fail_fn); return pid; }
处理子进程信号 先设置子进程信号处理器
1 2 3 4 5 6 7 8 9 10 11 12 13 static void SetSignalHandlers () { struct sigaction sig_chld = {.sa_flags = SA_SIGINFO, .sa_sigaction = SigChldHandler}; if (sigaction(SIGCHLD, &sig_chld, nullptr) < 0 ) { ALOGW("Error setting SIGCHLD handler: %s" , strerror(errno)); } struct sigaction sig_hup = {}; sig_hup.sa_handler = SIG_IGN; if (sigaction(SIGHUP, &sig_hup, nullptr) < 0 ) { ALOGW("Error setting SIGHUP handler: %s" , strerror(errno)); } }
关于信号的处理,我们在Android源码分析 - init进程 中已经了解过一次,SA_SIGINFO
这个flag代表调用信号处理函数sa_sigaction
的时候,会将信号的信息通过参数siginfo_t
传入
SIGHUP
表示终端断开信号,SIG_IGN
表示忽略信号,即忽略终端断开信号
我们看一下Zygote
是怎么处理其子进程信号的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 static void SigChldHandler (int , siginfo_t* info, void * ) { pid_t pid; int status; ... int saved_errno = errno; while ((pid = waitpid(-1 , &status, WNOHANG)) > 0 ) { sendSigChildStatus(pid, info->si_uid, status); ... if (pid == gSystemServerPid) { async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "Exit zygote because system server (pid %d) has terminated" , pid); kill(getpid(), SIGKILL); } } ... errno = saved_errno; }
如果检测到有子进程退出,通知SystemServer
,如果这个进程是SystemServer
进程,杀掉Zygote
进程重启
ZygoteFailure 这里先需要理解一下C++11 中的std::function和std::bind
简单来说,std::bind
返回了一个std::function
对象,它是一个可调用对象,实际调用的就是传入的第一个参数:ZygoteFailure
函数,这个函数接受4个参数,前三个参数都在std::bind
时提供好了,第四个参数以_1
占位符替代(std::placeholders::_1
)
实际上调用fail_fn(msg)
就相当于调用函数ZygoteFailure(env, "system_server", nullptr, msg)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 static void ZygoteFailure (JNIEnv* env, const char * process_name, jstring managed_process_name, const std::string& msg) { std::unique_ptr<ScopedUtfChars> scoped_managed_process_name_ptr = nullptr; if (managed_process_name != nullptr) { scoped_managed_process_name_ptr.reset(new ScopedUtfChars(env, managed_process_name)); if (scoped_managed_process_name_ptr->c_str() != nullptr) { process_name = scoped_managed_process_name_ptr->c_str(); } } const std::string& error_msg = (process_name == nullptr) ? msg : StringPrintf("(%s) %s" , process_name, msg.c_str()); env->FatalError(error_msg.c_str()); __builtin_unreachable(); }
当发生错误后,最终向Java层抛出了一个异常
BlockSignal & UnblockSignal 在fork
期间需要阻塞住SIGCHLD
信号,避免在SIGCHLD
信号处理函数中打印log,导致后面关闭的日志fd重新被打开
1 2 3 4 5 6 7 8 9 static void BlockSignal (int signum, fail_fn_t fail_fn) { sigset_t sigs; sigemptyset(&sigs); sigaddset(&sigs, signum); if (sigprocmask(SIG_BLOCK, &sigs, nullptr) == -1 ) { fail_fn(CREATE_ERROR("Failed to block signal %s: %s" , strsignal(signum), strerror(errno))); } }
等fork
结束,取消阻塞SIGCHLD
信号
1 2 3 4 5 6 7 8 9 static void UnblockSignal (int signum, fail_fn_t fail_fn) { sigset_t sigs; sigemptyset(&sigs); sigaddset(&sigs, signum); if (sigprocmask(SIG_UNBLOCK, &sigs, nullptr) == -1 ) { fail_fn(CREATE_ERROR("Failed to un-block signal %s: %s" , strsignal(signum), strerror(errno))); } }
信号集函数我们之前已经在Android源码分析 - init进程 中介绍过了,很简单,就是将SIGCHLD
信号添加到屏蔽集中,fork
完后再将这个信号从屏蔽集中移除
SpecializeCommon 至此,fork操作结束,我们看一下在SystemServer进程中执行的SpecializeCommon
函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 static void SpecializeCommon (JNIEnv* env, uid_t uid, gid_t gid, jintArray gids, jint runtime_flags, jobjectArray rlimits, jlong permitted_capabilities, jlong effective_capabilities, jint mount_external, jstring managed_se_info, jstring managed_nice_name, bool is_system_server, bool is_child_zygote, jstring managed_instruction_set, jstring managed_app_data_dir, bool is_top_app, jobjectArray pkg_data_info_list, jobjectArray whitelisted_data_info_list, bool mount_data_dirs, bool mount_storage_dirs) { const char * process_name = is_system_server ? "system_server" : "zygote" ; auto fail_fn = std::bind(ZygoteFailure, env, process_name, managed_nice_name, _1); auto extract_fn = std::bind(ExtractJString, env, process_name, managed_nice_name, _1); auto se_info = extract_fn(managed_se_info); auto nice_name = extract_fn(managed_nice_name); auto instruction_set = extract_fn(managed_instruction_set); auto app_data_dir = extract_fn(managed_app_data_dir); if (uid != 0 ) { EnableKeepCapabilities(fail_fn); } SetInheritable(permitted_capabilities, fail_fn); DropCapabilitiesBoundingSet(fail_fn); ... MountEmulatedStorage(uid, mount_external, need_pre_initialize_native_bridge, fail_fn); ... SetGids(env, gids, is_child_zygote, fail_fn); SetRLimits(env, rlimits, fail_fn); ... if (setresgid(gid, gid, gid) == -1 ) { fail_fn(CREATE_ERROR("setresgid(%d) failed: %s" , gid, strerror(errno))); } SetUpSeccompFilter(uid, is_child_zygote); SetSchedulerPolicy(fail_fn, is_top_app); if (setresuid(uid, uid, uid) == -1 ) { fail_fn(CREATE_ERROR("setresuid(%d) failed: %s" , uid, strerror(errno))); } ... SetCapabilities(permitted_capabilities, effective_capabilities, permitted_capabilities, fail_fn); __android_log_close(); AStatsSocket_close(); ... if (nice_name.has_value()) { SetThreadName(nice_name.value()); } else if (is_system_server) { SetThreadName("system_server" ); } UnsetChldSignalHandler(); if (is_system_server) { env->CallStaticVoidMethod(gZygoteClass, gCallPostForkSystemServerHooks, runtime_flags); if (env->ExceptionCheck()) { fail_fn("Error calling post fork system server hooks." ); } ... } ... env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, runtime_flags, is_system_server, is_child_zygote, managed_instruction_set); setpriority(PRIO_PROCESS, 0 , PROCESS_PRIORITY_DEFAULT); if (env->ExceptionCheck()) { fail_fn("Error calling post fork hooks." ); } }
这里做了很多工作,有Capabilities
相关,selinux
相关,权限相关等等,有点太多了,我标了注释,就不再一一分析了
接下来回到nativeForkSystemServer
中,在Zygote
进程中继续执行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 static jint com_android_internal_os_Zygote_nativeForkSystemServer ( JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids, jint runtime_flags, jobjectArray rlimits, jlong permitted_capabilities, jlong effective_capabilities) { ... if (pid == 0 ) { ... } else if (pid > 0 ) { ... gSystemServerPid = pid; int status; if (waitpid(pid, &status, WNOHANG) == pid) { ALOGE("System server process %d has died. Restarting Zygote!" , pid); RuntimeAbort(env, __LINE__, "System server process has died. Restarting Zygote!" ); } if (UsePerAppMemcg()) { if (!SetTaskProfiles(pid, std ::vector <std ::string >{"SystemMemoryProcess" })) { ALOGE("couldn't add process %d into system memcg group" , pid); } } } return pid; }
通过Linux函数waitpid
检查SystemServer
进程状态,这个函数和之前在Android源码分析 - init进程 中提过的waitid
函数类似,WNOHANG
表示非阻塞等待
如果SystemServer
进程死亡,重启整个Zygote
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 private static Runnable forkSystemServer (String abiList, String socketName, ZygoteServer zygoteServer) { ... if (pid == 0 ) { if (hasSecondZygote(abiList)) { waitForSecondaryZygote(socketName); } zygoteServer.closeServerSocket(); return handleSystemServerProcess(parsedArgs); } return null ; }
cgroups 如果是小内存设备,使用Linux的cgroups
机制,限制SystemServer
进程使用内存大小
1 2 3 4 bool UsePerAppMemcg () { bool low_ram_device = GetBoolProperty("ro.config.low_ram" , false ); return GetBoolProperty("ro.config.per_app_memcg" , low_ram_device); }
关于Linux的cgroups
机制,可以查看这篇文档:cgroups(7) — Linux manual page
关于Android的Cgroups
机制,可以看这篇官方文档:Cgroup 抽象层
运行 初始化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 private static Runnable handleSystemServerProcess (ZygoteArguments parsedArgs) { Os.umask(S_IRWXG | S_IRWXO); if (parsedArgs.mNiceName != null ) { Process.setArgV0(parsedArgs.mNiceName); } final String systemServerClasspath = Os.getenv("SYSTEMSERVERCLASSPATH" ); if (systemServerClasspath != null ) { performSystemServerDexOpt(systemServerClasspath); ... } if (parsedArgs.mInvokeWith != null ) { ... } else { ClassLoader cl = null ; if (systemServerClasspath != null ) { cl = createPathClassLoader(systemServerClasspath, parsedArgs.mTargetSdkVersion); Thread.currentThread().setContextClassLoader(cl); } return ZygoteInit.zygoteInit(parsedArgs.mTargetSdkVersion, parsedArgs.mDisabledCompatChanges, parsedArgs.mRemainingArgs, cl); } }
处理一些初始化操作,然后调用ZygoteInit.zygoteInit
方法
1 2 3 4 5 6 7 8 9 10 public static final Runnable zygoteInit (int targetSdkVersion, long [] disabledCompatChanges, String[] argv, ClassLoader classLoader) { ... RuntimeInit.commonInit(); ZygoteInit.nativeZygoteInit(); return RuntimeInit.applicationInit(targetSdkVersion, disabledCompatChanges, argv, classLoader); }
RuntimeInit
的路径为frameworks/base/core/java/com/android/internal/os/RuntimeInit.java
,先执行通用初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 protected static final void commonInit () { LoggingHandler loggingHandler = new LoggingHandler(); RuntimeHooks.setUncaughtExceptionPreHandler(loggingHandler); Thread.setDefaultUncaughtExceptionHandler(new KillApplicationHandler(loggingHandler)); RuntimeHooks.setTimeZoneIdSupplier(() -> SystemProperties.get("persist.sys.timezone" )); LogManager.getLogManager().reset(); new AndroidConfig(); String userAgent = getDefaultUserAgent(); System.setProperty("http.agent" , userAgent); NetworkManagementSocketTagger.install(); ... initialized = true ; }
接着执行RuntimeInit.applicationInit
1 2 3 4 5 6 7 8 9 10 11 12 13 protected static Runnable applicationInit (int targetSdkVersion, long [] disabledCompatChanges, String[] argv, ClassLoader classLoader) { nativeSetExitWithoutCleanup(true ); VMRuntime.getRuntime().setTargetSdkVersion(targetSdkVersion); VMRuntime.getRuntime().setDisabledCompatChanges(disabledCompatChanges); final Arguments args = new Arguments(argv); ... return findStaticMain(args.startClass, args.startArgs, classLoader); }
参数解析 我们看一下它是怎么解析参数的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Arguments(String args[]) throws IllegalArgumentException { parseArgs(args); } private void parseArgs (String args[]) throws IllegalArgumentException { int curArg = 0 ; for (; curArg < args.length; curArg++) { String arg = args[curArg]; if (arg.equals("--" )) { curArg++; break ; } else if (!arg.startsWith("--" )) { break ; } } if (curArg == args.length) { throw new IllegalArgumentException("Missing classname argument to RuntimeInit!" ); } startClass = args[curArg++]; startArgs = new String[args.length - curArg]; System.arraycopy(args, curArg, startArgs, 0 , startArgs.length); }
循环读参数直到有一项参数为”–”或者不以”–”开头,然后以下一个参数作为startClass
,用再下一个参数到args数组结尾生成一个新的数组作为startArgs
,我们观察一下forkSystemServer
方法中设置的args
1 2 3 4 5 6 7 8 9 10 11 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,3006,3007,3009,3010,3011" , "--capabilities=" + capabilities + "," + capabilities, "--nice-name=system_server" , "--runtime-args" , "--target-sdk-version=" + VMRuntime.SDK_VERSION_CUR_DEVELOPMENT, "com.android.server.SystemServer" , };
可以看出,startClass
应该为com.android.server.SystemServer
,startArgs
数组为空
反射执行 接着调用findStaticMain
方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 protected static Runnable findStaticMain (String className, String[] argv, ClassLoader classLoader) { Class<?> cl; try { cl = Class.forName(className, true , classLoader); } catch (ClassNotFoundException ex) { throw new RuntimeException( "Missing class when invoking static main " + className, ex); } Method m; try { m = cl.getMethod("main" , new Class[] { String[].class }) ; } catch (NoSuchMethodException ex) { throw new RuntimeException( "Missing static main on " + className, ex); } catch (SecurityException ex) { throw new RuntimeException( "Problem getting static main on " + className, ex); } int modifiers = m.getModifiers(); if (! (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) { throw new RuntimeException( "Main method is not public and static on " + className); } return new MethodAndArgsCaller(m, argv); }
这里使用了Java中的反射,找到了SystemServer
中对应的main
方法,并用其创建了一个Runnable
对象MethodAndArgsCaller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 static class MethodAndArgsCaller implements Runnable { private final Method mMethod; private final String[] mArgs; public MethodAndArgsCaller (Method method, String[] args) { mMethod = method; mArgs = args; } public void run () { try { mMethod.invoke(null , new Object[] { mArgs }); } catch (IllegalAccessException ex) { throw new RuntimeException(ex); } catch (InvocationTargetException ex) { Throwable cause = ex.getCause(); if (cause instanceof RuntimeException) { throw (RuntimeException) cause; } else if (cause instanceof Error) { throw (Error) cause; } throw new RuntimeException(ex); } } }
我们最后再回到ZygoteInit
的main
方法中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public static void main (String argv[]) { ... if (startSystemServer) { Runnable r = forkSystemServer(abiList, zygoteSocketName, zygoteServer); if (r != null ) { r.run(); return ; } } }
执行这个在子进程中返回出去的Runnable
:MethodAndArgsCaller
,反射调用SystemServer.main
方法
结束 至此,SystemServer
的启动我们就分析完了,下一篇我们将分析SystemServer
启动后做了什么