3. 执行 early-init action in the two files parsed in step 2.
4. 设备初始化,例如:在 /dev 下面创建所有设备节点,下载 firmwares.
5. 初始化属性服务器,Actually the property system is working as a share memory. Logically it looks like a registry under Windows system.
6. 执行 init action in the two files parsed in step 2.
7. 开启 属性服务。
8. 执行 early-boot and boot actions in the two files parsed in step 2.
9. 执行 Execute property action in the two files parsed in step 2.
10. 进入一个无限循环 to wait for device/property set/child process exit events.例如, 如果SD卡被插入,init会收到一个设备插入事件,它会为这个设备创建节点。系统中比较重要的进程都是由init来fork的,所以如果他们他谁崩溃 了,那么init 将会收到一个 SIGCHLD 信号,把这个信号转化为子进程退出事件, 所以在loop中,init 会操作进程退出事件并且执行 *.rc 文件中定义的命令。 例如,在init.rc中,因为有: service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server socket zygote stream 666 onrestart write /sys/android_power/request_state wake onrestart write /sys/power/state on 所以,如果zygote因为启动某些服务导致异常退出后,init将会重新去启动它。
int main(int argc, char **argv) { ... //需要在后面的程序中看打印信息的话,需要屏蔽open_devnull_stdio()函数 open_devnull_stdio(); ... //初始化log系统 log_init(); //解析/init.rc和/init.%hardware%.rc文件 parse_config_file("/init.rc"); ... snprintf(tmp, sizeof(tmp), "/init.%s.rc", hardware); parse_config_file(tmp); ... //执行 early-init action in the two files parsed in step 2. action_for_each_trigger("early-init", action_add_queue_tail); drain_action_queue(); ... /* execute all the boot actions to get us started */ /* 执行 init action in the two files parsed in step 2 */ action_for_each_trigger("init", action_add_queue_tail); drain_action_queue(); ... /* 执行 early-boot and boot actions in the two files parsed in step 2 */ action_for_each_trigger("early-boot", action_add_queue_tail); action_for_each_trigger("boot", action_add_queue_tail); drain_action_queue();
/* run all property triggers based on current state of the properties */ queue_all_property_triggers(); drain_action_queue();
if (process_needs_restart) { timeout = (process_needs_restart - gettime()) * 1000; if (timeout < 0) timeout = 0; } ... nr = poll(ufds, 3, timeout); if (nr <= 0) continue;
if (ufds[2].revents == POLLIN) { /* we got a SIGCHLD - reap and restart as needed */ read(signal_recv_fd, tmp, sizeof(tmp)); while (!wait_for_one_process(0)) ; continue; }
if (ufds[0].revents == POLLIN) handle_device_fd(device_fd);
if (ufds[1].revents == POLLIN) { handle_property_set_fd(property_set_fd); } }
对 service 关键字开头的内容进行解析 static void *parse_service(struct parse_state *state, int nargs, char **args) { struct service *svc; if (nargs < 3) { parse_error(state, "services must have a name and a program\n"); return 0; } if (!valid_name(args[1])) { parse_error(state, "invalid service name '%s'\n", args[1]); return 0; } //如果服务已经存在service_list列表中将会被忽略 svc = service_find_by_name(args[1]); if (svc) { parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]); return 0; }