您的位置:首页 > 移动开发 > Android开发

Android源码分析之SystemServer的创建过程

2017-01-07 16:22 686 查看

SystemServer进程的创建

前言

SystemServer进程是Android系统的核心之一,大多数的服务都运行在这个进程中。Android的应用程序没有权限访问设备的底层资源,都要通过SystemServer提供的代理来访问。



SystemServer的创建过程

SystemServer的创建分为两个部分,一部分是在Zygote进程中fork并进行初始化,另一部分是执行SystemServer类main来启动服务

Zygote进程fork出SystemServer进程

ZygoteInit类中的main方法会调用startSystemServer来启动SystemServer

public static void main(String argv[]) {
try {
// Start profiling the zygote initialization.
SamplingProfilerIntegration.start();

boolean startSystemServer = false;
String socketName = "zygote";
String abiList = null;
for (int i = 1; i < argv.length; i++) {
if ("start-system-server".equals(argv[i])) {
startSystemServer = true;
} else if (argv[i].startsWith(ABI_LIST_ARG)) {
abiList = argv[i].substring(ABI_LIST_ARG.length());
} else if (argv[i].startsWith(SOCKET_NAME_ARG)) {
socketName = argv[i].substring(SOCKET_NAME_ARG.length());
} else {
throw new RuntimeException("Unknown command line argument: " + argv[i]);
}
}

if (abiList == null) {
throw new RuntimeException("No ABI list supplied.");
}

registerZygoteSocket(socketName);
EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START,
SystemClock.uptimeMillis());
preload();
EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END,
SystemClock.uptimeMillis());

// Finish profiling the zygote initialization.
SamplingProfilerIntegration.writeZygoteSnapshot();

// Do an initial gc to clean up after startup
gc();

// Disable tracing so that forked processes do not inherit stale tracing tags from
// Zygote.
Trace.setTracingEnabled(false);

if (startSystemServer) {
startSystemServer(abiList, socketName);
}

Log.i(TAG, "Accepting command socket connections");
runSelectLoop(abiList);

closeServerSocket();
} catch (MethodAndArgsCaller caller) {
caller.run();
} catch (RuntimeException ex) {
Log.e(TAG, "Zygote died with exception", ex);
closeServerSocket();
throw ex;
}
}


在启动SystemServer的过程中,根据init.rc中定义的zygote的启动参数 “–start-system-server”,会给startSystemServer 赋值为ture,进而执行到 startSystemServer(abiList, socketName); 这个方法。

在启动SystemServer的最后会throw一个例外。从而执行到

caller.run();进行执行启动系统众多服务的功能。

startSystemServer

这个方法主要做了三件事

- 为SystemServer启动准备参数

- fork出SystemServer进程

- 对SystemServer进行初始化的操作

/* 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,1032,3001,3002,3003,3006,3007",
"--capabilities=" + capabilities + "," + capabilities,
"--runtime-init",
"--nice-name=system_server",
"com.android.server.SystemServer",
};


为SystemServer准备了启动参数 ,设置进程id和组id为1000。指定了SystemServer的执行类为com.android.server.SystemServer” ,这个参数将会在parsedArgs.remainingArgs 中存储。

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);
}


通过Zygore.forkSystemServer fork出SystemServer进程。

会调用native层的 nativeForkSystemServer来fork子进程。

通过还会检查如果SystemServer创建不成功。则Zygote会杀死自己,重新启动。

/* For child process */
if (pid == 0) {
if (hasSecondZygote(abiList)) {
waitForSecondaryZygote(socketName);
}

handleSystemServerProcess(parsedArgs);
}


如果pid ==0 ,则表示是子进程即SystemServer进程。在子进程中会执行 handleSystemServerProcess(parsedArgs);

来做初始化的操作。

在这个方法中,主要是通过

RuntimeInit.zygoteInit(parsedArgs.targetSdkVersion, parsedArgs.remainingArgs, cl);


进行初始化的。这里的parsedArgs.remainingArgs 即为com.android.server.SystemServer 。

“`

public static final void zygoteInit(int targetSdkVersion, String[] argv, ClassLoader classLoader)

throws ZygoteInit.MethodAndArgsCaller {

if (DEBUG) Slog.d(TAG, “RuntimeInit: Starting application from zygote”);

redirectLogStreams();

commonInit();
nativeZygoteInit();

applicationInit(targetSdkVersion, argv, classLoader);
}


“`

调用applicationInit实现初始化的操作。在初始化中,通过反射的机制找到 SystemServer类的main方法。最后会throw一个例外

throw new ZygoteInit.MethodAndArgsCaller(m, argv);

这个例外被MethodAndArgsCaller 进行catch到。执行caller.run,就会通过反射执行到SystemServer的main方法。

从此SystemServer进程就启动系统中绝大多数的服务。

# SystemServer的初始化过程

SystemSever服务的启动

SystemServer进程在Zygote中被创建,最后会调用caller.run来运行SystemServer的main方法。

从而开启系统的大多数的服务。系统的服务主要分为 BootstrapService、CoreService和OtherService

BootStrapService

在系统所开启的服务中有些服务是相互依赖的,并不是独立的。在BootstrapService中,启动的服务就是这些必须先启动的服务。

Installer

ActivityManagerService

PowerManagerService

DisplayManagerService

PackageManagerService

UserManagerService

Installer 服务和后续的PackagerManagerService想关联,和应用的安装项关联

ActivityManagerService 是Android系统中最重要的服务。管理Android系统的4大组建 Activity、Service、BroardCast、ContentProvider

PowerManagerService 电源管理服务需要先启动起来。其他的Service需要电源管理服务。

CoreServices

系统的核心服务有

LightsService

BatteryService

UsageStatsService

WebViewUpdateService

LightsService 灯光服务包括了系统的指示灯、背光灯、按键灯等灯光系统

BatteryService 和电池相关的服务,当电池状态改变时会发送广播

OtherService

OtherService中,会启动众多的服务服务。

AccountManagerService

ContentService

VibratorService

LedService

IAlarmManager

MountService

NetworkManagementService

NetworkStatsService

NetworkPolicyManagerService

ConnectivityService

NetworkScoreService

NsdService

WindowManagerService

BluetoothManagerService

UsbService

SerialService

NetworkTimeUpdateService

CommonTimeManagementService

InputManagerService

TelephonyRegistry

ConsumerIrService

AudioService

MmsServiceBroker

等等

众多的服务会添加到到ServiceManager中,提供应用程序使用

在OtherServices的最后,会调用到ActivityManagerService的systemReady。来执行

一些服务的systemReady

总结

SystemServer进程是由Zygote进程创建出来。 在init.rc中进行指定启动的参数,

SystemServer启动后,会运行SystemServer.java的main方法,从此启动系统众多的服务,比较PMS WMS AMS等。

SystemServer启动的服务,应用程序可以通过Context.getSystemServer()来调用。

其他

SystemServer分析之启动服务

涉及的代码

/frameworks/base/core/java/com/android/internal/os/Zygote.java
/frameworks/base/core/java/com/android/internal/os/RuntimeInit.java
/frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
/frameworks/base/services/java/com/android/server/SystemServer.java
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android