Android init.rc语法详解

1. 简介 

      init.rc由许多的Action和Service组成。

      每一个语句占据一行,并且各个关键字被空格分开。c规范中的(如 /n)反斜杠将被忽略(backslash escapes)而被认为是一个空格 ,双引号用来保证空格不会把一个文字串分分为多个关键字。行最后的反斜杠用作续行。

由 # (前面允许有空格)开始的行都是注释行(comment)

      一个actions 或 services 的开始隐含声明了一个新的段,所有commands 或 options 属于最近的声明。在第一个段之前的 commands 或 options 都会被忽略

       每一个actions 和 services 都有不同的名字。后面与前面发生重名的,那么这个后面重名的将被忽略或被认为是一个错误。

2. Action

      action其实就是一组被命名的命令序列。actions 都有一个触发条件,触发条件决定了action何时执行。当一个事件发生如果匹配action的触发条件,那么这个action将会被添加到预备执行队列的尾部(除非它已经在队列当中) 

每一个action中的命令将被顺序执行。init进程负责在其它activities(如:设备创建/销毁,属性设置,进程重启)之间执行这些命令序列。

     每一个action格式如下:

    on <trigger>

       <command>

       <command>

       ...

2.1 Trigger 

      trigger是一个action触发的条件,一共有如下几种:

    1) boot

          发生在init启动时,/init.conf被加载以后。

     2) <name>=<value>

         发生在名字为<name>的属性的值被设置为<value>时。

     3) device-added-<path>/device-removed-<path>

         当一个device node被添加/删除时。

     4) service-exited-<name>当某个服务退出时。

              Triggers are strings used to match certain kinds of events that cause an action to occur.

TriggerDescription
bootThis is the first trigger that occurs when init starts (after /init.conf is loaded).
<name>=<value>Triggers of this form occur when the property <name> is set to the specific value<value>.
device-added-<path>
device-removed-<path>
Triggers of these forms occur when a device node is added or removed.
service-exited-<name>Triggers of this form occur when the specified service exits.


 

2.2 Command

    command一共有如下几种:

    1) exec <path> [<argument>]*

        fork并execute一个路径<path>下面的程序,直到程序执行完毕后,init才会继续前进。尽量避免使用这个command,它有可能导致init阻塞。其它command不存在这个问题。

    2) export <name> <value>

        把全局环境变量<name>的值设置为<value>。这个命令执行完毕以后启动的所有进程都会继承这个全局变量。

    3) ifup <interface>

       Bring the network interface <interface> online.(打开某个网卡)

    4) import <filename>

        Parse an init config file, extending the current configuration.

    5) hostname <name>

        Set the host name.

    6) class_start <serviceclass>

        如果某一类service没有运行,启动它们。

    7) class_stop <serviceclass>

        如果某一类service正在运行,停止它们。

    8) domainname <name>

       Set the domain name.

    9) insmod <path>

        安装路径<path>指定的模块。

    10) mkdir <path>

          创建<path>代表的文件夹,只能一层层地创建。

    11) mount <type> <device> <dir> [ <mountoption> ]*

          把<device>挂载到系统类型为<type>的文件系统的<dir>目录下。<device>可能有mtd@name的形式,代表名字为name的mtd块设备。

    12) setkey

          未定义

    13) setprop <name> <value>

          设置系统属性。

    14) setrlimit <resource> <cur> <max>

          Set the rlimit for a resource.

    15) start <service>

          如果服务没有运行,启动它。

    16) stop <service>

          如果服务正在运行,停止它。

    17) symlink <target> <path>

          把<target>链接到目录<path>下。

    18) write <path> <string> [ <string> ]*

          打开<path>所指的文件,并把<string>写入。

     关于3、5、8,参见init.rc里面的

     on boot

     # basic network init

         ifup lo

         hostname localhost

         domainname localdomain

     关于14,参见init.rc里面的

         # set RLIMIT_NICE to allow priorities from 19 to -20

         setrlimit 13 40 40

CommandDescription
exec <path> [ <argument> ]*Fork and execute a program (<path>). This will block until the program completes execution. Try to avoid exec. Unlike thebuiltin commands, it runs the risk of getting init "stuck".
export <name> <value>Set the environment variable <name> equal to<value> in the global environment (which will be inherited by all processes started after this command is executed).
ifup <interface>Bring the network interface <interface> online.
import <filename>Parse an init config file, extending the current configuration.
hostname <name>Set the host name.
class_start <serviceclass>Start all services of the specified class if they are not already running.
class_stop <serviceclass>Stop all services of the specified class if they are currently running.
domainname <name>Set the domain name.
insmod <path>Install the module at <path>.
mkdir <path>Make a directory at <path>.
mount <type> <device> <dir> [ <mountoption> ]*Attempt to mount the named device at the directory<dir><device>. This may be of the form mtd@name to specify a mtd block device by name.
setkey- currenlty undefined -
setprop <name> <value>Set system property <name> to <value>.
setrlimit <resource> <cur> <max>Set the rlimit for a resource.
start <service>Start a service running if it is not already running.
stop <service>Stop a service from running if it is currently running.
symlink <target> <path>Create a symbolic link at <path> with the value<target>.
write <path> <string> [ <string> ]*Open the file at <path> and write one or more strings to it with write(2).

3. Service  

      Service是一些由init 启动或重新(如果有需要)启动的程序,当然这些程序如果是存在的。

     每一个service格式如下:

     service <name> <pathname> [ <argument> ]*

         <option>

         <option>

         ...

     option是service的修饰符,用来告诉init 怎样及何时启动service。一共有如下几种:

     1) disabled

         这个服务不能通过启动一类服务来启动,只能单独以名字来启动。

      2) socket <type> <name> <perm> [ <user> [ <group> ] ]

          创建一个名字为/dev/socket/<name>的unix domain socket,并把它的fd传递给加载的进程。<type>的值是dgram或stream。user和group默认值是0.

         注意:在init.rc中使用socket时,<type>是放在<name>之后的。

      3) user <username>

          在启动服务之前,把用户名切换到<username>。默认是root

      4) group <groupname> [ <groupname> ]*

           在启动服务之前,把组名切换到<groupname>。一个服务可能属于多个组。

      5) capability [ <capability> ]+

          Set linux capability before executing  this service

      6) oneshot

          服务之运行一次,退出后不再重启。

      7) class <name>

          为服务设定一个类别,一个类别是中的服务可以同时启动或停止。如果没有这个属性,服务的默认类别是“default”

      8) console

          服务的STDIO被重定向到/dev/console,而不是默认的/dev/null 

         默认情况下,通过init启动的程序都会把stdout和stderr定向到/dev/null。有时为了调试方便,可以通过Android的logwrapper程序启动某个程序。这样,被启动程序stdout和stderr就被定向到了Android的LOG系统中,可以通过logcat来查看了。

例如:

service akmd /system/bin/logwraper /sbin/akmd

OptionDescription
disabledThis service will not automatically start with its class. It must be explicitly started by name.
socket <type> <name> <perm> [ <user> [ <group> ] ]

Create a unix domain socket named /dev/socket/<name> and pass its fd to the launched process. Valid<type>dgram and stream. user andgroup default to 0.(type 实际中是放在name之后)

values include
user <username>Change to username before exec'ing this service. Currently defaults to root.
group <groupname> [ <groupname> ]*Change to groupname before exec'ing this service.  Additional  groupnames beyond the first, which is required, are used to set additional groups of the process (withsetgroups()). Currently defaults to root.
capability [ <capability> ]+Set linux capability before exec'ing this service
oneshotDo not restart the service when it exits.
class <name>Specify a class name for the service.  All services in a named class must start and stop together. A service is considered of class "default" if one is not specified via the class option.

 

4. Property

    init程序在运行过程中可能会设置几个特殊属性的值,来告诉其它程序它正在做什么。这些属性是:

   1) init.action

       当前正在执行的action的名字,如果没有,就是“”。

   2) init.command

       当前正在执行的command的名字,如果没有,就是“”。

   3) init.svc.<name>

      一个服务的状态。可能的值有:“stopped”,"running","restarting"

PropertyDescription
init.actionEqual to the name of the action currently being executed or "" if none.
init.commandEqual to the command being executed or "" if none.
init.svc.<name>State of a named service ("stopped", "running", or "restarting").

 init.rc中使用属性的范例如下:

setprop ro.FOREGROUND_APP_MEM 1536

setprop ro.VISIBLE_APP_MEM 2048

on property:ro.kernel.qemu=1

start adbd

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

英文描述参见:/system/core/init/readme.txt

init脚本中的关键字参见:system/core/init/keywords.h
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值