首先在system/core/init/init.cpp中
am.QueueBuiltinAction(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
am.QueueBuiltinAction(keychord_init_action, "keychord_init"); //这里加入启动任务
am.QueueBuiltinAction(console_init_action, "console_init");
然后到了
static int keychord_init_action(const std::vector<std::string>& args)
{
keychord_init();
return 0;
}
接着
void keychord_init() {
ServiceManager::GetInstance().ForEachService(add_service_keycodes);
// Nothing to do if no services require keychords.
if (!keychords) {
return;
}
keychord_fd = TEMP_FAILURE_RETRY(open("/dev/keychord", O_RDWR | O_CLOEXEC)); //这里的open调用的就是之前的那篇文章的fops里的open,完成了各种connect
if (keychord_fd == -1) {
ERROR("could not open /dev/keychord: %s\n", strerror(errno));
return;
}
int ret = write(keychord_fd, keychords, keychords_length);
if (ret != keychords_length) {
ERROR("could not configure /dev/keychord %d: %s\n", ret, strerror(errno));
close(keychord_fd);
}
free(keychords);
keychords = nullptr;
register_epoll_handler(keychord_fd, handle_keychord); //后面这个就是根据kernel的信息决定启动那个service
}
看看
static void handle_keychord() {
int ret;
__u16 id;
ret = read(keychord_fd, &id, sizeof(id));
if (ret != sizeof(id)) {
ERROR("could not read keychord id\n");
return;
}
// Only handle keychords if adb is enabled.
std::string adb_enabled = property_get("init.svc.adbd");
if (adb_enabled == "running") {
Service* svc = ServiceManager::GetInstance().FindServiceByKeychord(id);
if (svc) {
INFO("Starting service %s from keychord\n", svc->name().c_str());
svc->Start();
} else {
ERROR("service for keychord %d not found\n", id);
}
}
}