init是Linux用户空间的第一个进程,Kernel启动后会调用/system/core/init/Init.cpp的main()方法.
int main(int argc, char** argv) {
...
init_parse_config_file("/init.rc"); //解析init.rc文件
//执行rc文件中触发器为 on early-init的语句
action_for_each_trigger("early-init", action_add_queue_tail);
//执行rc文件中触发器为 on init的语句
action_for_each_trigger("init", action_add_queue_tail);
//执行rc文件中触发器为 on late-init的语句
action_for_each_trigger("late-init", action_add_queue_tail);
...
}
init.rc文件是系统配置脚本文件
如下配置写明了zygote的配置
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
// class main //伴随着main class的启动而启动
socket zygote stream 660 root system //创建socket
onrestart write /sys/android_power/request_state wake
onrestart write /sys/power/state on
onrestart restart media //当zygote重启时,则会重启media
onrestart restart netd
解析到这些脚本之后,就会经过一系列调用,来启动zygote。
其中最后一个调用节点是init中的service_start。
在这个函数下会调用fork和execv方法,用来创建和执行一个新的进程。
所以说由init启动的服务都在不同的进程里
同zygote一起启动的服务,还有一个比较重要的ServiceManager
以及一众本地服务
ServiceManager是Bindr通信的核心
service servicemanager /system/bin/servicemanager
// class core
user system
group system
critical
onrestart restart healthd
onrestart restart zygote
onrestart restart media
onrestart restart surfaceflinger
onrestart restart drm
当Zygote进程启动后, 便会执行到frameworks/base/cmds/app_process/App_main.cpp文件的main()方法.
在main方法里调用了runtime的start函数
在这里面调用启动虚拟机的函数
startVm
和注册Android方法的函数
startReg
然后调用到ZygoteInit.java
在ZygoteInit的main函数中调用了startSystemServer方法
SystemServer是fork出来的,所以它在一个新的进程里。也就是和Zygote不是一个进程了。
private static boolean startSystemServer(String abiList, String socketName)
throws MethodAndArgsCaller, RuntimeException {
/* 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,1032,3001,3002,3003,3006,3007",
"--capabilities=" + capabilities + "," + capabilities,
"--nice-name=system_server",
"--runtime-args",
"com.android.server.SystemServer",
};
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) {
if (hasSecondZygote(abiList)) {
waitForSecondaryZygote(socketName);
}
handleSystemServerProcess(parsedArgs);
}
return true;
}
handleSystemServerProcess就在本类里
此方法调用了
RuntimeInit.zygoteInit(parsedArgs.targetSdkVersion, parsedArgs.remainingArgs, cl);
在这个函数里调用了
nativeZygoteInit();
invokeStaticMain(args.startClass, args.startArgs, classLoader);
## nativeZygoteInit
这个native方法实现在
/frameworks/base/core/jni/AndroidRuntime.cpp
static void com_android_internal_os_RuntimeInit_nativeZygoteInit(JNIEnv* env, jobject clazz)
{
gCurRuntime->onZygoteInit();
}
gCurRuntime的类型是一个AppRuntime,它定义在
/frameworks/base/cmds/app_process/app_main.cpp
中
virtual void onZygoteInit()
{
sp proc = ProcessState::self();
proc->startThreadPool();
}
在这里启动了Binder通信,好SystemServer可以用Binder通信了。
invokeStaticMain
private static void invokeStaticMain(StringclassName, String[] argv)
throws ZygoteInit.MethodAndArgsCaller {
...
Class<?> cl;
try {
cl = Class.forName(className);
}catch (ClassNotFoundException ex) {
throw new RuntimeException("Missing class wheninvoking static main " + className,ex);
}
Method m;
try {
// get到SystemServer的main方法
m = cl.getMethod("main", new Class[] { String[].class });
}catch (NoSuchMethodException ex) {
...
}catch (SecurityException ex) {
...
}
int modifiers = m.getModifiers();
if(! (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) {
...
}
//抛出一个异常,这个异常在ZygoteInit.main()捕获。
throw new ZygoteInit.MethodAndArgsCaller(m, argv);
}
invokeStaticMain是通过ZygoteInit.main()层层调用到的,通过层层查询,发现这个异常就在
main函数中捕获,语句如下。
catch (MethodAndArgsCaller caller) {
caller.run();
}
private void run() {
Looper.prepareMainLooper();
// Initialize native services.
System.loadLibrary("android_servers");
// Create the system service manager.
mSystemServiceManager = new SystemServiceManager(mSystemContext);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
// Start services.
try {
startBootstrapServices();
startCoreServices();
startOtherServices();
} catch (Throwable ex) {
throw ex;
}
// Loop forever.
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
}
在这里new了一个SystemServiceManager
LocalServices.addService方法是把它添加到一个全局静态的ArrayMap变量sLocalServiceObjects里
private void startBootstrapServices() {
mActivityManagerService = mSystemServiceManager.startService(
ActivityManagerService.Lifecycle.class).getService();
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
// Set up the Application instance for the system process and get started.
mActivityManagerService.setSystemProcess();
}
调用了mSystemServiceManager的startService方法
startService方法内部调用另一个重载的startService
省略部分代码
public <T extends SystemService> T startService(Class<T> serviceClass) {
final String name = serviceClass.getName();
// 通过构造函数 创建一个实例
final T service;
Constructor<T> constructor = serviceClass.getConstructor(Context.class);
service = constructor.newInstance(mContext);
// 注册这个service
mServices.add(service);
// 启动它
service.onStart();
return service;
}
mServices是一个以SystemService为泛型的ArrayList
ActivityManagerService继承自ActivityManagerNative
Lifecycle内部类继承自SystemService,所以可以添加进去
private final ArrayList<SystemService> mServices = new ArrayList<SystemService>();
在这里启动了一众Android核心的Service,也就是JAVA世界的Service。
但是这些Service运行在哪个进程里呢,和VM又是什么关系呢?
ServiceManager 由 init 进程解析 rc 脚本时启动
Zygote 同样由 init 解析 rc 脚本时启动
SystemServer是Zygote的第一个子进程,由其fork出来。
这里启动了一批Android的服务比如AMS PMS等
可见AMS和VM是在不同的进程的,平行的存在。
本文详细介绍了Android系统的启动过程,从init进程开始,通过解析init.rc文件启动关键服务如Zygote和服务管理器ServiceManager,再到SystemServer的启动及核心服务的初始化。
1656

被折叠的 条评论
为什么被折叠?



