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

Android Init Language介绍

2018年05月03日 ⁄ 综合 ⁄ 共 4141字 ⁄ 字号 评论关闭

Android init language用于init解析init.rc等初始化文件时使用

官方的解释文档详见system/core/init/readme.txt

关于Init language的解析的代码分析可参见http://blog.csdn.net/orz415678659/article/details/8769814

init lang基本上由四个Statements:Actions,Commands,Services和Options,下面分别介绍:


  • 每一个语句占据一行,并且各个关键字被空格分开。c规范中的(如 \n)反斜杠将被忽略(backslash escapes)而被认为是一个空格 ,双引号用来保证空格不会把一个文字串分分为多个关键字。行最后的反斜杠用作续行。
  • 由 # (前面允许有空格)开始的行都是注释行(comment)
  • 一个actions 或 services 的开始隐含声明了一个新的段,所有commands 或 options 属于最近的声明。在第一个段之前的 commands 或 options 都会被忽略
  • 每一个actions 和 services 都有不同的名字。后面与前面发生重名的,那么这个后面重名的将被忽略或被认为是一个错误 

Actions和Services会建立一个section,command和options是Section的修饰

一、Action的格式是

on <trigger>
   <command>
   <command>
   <command>

1)trigger

trgger的定义是Triggers are strings used to match certain kinds of events that cause an action to occur.

本意是某件事情发生时,比如boot,就执行相应的command,但实际上只是把相关阶段的命令集合放在一起执行,并控制

命令集合的执行顺序。实际使用中的trigger可以根据需要自由定义,只要相应的Init程序中添加相应trigger处理(action_for_each_trigger)

即可,常见的trigger有:early-init,
init,
early-fs,fs,post-fs,post-fs-data,early-boot,boot

Trigger Description
boot This 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)Command

command就是在action或section中执行的命令,可以定义多个command


chmod <octal-mode> <path>  Change file access permissions.

chown <owner> <group> <path>
   Change file owner and group.

chown <owner> <group> <path>
   Change file owner and group.

chroot <directory>
  Change process root directory.

Command Description
exec <path> [ <argument> ]* Fork and execute a program (<path>). This will block until the program completes execution. Try to avoid exec. Unlike the builtin 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).



二、Services的格式是

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

1)option

onrestart   Execute a Command (see below) when service restarts.

critical This is a device-critical service. If it exits more than four times in four minutes, the device will reboot into recovery mode.

console  指定console时使用,例如:

service console /system/bin/sh
    class core
    console
    disabled
    user shell
    group log

Option Description
disabled This 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 streamuser and group 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 (with setgroups()). Currently defaults to root.
capability [ <capability> ]+ Set linux capability before exec'ing this service
oneshot Do 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.


抱歉!评论已关闭.