intueventd_main(int argc, char** argv){ /* * init sets the umask to 077 for forked processes. We need to * create files with exact permissions, without modification by * the umask. */ umask(000);
// Queue an action that waits for coldboot done so we know ueventd has set up all of /dev... am.QueueBuiltinAction(wait_for_coldboot_done_action, "wait_for_coldboot_done"); // ... so that we can start queuing up actions that require stuff from /dev. am.QueueBuiltinAction(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng"); am.QueueBuiltinAction(set_mmap_rnd_bits_action, "set_mmap_rnd_bits"); am.QueueBuiltinAction(set_kptr_restrict_action, "set_kptr_restrict"); am.QueueBuiltinAction(keychord_init_action, "keychord_init"); am.QueueBuiltinAction(console_init_action, "console_init");
// Trigger all the boot actions to get us started. am.QueueEventTrigger("init");
while (true) { // By default, sleep until something happens. int epoll_timeout_ms = -1;
if (do_shutdown && !shutting_down) { do_shutdown = false; if (HandlePowerctlMessage(shutdown_command)) { shutting_down = true; } }
if (!(waiting_for_prop || sm.IsWaitingForExec())) { am.ExecuteOneCommand(); }
am执行命令接口,先从queue里找到最先入队action,就是early-init了:
voidActionManager::ExecuteOneCommand(){ // Loop through the event queue until we have an action to execute while (current_executing_actions_.empty() && !event_queue_.empty()) { for (constauto& action : actions_) { if (std::visit([&action](constauto& event) { return action->CheckEvent(event); }, event_queue_.front())) { current_executing_actions_.emplace(action.get()); } } event_queue_.pop(); }
if (current_executing_actions_.empty()) { return; }
voidAction::ExecuteCommand(const Command& command)const{ android::base::Timer t; int result = command.InvokeFunc(); //这里
auto duration = t.duration(); // Any action longer than 50ms will be warned to user as slow operation if (duration > 50ms || android::base::GetMinimumLogSeverity() <= android::base::DEBUG) { std::string trigger_name = BuildTriggersString(); std::string cmd_str = command.BuildCommandString();
boolAction::AddCommand(const std::vector<std::string>& args, int line, std::string* err){ if (!function_map_) { *err = "no function map available"; returnfalse; }
auto function = function_map_->FindFunction(args, err); //从function map找到start命令对应的func:do_start if (!function) { returnfalse; }
staticintdo_start(const std::vector<std::string>& args){ Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]); if (!svc) { LOG(ERROR) << "do_start: Service " << args[1] << " not found"; return-1; } if (!svc->Start()) return-1; return0; }
服务启动入口:
boolService::Start(){ // Starting a service removes it from the disabled or reset state and // immediately takes it out of the restarting state if it was in there. flags_ &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START));
// Running processes require no additional work --- if they're in the // process of exiting, we've ensured that they will immediately restart // on exit, unless they are ONESHOT. if (flags_ & SVC_RUNNING) { returnfalse; } ... LOG(INFO) << "starting service '" << name_ << "'...";
这个service就是ueventd了,入口是ueventd_main:
intueventd_main(int argc, char** argv){ /* * init sets the umask to 077 for forked processes. We need to * create files with exact permissions, without modification by * the umask. */ umask(000);
// Historically we had a 1s timeout here because we weren't otherwise // tracking boot time, and many OEMs made their sepolicy regular // expressions too expensive (http://b/19899875).
// Now we're tracking boot time, just log the time taken to a system // property. We still panic if it takes more than a minute though, // because any build that slow isn't likely to boot at all, and we'd // rather any test lab devices fail back to the bootloader. if (wait_for_file(COLDBOOT_DONE, 60s) < 0) { LOG(ERROR) << "Timed out waiting for " COLDBOOT_DONE; panic(); }