Chinaunix首页 | 论坛 | 博客
  • 博客访问: 655137
  • 博文数量: 171
  • 博客积分: 2246
  • 博客等级: 大尉
  • 技术积分: 1574
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-31 11:45
文章分类

全部博文(171)

文章存档

2018年(3)

2017年(4)

2015年(1)

2014年(20)

2013年(57)

2012年(86)

分类: 嵌入式

2012-08-12 17:07:05


来源:http://hi.baidu.com/donghaozheng/blog/item/e2f5045577cb73c8b645aec0.html

摘自《Android系统原理及开发要点详解》

    在 Android中的启动脚本init.rc,可以在系统的初始化过程中进行一些简单的初始化操作。这个脚本被直接安装到目标系统的根文件系统中,被 init可执行程序解析。 init.rc是在init启动后被执行的启动脚本,其语法主要包含了以下内容:

Commands:    命令
Actions:     动作
Triggers:     触发条件
Services:    服务
Options:     选项
Propertise:  属性

(1) Commands是一些基本的操作,例如:
    mkdir /sdcard 0000 system system
    mkdir /system
    mkdir /data 0771 system system
    mkdir /cache 0770 system cache
    mkdir /config 0500 root root
    mkdir /sqlite_stmt_journals 01777 root root
    mount tmpfs tmpfs /sqlite_stmt_journals size=4m
这些命令在init可执行程序中被解析,然后调用相关的函数来实现。

(2) Actions(动作)表示一系列的命令,通常在Triggers(触发条件)中调用,动作和触发条件的形式为:
   on
     
     
     
示例如下:
   on init
//init是触发条件,触发后,export和mkdir作为一个动作Action 
   export PATH /sbin:/system/sbin:/system/bin:/system/xbin //设置环境变量
   mkdir /system                                           //建立目录

(3) Services(服务)通常表示启动一个可执行程序,Options(选项)是服务的附加内容,用于配合服务使用。

service vold /system/bin/vold 
    socket vold stream 0660 root mount

service bootsound /system/bin/playmp3
    user media
    group audio
    oneshot

vold和bootsound分别是两个服务的名称,/system/bin/vold和/system /bin/playmp3分别是他们所对应的可执行程序。
socket、user、group、oneshot就是配合服务使用的选项。其中oneshot选项表示该服务只启动一次,而如果没有oneshot选项,这个可执行程序会一直存在--如果可执行程序被杀死,则会重新启动。

(4) Properties(属性)是系统中使用的一些值,可以进行设置和读取。

    setprop ro.FOREGROUND_APP_MEM 1536
    setprop ro.VISIBLE_APP_MEM 2048
    on property:ro.kernel.qemu=1
    start adbd
setprop 用于设置属性,on property可以用于判断属性,这里的属性在整个Android系统运行中都是一致的。

init脚本的关键字可以参考init进程的system/core/init/keyword.h文件。
init.rc的使用方法,可以参考说明文件system/core/init/readme.txt

如果想要修改启动过程只需要修改init.c(system/core/init)或者init.rc里的内容即可.


附录:system/core/init/readme.txt


点击(此处)折叠或打开

  1. Android Init Language
  2. ---------------------

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

  5. All of these are line-oriented, consisting of tokens separated by
  6. whitespace. The c-style backslash escapes may be used to insert
  7. whitespace into a token. Double quotes may also be used to prevent
  8. whitespace from breaking text into multiple tokens. The backslash,
  9. when it is the last character on a line, may be used for line-folding.

  10. Lines which start with a # (leading whitespace allowed) are comments.

  11. Actions and Services implicitly declare a new section. All commands
  12. or options belong to the section most recently declared. Commands
  13. or options before the first section are ignored.

  14. Actions and Services have unique names. If a second Action or Service
  15. is declared with the same name as an existing one, it is ignored as
  16. an error. (??? should we override instead)


  17. Actions
  18. -------
  19. Actions are named sequences of commands. Actions have a trigger which
  20. is used to determine when the action should occur. When an event
  21. occurs which matches an action's trigger, that action is added to
  22. the tail of a to-be-executed queue (unless it is already on the
  23. queue).

  24. Each action in the queue is dequeued in sequence and each command in
  25. that action is executed in sequence. Init handles other activities
  26. (device creation/destruction, property setting, process restarting)
  27. "between" the execution of the commands in activities.

  28. Actions take the form of:

  29. on <trigger>
  30.    <command>
  31.    <command>
  32.    <command>


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

  37. service <name> <pathname> [ <argument> ]*
  38.    <option>
  39.    <option>
  40.    ...


  41. Options
  42. -------
  43. Options are modifiers to services. They affect how and when init
  44. runs the service.

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

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

  51. setenv <name> <value>
  52.    Set the environment variable <name> to <value> in the launched process.

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

  57. user <username>
  58.    Change to username before exec'ing this service.
  59.    Currently defaults to root. (??? probably should default to nobody)
  60.    Currently, if your process requires linux capabilities then you cannot use
  61.    this command. You must instead request the capabilities in-process while
  62.    still root, and then drop to your desired uid.

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

  68. oneshot
  69.    Do not restart the service when it exits.

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

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

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

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

  84. <name>=<value>
  85.    Triggers of this form occur when the property <name> is set
  86.    to the specific value <value>.

  87. device-added-<path>
  88. device-removed-<path>
  89.    Triggers of these forms occur when a device node is added
  90.    or removed.

  91. service-exited-<name>
  92.    Triggers of this form occur when the specified service exits.


  93. Commands
  94. --------

  95. exec <path> [ <argument> ]*
  96.    Fork and execute a program (<path>). This will block until
  97.    the program completes execution. It is best to avoid exec
  98.    as unlike the builtin commands, it runs the risk of getting
  99.    init "stuck". (??? maybe there should be a timeout?)

  100. export <name> <value>
  101.    Set the environment variable <name> equal to <value> in the
  102.    global environment (which will be inherited by all processes
  103.    started after this command is executed)

  104. ifup <interface>
  105.    Bring the network interface <interface> online.

  106. import <filename>
  107.    Parse an init config file, extending the current configuration.

  108. hostname <name>
  109.    Set the host name.

  110. chdir <directory>
  111.    Change working directory.

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

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

  116. chroot <directory>
  117. Change process root directory.

  118. class_start <serviceclass>
  119.    Start all services of the specified class if they are
  120.    not already running.

  121. class_stop <serviceclass>
  122.    Stop all services of the specified class if they are
  123.    currently running.

  124. domainname <name>
  125.    Set the domain name.

  126. insmod <path>
  127.    Install the module at <path>

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

  132. mount <type> <device> <dir> [ <mountoption> ]*
  133.    Attempt to mount the named device at the directory <dir>
  134.    <device> may be of the form mtd@name to specify a mtd block
  135.    device by name.
  136.    <mountoption>s include "ro", "rw", "remount", "noatime", ...

  137. setkey
  138.    TBD

  139. setprop <name> <value>
  140.    Set system property <name> to <value>.

  141. setrlimit <resource> <cur> <max>
  142.    Set the rlimit for a resource.

  143. start <service>
  144.    Start a service running if it is not already running.

  145. stop <service>
  146.    Stop a service from running if it is currently running.

  147. symlink <target> <path>
  148.    Create a symbolic link at <path> with the value <target>

  149. sysclktz <mins_west_of_gmt>
  150.    Set the system clock base (0 if system clock ticks in GMT)

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

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


  157. Properties
  158. ----------
  159. Init updates some system properties to provide some insight into
  160. what it's doing:

  161. init.action
  162.    Equal to the name of the action currently being executed or "" if none

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

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


  167. Example init.conf
  168. -----------------

  169. # not complete -- just providing some examples of usage
  170. #
  171. on boot
  172.    export PATH /sbin:/system/sbin:/system/bin
  173.    export LD_LIBRARY_PATH /system/lib

  174.    mkdir /dev
  175.    mkdir /proc
  176.    mkdir /sys

  177.    mount tmpfs tmpfs /dev
  178.    mkdir /dev/pts
  179.    mkdir /dev/socket
  180.    mount devpts devpts /dev/pts
  181.    mount proc proc /proc
  182.    mount sysfs sysfs /sys

  183.    write /proc/cpu/alignment 4

  184.    ifup lo

  185.    hostname localhost
  186.    domainname localhost

  187.    mount yaffs2 mtd@system /system
  188.    mount yaffs2 mtd@userdata /data

  189.    import /system/etc/init.conf

  190.    class_start default

  191. service adbd /sbin/adbd
  192.    user adb
  193.    group adb

  194. service usbd /system/bin/usbd -r
  195.    user usbd
  196.    group usbd
  197.    socket usbd 666

  198. service zygote /system/bin/app_process -Xzygote /system/bin --zygote
  199.    socket zygote 666

  200. service runtime /system/bin/runtime
  201.    user system
  202.    group system

  203. on device-added-/dev/compass
  204.    start akmd

  205. on device-removed-/dev/compass
  206.    stop akmd

  207. service akmd /sbin/akmd
  208.    disabled
  209.    user akmd
  210.    group akmd

  211. Debugging notes
  212. ---------------
  213. By default, programs executed by init will drop stdout and stderr into
  214. /dev/null. To help with debugging, you can execute your program via the
  215. Andoird program logwrapper. This will redirect stdout/stderr into the
  216. Android logging system (accessed via logcat).

  217. For example
  218. service akmd /system/bin/logwrapper /sbin/akmd




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