zygote孵化器在android系统中有着及其中要的位置. android系统中的进程都是有zygote孵化出来的, 所有应用进程都是zygote的子进程. 在init进程中的服务都是由命令启动起来的, zygote服务也不例外, 所以在分析zygote之前要分析一下init.rc中命令是如何执行的.
在分析init进程时知道在init.cpp中的main函数循环执行命令
while (true) {
if (!waiting_for_exec) {
am.ExecuteOneCommand(); //循环执行命令
restart_processes(); //重启死掉的进程
}
int timeout = -1;
if (process_needs_restart) {
timeout = (process_needs_restart - gettime()) * 1000;
if (timeout < 0)
timeout = 0;
}
if (am.HasMoreCommands()) {
timeout = 0;
}
bootchart_sample(&timeout);
epoll_event ev;
int nr = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd, &ev, 1, timeout));
if (nr == -1) {
ERROR("epoll_wait failed: %s\n", strerror(errno));
} else if (nr == 1) {
((void (*)()) ev.data.ptr)();
}
}
return 0;
void ActionManager::ExecuteOneCommand() {
// Loop through the trigger queue until we have an action to execute
while (current_executing_actions_.empty() && !trigger_queue_.empty()) { //当前的可执行action队列为空, trigger_queue_队列不为空
for (const auto& action : actions_) { //循环遍历action_队列,包含了所有需要执行的命令,解析init.rc获得
if (trigger_queue_.front()->CheckTriggers(*action)) { //获取队头的trigger, 检查actions_列表中的action的trigger,对比是否相同
current_executing_actions_.emplace(action.get()); //将所有具有同一trigger的action加入当前可执行action队列
}
}
trigger_queue_.pop(); //将队头trigger出栈
}
if (current_executing_actions_.empty()) {//当前可执行的actions队列为空就返回
return;
}
auto action = current_executing_actions_.front(); //获取当前可执行actions队列的首个action
if (current_command_ == 0) {
std::string trigger_name = action->BuildTriggersString();
INFO("processing action (%s)\n", trigger_name.c_str());
}
action->ExecuteOneCommand(current_command_); //执行当前的命令
// If this was the last command in the current action, then remove
// the action from the executing list.
// If this action was oneshot, then also remove it from actions_.
++current_command_; //不断叠加,将action_中的所有命令取出
if (current_command_ == action->NumCommands()) {
current_executing_actions_.pop();
current_command_ = 0;
if (action->oneshot()) {
auto eraser = [&action] (std::unique_ptr<Action>& a) {
return a.get() == action;
};
actions_.erase(std::remove_if(actions_.begin(), actions_.end(), eraser));
}
}
}
由于所有的命令执行顺序都是根据trigger的顺序执行的, 所以在获取现在需要执行的action就需要检查一下这个action的trigger.
不同的trigger对应这不同的CheckTriggers. 之前讲android7.0 init进程时知道
early-init等的trigger为EventTrigger
wait_for_coldboot_done_action等的trigger就为BuiltinTrigger
在init.rc可以看到类似与on property:sys.powerctl=*这样的trigger为PropertyTrigger
下面具体看一下这几个trigger的CheckTriggers函数
class EventTrigger : public Trigger {
public:
EventTrigger(const std::string& trigger) : trigger_(trigger) {
}
bool CheckTriggers(const Action& action) const override {
return action.CheckEventTrigger(trigger_);
}
private:
const std::string trigger_;
};
class PropertyTrigger : public Trigger {
public:
PropertyTrigger(const std::string& name, const std::string& value)
: name_(name), value_(value) {
}
bool CheckTriggers(const Action& action) const override {
return action.CheckPropertyTrigger(name_, value_);
}
private:
const std::string name_;
const std::string value_;
};
class BuiltinTrigger : public Trigger {
public:
BuiltinTrigger(Action* action) : action_(action) {
}
bool CheckTriggers(const Action& action) const override {
return action_ == &action; //如果action相等就返回true执行
}
private:
const Action*