上一篇[zygote进程的启动]我们讲述了zygote进程的启动,以及循环等待client请求的过程。期间,有一段代码,开启了systemserver进程。 现在我们来看下systemserver到底是怎么起起来的。

进程的创建与启动

启动源头

com.android.internal.os.ZygoteInit#main

1
2
3
if (startSystemServer) {
startSystemServer(abiList, socketName);
}

startSystemServer方法

Zygote.forkSystemServer的执行路径:

  1. com.android.internal.os.Zygote#nativeForkSystemServer
  2. frameworks/base/core/jni/com_android_internal_os_Zygote.cpp: com_android_internal_os_Zygote_nativeForkSystemServer
  3. frameworks/base/core/jni/com_android_internal_os_Zygote.cpp: ForkAndSpecializeCommon
  4. ForkAndSpecializeCommon: pid_t pid = fork(); //至此,调用fork,创建了新的进程
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
private static boolean startSystemServer(String abiList, String socketName)
throws MethodAndArgsCaller, RuntimeException {
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
);
/* Containers run without this capability, so avoid setting it in that case */
if (!SystemProperties.getBoolean(PROPERTY_RUNNING_IN_CONTAINER, false)) {
capabilities |= posixCapabilitiesAsBits(OsConstants.CAP_BLOCK_SUSPEND);
}
/* Hardcoded command line to start the system server */
String args[] = {
"--setuid=1000", //设置system_server进程id
"--setgid=1000", //设置system_server进程group id
"--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,1021,1032,3001,3002,3003,3006,3007,3009,3010",
"--capabilities=" + capabilities + "," + capabilities,
"--nice-name=system_server", //设置进程名字
"--runtime-args",
"com.android.server.SystemServer", //进程锁需要执行的class类名
};
ZygoteConnection.Arguments parsedArgs = null;
int pid;
try {
parsedArgs = new ZygoteConnection.Arguments(args);
ZygoteConnection.applyDebuggerSystemProperty(parsedArgs);
ZygoteConnection.applyInvokeWithSystemProperty(parsedArgs);
/* Request to fork the system server process */
pid = Zygote.forkSystemServer(
parsedArgs.uid, parsedArgs.gid,
parsedArgs.gids,
parsedArgs.debugFlags,
null,
parsedArgs.permittedCapabilities,
parsedArgs.effectiveCapabilities);
} catch (IllegalArgumentException ex) {
throw new RuntimeException(ex);
}
/* For child process */
if (pid == 0) {
//子进程,即zygote的子进程
if (hasSecondZygote(abiList)) {
//如果有第二个zygote进程,不断的重连第二个zygote进程
waitForSecondaryZygote(socketName);
}
//处理SystemServer
handleSystemServerProcess(parsedArgs);
}
return true;
}

handleSystemServerProcess

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
private static void handleSystemServerProcess(
ZygoteConnection.Arguments parsedArgs)
throws ZygoteInit.MethodAndArgsCaller {
//systerserver端不需要 sServerSocket,关闭
closeServerSocket();
// set umask to 0077 so new files and directories will default to owner-only permissions.
Os.umask(S_IRWXG | S_IRWXO);
//设置进程名
if (parsedArgs.niceName != null) {
Process.setArgV0(parsedArgs.niceName);
}
//获取环境变量SYSTEMSERVERCLASSPATH,环境变量位于init.environ.rc中
final String systemServerClasspath = Os.getenv("SYSTEMSERVERCLASSPATH");
////对环境变量SYSTEMSERVERCLASSPATH中的jar包进行dex优化
if (systemServerClasspath != null) {
performSystemServerDexOpt(systemServerClasspath);
}
//systerserver启动参数不包含--invoke-with
if (parsedArgs.invokeWith != null) {
String[] args = parsedArgs.remainingArgs;
// If we have a non-null system server class path, we'll have to duplicate the
// existing arguments and append the classpath to it. ART will handle the classpath
// correctly when we exec a new process.
if (systemServerClasspath != null) {
String[] amendedArgs = new String[args.length + 2];
amendedArgs[0] = "-cp";
amendedArgs[1] = systemServerClasspath;
System.arraycopy(parsedArgs.remainingArgs, 0, amendedArgs, 2, parsedArgs.remainingArgs.length);
}
WrapperInit.execApplication(parsedArgs.invokeWith,
parsedArgs.niceName, parsedArgs.targetSdkVersion,
VMRuntime.getCurrentInstructionSet(), null, args);
} else {
ClassLoader cl = null;
if (systemServerClasspath != null) {
cl = createSystemServerClassLoader(systemServerClasspath,
parsedArgs.targetSdkVersion);
Thread.currentThread().setContextClassLoader(cl);
}
/*
* Pass the remaining arguments to SystemServer.
*/
//最终调用到RuntimeInit.zygoteInit,并传参parsedArgs.remainingArgs = "com.android.server.SystemServer"
RuntimeInit.zygoteInit(parsedArgs.targetSdkVersion, parsedArgs.remainingArgs, cl);
}
/* should never reach here */
}

最终调用到RuntimeInit.zygoteInit,在上篇zygote进程的启动讲述过

SystemServer.main()的执行情况: