微信公众号:杨源鑫
如果你觉得文章对你有帮助,欢迎在评论区交流讨论
参考: android源码目录里的system/core/init/readme.txt.
Android系统里以*.rc为扩展名为系统初始化脚本,脚本里就是使用安卓初始化语言.
语句都是一行为一条语句,没有符号分隔. 语句里的每个词用空格隔开.
如: service ueventd /sbin/ueventd
备注语句以符号”#”作为注释.
语言基本上分为4个块: Actions Commands Services 和 Options.
每个Actions和Services关键词开始的语句作为一个新的区块.
Commands和Options就是属于就近的Actions或Services块.
如:
Actions的形式:
Services的形式:
[]*
rc文件里实例:
system/bin/logd
socketlogdstream0666logdlogd
socketlogdrseqpacket0666logdlogd
socketlogdwdgram0222logdlogd
system
system-background/tasks
s0
#Actions
#on
#Setinitanditsforkedchildren'soom_adj.
write/proc/1/oom_score_adj-1000
#Setthesecuritycontextof/adb_keysifpresent.
每个rc文件里又可以包含其它rc文件:
import/init.environ.rc
import/init.usb.rc
import/init.${ro.hardware}.rc
import/init.usb.configfs.rc
import/init.${ro.zygote}.rc
import/init.trace.rc
Actions里常用的trigger有:
onearly-init
oninit
onlate-init
onpost-fs
onboot
onproperty:sys.init_log_level=*//在设置属性值时触发
oncharger
onproperty:sys.powerctl=*
常用的Commands有://参考system/core/init/keywords.h
Startbootchartingifconfigured(seebelow).
isincludedinthedefaultinit.rc.
mode>
Changefileaccesspermissions.
Changefileownerandgroup.
Startallservicesofthespecifiedclassiftheyare
notalreadyrunning.
Stopanddisableallservicesofthespecifiedclassiftheyare
Stopallservicesofthespecifiedclassiftheyare
withoutdisablingthem.Theycanberestarted
usingclass_start.
towrite,butusefulforbinary/large
ofdata.
name>
Setthedomainname.
enableintoanenabledoneasiftheservicedidnot
Iftheserviceissupposedtoberunning,itwillbestartednow.
whenthebootloadersetsavariablethatindicatesaspecific
whenneeded.E.g.
onproperty:ro.boot.myfancyhardware=1
enablemy_fancy_service_for_my_fancy_hardware
[[]*]]--[]*
andexecutecommandwiththegivenarguments.Thecommandstarts
after"--"sothatanoptionalsecuritycontext,user,andsupplementary
groupscanbeprovided.Noothercommandswillberununtilthisone
canbea-todenotedefault.
export
Settheenvironmentvariableequaltointhe
globalenvironment(whichwillbeinheritedbyallprocesses
afterthiscommandisexecuted)
name>
Setthehostname.
interface>
interfaceonline.
importParseaninitconfigfile,extendingthecurrentconfiguration.
path>
Installthemoduleat
from/system,/vendor,etcetera.
isincludedinthedefaultinit.rc.
when/datahasbeendecrypted.
isincludedinthedefaultinit.rc.
level>
Setsthekernellogleveltolevel.Propertiesareexpandedwithin.
path>[mode][owner][group]
Createadirectoryat,optionallywiththegivenmode,owner,and
Ifnotprovided,thedirectoryiscreatedwithpermissions755and
bytherootuserandrootgroup.Ifprovided,themode,ownerandgroup
updatedifthedirectoryexistsalready.
onthegivenfs_mgr-formatfstab.
mount[]*[]
tomountthenameddeviceatthedirectorymaybeoftheformmtd@nametospecifyamtdblock
byname.
sinclude"ro","rw","remount","noatime",...
include"barrier=1","noauto_da_alloc","discard",...as
string,eg:barrier=1,noauto_da_alloc
torespondtochangestothe
"sys.powerctl"systemproperty,usedtoimplementrebooting.
Likestop,butdoesn'tdisabletheservice.
[]*
tothesecuritycontextspecified
[]*
tothe
to.Propertiesareexpanded
.
withthevalue.verifiedpropertiesusedbyadbremounttsetthemdirectlyitself.
wait[]
fortheexistenceofthegivenfileandreturnwhenfound,
orthetimeouthasbeenreached.Iftimeoutisnotspecifiedit
defaultstofiveseconds.
path>
Openthefileatandwriteastringtoitwithwrite(2).
Ifthefiledoesnotexist,itwillbecreated.Ifitdoesexist,
areexpandedwithin.
Services里常用的options有:
isadevice-criticalservice.Ifitexitsmorethanfourtimesin
intorecoverymode.
notautomaticallystartwithitsclass.
byname.
Settheenvironmentvariabletointhelaunchedprocess.
[[[]]]
andpass
tothelaunchedprocess.mustbe"dgram","stream"or"seqpacket".
andgroupdefaultto0.
'seclabel'istheSELinuxsecuritycontextforthesocket.
totheservicesecuritycontext,asspecifiedbyseclabelor
ontheserviceexecutablefilesecuritycontext.
tousernamebeforeexec'ingthisservice.
toroot.(???probablyshoulddefaulttonobody)
ifyourprocessrequireslinuxcapabilitiesthenyoucannotuse
in-processwhile
andthendroptoyourdesireduid.
group[]*
togroupnamebeforeexec'ingthisservice.Additional
tosetthe
oftheprocess(viasetgroups()).
toroot.(???probablyshoulddefaulttonobody)
to'seclabel'beforeexec'ingthisservice.
forusebyservicesrunfromtherootfs,e.g.ueventd,adbd.
onthesystempartitioncaninsteadusepolicy-definedtransitions
ontheirfilesecuritycontext.
Ifnotspecifiedandnotransitionisdefinedinpolicy,defaultstotheinitcontext.
Donotrestarttheservicewhenitexits.
classclassnamefortheservice.Allservicesina
classmaybestartedorstoppedtogether.Aservice
isintheclass"default"ifoneisnotspecifiedviathe
classoption.
whenservicerestarts.
'spidtothegivenfileswhenitforks.Meantfor
实现直接使用root用户进入终端, 修改android/out/target/product/tulip-p1/root/init.rc :
console/system/bin/sh
classcore
console
#usershell
#groupshelllog
#seclabelu:r:shell:s0
//备注使用shell用户进入终端. 会默认使用root用户操作
实现系统进入后执行自定义的脚本 :修改android/out/target/product/tulip-p1/root/init.rc , 增加内容:
servicestart_mytest/system/bin/mytest.sh
onproperty:sys.boot_completed=1
在android/out/target/product/tulip-p1/system/bin目录下,增加mytest.sh, 内容:
#!/system/bin/sh
echo"mytest...!">/dev/ttyS0
echo"mytest...!">/dev/ttyS0
echo"mytest...!">/dev/ttyS0
echo"mytest...!">/dev/ttyS0
echo"mytest...!">/dev/ttyS0
echo"mytest...!">/dev/ttyS0
echo"mytest...!">/dev/ttyS0