Android系统启动流程之Init说明
16lz
2021-01-24
要写学习总结了,先将资料整理一下,以备用。
int main(int argc, char **argv){ int fd_count = 0; struct pollfd ufds[4]; char *tmpdev; char* debuggable; char tmp[32]; int property_set_fd_init = 0; int signal_fd_init = 0; int keychord_fd_init = 0; bool is_charger = false;//uevent 热插拔,内核和用户空间的通信机制,netlink实现。 if (!strcmp(basename(argv[0]), "ueventd")) return ueventd_main(argc, argv);//strcmp返回0为相等,大于返回大于0,小于返回小于0.此处为电子狗 if (!strcmp(basename(argv[0]), "watchdogd")) return watchdogd_main(argc, argv);//赋予最大的权限,即初始化权限,权限的定义为文件666-umask的值。文件夹777-umask的值。 /* clear the umask */ umask(0);//创建各种目录 /* Get the basic filesystem setup we need put * together in the initramdisk on / and then we'll * let the rc file figure out the rest. */ mkdir("/dev", 0755); mkdir("/proc", 0755); mkdir("/sys", 0755);//虚拟内存文件系统,用于存放一些启动中的临时文件,于外界交互 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755"); mkdir("/dev/pts", 0755); mkdir("/dev/socket", 0755); mount("devpts", "/dev/pts", "devpts", 0, NULL); mount("proc", "/proc", "proc", 0, NULL); mount("sysfs", "/sys", "sysfs", 0, NULL);//查看.booting是否为可读写和创建 /* indicate that booting is in progress to background fw loaders, etc */ close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000)); /* We must have some place other than / to create the * device nodes for kmsg and null, otherwise we won't * be able to remount / read-only later on. * Now that tmpfs is mounted on /dev, we can actually * talk to the outside world. //貌似与tmpfs有关 */ open_devnull_stdio();//Klog日志初始化,内核级的log日志 klog_init();//初始化属性 property_init();//获取硬件设备名称,此机器名为trace get_hardware_name(hardware, &revision);//处理内核命令 process_kernel_cmdline();//SElinux是linux最杰出的新安全控制子系统,是2.6版本的linux内核中提供强制访问控制(MAC)系统。包含在内核中。 union selinux_callback cb; cb.func_log = log_callback; selinux_set_callback(SELINUX_CB_LOG, cb); cb.func_audit = audit_callback; selinux_set_callback(SELINUX_CB_AUDIT, cb); selinux_initialize(); /* These directories were necessarily created before initial policy load * and therefore need their security context restored to the proper value. * This must happen before /dev is populated by ueventd. */restorecon命令,恢复SElinux文件属性,即恢复文件的安全上下文。 restorecon("/dev"); restorecon("/dev/socket"); restorecon("/dev/__properties__"); restorecon_recursive("/sys");//是否充电? is_charger = !strcmp(bootmode, "charger");//当前启动信息描述,属性初始化 INFO("property init\n"); property_load_boot_defaults(); INFO("reading config file\n");//重点,分析init.rc文件内容 init_parse_config_file("/init.rc");//行为触发器,early-init阶段 action_for_each_trigger("early-init", action_add_queue_tail);//执行各种创建行为 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done"); queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng"); queue_builtin_action(keychord_init_action, "keychord_init"); queue_builtin_action(console_init_action, "console_init");//执行boot action,即init阶段 /* execute all the boot actions to get us started */ action_for_each_trigger("init", action_add_queue_tail); /* Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random * wasn't ready immediately after wait_for_coldboot_done *///再执行一遍mix-hwrng...防止/dev/hw-random或/dev/random(虚假随机的字符流)无法及时响应 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng"); queue_builtin_action(property_service_init_action, "property_service_init"); queue_builtin_action(signal_init_action, "signal_init");//充电模式下,会略过mount文件系统,开启核心服务 /* Don't mount filesystems or start core system services if in charger mode. */ if (is_charger) { action_for_each_trigger("charger", action_add_queue_tail); } else { action_for_each_trigger("late-init", action_add_queue_tail);//因为充电,所以延后初始化 }//基于当前的属性配置状态,启动属性触发器。 /* run all property triggers based on current state of the properties */ queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");//bootchart工具,能对系统的性能进行分析,并生成系统启动过程的图表,提供一些有价值的信息#if BOOTCHART queue_builtin_action(bootchart_init_action, "bootchart_init");#endif//进入无限循环 for(;;) { int nr, i, timeout = -1;//执行命令,重启死去的进程 execute_one_command(); restart_processes();//此处的几个if判断语句,2的源码在循环体的外面。现在移入到里面。每次循环都要判断,初始化状态,设备是否有效//监听来自于属性服务器事件,socket信号事件,keychord设备事件 if (!property_set_fd_init && get_property_set_fd() > 0) { ufds[fd_count].fd = get_property_set_fd(); ufds[fd_count].events = POLLIN; ufds[fd_count].revents = 0; fd_count++; property_set_fd_init = 1; } if (!signal_fd_init && get_signal_fd() > 0) { ufds[fd_count].fd = get_signal_fd(); ufds[fd_count].events = POLLIN; ufds[fd_count].revents = 0; fd_count++; signal_fd_init = 1; } if (!keychord_fd_init && get_keychord_fd() > 0) { ufds[fd_count].fd = get_keychord_fd(); ufds[fd_count].events = POLLIN; ufds[fd_count].revents = 0; fd_count++; keychord_fd_init = 1; } if (process_needs_restart) { timeout = (process_needs_restart - gettime()) * 1000; if (timeout < 0) timeout = 0; } if (!action_queue_empty() || cur_action) timeout = 0;// bootchart是一个性能统计工具,用于搜集硬件和系统的信息,并将其写入磁盘,以便其// 他程序使用#if BOOTCHART if (bootchart_count > 0) { if (timeout < 0 || timeout > BOOTCHART_POLLING_MS) timeout = BOOTCHART_POLLING_MS; if (bootchart_step() < 0 || --bootchart_count == 0) { bootchart_finish(); bootchart_count = 0; } }#endif // 等待下一个命令的提交 nr = poll(ufds, fd_count, timeout); if (nr <= 0) continue; for (i = 0; i < fd_count; i++) {//处理Uevent,属性服务,keychord事件//以前是==判断,改成&。能否提速? if (ufds[i].revents & POLLIN) { if (ufds[i].fd == get_property_set_fd()) handle_property_set_fd(); else if (ufds[i].fd == get_keychord_fd()) handle_keychord(); else if (ufds[i].fd == get_signal_fd()) handle_signal(); } } } return 0;}
更多相关文章
- Data Binding自定义属性
- 在android中如何在代码中设置textview的属性和效果
- 管理应用自启动的方案
- Android(安卓)ViewPager的初始化及遇到的切换异常,界面异常拉伸
- android R文件生成错误
- Android简单明了的使用属性动画ObjectAnimator 旋转 平移 渐变
- Android(安卓)ListView拖动时背景颜色会变成黑色 的解决办法
- android 属性系统使用的小问题
- [置顶] Android(安卓)Gallery橱窗效果