**************************************************************************************************************************
作者:EasyWave 时间:2012.07.21

类别:Android系统源码分析 声明:转载,请保留链接

注意:如有错误,欢迎指正。这些是我学习的日志文章......

***************************************************************************************************************************

在android的系统中init程序是第一个运行的程序,这是一个守护进程,而具体的代码在android2.3.5的源码中,位于system/core/init/init.c文档中,这个是main函数的入口,也是android启动机制的入口。代码如下所示:

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"))// 得到运行程序ueventd.rc的全路径名下的ueventd        return ueventd_main(argc, argv);    // 如果可以找到ueventd.rc,则执行ueventd_main函数                                            // 解析uevent.rc以及/ueventd.%s.rc    /* 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);  //创建可读写的/dev目录    mkdir("/proc", 0755); //创建可读写的/proc目录    mkdir("/sys", 0755);  //创建可读写的/sys目录,下面也是创建和挂载一些文件系统,是不是似曾相识啊,这个就是建立一个基本的文件系统,是不是有点像busybox.    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);        /* 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__" 虚拟设备(类似于 Linux 系统中的 /dev/null 设备)并将                             // stdin/stdout/stderr 三个文件重定向到 "/dev/__null__"    log_init();  //创建和打开"/dev/__kmsg__" 虚拟设备用于记录 log        INFO("reading config file\n");    init_parse_config_file("/init.rc"); //初始化解析system/etc/init.rc文件    /* pull the kernel commandline and ramdisk properties file in */    import_kernel_cmdline(0); //导入proc/cmdline的command line参数等。    get_hardware_name(hardware, &revision); //透过/proc/cpuinfo得到硬件模组名称    snprintf(tmp, sizeof(tmp), "/init.%s.rc", hardware); //并且将其拷贝到init.xxxx.rc中。    init_parse_config_file(tmp); //初始化解析init.xxxx.rc文件    action_for_each_trigger("early-init", action_add_queue_tail); //下面的代码需要去了解android的一些文件系统初始化的一些原理,不多说了。     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 */    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 */    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(); //操作上面整理的 action_queue 中对应的 action        restart_processes(); //当前的服务进程状态,如果有服务进程退出,重启对应服务进程        //property_service 的事件句柄填充 poll event 结构体        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;}
1 . 关于 init.rc

启动脚本init.rc

在 Android中使用启动脚本init.rc,可以在系统的初始化过程中进行一些简单的初始化操作。这个脚本被直接安装到目标系统的根文件系统中,被 init可执行程序解析。 init.rc是在init启动后被执行的启动脚本,其余发主要包含了以下内容:

  • Commands:命令
  • Actions:动作
  • Triggers:触发条件
  • Services:服务
  • Options:选项
  • Propertise:属性

Commands是一些基本的操作,例如:

  mkdir /sdcard 0000 system system   mkdir /system   mkdir /data 0771 system system   mkdir /cache 0770 system cache   mkdir /config 0500 root root   mkdir /sqlite_stmt_journals 01777 root root   mount tmpfs tmpfs /sqlite_stmt_journals size=4m

这些命令在init可执行程序中被解析,然后调用相关的函数来实现。 Actions(动作)表示一系列的命令,通常在Triggers(触发条件)中调用,动作和触发条件例如:

  on init   export PATH /sbin:/system/sbin:/system/bin:/system/xbin

init表示一个触发条件,这个触发事件发生后,进行设置环境变量和建立目录的操作称为一个“动作” Services(服务)通常表示启动一个可执行程序,Options(选项)是服务的附加内容,用于配合服务使用。

service vold /system/bin/vold   socket vold stream 0660 root mount service bootsound /system/bin/playmp3   user media   group audio   oneshot

vold和bootsound分别是两个服务的名称,/system/bin /vold和/system /bin/playmp3分别是他们所对应的可执行程序。socket、user、group、oneshot就是配合服务使用的选项。 Properties(属性)是系统中使用的一些值,可以进行设置和读取。

  setprop ro.FOREGROUND_APP_MEM 1536   setprop ro.VISIBLE_APP_MEM 2048   start adbd

setprop 用于设置属性,on property可以用于判断属性,这里的属性在整个Android系统运行中都是一致的。

综上如果想要修改启动过程只需要修改init.c或者init.rc里的内容即可.


3. 总结:

(1)内核的init_post类似接口,会去文件系统中启动init类似的用户进程

(2)android实现了这样的init,这就是android框架启动的地方,当然linux内核也可说是android系统的一部分

(3)init进程无限分裂,启动框架,演变成android系统

(4)android的init进程的代码在system/core/init/init.c中,从main函数开始.

还没有分析完,详细的代码分析请见:基于android2.3.5学习之:开天辟地Android启动机制[二]


更多相关文章

  1. Android(安卓)获取当前进程
  2. android 开机启动
  3. android里的service和content provider简单介绍
  4. android显式启动与隐式启动
  5. android 模拟软件启动界面
  6. Android与服务器通信的方法之一-->TCP
  7. 设置android系统时间
  8. Android保活方案
  9. android获取手机信息以及服务商信息

随机推荐

  1. Android硬件加速注意事项
  2. 如何自学Android编程?(Android入门到精通的
  3. 【Android(安卓)Training - Multimedia】
  4. android 打造变化多端的SeekBar(垂直和水
  5. Android热修复原理总结
  6. 从架构师的角度分析Android(安卓)Handler
  7. android 面试题汇总
  8. Android中添加万普广告墙、推送广告等
  9. Android(安卓)仿微信图片选择器
  10. Android开发实践(六)字符串实现DES加密