摘要:入口函数是创建确认是进程执行进程如果忽略掉参数这些细节,剩下的就是的建立和调用的方法了,启动的是。下面再看下其实主要的就是这句话,前面的都是参数的配置。至此为止,的过程基本分析完毕。
android也是基于linux的系统,因此所有的进程都是从init进程开始的(直接或间接的从init进程fock出来的)。Zygote是受精卵进程,也是系统启动过程中由init进程创建的,具体的看下启动脚本/system/core/rootdir/init.zygote64.rc:
service zygote /system/bin/app_process64 -Xzygote /system/bin --zygote --start-system-server class main priority -20 user root group root readproc socket zygote stream 660 root system onrestart write /sys/android_power/request_state wake onrestart write /sys/power/state on onrestart restart audioserver onrestart restart cameraserver onrestart restart media onrestart restart netd onrestart restart wificond writepid /dev/cpuset/foreground/tasks
可以看出,要执行的进程是/system/bin/app_process64。代码在/frameworks/base/cmds/app_process/app_main.cpp。入口函数是main:
...... // 创建AppRuntime AppRuntime runtime(argv[0], computeArgBlockSize(argc, argv)); ...... while (i < argc) { const char* arg = argv[i++]; if (strcmp(arg, "--zygote") == 0) { // 确认是zygote进程 zygote = true; niceName = ZYGOTE_NICE_NAME; } else if (strcmp(arg, "--start-system-server") == 0) { startSystemServer = true; } else if (strcmp(arg, "--application") == 0) { application = true; } else if (strncmp(arg, "--nice-name=", 12) == 0) { niceName.setTo(arg + 12); } else if (strncmp(arg, "--", 2) != 0) { className.setTo(arg); break; } else { --i; break; } } ...... if (zygote) { // 执行zygote进程 runtime.start("com.android.internal.os.ZygoteInit", args, zygote); } else if (className) { runtime.start("com.android.internal.os.RuntimeInit", args, zygote); } else { fprintf(stderr, "Error: no class name or --zygote supplied. "); app_usage(); LOG_ALWAYS_FATAL("app_process: no class name or --zygote supplied."); } ......
如果忽略掉参数这些细节,剩下的就是AppRuntime的建立和调用AppRuntime的start方法了,启动的是com.android.internal.os.ZygoteInit。
看下AppRuntime:
class AppRuntime : public AndroidRuntime { public: AppRuntime(char* argBlockStart, const size_t argBlockLength) : AndroidRuntime(argBlockStart, argBlockLength) , mClass(NULL) { } ...... };
构造函数中调用了基类的构造方法,基类在/frameworks/base/core/jni/AndroidRuntime.cpp:
AndroidRuntime::AndroidRuntime(char* argBlockStart, const size_t argBlockLength) : mExitWithoutCleanup(false), mArgBlockStart(argBlockStart), mArgBlockLength(argBlockLength) { SkGraphics::Init(); // There is also a global font cache, but its budget is specified by // SK_DEFAULT_FONT_CACHE_COUNT_LIMIT and SK_DEFAULT_FONT_CACHE_LIMIT. // Pre-allocate enough space to hold a fair number of options. mOptions.setCapacity(20); assert(gCurRuntime == NULL); // one per process gCurRuntime = this; }
保留了自己作为全局gCurRuntime。
直接看start方法:
void AndroidRuntime::start(const char* className, const Vector& options, bool zygote) { ...... JniInvocation jni_invocation; jni_invocation.Init(NULL); JNIEnv* env; // 启动虚拟机 if (startVm(&mJavaVM, &env, zygote) != 0) { return; } // 回调虚拟机的创建 onVmCreated(env); /* * Register android functions. */ // 注册函数 if (startReg(env) < 0) { ALOGE("Unable to register all android natives "); return; } ...... jclass stringClass; jobjectArray strArray; jstring classNameStr; // 获得一个string的对象的引用 stringClass = env->FindClass("java/lang/String"); assert(stringClass != NULL); // 创建一个String数组对象 strArray = env->NewObjectArray(options.size() + 1, stringClass, NULL); assert(strArray != NULL); classNameStr = env->NewStringUTF(className); assert(classNameStr != NULL); // 设置第一个string数组的第一个元素是classNameStr,在这里就是ZygoteInit的全名 env->SetObjectArrayElement(strArray, 0, classNameStr); // 设置其他参数 for (size_t i = 0; i < options.size(); ++i) { jstring optionsStr = env->NewStringUTF(options.itemAt(i).string()); assert(optionsStr != NULL); env->SetObjectArrayElement(strArray, i + 1, optionsStr); } /* * Start VM. This thread becomes the main thread of the VM, and will * not return until the VM exits. */ // 转换类中间的.为/,这里是转换格式 char* slashClassName = toSlashClassName(className); // 从jni环境中找到这个类 jclass startClass = env->FindClass(slashClassName); if (startClass == NULL) { ALOGE("JavaVM unable to locate class "%s" ", slashClassName); /* keep going */ } else { // 调用找到的类的main方法,这里就是调用ZygoteInit的main方法 jmethodID startMeth = env->GetStaticMethodID(startClass, "main", "([Ljava/lang/String;)V"); if (startMeth == NULL) { ALOGE("JavaVM unable to find main() in "%s" ", className); /* keep going */ } else { env->CallStaticVoidMethod(startClass, startMeth, strArray); #if 0 if (env->ExceptionCheck()) threadExitUncaughtException(env); #endif } } free(slashClassName); ...... }
关键部分已经给出了注释。总结一下:
1.启动虚拟机startVM;
2.通过startReg注册jni方法;
3.调用ZygoteInit类的main方法;
startVm基本上就是为这个进程建立一个Dalvik虚拟机环境,为当前线程初始化一个jni环境。startReg基本上是注册一大堆的jni方法,以供后面调用。不是本文重点,因此这里不再累述。
下面我们要关注的是ZygoteInit类的main方法了。
/frameworks/base/core/java/com/android/internal/os/ZygoteInit.java:
public static void main(String argv[]) { ZygoteServer zygoteServer = new ZygoteServer(); ...... zygoteServer.registerServerSocket(socketName); ...... preload(); ...... if (startSystemServer) { startSystemServer(abiList, socketName, zygoteServer); } ...... zygoteServer.runSelectLoop(abiList); ...... }
1.创建ZygoteServer(可以看出是个cs架构的东西);
2.注册socket(使用socket进行通讯方式);
3.预加载;
4.启动SystemServer;
5.运行select循环体;
里面涉及到ZygoteHooks的运转,为了不影响整体,暂时做个标记,后面再阅读。
这里可以看到Zygote基本上是个cs架构的情况,并且通过socket进行这种架构的通讯。先来看看预加载过程:
static void preload() { Log.d(TAG, "begin preload"); Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "BeginIcuCachePinning"); beginIcuCachePinning(); Trace.traceEnd(Trace.TRACE_TAG_DALVIK); Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadClasses"); //预加载位于framework/base/preload-classes文件中的类 preloadClasses(); Trace.traceEnd(Trace.TRACE_TAG_DALVIK); Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadResources"); // 预加载资源 preloadResources(); Trace.traceEnd(Trace.TRACE_TAG_DALVIK); Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadOpenGL"); // 预加载资源 preloadOpenGL(); Trace.traceEnd(Trace.TRACE_TAG_DALVIK); //通过System.loadLibrary()方法,预加载"android","compiler_rt","jnigraphics"这3个共享库 preloadSharedLibraries(); //预加载文本连接符资源 preloadTextResources(); // webview的初始化 // Ask the WebViewFactory to do any initialization that must run in the zygote process, // for memory sharing purposes. WebViewFactory.prepareWebViewInZygote(); endIcuCachePinning(); warmUpJcaProviders(); Log.d(TAG, "end preload"); }
看到了吧,都是android本身的一些资源的初始化过程,就是在这里完成的。
下面再看下startSystemServer:
private static boolean startSystemServer(String abiList, String socketName, ZygoteServer zygoteServer) throws Zygote.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_RESOURCE, OsConstants.CAP_SYS_TIME, OsConstants.CAP_SYS_TTY_CONFIG, OsConstants.CAP_WAKE_ALARM ); /* 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", "--setgid=1000", "--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", }; 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); } zygoteServer.closeServerSocket(); handleSystemServerProcess(parsedArgs); } return true; }
其实主要的就是Zygote.forkSystemServer这句话,前面的都是参数的配置。再向下看一层/frameworks/base/core/java/com/android/internal/os/Zygote.java:
public static int forkSystemServer(int uid, int gid, int[] gids, int debugFlags, int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) { VM_HOOKS.preFork(); int pid = nativeForkSystemServer( uid, gid, gids, debugFlags, rlimits, permittedCapabilities, effectiveCapabilities); // Enable tracing as soon as we enter the system_server. if (pid == 0) { Trace.setTracingEnabled(true); } VM_HOOKS.postForkCommon(); return pid; }
根据传递进来的uid,gid等调用函数nativeForkSystemServer,最终会在/frameworks/base/core/jni/com_android_internal_os_Zygote.cpp下的ForkAndSpecializeCommon中调用fork函数,那么实际上就可以知道,就是在c层fork分裂出一个进程来作为SystemServer。
现在我们回来看java层的ZygoteInit.java,继续看看与socket相关的部分,首先是registerServerSocket:
void registerServerSocket(String socketName) { if (mServerSocket == null) { int fileDesc; final String fullSocketName = ANDROID_SOCKET_PREFIX + socketName; try { String env = System.getenv(fullSocketName); fileDesc = Integer.parseInt(env); } catch (RuntimeException ex) { throw new RuntimeException(fullSocketName + " unset or invalid", ex); } try { FileDescriptor fd = new FileDescriptor(); fd.setInt$(fileDesc); mServerSocket = new LocalServerSocket(fd); } catch (IOException ex) { throw new RuntimeException( "Error binding to local socket "" + fileDesc + """, ex); } } }
这里设置了文件描述符,然后创建了LocalServerSocket赋值给了mServerSocket。/frameworks/base/core/java/android/net/LocalServerSocket.java:
public LocalServerSocket(FileDescriptor fd) throws IOException { impl = new LocalSocketImpl(fd); impl.listen(LISTEN_BACKLOG); localAddress = impl.getSockAddress(); }
new出LocalSocketImpl后,直接就开始listen了。下面暂时不用特别看了吧,就是一个走的正常的网络socket了。这里应该就可以证明是以socket的方法进行的通讯。然后再来看看runSelectLoop:
void runSelectLoop(String abiList) throws Zygote.MethodAndArgsCaller { ArrayListfds = new ArrayList (); ArrayList peers = new ArrayList (); 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); } } } } }
进入一个死循环,每次都将所有要观察的fd建立成数组,然后调用Os.poll(pollFds, -1)阻塞等待fd的变化。后面一个for循环是当fd有变化(即有客户端连接,也就是说有其他进程想要与ZygoteServer通讯),此时调用ZygoteConnection的runOnce方法。这个方法如果简单看下的话,最终是要调用Zygote.forkAndSpecialize分裂出进程来的,也就是说这个方法是一旦有连接建立后就表示有app启动了,此时就要fork分裂出新的进程来,代码暂时就不贴了。
至此为止,Zygote的过程基本分析完毕。总结一下:
1.系统启动,通过init进程会启动Zygote进程。确切的将是通过runtime调用了ZygoteInit,这个初始化过程;
2.Zygote是cs架构的,基于socket通讯机制的,在ZygoteInit过程中会启动ZygoteServer,为了等待接收socket的通讯来进行启动app进程的处理;
3.分裂出SystemServer进程,负责启动系统的一些关键服务。包括3类(广播类、核心类、其他类);
最后附图一张便于理解:
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/66824.html
摘要:注此次分析以源码为例。孵化器受精卵名字是受精卵,其实就是帮助或其他进程启动的一个玩意儿。启动系统服务是系统的大核心之一,和一并重要,专管所有的系统服务。每个进程都走这一步这个分支到此先不往下跟踪了,和启动的过程关系不大了。 注:此次分析以6.0源码为例。 android系统是从linux改过来的,因此这里从init进程开始进行分析。 init初始化过程 让我们进入init.cpp来看看...
阅读 604·2023-04-26 01:53
阅读 2714·2021-11-17 17:00
阅读 2857·2021-09-04 16:40
阅读 1959·2021-09-02 15:41
阅读 795·2019-08-26 11:34
阅读 1206·2019-08-26 10:16
阅读 1297·2019-08-23 17:51
阅读 781·2019-08-23 16:50