init进程事Linux内核启动后创建的第一个进程。init在初始化过程中会启动很多重要的守护进程。
|守护进程 BUD ADB
---fastboot(通过USB更新手机分区映像) | --应用程 launcher phonebook
|Zygote | --sensorservice
| ---systemserver
(boot.img) | --ActivityManager service contentservice Usbservwice
bootloader---Linux kernel-------------------------------------init|
| --camera service
----Recover |Mediaserver
|--MediaPlayer service
|serviceManager
在init启动过程中会解析init.rc,根据init.rc装载android的文件系统,创建系统目录,启动android系统重要的守护进程,这些进程包括USB守护进程,ADB守护进程,Vold守护进程,rild守护进程
Init进程的源码在system/core/init/init.c main函数中
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; if (!strcmp(basename(argv[0]), "ueventd")). //检查启动程序文件名 return ueventd_main(argc, argv); if (!strcmp(basename(argv[0]), "watchdogd")) return watchdogd_main(argc, argv); /* 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); /* indicate that booting is in progress to background fw loaders, etc */ close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000));//is_booting()来判断是否进程处于初始化中,初始化结束后这个文件将被删除 /* 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. */ open_devnull_stdio();//把标准输入,标准输出和标准出错重定向到空设备文件 klog_init();//这个函数用来创建节点/dev/_kmsg_,这样init进程可以使用kernel的log系统来输出log memlog_init(); property_init();//主要是创建一个共享区域来储存属性值 rk_parse_cpu(); get_hardware_name(hardware, &revision);//通过/proc/cpuinfo文件取得系统硬件名称 process_kernel_cmdline();//解析kernel的启动参数,启动参数通常放在/proc/cmdline union selinux_callback cb;//初始化SELinux cb.func_log = klog_write; 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("/dev"); restorecon("/dev/socket"); restorecon("/dev/__properties__"); restorecon_recursive("/sys"); is_charger = !strcmp(bootmode, "charger"); INFO("property init\n"); if (!is_charger) property_load_boot_defaults();//解析设备根目录下的default。prop文件把文件 中定义的属性值读出来设置到属性系统中 INFO("reading config file\n");
init_parse_config_file("/init.rc");//解析init,rc文件 action_for_each_trigger("early-init", action_add_queue_tail);//用来将指定的action加入到执行列表action_queue 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");//从硬件RNG的设备文件/dev/HW_random中读取512字节并写到Linux RNG的设备文件/dev/urandom中 queue_builtin_action(keychord_init_action, "keychord_init");//初始化组合键监听模块 queue_builtin_action(console_init_action, "console_init");//在屏幕上显示android 字样Logo /* execute all the boot actions to get us started */ action_for_each_trigger("init", action_add_queue_tail); /* skip mounting filesystems in charger mode */ if (!is_charger) { action_for_each_trigger("early-fs", action_add_queue_tail); action_for_each_trigger("fs", action_add_queue_tail); action_for_each_trigger("post-fs", action_add_queue_tail); action_for_each_trigger("post-fs-data", 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 */ 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");//初始化信号处理模块 queue_builtin_action(check_startup_action, "check_startup");//检查是否已经完成Init进程初始化,如果初始化完成删除.booting文件 if (is_charger) { action_for_each_trigger("charger", action_add_queue_tail); } else { action_for_each_trigger("early-boot", action_add_queue_tail); action_for_each_trigger("boot", 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"); #if BOOTCHART queue_builtin_action(bootchart_init_action, "bootchart_init"); #endif for(;;) { int nr, i, timeout = -1; execute_one_command();//每循环执行命令列表中的一条命令 restart_processes(); 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; #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++) { 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();//监听到信号 } } } memlog_close(); return 0; }