Android 9.0 系统启动流程

摘要:
1、 启动过程概述II、Android启动分析III、init进程启动分析IV、init启动脚本分析V、init进程分析VI、init脚本执行VII、init进程守护程序VIII、initrc脚本启动Zygote IX、启动分析摘要I、启动过程概述Android启动过程与Linux启动类似,并将首先在系统中查找init.rc文件(大多数文件存储在/system/core/init目录中),常见的init.xxx.rc进程如下:

Android 9.0 系统启动流程第1张

极力推荐文章:欢迎收藏
Android 干货分享

Android 9.0 系统启动流程第2张

阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android

本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:

一、启动流程概述
二、Android启动分析
三、init 进程启动分析
四、init 启动脚本分析
五、init 进程分析
六、init 脚本执行
七、init 进程守护
八、init rc 脚本启动Zygote
九、启动分析小结

一、 启动流程概述

Android 启动流程跟 Linux 启动类似,大致分为如下五个阶段。

  • 1.开机上电,加载固化的ROM
  • 2.加载BootLoader,拉起Android OS
  • 3.加载Uboot,初始外设,引导Kernel启动等。
  • 4.启动Kernel,加载驱动,硬件。
  • 5.启动Android,挂载分区,加载驱动、服务,init 进程等。

Android系统启动大致过程如下:
Android 启动过程

由于水平有限,无法深入了理解驱动层代码,本文主要对 Android 上层启动流程进行分析。

二、Android启动分析

Uboot启动Kernel完成系统设置后,会首先在系统中寻找init.rc文件(文件多存放在/system/core/init目录下),并启动init进程。

Android启动 主要包含如下两种

  • 1.启动脚本
  • 2.Zygote 启动

Android 启动分析主要内容如下:

Android 启动分析

三、init 进程启动分析

Init进程是是Android 启动的第一个进程,进程号为1,是Android 的系统启动的核心进程,主要用来创建Zygote、属性服务等。源码主要存在systemcoreinit目录下。

常见init.xxx.rc 进程如下:
常见init.xxx.rc 进程

init 进程的入口 main 函数
init.cpp 中的main 函数,是init进程的入口函数(代码路径:systemcoreinitinit.cpp)

main 函数主要做的事情

1.创建挂载启动所需的文件系统(tmpfs、 devpts、 proc、 sysfs、 selinuxfs等)
2.初始化并启动属性服务
3.解析init.rc 脚本配置文件,并启动Zygote 进程。
init 进程

init.cpp main 函数实现代码如下:

int main(int argc, char** argv) {
    if (!strcmp(basename(argv[0]), "ueventd")) {
        return ueventd_main(argc, argv);
    }

    if (!strcmp(basename(argv[0]), "watchdogd")) {
		//启动看门狗函数
        return watchdogd_main(argc, argv);
    }

    if (argc > 1 && !strcmp(argv[1], "subcontext")) {
        InitKernelLogging(argv);
        const BuiltinFunctionMap function_map;
        return SubcontextMain(argc, argv, &function_map);
    }

    if (REBOOT_BOOTLOADER_ON_PANIC) {
        InstallRebootSignalHandlers();
    }

    bool is_first_stage = (getenv("INIT_SECOND_STAGE") == nullptr);

    //启动第一阶段
    if (is_first_stage) {
        boot_clock::time_point start_time = boot_clock::now();

        // 清理 umask.
        umask(0);

        clearenv();
        setenv("PATH", _PATH_DEFPATH, 1);

        // 在RAM内存上获取基本的文件系统,剩余的被 rc 文件所用
        mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
        mkdir("/dev/pts", 0755);
        mkdir("/dev/socket", 0755);
        mount("devpts", "/dev/pts", "devpts", 0, NULL);
        #define MAKE_STR(x) __STRING(x)
        mount("proc", "/proc", "proc", 0, "hidepid=2,gid=" MAKE_STR(AID_READPROC));
        // 非特权应用不能使用 Android 命令行
        chmod("/proc/cmdline", 0440);
        gid_t groups[] = { AID_READPROC };
        setgroups(arraysize(groups), groups);
        mount("sysfs", "/sys", "sysfs", 0, NULL);
        mount("selinuxfs", "/sys/fs/selinux", "selinuxfs", 0, NULL);

        mknod("/dev/kmsg", S_IFCHR | 0600, makedev(1, 11));

        if constexpr (WORLD_WRITABLE_KMSG) {
            mknod("/dev/kmsg_debug", S_IFCHR | 0622, makedev(1, 11));
        }

        mknod("/dev/random", S_IFCHR | 0666, makedev(1, 8));
        mknod("/dev/urandom", S_IFCHR | 0666, makedev(1, 9));

        // Mount staging areas for devices managed by vold
        // See storage config details at http://source.android.com/devices/storage/
        mount("tmpfs", "/mnt", "tmpfs", MS_NOEXEC | MS_NOSUID | MS_NODEV,
              "mode=0755,uid=0,gid=1000");
        //创建可供读写的 vendor目录
        mkdir("/mnt/vendor", 0755);

        // 在/dev目录下挂载好 tmpfs 以及 kmsg 
        // 这样就可以初始化 /kernel Log 系统,供用户打印log
        InitKernelLogging(argv);

        LOG(INFO) << "init first stage started!";

        if (!DoFirstStageMount()) {
            LOG(FATAL) << "Failed to mount required partitions early ...";
        }

        SetInitAvbVersionInRecovery();

        // Enable seccomp if global boot option was passed (otherwise it is enabled in zygote).
        global_seccomp();

        // 优先加载selinux log系统, 紧接着初始化selinux
        SelinuxSetupKernelLogging();
        SelinuxInitialize();

        // 添加 selinux 是否启动成功的log
        if (selinux_android_restorecon("/init", 0) == -1) {
            PLOG(FATAL) << "restorecon failed of /init failed";
        }

        setenv("INIT_SECOND_STAGE", "true", 1);

        static constexpr uint32_t kNanosecondsPerMillisecond = 1e6;
        uint64_t start_ms = start_time.time_since_epoch().count() / kNanosecondsPerMillisecond;
        setenv("INIT_STARTED_AT", std::to_string(start_ms).c_str(), 1);

        char* path = argv[0];
        char* args[] = { path, nullptr };
        execv(path, args);

        // execv() only returns if an error happened, in which case we
        // panic and never fall through this conditional.
        PLOG(FATAL) << "execv("" << path << "") failed";
    }

    //启动第二阶段
    InitKernelLogging(argv);
    LOG(INFO) << "init second stage started!";

    // Set up a session keyring that all processes will have access to. It
    // will hold things like FBE encryption keys. No process should override
    // its session keyring.
    keyctl_get_keyring_ID(KEY_SPEC_SESSION_KEYRING, 1);

    // Indicate that booting is in progress to background fw loaders, etc.
    close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));
    //初始化属性
    property_init();

    // If arguments are passed both on the command line and in DT,
    // properties set in DT always have priority over the command-line ones.
    process_kernel_dt();
    process_kernel_cmdline();

    // Propagate the kernel variables to internal variables
    // used by init as well as the current required properties.
    export_kernel_boot_props();

    // Make the time that init started available for bootstat to log.
    property_set("ro.boottime.init", getenv("INIT_STARTED_AT"));
    property_set("ro.boottime.init.selinux", getenv("INIT_SELINUX_TOOK"));

    // Set libavb version for Framework-only OTA match in Treble build.
    const char* avb_version = getenv("INIT_AVB_VERSION");
    if (avb_version) property_set("ro.boot.avb_version", avb_version);

    // 清空设置的环境变量
    unsetenv("INIT_SECOND_STAGE");
    unsetenv("INIT_STARTED_AT");
    unsetenv("INIT_SELINUX_TOOK");
    unsetenv("INIT_AVB_VERSION");

    // 设置第二阶段的selinux
    SelinuxSetupKernelLogging();
    SelabelInitialize();
    SelinuxRestoreContext();
    //创建 epoll 句柄
    epoll_fd = epoll_create1(EPOLL_CLOEXEC);
    if (epoll_fd == -1) {
        PLOG(FATAL) << "epoll_create1 failed";
    }
    //设置 子进程处理函数
    sigchld_handler_init();

    if (!IsRebootCapable()) {
        // If init does not have the CAP_SYS_BOOT capability, it is running in a container.
        // In that case, receiving SIGTERM will cause the system to shut down.
        InstallSigtermHandler();
    }

    LoadRscRoProps();
    property_load_boot_defaults();
    export_oem_lock_status();
	//启动属性服务
    start_property_service();
	//为USB存储设置udc Contorller, sys/class/udc
    set_usb_controller();

    const BuiltinFunctionMap function_map;
    Action::set_function_map(&function_map);

    subcontexts = InitializeSubcontexts();

    ActionManager& am = ActionManager::GetInstance();
    ServiceList& sm = ServiceList::GetInstance();

    LoadBootScripts(am, sm);

    // Turning this on and letting the INFO logging be discarded adds 0.2s to
    // Nexus 9 boot time, so it's disabled by default.
    if (false) DumpState();

    am.QueueEventTrigger("early-init");

    // Queue an action that waits for coldboot done so we know ueventd has set up all of /dev...
    am.QueueBuiltinAction(wait_for_coldboot_done_action, "wait_for_coldboot_done");
    // ... so that we can start queuing up actions that require stuff from /dev.
    am.QueueBuiltinAction(MixHwrngIntoLinuxRngAction, "MixHwrngIntoLinuxRng");
    am.QueueBuiltinAction(SetMmapRndBitsAction, "SetMmapRndBits");
    am.QueueBuiltinAction(SetKptrRestrictAction, "SetKptrRestrict");
    am.QueueBuiltinAction(keychord_init_action, "keychord_init");
    am.QueueBuiltinAction(console_init_action, "console_init");

    // Trigger all the boot actions to get us started.
    am.QueueEventTrigger("init");

    // Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
    // wasn't ready immediately after wait_for_coldboot_done
    am.QueueBuiltinAction(MixHwrngIntoLinuxRngAction, "MixHwrngIntoLinuxRng");

    // Don't mount filesystems or start core system services in charger mode.
    std::string bootmode = GetProperty("ro.bootmode", "");
    if (bootmode == "charger") {
        am.QueueEventTrigger("charger");
    } else {
        am.QueueEventTrigger("late-init");
    }

    // Run all property triggers based on current state of the properties.
    am.QueueBuiltinAction(queue_property_triggers_action, "queue_property_triggers");

    while (true) {
        // By default, sleep until something happens.
        int epoll_timeout_ms = -1;

        if (do_shutdown && !shutting_down) {
            do_shutdown = false;
            if (HandlePowerctlMessage(shutdown_command)) {
                shutting_down = true;
            }
        }

        if (!(waiting_for_prop || Service::is_exec_service_running())) {
            am.ExecuteOneCommand();
        }
        if (!(waiting_for_prop || Service::is_exec_service_running())) {
            if (!shutting_down) {
                auto next_process_restart_time = RestartProcesses();

                // If there's a process that needs restarting, wake up in time for that.
                if (next_process_restart_time) {
                    epoll_timeout_ms = std::chrono::ceil<std::chrono::milliseconds>(
                                           *next_process_restart_time - boot_clock::now())
                                           .count();
                    if (epoll_timeout_ms < 0) epoll_timeout_ms = 0;
                }
            }

            // If there's more work to do, wake up again immediately.
            if (am.HasMoreCommands()) epoll_timeout_ms = 0;
        }

        epoll_event ev;
        int nr = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd, &ev, 1, epoll_timeout_ms));
        if (nr == -1) {
            PLOG(ERROR) << "epoll_wait failed";
        } else if (nr == 1) {
            ((void (*)()) ev.data.ptr)();
        }
    }

    return 0;
}

Init 进程源码分析
Init 进程源码分析
基于MTK 平台 init.cpp 源码分析
基于MTK 平台 init.cpp 主要作用

四、 init 启动脚本分析

init.rc 路径 一般在system/core/rootdir下,init 脚本是有Android 初始化语言编写。

Android Init Language 语句类型:

  • 1.Action
  • 2.Command
  • 3.Service
  • 4.Option
  • 5.Import

init.rc分析

Android 启动脚本

init.rc on

init.rc services

init.rc import

五、init 进程分析

init 进程分析

init 解析脚本分析

init 事件列表

init 事件结构

六 、init 脚本执行

init 进程解析和执行

启动脚本解析结果

整理事件列表

init 构建事件

Service 事件分类

init 进程执行命令和启动服务

七、init 进程守护

init进程处理消息事件

    1. 根据Shell 或者系统中消息设置系统prop
    1. 守护系统服务,如果服务退出,重启退出的服务。

init守护进程

init 处理 prop 消息分析

init 守护服务分析

八、init rc 脚本启动Zygote

Zygoteclassnamemain .
init.rc 文件配置代码如下:

... ... 
on nonencrypted
    class_start main
    class_start late_start

on property:sys.init_log_level=*
    loglevel ${sys.init_log_level}

... ...
九、启动分析小结

启动分析小结

xmind 小结文件下载地址如下:
小结源文件下载地址

至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!

微信关注公众号: 程序员Android,领福利

免责声明:文章转载自《Android 9.0 系统启动流程》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Uniapp 之小程序版本更新检查——uni.getUpdateManager()Layui用户中心模板下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Android酷炫实用的开源框架(UI框架)

    原文链接:http://www.androidchina.net/1992.html 前言   忙碌的工作终于可以停息一段时间了,最近突然有一个想法,就是自己写一个app,所以找了一些合适开源控件,这样更加省时,再此分享给大家,希望能对大家有帮助,此博文介绍的都是UI上面的框架,接下来会有其他的开源框架(如:HTTP框架、DB框架)。 1.Side...

沉浸式-- Android沉浸式状态栏完全解析

转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/51763825本文同步发表于我的微信公众号,扫一扫文章底部的二维码或在微信搜索 郭霖 即可关注,每天都有文章更新。记得之前有朋友在留言里让我写一篇关于沉浸式状态栏的文章,正巧我确实有这个打算,那么本篇就给大家带来一次沉浸式状态栏的微技巧讲解。其...

Linux 后台运行python .sh等程序,以及查看和关闭后台运行程序操作

1、运行.sh文件 直接用./sh 文件就可以运行,但是如果想后台运行,即使关闭当前的终端也可以运行的话,需要nohup命令和&命令。 (1)&命令 功能:加在一个命令的最后,可以把这个命令放在后台执行   (2)nohup命令 功能:不挂断的运行命令   2、查看当前后台运行的命令 有两个命令可以用,jobs和p...

使 nodejs 代码 在后端运行(nohup)

启动程序,日志输出到out.log nohup node server.js > out.log & nohup 命令对 server.js 进程做了三件事 (1)阻止SIGHUP信号发到这个进程。(2)关闭标准输入。该进程不再能够接收任何输入,即使运行在前台。(3)重定向标准输出和标准错误到文件nohup.out。 也就是说,nohup命令...

安装与配置Flutter开发环境

这篇博客我们介绍了Flutter,并且对比了H5,React Native,Flutter。 由于Flutter是跨平台的开发框架,开发一次可以同时运行在Android和iOS上面,所以我们开发时最好使用Mac系统,这样我们可以同时测试两个平台的运行效果。 本文我们就来介绍在Mac系统下安装与配置Flutter开发环境,并且运行我们的第一个Flutter应...

android dlopen调用so库

转自:http://hi.baidu.com/mcu99/blog/item/216f1fce17e1c00b92457edd.html 以下程序在Ubuntu操作系统上,gcc编译通过。但是当我想把它们移到Android下运行时,则出现错误,问题出在什么地方尚不得而知。(补充:该问题已经解决,详见http://hi.baidu.com/mcu99/blo...