安卓系统启动脚本init.rc说明文件readme.txt翻译

 

本说明文件位于system/core/init/readme.txt

本文参考深入解析安卓系统一书,进行翻译,版权部分归书的作者  刘超,资深Android专家,系统架构师。

博客地址:http://blog.csdn.net/u013234805/article/details/45438219


Android Init Language

---------------------

The Android Init Language consists of four broadclasses of statements,

which are Actions, Commands, Services, and Options.

安卓的初始化脚本语言包括了4种基本的类型声明:行为,命令, 服务和选项。

 

All of these are line-oriented, consisting of tokensseparated by

whitespace.  Thec-style backslash escapes may be used to insert

whitespace into a token.  Double quotes may also be used to prevent

whitespace from breaking text into multipletokens.  The backslash,

when it is the last character on a line, may be usedfor line-folding.

所有的这些都以行为单位,各种符号则由空格分开。当然,C语言风格种的\反斜杠符号用在符号的期间插入相应的空格。双引号可以用于防止字符串被空格分割成多个记号。行的最后的反斜杠用于折行。

Lines which start with a # (leading whitespaceallowed) are comments.

注释的方法是以#号开头,当然也可以以空格开头。

 

Actions and Services implicitly declare a newsection.  All commands

or options belong to the section most recentlydeclared.  Commands

or options before the first section are ignored.

行为和服务声明一个新的分组。所有命令或者是选项都依赖于最近所声明的分组里。其中位于分组之前的命令或者是选项将会被忽略。

 

Actions and Services have unique names.  If a second Action or Service

is declared with the same name as an existing one, itis ignored as

an error.  (???should we override instead)

行为和服务有着独特的名字。如果第二行为或者服务宣告名字已经存在。那么这会导致行为或者服务被忽略或者产生相应的错误。(也许会被覆盖)

 

Actions  行为

-------

Actions are named sequences of commands.  Actions have a trigger which

is used to determine when the action shouldoccur.  When an event

occurs which matches an action's trigger, that actionis added to

the tail of a to-be-executed queue (unless it isalready on the

queue).

行为Actions其实就是一个序列的命令的集合。每个行为都有一个触发器trigger,触发器的作用决定了行为的什么时候将要执行。当一个符合Action触发条件的事件发生了,这个行为会加入到执行这个队列的结尾。

Each action in the queue is dequeued in sequence andeach command in

that action is executed in sequence.  Init handles other activities

(device creation/destruction, property setting,process restarting)

"between" the execution of the commands inactivities.

每一个行为都依次从队列里面被不断的取出来,然后这个行为的的每一个命令都将被一一执行。在这些命令执行的时候,init还同时处理着其它的活动,包括了设备节点的创建和销毁,设置属性,重新启动进程。

 

Actions take the form of:

Actions行为的格式如下:

 

on <trigger>

  <command>

  <command>

  <command>

 

 

Services 服务

--------

Services are programs which init launches and(optionally) restarts

when they exit. Services take the form of:

服务其实是一个后台的程序,这个程序在init进程中被启动,如果退出了可以由系统重新启动(可选择)。

服务的格式如下:

 

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

  <option>

  <option>

   ...

 

 

Options 选项

-------

Options are modifiers to services.  They affect how and when init

runs the service.

选项是服务的一个修订项,他们决定了一个服务什么时候执行以及是如何去执行。

 

critical

   This is adevice-critical service. If it exits more than four times in

   four minutes,the device will reboot into recovery mode.

   Critical表示这是一个关键的服务。如果服务4分钟内重新启动超过4次的情况下,系统将会重新启动进入recovery模式。

disabled

   This servicewill not automatically start with its class.

   It must beexplicitly started by name.

   Disable表示服务不会通过trigger触发器启动,它必须是以命令“start service_name”的形式来进行启动。

 

setenv <name> <value>

   Set theenvironment variable <name> to <value> in the launched process.

    在服务启动的时候将环境变量name设置成value

socket <name> <type> <perm> [<user> [ <group> [ <context> ] ] ]

   Create a unixdomain socket named /dev/socket/<name> and pass

   its fd to thelaunched process.  <type> must be"dgram", "stream" or "seqpacket".

   User andgroup default to 0.  用户和组默认为0

   Context isthe SELinux security context for the socket.

   It defaultsto the service security context, as specified by seclabel or

   computedbased on the service executable file security context.

    创建名为/dev /socket/<name> UNIX域套接字,并传递

    ???其文件描述符fd的推出进程。 <类型>必须是“DGRAM”,“流”或“SEQPACKET”。

    ???用户和组默认为0

    ???Context是套接字SELinux的安全上下文。

    ???它默认到服务的安全上下文,如SECLABEL指定或

    ???基于服务的可执行文件的安全上下文来计算。

user <username>

   Change tousername before exec'ing this service.

   Currentlydefaults to root.  (??? probably shoulddefault to nobody)

   Currently, ifyour process requires linux capabilities then you cannot use

   this command.You must instead request the capabilities in-process while

   still root,and then drop to your desired uid.

在启动这个服务前设置服务的用户名,默认是root。如果你的进程没有相应的权限,你将不能使用这个命令。如果这个进程由root权限,可以在程序中设置到你想要

的组uid

group <groupname> [ <groupname> ]*

   Change togroupname before exec'ing this service. Additional

   groupnamesbeyond the (required) first one are used to set the

   supplementalgroups of the process (via setgroups()).

   Currentlydefaults to root.  (??? probably shoulddefault to nobody)

在启动这个服务前设置服务的组名。除了第一个组名,剩下的组名通常用于设置进程的附加组(通过setgroups())。默认是root

seclabel <securitycontext>

  Change tosecuritycontext before exec'ing this service.

  Primarily foruse by services run from the rootfs, e.g. ueventd, adbd.

  Services onthe system partition can instead use policy-defined transitions

  based on theirfile security context.

  If notspecified and no transition is defined in policy, defaults to the init context.

SECLABEL<SecurityContext>更改为执行该服务之前SecurityContext中。主要为按服务使用从根文件系统,例如运行ueventdadbd。在系统分区服务可以改用基于其文件的安全上下文策略定义的转换。如果不指定,不转变政策规定,默认为初始化上下文。

oneshot

   Do not restartthe service when it exits.服务在退出后不会再重新启动。

 

class <name>

   Specify aclass name for the service.  All servicesin a

   named classmay be started or stopped together.  Aservice

   is in theclass "default" if one is not specified via the

   class option.

给服务指定一个名字,所有的同名字的服务可以同时启动和停止。如果不通过class选项指定一个名字,则默认的名字是default

 

onrestart

    Execute aCommand (see below) when service restarts.

    当服务重启的时候,执行一条命令。

 

Triggers

--------

   Triggers arestrings which can be used to match certain kinds

   of events andused to cause an action to occur.

    触发器是一个用于匹配某种事件类型的字符串,它将使对于的行为执行。

 

boot

   This is thefirst trigger that will occur when init starts

   (after/init.conf is loaded)

    Bootinit执行后第一个触发的行为。在/init.conf被装载以后。

 

<name>=<value>

   Triggers ofthis form occur when the property <name> is set

   to thespecific value <value>.

这种形式的触发会在属性name设置为指定的值value时触发或者删除

 

device-added-<path>

device-removed-<path>

   Triggers ofthese forms occur when a device node is added

   or removed.

这种形式的触发器会在一个设备文件增加或者删除的时候触发。

 

service-exited-<name>

   Triggers ofthis form occur when the specified service exits.

这种形式的触发器会在一个指定的服务退出的时候触发。

 

 

Commands 命令

--------

 

exec <path> [ <argument> ]*

   Fork andexecute a program (<path>).  Thiswill block until

   the programcompletes execution.  It is best to avoidexec

   as unlike thebuiltin commands, it runs the risk of getting

   init"stuck". (??? maybe there should be a timeout?)

   Fork和启动第一个程序。在程序完成启动前,init将会阻塞。应该尽量避免使用exec,它可能会引起init卡死。

 

export <name> <value>

   Set theenvironment variable <name> equal to <value> in the

   globalenvironment (which will be inherited by all processes

   started afterthis command is executed)

设置全局变量<name><value>,所有在这个命令执行后运行的进程都将继承该环境变量。

 

ifup <interface>

   Bring thenetwork interface <interface> online.

启动网络接口interface

 

import <filename>

   Parse an initconfig file, extending the current configuration.

引入一个init的配置文件,拓展当前配置。

 

hostname <name>

   Set the hostname.

设置主机名。

 

chdir <directory>

   Changeworking directory.

改变进程当前的工作目录。

 

chmod <octal-mode> <path>

   Change fileaccess permissions.

设置文件或者目录的访问权限。

 

 

chown <owner> <group> <path>

   Change fileowner and group.

设置文件或目录的所有者和组。

 

chroot <directory>

  Change processroot directory.

设置进程的根目录。

 

class_start <serviceclass>

   Start allservices of the specified class if they are

   not alreadyrunning.

启动所有指定服务名称下的未运行服务。

 

class_stop <serviceclass>

   Stop allservices of the specified class if they are

   currently running.

停止所有指定服务名称下的已经运行的服务。

 

domainname <name>

   Set thedomain name.

设置域名。

 

enable <servicename>

   Turns adisabled service into an enabled one as if the service did not

   specifydisabled.

   If theservice is supposed to be running, it will be started now.

   Typicallyused when the bootloader sets a variable that indicates a specific

   serviceshould be started when needed. E.g.

     onproperty:ro.boot.myfancyhardware=1

        enablemy_fancy_service_for_my_fancy_hardware

启用<服务>如果服务没有指定禁用,则打开这个被禁用的服务。

如果服务是应该运行时,将立即开始。当引导加载程序设置一个变量,表示需要时一个特定的服务应启动通常使用。 例如:

  service shouldbe started when needed. E.g.

     onproperty:ro.boot.myfancyhardware=1

 

insmod <path>

   Install themodule at <path>

安装一个驱动模块

 

mkdir <path> [mode] [owner] [group]

   Create adirectory at <path>, optionally with the given mode, owner, and

   group. If notprovided, the directory is created with permissions 755 and

   owned by theroot user and root group.

新建一个目录,可以指定访问权限、拥有者和组。如果没有被最后定,默认的访问权限是755,属于root用户和root组。

 

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

   Attempt tomount the named device at the directory <dir>

  <device> may be of the form mtd@name to specify a mtd block

   device byname.

  <mountoption>s include "ro", "rw","remount", "noatime", ...

在指定的目录下挂载一个设备。可以是以mtd@name的格式指定的一个mtd块设备。

<mountoption>包括了 "ro","rw", "remount", "noatime", ...

 

restorecon <path> [ <path> ]*

   Restore thefile named by <path> to the security context specified

   in thefile_contexts configuration.

   Not requiredfor directories created by the init.rc as these are

   automaticallylabeled correctly by init.

重新存储指定的文件到一个由file_contexts配置的安全上下文。不用指定目录,它们会被init进程自动创建。

 

restorecon_recursive <path> [ <path> ]*

   Recursivelyrestore the directory tree named by <path> to the

   securitycontexts specified in the file_contexts configuration.

   Do NOT usethis with paths leading to shell-writable or app-writable

   directories,e.g. /data/local/tmp, /data/data or any prefix thereof.

递归的restorecon<路径> [<路径>] *递归恢复通过<路径>命名的目录树中存储在file_contexts配置中指定的安全上下文。不要用路径,这会导致shell可写或应用可写目录,

例如使用/data/local/tmp, /data/data数据或任何其前缀。

 

setcon <securitycontext>

   Set thecurrent process security context to the specified string.

   This istypically only used from early-init to set the init context

   before anyother process is started.

设置当前进程的安全上下文为指定的串。主要用在early-init中去设置init的安全上下文。

setenforce 0|1

   Set theSELinux system-wide enforcing status.

   0 ispermissive (i.e. log but do not deny), 1 is enforcing.

设置SELinux系统级的enforcing状态。0代表permissive(如记录但是不拒绝服务),1代表enforcing

 

setkey

   TBD 目前没有使用

 

setprop <name> <value>

   Set systemproperty <name> to <value>.

设置系统属性<name><value>值。

 

setrlimit <resource> <cur> <max>

   Set therlimit for a resource.

设置系统属性<resource>rlimit值。

 

setsebool <name> <value>

   Set SELinuxboolean <name> to <value>.

   <value>may be 1|true|on or 0|false|off

设置SELinuxboolean型属性"name"的值为"value""value"可以是1|true|on或者0|false|off

 

start <service>

   Start aservice running if it is not already running.

如果服务没有被执行就启动指定的服务

 

stop <service>

   Stop aservice from running if it is currently running.

如果服务在运行,那么就停止指定的服务。

 

symlink <target> <path>

   Create asymbolic link at <path> with the value <target>

创建一个符号链接。

 

sysclktz <mins_west_of_gmt>

   Set thesystem clock base (0 if system clock ticks in GMT)

设置系统时钟基准,0代表格林尼治平均时GMT为准。

 

trigger <event>

   Trigger anevent.  Used to queue an action fromanother

   Action.

触发一个事件。用于将一个行为和另一个行为连接在一起执行。

 

wait <path> [ <timeout> ]

  Poll for theexistence of the given file and return when found,

  or the timeouthas been reached. If timeout is not specified it

  currentlydefaults to five seconds.

等待指定路径的文件创建出来,创建完成就停止等待,或者等待超时时间到。如果未指定超时时间,默认是5秒。

write <path> <string>

   Open the fileat <path> and write a string to it with write(2)

   withoutappending.

打开指定的文件,并写入一个或者多个字符串。

 

Properties 属性

----------

Init updates some system properties to provide someinsight into

what it's doing:

初始化更新一些系统属性来提供一些见解

它在做什么:

 

init.action

   Equal to thename of the action currently being executed or "" if none

等于当前正在执行或行动“的名义”,如果没有

 

init.command

   Equal to thecommand being executed or "" if none.

等于命令被执行或“”如果没有。

init.svc.<name>

   State of anamed service ("stopped", "running","restarting")

一个名为服务的状态(“停止”,“运行”,“重新启动”)

 

Example init.conf 例如init.conf

-----------------

 

# not complete -- just providing some examples ofusage

#

#不完整 - 只是提供一些用法示例

on boot

   export PATH/sbin:/system/sbin:/system/bin

   export LD_LIBRARY_PATH /system/lib

 

   mkdir /dev

   mkdir /proc

   mkdir /sys

 

   mount tmpfstmpfs /dev

   mkdir/dev/pts

   mkdir/dev/socket

   mount devptsdevpts /dev/pts

   mount procproc /proc

   mount sysfssysfs /sys

 

   write /proc/cpu/alignment4

 

   ifup lo

 

   hostnamelocalhost

   domainnamelocalhost

 

   mount yaffs2mtd@system /system

   mount yaffs2mtd@userdata /data

 

   import/system/etc/init.conf

 

   class_startdefault

 

service adbd /sbin/adbd

   user adb

   group adb

 

service usbd /system/bin/usbd -r

   user usbd

   group usbd

   socket usbd666

 

service zygote /system/bin/app_process -Xzygote/system/bin --zygote

   socket zygote666

 

service runtime /system/bin/runtime

   user system

   group system

 

on device-added-/dev/compass

   start akmd

 

on device-removed-/dev/compass

   stop akmd

 

service akmd /sbin/akmd

   disabled

   user akmd

   group akmd

 

Debugging notes 调试笔记

---------------

By default, programs executed by init will drop stdoutand stderr into

/dev/null. To help with debugging, you can executeyour program via the

Andoird program logwrapper. This will redirectstdout/stderr into the

Android logging system (accessed via logcat).

 

默认情况下,由init执行的程序将标准输出和标准错误信息输出定向到/dev/ null的。为帮助调试,您可以通过执行Android程序logwrapper。这将标准输出/标准错误重定向到Android的日志系统(通过logcat的访问)。

 

For example

service akmd /system/bin/logwrapper /sbin/akmd

例如

service akmd /system/bin/logwrapper /sbin/akmd

上一篇:Spring AOP 源码分析 - 拦截器链的执行过程


下一篇:FunDA:一个开源的函数式数据处理工具库,也是Slick的补充