Android 9.0 Zygote进程启动源码分析指南二
* 站在巨人的肩膀上可以看的更远 *
直接参考博文:
系统启动2——启动zygote
系统启动3——Zygote的使命
===== 补充个人的一些儿笔记 =====
一、zygote socket插曲
参见init.zygoteXXXX.rc的内容
在做rc文件解析时,Service解析过程中:
http://androidxref.com/9.0.0_r3/xref/system/core/init/service.cpp#759
>> Service::ParseLine == 解析每行的文字内容
>> Service::OptionParserMap::FindFunction == 根据keyword(socket)找到对应的ParseSocket函数指针
>> Service::ParseSocket == 构造SocketInfo到descriptors_中
待Service:Start()被调用时:
http://androidxref.com/9.0.0_r3/xref/system/core/init/service.cpp#873
>> Service::Start()
>> DescriptorInfo::CreateAndPublish
>> SocketInfo::Create()
>> util.cpp#CreateSocket()
http://androidxref.com/9.0.0_r3/xref/system/core/init/descriptors.cpp#52
void DescriptorInfo::CreateAndPublish(const std::string& globalContext) const { // Create const std::string& contextStr = context_.empty() ? globalContext : context_; int fd = Create(contextStr); if (fd < 0) return; // Publish std::string publishedName = key() + name_; //ANDROID_SOCKET_zygote std::for_each(publishedName.begin(), publishedName.end(), [] (char& c) { c = isalnum(c) ? c : '_'; }); std::string val = std::to_string(fd); // socket句柄int值 setenv(publishedName.c_str(), val.c_str(), 1); // 设置环境变量 // make sure we don't close on exec fcntl(fd, F_SETFD, 0); }
#define ANDROID_SOCKET_ENV_PREFIX "ANDROID_SOCKET_" #define ANDROID_SOCKET_DIR "/dev/socket" const std::string SocketInfo::key() const { return ANDROID_SOCKET_ENV_PREFIX; }
setenv 对于SocketInfo而言设置的是环境变量 ANDROID_SOCKET_socket_name
即,在Service启动时就同时创建了名为zygote的socket,具体的socket地址是:/dev/socket/zygote
创建完成后,将socket句柄数据以环境变量的方式,放置在了 ANDROID_SOCKET_zygote 的key下。