现在的位置: 首页 > 综合 > 正文

android启动过程中init.c文件分析

2013年08月07日 ⁄ 综合 ⁄ 共 5261字 ⁄ 字号 评论关闭

这里只贴出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

抱歉!评论已关闭.