在以前版本的Android中,系统Native服务,不管它们的可执行文件位于系统什么位置都定义在根分区的init.*.rc文件中。这造成init*.rc文件臃肿庞大,给维护带来了一些不便,而且其中定义的一些服务的二进制文件根本不存在。
但在Android 8中,对该机制做了一些改变 。
单一的init*.rc,被拆分,服务根据其二进制文件的位置(/system,/vendor,/odm)定义到对应分区的etc/init目录中,每个服务一个rc文件。与该服务相关的触发器、操作等也定义在同一rc文件中。
/system/etc/init,包含系统核心服务的定义,如SurfaceFlinger、MediaServer、Logcatd等。
/vendor/etc/init, SOC厂商针对SOC核心功能定义的一些服务。比如高通、MTK某一款SOC的相关的服务。
/odm/etc/init,OEM/ODM厂商如小米、华为、OPP其产品所使用的外设以及差异化功能相关的服务。
这样的目录结构拆分,也与Android产品的开发流程相吻合,减轻了维护的负担。下图为Android8 模拟器/system/etc/init中定义的服务。
david@daviddeMBP:~$ adb shell
generic_x86:/ $ su
generic_x86:/ # cd /system/etc/init
generic_x86:/system/etc/init # ls
atrace.rc gatekeeperd.rc mediacodec.rc rild.rc
audioserver.rc init-debug.rc mediadrmserver.rc servicemanager.rc
bootanim.rc installd.rc mediaextractor.rc surfaceflinger.rc
bootstat.rc keystore.rc mediaserver.rc uncrypt.rc
cameraserver.rc lmkd.rc mtpd.rc vdc.rc
debuggerd.rc logcatd.rc netd.rc vold.rc
drmserver.rc logd.rc perfprofd.rc
dumpstate.rc mdnsd.rc racoon.rc
generic_x86:/system/etc/init #
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
2.提供一个编译宏LOCAL_INIT_RC用于将服务相关的RC文件编译到相应位置。这能确保服务定义和服务的可执行文件同时存在,避免了之前出现的服务对应的可执行程序不存在的问题。
例如logcatd的Android.mk中,通过LOCAL_INIT_RC将logcatd对应的logcatd.rc编译到/system/etc/init目录中。
system/core/logcat/Android.mk
include $(CLEAR_VARS)
LOCAL_MODULE := logpersist.start
LOCAL_MODULE_TAGS := debug
LOCAL_MODULE_CLASS := EXECUTABLES
LOCAL_INIT_RC := logcatd.rc
LOCAL_MODULE_PATH := $(bin_dir)
LOCAL_SRC_FILES := logpersist
ALL_TOOLS := logpersist.start logpersist.stop logpersist.cat
LOCAL_POST_INSTALL_CMD := $(hide) $(foreach t,$(filter-out $(LOCAL_MODULE),$(ALL_TOOLS)),ln -sf $(LOCAL_MODULE) $(TARGET_OUT)/bin/$(t);)
include $(BUILD_PREBUILT)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
3.在init执行mount_all指令挂载分区时,会加载这些目录中的rc文件,并在合适的时机运行这些服务和操作。下面位Android7.0模拟器的log。
[ 0.912389] init: init first stage started!
---------加载根分区的rc文件
[ 0.996447] init: waitpid failed: No child processes
[ 0.996558] init: (Loading properties from /default.prop took 0.00s.)
[ 0.997274] init: (Parsing /init.environ.rc took 0.00s.)
[ 0.997457] init: (Parsing /init.usb.rc took 0.00s.)
[ 0.997561] init: (Parsing /init.ranchu.rc took 0.00s.)
[ 0.997823] init: (Parsing /init.usb.configfs.rc took 0.00s.)
[ 0.997876] init: (Parsing /init.zygote32.rc took 0.00s.)
[ 0.997895] init: (Parsing /init.rc took 0.00s.)
[ 1.121126] init: Starting service 'ueventd'...
[ 1.121422] init: Waiting for /dev/.coldboot_done...
-----------挂载分区,加载附加rc文件
[ 1.384113] EXT4-fs (vdc): Ignoring removed nomblk_io_submit option
[ 1.386155] EXT4-fs (vdc): mounted filesystem with ordered data mode. Opts: nomblk_io_submit,errors=panic
[ 1.386165] SELinux: initialized (dev vdc, type ext4), uses xattr
[ 1.386179] fs_mgr: __mount(source=/dev/block/vdc,target=/data,type=ext4)=0
[ 1.387397] init: (Parsing /system/etc/init/atrace.rc took 0.00s.)
[ 1.387762] init: (Parsing /system/etc/init/audioserver.rc took 0.00s.)
[ 1.388078] init: (Parsing /system/etc/init/bootanim.rc took 0.00s.)
[ 1.388424] init: (Parsing /system/etc/init/bootstat.rc took 0.00s.)
[ 1.388805] init: (Parsing /system/etc/init/cameraserver.rc took 0.00s.)
[ 1.389125] init: (Parsing /system/etc/init/debuggerd.rc took 0.00s.)
[ 1.389443] init: (Parsing /system/etc/init/drmserver.rc took 0.00s.)
[ 1.389781] init: (Parsing /system/etc/init/dumpstate.rc took 0.00s.)
[ 1.392698] init: (Parsing /system/etc/init/gatekeeperd.rc took 0.00s.)
[ 1.393060] init: (Parsing /system/etc/init/init-debug.rc took 0.00s.)
[ 1.393361] init: (Parsing /system/etc/init/installd.rc took 0.00s.)
[ 1.393674] init: (Parsing /system/etc/init/keystore.rc took 0.00s.)
[ 1.393992] init: (Parsing /system/etc/init/lmkd.rc took 0.00s.)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
相关内容可以参考android 8 system/core/init/readme.txt.