Skip to content

☆ Android Init Language

https://scz.617.cn/android/201404211328.txt

参看:

https://android.googlesource.com/platform/system/core/+/master/init/readme.txt

Android Init Language包含四种元素:

Action
Command
Service
Option

支持C风格的反斜杠。#号开头表示注释。

Action、Service定义新的Section,所有的Command、Option隶属于前二者,不在前 二者范围内的Command、Option被忽略,比如位于第一个Section之前Command、Option。

Action、Service的名字唯一,如果有同名元素出现,先出现的有效,后出现的被忽 略,而不是后者覆盖前者。


Action形如:

on

Action有一个触发器,当一个符合Action触发条件的事件发生时,该Action被加入一 个待执行队列的尾部,除非该Action已经位于队列中了。

队列中的Action被依次取出,隶属Action的Command被依次执行。Init在执行Command 之余还负责设备创建/销毁、属性设置、进程重启等其他活动。


Service形如:

service []*

[]* 可以是"ro"、"rw"、"remount"、"noatime"等等 setprop 设置属性 setrlimit Set the rlimit for a resource start 如果指定服务未启动,则启动它 stop 如果指定服务未停止,则停止它 symlink 创建符号链接 sysclktz 设置时区 trigger 触发指定事件,这将导致匹配的Action被加入待执行队列尾部。 wait [] 轮询指定文件是否存在,如果存在则返回,否则等待超时后返回。缺省超时是5 秒。 write []* 打开指定文件,调用write(2)向其中写入 -------------------------------------------------------------------------- Init会更新某些系统属性以提供某种监控能力: init.action 值为正在被处理的Action名字,如果没有则为"" init.command 值为正在被执行的Command,如果没有则为"" init.svc. 指定服务的当前状态,可取值有stopped、running、restarting。 -------------------------------------------------------------------------- 这份文档有点老了,尤其对Trigger可取值的描述不全。红米/init.rc的部分内容: -------------------------------------------------------------------------- #import /init.${ro.hardware}.rc import /init.usb.rc import /init.trace.rc import init.xlog.rc on early-init # Set the security context for the init process. setcon u:r:init:s0 start ueventd mkdir /mnt 0775 root system on early_property:ro.build.type=user write /proc/bootprof "INIT: user build setting" export BOOTCLASSPATH ... on early_property:ro.build.type=eng write /proc/bootprof "INIT: eng build setting" export BOOTCLASSPATH ...:/system/framework/emma.jar on early_property:ro.hardware=mt6589 setprop ro.mtk.hardware mt6589 on init sysclktz 0 loglevel 6 write /proc/bootprof "INIT: on init start" # setup the global environment export PATH /sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin export LD_LIBRARY_PATH /vendor/lib:/system/lib export ANDROID_BOOTLOGO 1 export ANDROID_ROOT /system export ANDROID_ASSETS /system/app export ANDROID_DATA /data export ANDROID_STORAGE /storage export ASEC_MOUNTPOINT /mnt/asec export LOOP_MOUNTPOINT /mnt/obb # Backward compatibility symlink /system/etc /etc symlink /sys/kernel/debug /d # Right now vendor lives on the same filesystem as system, # but someday that may change. symlink /system/vendor /vendor write /proc/sys/kernel/panic_on_oops 1 write /proc/cpu/alignment 4 write /proc/sys/kernel/randomize_va_space 2 write /proc/sys/kernel/kptr_restrict 2 write /proc/sys/kernel/dmesg_restrict 1 # mount different fs start on fs_property:ro.mount.fs=EXT4 write /proc/bootprof "INIT:eMMC:Mount_START" exec /sbin/e2fsck -p /emmc@android # Mount /system rw first to give the filesystem a chance to save a checkpoint #mount ext4 emmc@android /system wait commit=1,data=journal,noauto_da_alloc mount ext4 /emmc@android /system wait commit=1,noauto_da_alloc mkdir /system/secro 0600 system system # RIL need to do this before the folder /system changed to read only chown radio system /system/etc/ril chmod 0770 /system/etc/ril chmod 0444 /system/etc/ril/oper.lis # Wifi thermal need to chmod before system is changed to read only chown root system /system/etc/throttle.sh chmod 0750 /system/etc/throttle.sh mount ext4 /emmc@android /system noatime ro remount wait exec /sbin/e2fsck -pfD /emmc@usrdata exec /sbin/tune2fs -O has_journal /emmc@usrdata exec /system/bin/ext4_resize mount ext4 /emmc@usrdata /data noatime nosuid nodev wait noauto_da_alloc,discard exec /sbin/e2fsck -p /emmc@cache exec /sbin/tune2fs -O has_journal /emmc@cache mount ext4 /emmc@cache /cache noatime nosuid nodev wait noauto_da_alloc,discard mount ext4 /emmc@sec_ro /system/secro ro wait write /proc/bootprof "INIT:eMMC:Mount_END" # mount different fs end on post-fs-data # Add by MTK # create the directory and symbol link file, wubin 20130321 mkdir /dev/block/platform 0770 root root mkdir /dev/block/platform/mtk-msdc.1 0770 root root mkdir /dev/block/platform/mtk-msdc.1/by-name 0770 root root symlink /dev/block/mmcblk0p5 /dev/block/platform/mtk-msdc.1/by-name/emmc@android symlink /dev/block/mmcblk0p6 /dev/block/platform/mtk-msdc.1/by-name/emmc@cache symlink /dev/block/mmcblk0p1 /dev/block/platform/mtk-msdc.1/by-name/emmc@ebr1 symlink /dev/block/mmcblk0p8 /dev/block/platform/mtk-msdc.1/by-name/emmc@fat symlink /dev/block/mmcblk0p2 /dev/block/platform/mtk-msdc.1/by-name/emmc@protect_f symlink /dev/block/mmcblk0p3 /dev/block/platform/mtk-msdc.1/by-name/emmc@protect_s symlink /dev/block/mmcblk0p4 /dev/block/platform/mtk-msdc.1/by-name/emmc@sec_ro symlink /dev/block/mmcblk0p7 /dev/block/platform/mtk-msdc.1/by-name/emmc@usrdata # End of adding by MTK drivers on boot # basic network init ifup lo hostname localhost domainname localdomain # set RLIMIT_NICE to allow priorities from 19 to -20 setrlimit 13 40 40 class_start core class_start main class_start default # Daemon processes to be run by init. # Update the second boot logo service bootlogoupdater /system/bin/boot_logo_updater user graphics group graphics media oneshot service ueventd /sbin/ueventd class core critical seclabel u:r:ueventd:s0 on property:selinux.reload_policy=1 restart ueventd restart installd service console /system/bin/sh class core console disabled user root group log on property:ro.debuggable=1 start console # adbd is controlled via property triggers in init..usb.rc service adbd /sbin/adbd class core socket adbd stream 660 system system disabled seclabel u:r:adbd:s0 #mdbd service mdbd /sbin/mdbd class core socket mdbd stream 660 system system disabled seclabel u:r:mdbd:s0 # adbd on at boot in emulator on property:ro.kernel.qemu=1 start adbd service debuggerd /system/bin/debuggerd class main service sshd /system/bin/start-ssh class main disabled on boot chmod 0666 /dev/hardwareinfo chmod 0666 /dev/gsensor -------------------------------------------------------------------------- 缺省情况下,init启动的进程,其stdout、stderr被重定向到/dev/null。调试这些 进程时如果需要看到stdout、stderr的输出,可以用/system/bin/logwrapper启动这 些进程,此时后者的stdout、stderr被重定向到Android的日志系统,通过logcat可 以看到输出。比如: service akmd /system/bin/logwrapper /sbin/akmd