美文网首页
Android开机流程之init.rc解析

Android开机流程之init.rc解析

作者: 你身边的那个TA | 来源:发表于2018-06-17 16:20 被阅读306次

一、概述

init进程在做完log系统初始化、文件系统挂载、property系统初始化等一系列工作之后,init进入init.rc的解析,这一部分是init的重中之重。
init.rc是一个配置文件,是由Android初始化语言编写(Android Init Language)编写的脚本。主要包含三大部分:

  • 以 import 关键字开头的导入动作
  • 以 on 关键字开头的动作列表(action list)
  • 以 service 关键字开头的服务列表(service list)
# Import类型语句,导入init.usb.rc文件
import /init.usb.rc

# Action 类型语句
on early-init
    write /proc/1/oom_score_adj -1000
    mkdir /mnt 0775 root system
    start ueventd //启动名为ueventd的service

on zygote-start && property:ro.crypto.state=unsupported
    # A/B update verifier that marks a successful boot.
    exec_start update_verifier_nonencrypted
    start netd
    start zygote
    start zygote_secondary

# Service 类型语句
service ueventd /sbin/ueventd
    class core
    critical
    seclabel u:r:ueventd:s0
    shutdown critical

二、.rc 文件结构

其中的action和service是以section形式呈现的,每个ActionSection可以含有若干Command,而每个ServiceSection可以含有若干Option。Service不能出现重名, Action可以重复,但最后会合并到一起。

2.1 Import 类型语句

从init main函数的代码可以看出,根路径中只解析了init.rc,而其它的init.*.rc就是通过import导入进来的。如: import /init.${ro.hardware}.rc。

2.2 Action 类型语句

Action需要有一个触发器(trigger)来触发它,一旦满足了触发条件,这个Action就会被加到执行队列的末尾。Action类型语句格式为:

on <trigger> [&& <trigger>]*    // 设置触发器  
    <command>    
    <command>    // 动作触发之后要执行的命令  
    ...

动作列表用于创建所需目录,以及为某些特定文件指定权限等。
AndroidO上支持的Command详见/system/core/init/builtins.cpp,如:

    static const Map builtin_functions = {
        {"bootchart",               {1,     1,    do_bootchart}},
        {"chmod",                   {2,     2,    do_chmod}},
        {"chown",                   {2,     3,    do_chown}},
        {"class_reset",             {1,     1,    do_class_reset}},
        {"class_restart",           {1,     1,    do_class_restart}},
        {"class_start",             {1,     1,    do_class_start}},
        {"class_stop",              {1,     1,    do_class_stop}},
        ...

2.3 Service 类型语句

Service表示一个服务程序,会通过start command执行。并根据option参数判断服务在退出时是否需要自动重启。Service 类型语句格式为:

service <name> <pathname> [<argument>]*    //service名字、执行程序路径、传递参数    
    <option>    // option是修饰词,影响什么时候、如何启动service 
    <option>    
    ...

服务列表用来记录init进程需要启动的一些子进程。如上面的例子为启动名为ueventd的service(子进程),其可执行文件路径为/sbin/ueventd。
AndroidO上支持的Option详见/system/core/init/service.cpp,如:

    static const Map option_parsers = {
        {"capabilities",
                        {1,     kMax, &Service::ParseCapabilities}},
        {"class",       {1,     kMax, &Service::ParseClass}},
        {"console",     {0,     1,    &Service::ParseConsole}},
        {"critical",    {0,     0,    &Service::ParseCritical}},
        {"disabled",    {0,     0,    &Service::ParseDisabled}},
        {"group",       {1,     NR_SVC_SUPP_GIDS + 1, &Service::ParseGroup}},
        {"ioprio",      {2,     2,    &Service::ParseIoprio}},
        {"priority",    {1,     1,    &Service::ParsePriority}},
        ......

2.4 More Infomation

详见文件 /system/core/init/README.md,里面啥都有很详细,如:

......
critical
This is a device-critical service. If it exits more than four times in
four minutes, the device will reboot into recovery mode.
disabled
This service will not automatically start with its class.
It must be explicitly started by name.

oneshot

Do not restart the service when it exits.
......

三、.rc 文件解析

解析就是将上述action(command)、service(option)依次解析保存在对应的vector中。
Please read the fu**ing source code. :)

相关文章

网友评论

      本文标题:Android开机流程之init.rc解析

      本文链接:https://www.haomeiwen.com/subject/bleweftx.html