在Init.rc中,用service关键字声明了一系列服务.
init.rc对service的说明如下:(详见system/core/init/readme.txt)
Services--------Services are programs which init launches and (optionally) restartswhen they exit.
Services take the form of:service <name> <pathname> [ <argument> ]* <option> <option> ...
Options-------Options are modifiers to services.
They affect how and when initruns 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.
setenv <name> <value>
Set the environment variable <name> to <value> in the launched process.
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.
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.
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 Do not restart the service when it exits.
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.
可以看出,service中比较关键的几个选项是类别(class),启动选项(oneshot disabled,critical),用户组(group,user)。
以surfaceflinger为例:
service surfaceflinger /system/bin/surfaceflinger
class main
user system
group graphics
onrestart restart zygote
它的类别是main,用户是system,属于graphics ,且没有声明disabled,所以在启动main这个类别的时候,surfaceflinger就会被启动。
main类别是在哪里启动的呢?
搜索class_start关键字,在On boot的时候,就会启动了
on boot
#省略无关.....
class_start core
class_start main
readme中对class_start有如下说明:
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.
所以,在系统启动触发boot这个trigger之后,core和main类别的没有声明为disabled的守护进程就被系统启动了,
具体的过程还得去看init对init.rc文件的解析过程,暂且放着。
那么还有一个疑问,那些声明了为disabled的守护进程是怎么启动的呢?
以Bootanimation为例:
service bootanim /system/bin/bootanimation
class main
user graphics
group graphics
disabled
oneshot
它也是main组别的,用户和组均是graphics,且是d