init.rc翻译

每次看init.rc都看不懂。

索性翻译一下。给自己个备份。

翻译的是system/core/init/readme.txt


Android Init Language
---------------------

The Android Init Language consists of four broad classes of statements,
which are Actions, Commands, Services, and Options.

Android Init 语法

android init语法由四类语句组成:Actions,Commonds,Services,options。

                                                            动作,命令,服务,选项。

所有的这些都是面向行的,由空格隔开的符号组成的。c的反斜杠转义符可用于在符号之间插入空格。双引号也可用于防止空格。。

反斜杠如果用在一行的末尾,可用于行分隔符。

All of these are line-oriented, consisting of tokens separated by
whitespace.  The c-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 multiple tokens.  The backslash,
when it is the last character on a line, may be used for line-folding.

用#开头的语句是注释
Lines which start with a # (leading whitespace allowed) are comments.

动作和服务声明了新的section。

所有的命令或者选项属于最近刚被声明的section。在第一个被声明的section之前声明的命令和选项是被忽略的。
Actions and Services implicitly declare a new section.  All commands
or options belong to the section most recently declared.  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, it is 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 should occur.  When an event
occurs which matches an action's trigger, that action is added to
the tail of a to-be-executed queue (unless it is already on the
queue).

队列里面的每个动作按顺序移出队列,并按照顺序执行每个动作中的命令。

init进程负责在其它activities(如:设备创建/销毁,属性设置,进程重启)之间执行这些命令序列。
Each action in the queue is dequeued in sequence and each 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 in activities.

动作语法结构:
Actions take the form of:

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

服务是在init启动执行或者(可选)重启的时候退出执行的程序。

服务的语法格式:


Services
--------
Services are programs which init launches and (optionally) restarts
when they exit.  Services take the form of:


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

选项
选项给服务起修饰作用。他们影响init什么时候执行这个服务。
Options
-------
Options are modifiers to services.  They affect how and when init
runs the service.

重要的

这个是设备重要的服务,如果它在四分钟内退出超过四次,这个设备会重启进入安全模式。

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.

在启动进程中设置环境变量 将value赋值给name 
setenv <name> <value>
   Set the environment variable <name> to <value> in the launched process.

socket

创建一个用/dev/socket/name命名的socket,并将它的fd传递给启动进程。type必须是dgram、stream、seqpacket中的一个。默认用户和组是0

socket <name> <type> <perm> [ <user> [ <group> ] ]
   Create a unix domain socket named /dev/socket/<name> and pass
   its fd to the launched process.  <type> must be "dgram", "stream" or "seqpacket".
   User and group default to 0.

在执行服务之前切换用户到username。一般默认用户是root。一般,如果你的进程需要linux 功能那么你不能使用这个命令。你必须在root用户的时候,去请求这个命令,然后在降到你想要的uid。

user <username>
   Change to username before exec'ing this service.
   Currently defaults to root.  (??? probably should default to nobody)
   Currently, if your 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.

在执行服务之前,切换用户组到groupname。额外的用户组除了第一个都是用于设置进程的附加用户组。

默认的用户组是root。

group <groupname> [ <groupname> ]*
   Change to groupname before exec'ing this service.  Additional
   groupnames beyond the (required) first one are used to set the
   supplemental groups of the process (via setgroups()).
   Currently defaults to root.  (??? probably should default to nobody)

oneshot只一次

在服务退出之后不重启这个服务。
oneshot
   Do not restart the service when it exits.

给服务指定一个class name。用一个class name的所有的服务能够一块启动或者一块停止。

如果没有通过class 这个选项指定class name,那么这个服务的class是“default”。

class <name>
   Specify a class name for the service.  All services in a
   named class may be started or stopped together.  A service
   is in the class "default" if one is not specified via the
   class option.

当一个服务重启的时候启动一个命令。

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

触发条件

触发条件是一串字符串用于匹配特定的事件类型,这个事件能够引起一个动作发生。

Triggers
--------
   Triggers are strings which can be used to match certain kinds
   of events and used to cause an action to occur.


boot是第一个触发条件在init执行的时候(在/init.conf被加载之后)

boot
   This is the first trigger that will occur when init starts
   (after /init.conf is loaded)

这个格式的触发条件当name属性被赋值为value的时候触发

<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.

命令

Commands
--------
exec <path> [ <argument> ]*
   Fork and execute a program (<path>).  This will block until
   the program completes execution.  It is best to avoid exec
   as unlike the builtin commands, it runs the risk of getting
   init "stuck". (??? maybe there should be a timeout?)
   fork和执行一个程序。这个会被锁定一直到程序执行完毕。最好避免执行相似的内建命令。这样init运行起来有卡住的风险(或许这里应该有个延时)

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.
   解析一个init 配置文件,扩展当前的配置信息

hostname <name>
   Set the host name.
   设置hostname

chdir <directory>
   Change working directory.
   切换工作目录

chmod <octal-mode> <path>
   Change file access permissions.
   更改文件访问权限

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

    更改文件所有者和组


chroot <directory>
  Change process root directory.
  更改进程root目录?

class_start <serviceclass>
   Start all services of the specified class if they are
   not already running.
  启动所有class name为serviceclass命名的服务,如果这个服务还没开始运行的话。

class_stop <serviceclass>
   Stop all services of the specified class if they are
   currently running.
停止所有class name为serviceclass的,当前正在运行的服务,

domainname <name>
   Set the domain name.
 设置域名

insmod <path>

Install the module at <path>
安装这个路径的模块

mkdir <path> [mode] [owner] [group]
   Create a directory at <path>, optionally with the given mode, owner, and
   group. If not provided, the directory is created with permissions 755 and
   owned by the root user and root group.

   创建path指定的目录,(可选)使用给定的属性、所有者和组。如果没有提供,默认创建的目录属性是755,root所有和root组。


mount <type> <device> <dir> [ <mountoption> ]*
   Attempt to mount the named device at the directory <dir>
   <device> may be of the form mtd@name to specify a mtd block
   device by name.
   <mountoption>s include "ro", "rw", "remount", "noatime", ...
  尝试将device指定的挂载到dir下,device或许是mtd@name格式的,用于指定一个mtd块。

挂载选项包括 ro、rw、remount、noatime

setkey
   TBD


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>
   创建一个符号连接

sysclktz <mins_west_of_gmt>
   Set the system clock base (0 if system clock ticks in GMT)
   设置系统时钟基线

trigger <event>
   Trigger an event.  Used to queue an action from another
   action.

   触发一个事件,用于对动作进行排队。

wait <path> [ <timeout> ]
  Poll for the existence of the given file and return when found,
  or the timeout has been reached. If timeout is not specified it
  currently defaults to five seconds.
  在特定时间内看给定文件是否存在,如果找到则返回,或者时间到了。

如果timeout没有指定,默认的是5s。

write <path> <string> [ <string> ]*
   Open the file at <path> and write one or more strings
   to it with write(2)

   打开路径上的文件,使用write写入一串或者多串字符串到文件

属性

Properties
----------
Init updates some system properties to provide some insight into
what it's doing:
init会更新一些系统属性提供当前init在做什么。

init.action 
   Equal to the name of the action currently being executed or "" if none
   当前正在执行的动作,如果没有就是“”

init.command
   Equal to the command being executed or "" if none.
   当前正在执行的命令,如果没有就是""

init.svc.<name>
   State of a named service ("stopped", "running", "restarting")

   服务的状态,stopped、running、restarting。

Example init.conf
-----------------
例子
# not complete -- just providing some examples of usage
#
on boot
   export PATH /sbin:/system/sbin:/system/bin
   export LD_LIBRARY_PATH /system/lib


   mkdir /dev
   mkdir /proc
   mkdir /sys


   mount tmpfs tmpfs /dev
   mkdir /dev/pts
   mkdir /dev/socket
   mount devpts devpts /dev/pts
   mount proc proc /proc
   mount sysfs sysfs /sys


   write /proc/cpu/alignment 4


   ifup lo


   hostname localhost
   domainname localhost


   mount yaffs2 mtd@system /system
   mount yaffs2 mtd@userdata /data


   import /system/etc/init.conf


   class_start default


service adbd /sbin/adbd
   user adb
   group adb


service usbd /system/bin/usbd -r
   user usbd
   group usbd
   socket usbd 666


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


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 stdout and stderr into
/dev/null. To help with debugging, you can execute your program via the
Andoird program logwrapper. This will redirect stdout/stderr into the
Android logging system (accessed via logcat).


For example
service akmd /system/bin/logwrapper /sbin/akmd

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值