android源码分析-Zygote

news/2024/7/17 22:09:31

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.\n");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 processgCurRuntime = this;
}

保留了自己作为全局gCurRuntime。
直接看start方法:

void AndroidRuntime::start(const char* className, const Vector<String8>& 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\n");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'\n", 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'\n", className);/* keep going */} else {env->CallStaticVoidMethod(startClass, startMeth, strArray);#if 0if (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 {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);}}}}}

进入一个死循环,每次都将所有要观察的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类(广播类、核心类、其他类);

最后附图一张便于理解:
图片描述


http://lihuaxi.xjx100.cn/news/282062.html

相关文章

简单介绍使用Nginx限制百度蜘蛛频繁抓取的问题

这篇文章主要介绍了使用Nginx限制百度蜘蛛频繁抓取的问题,百度蜘蛛对网站的抓取频率高和抓取量骤增导致服务器负载高&#xff0c;经常收到警告信息&#xff0c;每分钟允许百度蜘蛛抓取200次&#xff0c;超过频率限制的返回503&#xff0c;对Nginx限制蜘蛛频繁抓取相关知识感兴趣…

表情的机器自动识别(有图有真相)

这幅图片是我自己用C#编写的表情的机器自动识别。主要是AdaBoost的实现&#xff0c;训练做了几个不同版本&#xff1a;线性、并行和分布式&#xff0c;训练数据集采用的JAFFE。 有朋友问这东西有什么用处&#xff0c;其实主要是为了玩而已了。这是基于Paul Ekman那本著名的《情…

易想团购 注入 user.php,易想团购系统通杀SQL注入漏洞分析及利用漏洞预警 -电脑资料...

刚打开红黑看到J8基友写的一个{易想团购系统 最新版 通杀}的文章&#xff0c;看他贴的代码里面有个get_client_ip()函数&#xff0c;哈哈&#xff0c;我猜没过滤&#xff0c;果断下了一套程序&#xff0c;找到get_client_ip()函数。// 获取客户端IP地址function get_client_ip(…

图像处理基础:颜色空间及其OpenCV实现

点击上方“小白学视觉”&#xff0c;选择加"星标"或“置顶”重磅干货&#xff0c;第一时间送达作者&#xff1a;Soumyadip Sarkar编译&#xff1a;ronghuaiyang转自&#xff1a;AI公园导读对图像的颜色空间做了一个概念性的介绍&#xff0c;并通过代码的方式可视化了…

50行Python代码,获取公众号全部文章

作者 | 胖虎转载自Python3X&#xff08;ID: python3xxx &#xff09;爬取公众号的方式常见的有两种&#xff1a;通过搜狗搜索去获取&#xff0c;缺点是只能获取最新的十条推送文章。通过微信公众号的素材管理&#xff0c;获取公众号文章。缺点是需要申请自己的公众号。今天介绍…

借助阿里云数加,小小的美甲行业被美甲帮玩出了天价。

原文链接&#xff1a;http://click.aliyun.com/m/13860/ 免费开通大数据服务&#xff1a;https://www.aliyun.com/product/odps“数加对于创业公司来说是一个非常好的平台&#xff0c;可以在有限的投入下&#xff0c;充分地享受到阿里云已经有的技术和经验&#xff0c;站在巨人…

Emptyproject分析

Emptyproject分析(SimpleSample)1&#xff0c;InitApp()WinMain中有一个InitApp()&#xff0c;在sample中存在&#xff0c;但是在emptyproject中没有&#xff0c;该函数是用于设定已经声明的一些一般变量的初始值的。比如某些按钮。2&#xff0c;IsDeviceAcceptable()被WinMain…

oracle9201怎么安装,Solaris10上安装64位Oracle9201

根据Oracle官方文档的步骤进行安装&#xff0c;首先是对Solaris10上系统需求的检查&#xff1a;# /usr/sbin/prtconf | grep "Memory size"Memory size: 16384 Megabytes# /usr/sbin/swap -lswapfile dev swaplo blocks free/dev/dsk/c1t0d0s1 118,9 16 36877808 368…