Android 开机启动流程分析 02 INIT的启动流程分析

在这里插入图片描述

1 INIT解读

init是命令行程序;因此分析init.c首先应从main函数开始,然后按照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;
    /*
      首先,watchdog和uevent命令已经集成到了init。
      sbin/ueventd和/sbin/watchdogd是一个链接文件,它直接链接到/init
      当执行/sbin/ueventd或/sbin/watchdogd时,将会进入相应的ueventd_main或watchdogd_main入口点。
      ueventd伺服程序将解析/ueventd.rc文件,并创建相应的设备结点。
      watchdogd伺服程序是一个看门狗程序,它的任务就是定期向看门狗设备文件执行写操作,以判断系统是否正常运行。
    */
    //----1 uevent和watchdog相关,start <关键点1,针对uevent和watchdog两个进程进行分析>
    if (!strcmp(basename(argv[0]), "ueventd"))
        return ueventd_main(argc, argv);//关键点<1-1>,进入到uevent进程中
    if (!strcmp(basename(argv[0]), "watchdogd"))
        return watchdogd_main(argc, argv);//关键点<1-2>,进入到watchdog进程中
    //----1 uevent和watchdog相关,end
    /* clear the umask */
    umask(0);
    //下面的代码开始建立各种用户空间的目录,如/dev、/proc、/sys等
    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));
        /* 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();//重定向输入/输出/错误输出到/dev/_null_
    klog_init();         //设置init的日志输出设备为/dev/__kmsg__,文件被打开后立刻被unlink,其他进程无法打开读取日志
    property_init();     //<关键点3,属性流程分析><关键点3-1,属性初始化>
    get_hardware_name(hardware, &revision);    //读取/proc/cpuinfo得到Hardware名
    process_kernel_cmdline();        //处理内核命令行
    //----2 android SElinux设置相关,start,<关键点2,针对selinux进行分析>
    union selinux_callback cb;
    cb.func_log = log_callback;
    selinux_set_callback(SELINUX_CB_LOG, cb);<关键点2-1>
    cb.func_audit = audit_callback;
    selinux_set_callback(SELINUX_CB_AUDIT, cb);
    selinux_initialize();<关键点2-2>
    /* 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");<关键点2-3>
    restorecon("/dev/socket");
    restorecon("/dev/__properties__");
    restorecon_recursive("/sys");<关键点2-4>
    //----2 android SElinux设置相关,end
    is_charger = !strcmp(bootmode, "charger");//从启动模式判断是否是charger模式
    INFO("property init\n");
    property_load_boot_defaults();//<关键点3-2,属性加载>
    INFO("reading config file\n");
    
    init_parse_config_file("/init.rc");//----4 android init.rc相关,<关键点4,解析init.rc>
    //----5 初始化各个action队列并执行,start <关键点5,整体流程分析>
    //触发在init脚本文件中名字为early-init的action,并且执行其commands,其实是: on early-init
    action_for_each_trigger("early-init", action_add_queue_tail);<关键点5-1,函数分析>
    queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");<关键点5-2,函数分析>
    queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
    queue_builtin_action(keychord_init_action, "keychord_init");
    //控制台相关初始化,在这里会加载启动动画,如果动画打开失败,则在屏幕上输出ANDROID字样
    queue_builtin_action(console_init_action, "console_init");
    /* execute all the boot actions to get us started */
    //init_进程_A 触发在init脚本文件中名字为init的action,并且执行其commands,其实是:on init
    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
     */
    queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
    // 启动系统属性服务: system property service
    queue_builtin_action(property_service_init_action, "property_service_init");//<关键点3-3,属性服务启动>
    queue_builtin_action(signal_init_action, "signal_init");
    /* 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 */
    // 启动所有属性变化触发命令,其实是: on property:ro.xx.xx=xx,使其触发
    queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
#if BOOTCHART
    queue_builtin_action(bootchart_init_action, "bootchart_init");
#endif
    //----5 初始化各个action队列并执行,end
    //----6 无限循环,监听并处理属性变化,子进程信号,组合按键,start,<关键点6,整体流程分析>
    for(;;) {
    
    
        int nr, i, timeout = -1;
        execute_one_command();    
        restart_processes();      // 启动所有init脚本中声明的service
        if (!property_set_fd_init && get_property_set_fd() > 0) {
    
    //<关键点3-4,属性变化处理>
            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) {
    
    //<关键点6-1,接收子进程信号处理流程>
            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) {
    
    //<关键点6-2,接收组合按键处理流程>
            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  //bootchart是一个性能统计工具,用于搜集硬件和系统的信息,并将其写入磁盘,以便其他程序使用
        if (bootchart_count > 0) {
    
    //<关键点6-3,bootchart分析>
            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();
            }
        }
    }
    //----6 无限循环,监听并处理属性变化,子进程信号,组合按键,end
    return 0;
}

2 main函数功能,总结如下:

@1 uevent和watchdog相关,建立各种用户空间的目录,如/dev、/proc、/sys等
@2 属性服务
@3 android SElinux设置相关
@4 解析init.rc
@5 初始化各个action队列并执行
@6 无限循环,监听并处理属性变化,子进程信号,组合按键

3研究开机启动流程init模块可以做的事情

3.1 通过开启bootchart选项分析,可以清楚系统性能{根据需求优化需要的进程}
3.2 基于init.rc解析规则,可以实现以下功能:
添加新的关键词{
    
    section(service,action,import)}{
    
    keywords与对应的执行方法}
action与command:根据自身需求,添加、删除、调整action和command{
    
    系统新功能添加+系统裁剪}
service{
    
    native可执行程序、Java可执行程序(用app_process执行)}与option
property的控制{
    
    设置属性,当属性变化时触发事件控制}
3.3 systemserver:启动apk、java service等,对于一些智能硬件项目还会对其进行裁剪

猜你喜欢

转载自blog.csdn.net/wzx311/article/details/129946595