android啓動過程中init.c文件分析

這裏只貼出main函數的分析。

init的主要工作:
1、初始化log系統
2、解析init.rc init%hardware%.rc等文件
3、執行early-init action in the two files paresed in step2
4、設備初始化,如:/dev下面創建所有設備節點,下載firmwares
5、初始化屬性服務器,Actually the property system is
    working as share memory.Logically it looks like a registry
    under windows system
6、執行init action in the two files parsed in step2
7、開啓屬性服務
8、執行earl-boot and boot actions in the two files parsed in step2
9、執行Execute property action in the two files parsed in step2
10、進入一個死循環,to wait for device/property set/child process
    exit events.例如:如果SD卡被插入,init會收到一個設備插入事件,它會爲
    這個設備創建節點。系統中比較重要的進程都是由init來fork的,所以如果他們有誰
    崩潰了,那麼init將會收到一個SIGCHILD信號,把這個信號轉化爲子進程退出事件,
    所以在loop中,init會操作進程退出事件並執行*.rc文件中定義的命令

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;

    if (!strcmp(basename(argv[0]), "ueventd"))
        return ueventd_main(argc, argv);//這個函數是什麼作用?

    /* clear the umask */
    umask(0);//創建的目錄,文件的默認權限就是777

        /* 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.
         */
         
    //掛載幾個特殊的文件系統,tmpfs、proc、sysfs這三個特殊的文件系統
    mkdir("/dev", 0755);
    mkdir("/proc", 0755);
    mkdir("/sys", 0755);

    mount("tmpfs", "/dev", "tmpfs", 0, "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);
#ifdef INIT_ENG_BUILD
    mount("debugfs", "/sys/kernel/debug", "debugfs", 0, NULL);
#endif
        /* 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.
         */
    //打開/dev/null這個設備節點,用以屏蔽不想要的打印信息
    open_devnull_stdio();
    
    //出示化log系統   /dev/kmsg
    log_init();

// JK@MTK, add for AEE {
#ifdef HAVE_AEE_FEATURE    
#ifdef AEE_CORE_DUMP
    {
        extern int aee_enable_core_dump(int); 
        if (aee_enable_core_dump(1) < 0) {
            ERROR("enable core dump fail\n");
        }
    }
#endif

		//解析init.aee.rc文件
    INFO("reading AEE config file\n");
    init_parse_config_file("/init.aee.rc");
#endif
// JK@MTK, add for AEE }

    INFO("reading config file\n");
#ifdef USE_BUILT_IN_FACTORY
    if (is_factory_boot()) {//如果進入factory啓動模式,需要額外解析init.factory.rc文件
    		//解析init.factory.rc跟init.rc文件
        if (init_parse_config_file("/init.factory.rc") < 0)
            init_parse_config_file("/init.rc");
    } else {//否則只需要解析init.rc文件
        init_parse_config_file("/init.rc");
    }

#else
    init_parse_config_file("/init.rc");
#endif

    /* pull the kernel commandline and ramdisk properties file in */
    //導入內核命令行~
    //通過查看proc/cmdline文件可以查看具體的命令行,如下:
    //問題,內核啓動命令行的解析應該是在內核啓動之前,uboot來解析,爲什麼在init進程中還要解析
    //
    import_kernel_cmdline(0);
		//獲得硬件板的信息,從/proc/cpuinfo裏面取得。
    get_hardware_name(hardware, &revision);
    snprintf(tmp, sizeof(tmp), "/init.%s.rc", hardware);
    init_parse_config_file(tmp);

		//執行early-init actions in the two files parsed in step2
    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(property_init_action, "property_init");
    queue_builtin_action(keychord_init_action, "keychord_init");
    queue_builtin_action(console_init_action, "console_init");
    queue_builtin_action(set_init_properties_action, "set_init_properties");

        /* execute all the boot actions to get us started */
    //執行init actions in the two files parsed in step2
    action_for_each_trigger("init", action_add_queue_tail);
    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);

    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");

    /* execute all the boot actions to get us started */
    //執行early-boot and boot actions in the two files parsed in step2
    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_propety_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();
            }
        }
    }
    return 0;
}
init解析的腳本文件init.rc文件的結構分析可以參考我的博文:http://blog.csdn.net/yinwei520/article/details/6598453


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章