SystemServer是什么?
SystemServer是Android主要核心服务所在的进程,像AMS、WMS、PMS这些服务都是运行在SystemServer进程中的,从Zygote的启动流程中可以知道SystemServer是Zygote进程fork的,SystemServer同时也算是Android系统的应用进程。
SystemServer的作用?
- 创建服务
- 启动服务
- 注册服务
SystemServer的启动流程
1.在Zygote.java的main()方法中调用forkSystemServer来创建进程,主要流程如下:
- 解析参数;
- 调用native函数fork进程;
- 关闭ZygoteServer,进行后续处理;
private static Runnable forkSystemServer(String abiList, String socketName,
ZygoteServer zygoteServer) {
... ...
//1.解析参数
/* 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,3006,3007,3009,3010",
"--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 {
//2.调用native函数fork进程
/* Request to fork the system server process */
pid = Zygote.forkSystemServer(
parsedArgs.mUid, parsedArgs.mGid,
parsedArgs.mGids,
parsedArgs.mRuntimeFlags,
null,
parsedArgs.mPermittedCapabilities,
parsedArgs.mEffectiveCapabilities);
} catch (IllegalArgumentException ex) {
throw new RuntimeException(ex);
}
/* For child process */
if (pid == 0) {
if (hasSecondZygote(abiList)) {
waitForSecondaryZygote(socketName);
}
//3.pid=0为子进程,进行后续处理;
zygoteServer.closeServerSocket();
return handleSystemServerProcess(parsedArgs);
}
return null;
}
2.fork成功之后进入handleSystemServerProcess(parseArgs),根据parsedArgs.mInvokeWith判断是启动SystemServer还是普通应用进程,
最终调用ZygoteInit.zygoteInit();
private static Runnable handleSystemServerProcess(ZygoteArguments parsedArgs) {
if (parsedArgs.mNiceName != null) {
//设置进程名字
Process.setArgV0(parsedArgs.mNiceName);
}
... ...
if (parsedArgs.mInvokeWith != null) {
... ...
//启动应用程序进程
WrapperInit.execApplication(parsedArgs.mInvokeWith,
parsedArgs.mNiceName, parsedArgs.mTargetSdkVersion,
VMRuntime.getCurrentInstructionSet(), null, args);
throw new IllegalStateException("Unexpected return from WrapperInit.execApplication");
} else {
//创建classloader
createSystemServerClassLoader();
ClassLoader cl = sCachedSystemServerClassLoader;
if (cl != null) {
Thread.currentThread().setContextClassLoader(cl);
}
/*
* Pass the remaining arguments to SystemServer.
*/
//进行初始化
return ZygoteInit.zygoteInit(parsedArgs.mTargetSdkVersion,
parsedArgs.mRemainingArgs, cl);
}
/* should never reach here */
}
3.在zygoteInit方法中主要做的:
- 初始化操作 RuntimeInit.commonInit(); 和ZygoteInit.nativeZygoteInit(),其中在ZygoteInit.nativeZygoteInit()中调用了native方法来启动binder线程;
- 最终反射调用SystemServer.java的main方法,正式进入SystemServer处理;
4.运行SystemServer.java的main():
- 创建主线程Looper;
- 加载系统库“ android_servers”;
- 创建系统上下文;
- 分批、分阶段启动各种服务;
- Looper.loop();
public static void main(String[] args) {
new SystemServer().run();
}
private void run() {
// 主线程looper就在当前线程运行
Looper.prepareMainLooper();
//加载android_servers.so库,该库包含的源码在frameworks/base/services/目录下
System.loadLibrary("android_servers");
//初始化系统上下文
createSystemContext();
//创建系统服务管理
mSystemServiceManager = new SystemServiceManager(mSystemContext);
//启动各种系统服务
try {
startBootstrapServices(); // 启动引导服务
startCoreServices(); // 启动核心服务
startOtherServices(); // 启动其他服务
} catch (Throwable ex) {
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
}
//一直循环执行
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
}
- createSystemContext(),该方法主要创建了ActivityThread,并创建了SystemContext:
private void createSystemContext() {
ActivityThread activityThread = ActivityThread.systemMain();
mSystemContext = activityThread.getSystemContext();
mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);
final Context systemUiContext = activityThread.getSystemUiContext();
systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);
}
6.分批、分阶段启动各种Service,并注册到ServiceManager中;
网友评论