OS:Android8.1
Platform:PX30
我们这儿以创建一个开机自启动日志服务为例
1、创建需要自启动的脚本文件
创建一个名字为init_tool.sh的脚本文件,内容如下:
#!/system/bin/sh
mkdir -p /sdcard/logs/
logcat -b system -b events -b main -b radio -b kernel -n 10 -v threadtime -f /sdcard/logs/alllog -r5000
脚本名称及内容可自定义。
2、将脚本文件编译至系统,并放置在系统的system/bin目录下。
在device/rockchip/rk3326/device-common.mk文件中添加
# add for log inittool.sh
PRODUCT_COPY_FILES += \
$(TARGET_DEVICE_DIR)/othertool/init_tool.sh:system/bin/init_tool.sh
其中$(TARGET_DEVICE_DIR)/othertool/init_tool.sh 这个是脚本在SDK的路径,可自定义。system/bin/init_tool.sh这个是脚本拷贝至系统的路径,需固定为system/bin。
完成后编译代码,可在out/target/product/rk3326_evb/system/bin/查看是否修改成功,如没有init_tool.sh 则需检查文件修改是否正确。
3、添加系统启动服务
在device/rockchip/common/init.rockchip.rc中添加
service inittool /system/bin/sh /system/bin/init_tool.sh
#class core
user root
group root
disabled
oneshot
seclabel u:r:inittool:s0
init.rc 的语法可自行网上查阅,因设计较多,这儿就不一一讲解,需要用到的时候再去查相关用法。
4、设置服务开机自启动
在device/rockchip/common/init.rk30board.rc中添加
@@ -249,7 +249,12 @@ on property:sys.boot_completed=1
setprop persist.sys.first_booting false
# if data is encrypt, when start gps too early, gpsd crashed
start gpsd
-
+#Add INIT_TOOL SERVICE
+ wait /sdcard/Android
+ start inittool
注意,这儿启动服务一定要有on property:sys.boot_completed=1
即等待系统启动完成后,再启动inittool服务。
5、给服务添加系统权限
在device/rockchip/common/sepolicy/file_contexts中添加
#for log tool
/system/bin/init_tool.sh u:object_r:inittool_exec:s0
device/rockchip/common/sepolicy/init.te中添加
#init tool
allow init inittool_exec:file { getattr };
创建inittool.te放置在device/rockchip/common/sepolicy文件夹下
内容为
type inittool, domain;
type inittool_exec, exec_type, file_type;
#init_daemon_domain(rftoolcopy)
allow inittool shell_exec:file { getattr open read };
#allow rftoolcopy toolbox_exec:file { getattr execute read open execute_no_trans };
allow inittool storage_file:lnk_file { read };
allow inittool tmpfs:dir { search };
allow inittool mnt_user_file:dir { search };
allow inittool mnt_user_file:lnk_file { read };
注意:inittool.te 这个文件名称需与上面创建的服务名称一致(上面创建的服务名称为inittool,即我们的权限文件为inittool.te),包括文件内容的名字,都需要以第三点创建的服务名称为标准,不可自定义命名
至此,我们的开机服务已经添加完成,重新编译系统,刷机后可验证服务已一直启动在后台,如需杀掉该服务,可使用kill 命令清理掉后台服务。但每次重启后都会再次启动。如不需要使用了,需在代码里设置关闭。
如有疑问,或认为有不对的地方,欢迎留言或私信交流,谢谢!