Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3010900
  • 博文数量: 674
  • 博客积分: 17881
  • 博客等级: 上将
  • 技术积分: 4849
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-17 10:15
文章分类

全部博文(674)

文章存档

2013年(34)

2012年(146)

2011年(197)

2010年(297)

分类: LINUX

2010-07-05 00:03:33

init is the first process after kernel started. The corresponding source code lies in: device/system/init. It does the following tasks step by step:

init是内核启动后的第一个进程任务, 他的源代码部分在device/system/init.他主要做了以下的工作。

1.       Initialize log system. 初始化系统日志任务。

2.       Parse /init.rc and /init.%hardware%.rc. 解析 init.rc和 init.hardware.rc两个文件

3.       Execute early-init action in the two files parsed in step 2.执行rc文件中的预初始化部分。即on early-init 部分

4.       Device specific initialize. For example, make all device node in /dev and download firmwares. 设备初始化,做设备节点和硬件初始化。

5.       Initialize property system. Actually the property system is working as a share memory. Logically it looks like a registry under Windows system. 初始化属性设置

6.       Execute init action in the two files parsed in step 2.执行rc文件中的初始化部分.即on-init部分。

7.       Start property service.启动属性服务。

8.       Execute early-boot and boot actions in the two files parsed in step 2. 执行rc文件中的预引导和引导部分内容。即 on-early-boot 和 on-boot部分。

9.       Execute property action in the two files parsed in step 2.执行rc文件中的属性任务。即 on property:key=value 的动作。

10.   Enter into an indefinite loop to wait for device/property set/child process exit events. For example, if an SD card is plugined, init will receive a device add event, so it can make node for the device. Most of the important process is forked in init, so if any of them crashed, init will receive a SIGCHLD then translate it into a child process exit event, so in the loop init can handle the process exit event and execute the commands defined in *.rc(it will run command onrestart). 循环等待 设备/树形设置/子进程等任务的退出。


The .rc file is a script file defined by Android. The default is device/system/rootdir/init.rc. We can take a loot at the file format(device/system/init/readme.txt is a good overall introduction of the script). Basically the script file contains actions and services.


rc 文件是一个android系统定义的脚本文件, 默认是device/system/rootdir/init.rc文件,

基本上这个脚本文件主要包括动作和服务。具体文件格式说明信息在文件中 system/init/readme.txt



需要对这个文章进行的一点小补充是:
1. 对于这个脚本中定义的所有服务,如果该服务选项中没有disabled定义,则在init.rc中解析到这个服务的时候,会马上执行这个服务。而如果在服务的选项中增加了disabled定义,则该服务不会在init.rc中启动,需要我们在代码中使用以下方法来启动:
property_set("ctl.start", "wifi_dhcpcd");
上句话的意思是,我要启动wifi_dhcpd这个服务了。


==================  脚本说明 ====================

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

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

Android 初始化语言主要包括部分:Actions, Commands, Services, and Options

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.

#符号开始的行为注释行。

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 暗示开启一个区域,所有命令和选项从属于最近声明的区域。第一个区域前的命令和选项声明将被忽略。



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 and Services名称应唯一, 如果出现重名将报错。


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


Actions 是一个命名的commands序列。 拥有一个触发器,此触发器决定此Actions被触发的时机。 当触发时机发生时,action将被加入到to-be-executed队列的末尾(如果还没有加入到队列的情况下)



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.

执行时,action将被从队列中提取出来(出列)并且action中的每条命令将在队列中执行。 init进程负责管理在命令执行过程中的其他获得的管理(设备的创建/销毁 属性设置 进程重启等)


Actions区域的格式如下:
Actions take the form of:

on
  
  
  


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

Services区域定义一些程序,这些程序被init进程加载和监视,如果他们退出将被init重新启动。


Services区域格式定义:
service [ ]*
  
  
   ...


Options
-------
Options are modifiers to services.  They affect how and when init
runs the service.
Options 专用于services区域。他主要定义init进程如何加载和运行services所定义的服务进程。
critical
   This is a device-critical service. If it exits more than four times in
   four minutes, the device will reboot into recovery mode.

critical定性的. 此选项说明服务是一个对终端来说关键性的服务,如果在4分钟内被启动了4次,则终端会被重新启动。


disabled
   This service will not automatically start with its class.
   It must be explicitly started by name.

disabled 此服务不会自动执行,需要在程序中显示调用。

setenv
   Set the environment variable to in the launched process.

setenv 建立程序运行环境变量(键值对)

socket [ [ ] ]
   Create a unix domain socket named /dev/socket/ and pass
   its fd to the launched process.  must be "dgram" or 

"stream".
   User and group default to 0.

socket 创建 unix 的 socket文件并把其 socket句柄传递给启动进程。需要指定socket的类型为 dggram和stream。

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

在执行此服务之前改变执行用户为 user 所指定的用户。一般指定为root用户以提高权限。

group [ ]*
   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)

在执行此服务之前改变执行用户组为 group 指定的组。

oneshot
   Do not restart the service when it exits.

oneshot: 告诉init 此服务不重复启动

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

class 为 service 命名。所有命名的服务能被外部启动和停止。 没有命名的服务默认被default命名。

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

onrestart 表明服务被重启时要执行一个命令。



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

触发器使用字符串来描述一类事件,并对匹配的事件产生反应。

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

boot 是init进程首先执行的的事件触发器。

property:=
   Triggers of this form occur when the property is set
   to the specific value .

键值对触发器为当 Key 被正确的 value 赋值时发生。

device-added-
device-removed-
   Triggers of these forms occur when a device node is added
   or removed.

device-added-: 这种格式的触发器在设备节点被加载或者移除时发生。

service-exited-
   Triggers of this form occur when the specified service exits.

service-exited- 格式的触发器在指定的进程退出时触发。


Commands
--------

exec [ ]*
   Fork and execute a program ().  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?)

exec 执行指定进程并等待其执行结束。

export
   Set the environment variable equal to in the
   global environment (which will be inherited by all processes
   started after this command is executed)

export 产生全局的环境变量。

ifup
   Bring the network interface online.

ifup 启动网络接口设备。

import
   Parse an init config file, extending the current configuration.

import 引入一个新的配置文件。

hostname
   Set the host name.

hostname 设置本机名称。

chmod
   Change file access permissions.

chmod 改变文件权限

chown
   Change file owner and group.

chown 改变文件属性

class_start

start
   Start all services of the specified class if they are
   not already running.

class_start 启动一个命名服务类中的所有的尚未启动的服务。

class_stop

stop
   Stop all services of the specified class if they are
   currently running.

class_sop 停止一个命名服务类中的所有的尚未停止的服务。



domainname
   Set the domain name.

domainname 设置域名

insmod
   Install the module at

insmod 加载设备节点

mkdir [mode] [owner] [group]
   Create a directory at , 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.

mkdir 创建目录


mount

[ ]*
   Attempt to mount the named device at the directory
   may be of the form mtd@name to specify a mtd block
   device by name.
   s include "ro", "rw", "remount", "noatime", ...

mount 加载设备

setkey
   TBD

setprop
   Set system property to .

setprop 设置系统属性

setrlimit
   Set the rlimit for a resource.

setrlimit 设置资源限制。

start
   Start a service running if it is not already running.

start 启动一个服务

stop
   Stop a service from running if it is currently running.

stop 停止一个服务

symlink
   Create a symbolic link at with the value

symlink 建立链接

sysclktz
   Set the system clock base (0 if system clock ticks in GMT)

sysclktz 设置系统时钟基数


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

triger 触发指定 event 。

write [ ]*
   Open the file at 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.action
   Equal to the name of the action currently being executed or "" if none

init.action 的值说明正在运行的action

init.command
   Equal to the command being executed or "" if none.

init.command 的值说明了正在运行的命令

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

init.svc. 的值说明了指定服务的运行状态。


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

阅读(1474) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~