Android系统如何启动,及涉及的类
目录
1、linux中的init.rc启动配置文件
配置启动的进程zygote,servermanager,surfaceflinger,media等
启动方式 fork+handle ,fork+execve
信号处理(-SIGCHLD)监听子进程的状态
信号是linux的跨进程通信之一,管道,socket,共享内存,信号
2、zygote的作用
启动Android虚拟机,注册AndroidJnI函数,进入Java世界
1、启动SystemServer
2、fork应用进程(例如打开App等)
zygoteInit文件路径…\frameworks\base\core\java\com\android\internal\os
linux Native切换到java 会调用ZygoteInit的main()方法,
启动SystemServer(startSystemServer(abiList, socketName, zygoteServer)),和开启Socket监听( zygoteServer.runSelectLoop(abiList))。
public static void main(String argv[]) {
ZygoteServer zygoteServer = new ZygoteServer();
.../
if (startSystemServer) {
startSystemServer(abiList, socketName, zygoteServer);
}
.../
zygoteServer.runSelectLoop(abiList);
.../
}
开启socket
void runSelectLoop(String abiList) throws Zygote.MethodAndArgsCaller {
ArrayList<FileDescriptor> fds = new ArrayList<FileDescriptor>();
ArrayList<ZygoteConnection> peers = new ArrayList<ZygoteConnection>();
fds.add(mServerSocket.getFileDescriptor());
peers.add(null);
while (true) {
StructPollfd[] pollFds = new StructPollfd[fds.size()];
for (int i = 0; i < pollFds.length; ++i) {
pollFds[i] = new StructPollfd();
pollFds[i].fd = fds.get(i);
pollFds[i].events = (short) POLLIN;
}
try {
Os.poll(pollFds, -1);
} catch (ErrnoException ex) {
throw new RuntimeException("poll failed", ex);
}
for (int i = pollFds.length - 1; i >= 0; --i) {
if ((pollFds[i].revents & POLLIN) == 0) {
continue;
}
if (i == 0) {
ZygoteConnection newPeer = acceptCommandPeer(abiList);
peers.add(newPeer);
fds.add(newPeer.getFileDesciptor());
} else {
boolean done = peers.get(i).runOnce(this);
if (done) {
peers.remove(i);
fds.remove(i);
}
}
}
}
}
socket核心方法是runOnce,Zygote每次孵化进程都会回调这个方法
pid==0是子进程,比如应用进程启动,handleChildProc会调用ActivityThread.main()函数,socket传过来的就是ActivityThread的类名
boolean runOnce(ZygoteServer zygoteServer) throws Zygote.MethodAndArgsCaller {
.../
pid = Zygote.forkAndSpecialize(....);
try {
if (pid == 0) {
// in child
zygoteServer.closeServerSocket();
IoUtils.closeQuietly(serverPipeFd);
serverPipeFd = null;
handleChildProc(parsedArgs, descriptors, childPipeFd, newStderr);
// should never get here, the child is expected to either
// throw Zygote.MethodAndArgsCaller or exec().
return true;
} else {
// in parent...pid of < 0 means failure
IoUtils.closeQuietly(childPipeFd);
childPipeFd = null;
return handleParentProc(pid, descriptors, serverPipeFd, parsedArgs);
}
.../
}
2、SystemServer
开启SystemServer
private static boolean startSystemServer(String abiList, String socketName, ZygoteServer zygoteServer)
throws Zygote.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,1023,1032,3001,3002,3003,3006,3007,3009,3010",
"--capabilities=" + capabilities + "," + capabilities,
"--nice-name=system_server",
"--runtime-args",
"com.android.server.SystemServer",
};
.../
.../
RuntimeInit.commonInit();//常规的初始化
ZygoteInit.nativeZygoteInit();//启动binder线程
//调用SystemServer.main() "com.android.server.SystemServer"
RuntimeInit.applicationInit(targetSdkVersion, argv, classLoader);
}
…\frameworks\base\services\java\com\android\server\SystemServer.java
开启进程的Looper
public static void main(String[] args) {
new SystemServer().run();
}
private void run() {
.../
Looper.prepareMainLooper();
.../
// Initialize native services.
System.loadLibrary("android_servers");
createSystemContext();
startBootstrapServices();
startCoreServices();
startOtherServices();
.../
Looper.loop();
}
发布系统服务,将SystemServer的Binder发布到ServiceManger里边,serviceManger也是一个进程,有init.rc启动。
protected final void publishBinderService(String name, IBinder service) {
publishBinderService(name, service, false);
}
protected final void publishBinderService(String name, IBinder service,
boolean allowIsolated) {
ServiceManager.addService(name, service, allowIsolated);
}
系统服务是跑在binder线程里的
SystemServer会陆续启动AMS,PMS,WMS,PKMS
3、AMS启动桌面
public void systemReady(final Runnable goingCallback, BootTimingsTraceLog traceLog) {
....
startHomeActivityLocked(currentUserId, "systemReady");
....
}
加载桌面图标
@WorkerThread
synchronized @Nullable LoaderTask getNextTaskOrReleaseBackgroundThread() {
Preconditions.checkState(mBackgroundThread != null);
for (final LoaderTask task : mTaskList) {
if (task.getState() == LoaderTask.STATE_LOADING) {
return task;
}
}
final Identifier identifier = mDatabase.getUnmappedDocumentsParent(mDevice.deviceId);
if (identifier != null) {
final LoaderTask existingTask = mTaskList.findTask(identifier);
if (existingTask != null) {
Preconditions.checkState(existingTask.getState() != LoaderTask.STATE_LOADING);
mTaskList.remove(existingTask);
}
final LoaderTask newTask = new LoaderTask(
mMtpManager, mDatabase, mDevice.operationsSupported, identifier);
newTask.loadObjectHandles();
mTaskList.addFirst(newTask);
return newTask;
}
mBackgroundThread = null;
return null;
}